    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017package org.apache.commons.release.plugin.velocity;
    018
    019import java.io.Writer;
    020
    021import org.apache.commons.lang3.StringUtils;
    022import org.apache.velocity.Template;
    023import org.apache.velocity.VelocityContext;
    024import org.apache.velocity.app.VelocityEngine;
    025import org.apache.velocity.runtime.RuntimeConstants;
    026import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
    027
    028/**
    029 * This class' purpose is to generate the <code>README.html</code> that moves along with the
    030 * release for the sake of downloading the release from the distribution area.
    031 *
    032 * @since 1.3
    033 */
    034public class ReadmeHtmlVelocityDelegate {
    035    /** The location of the velocity template for this class. */
    036    private static final String TEMPLATE = "resources/org/apache/commons/release/plugin"
    037                                         + "/velocity/README.vm";
    038    /** This is supposed to represent the maven artifactId. */
    039    private final String artifactId;
    040    /** This is supposed to represent the maven version of the release. */
    041    private final String version;
    042    /** The url of the site that gets set into the <code>README.html</code>. */
    043    private final String siteUrl;
    044
    045    /**
    046     * The private constructor to be used by the {@link ReadmeHtmlVelocityDelegateBuilder}.
    047     *
    048     * @param artifactId sets the {@link ReadmeHtmlVelocityDelegate#artifactId}.
    049     * @param version sets the {@link ReadmeHtmlVelocityDelegate#version}.
    050     * @param siteUrl sets the {@link ReadmeHtmlVelocityDelegate#siteUrl}.
    051     */
    052    private ReadmeHtmlVelocityDelegate(final String artifactId, final String version, final String siteUrl) {
    053        this.artifactId = artifactId;
    054        this.version = version;
    055        this.siteUrl = siteUrl;
    056    }
    057
    058    /**
    059     * Gets the {@link ReadmeHtmlVelocityDelegateBuilder} for constructing the {@link ReadmeHtmlVelocityDelegate}.
    060     *
    061     * @return the {@link ReadmeHtmlVelocityDelegateBuilder}.
    062     */
    063    public static ReadmeHtmlVelocityDelegateBuilder builder() {
    064        return new ReadmeHtmlVelocityDelegateBuilder();
    065    }
    066
    067    /**
    068     * Renders the <code>README.vm</code> velocity template with the variables constructed with the
    069     * {@link ReadmeHtmlVelocityDelegateBuilder}.
    070     *
    071     * @param writer is the {@link Writer} to which we wish to render the <code>README.vm</code> template.
    072     * @return a reference to the {@link Writer} passed in.
    073     */
    074    public Writer render(final Writer writer) {
    075        final VelocityEngine ve = new VelocityEngine();
    076        ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
    077        ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
    078        ve.init();
    079        final Template template = ve.getTemplate(TEMPLATE);
    080        final String[] splitArtifactId = artifactId.split("-");
    081        final String wordCommons = "commons";
    082        String artifactShortName = "";
    083        if (splitArtifactId.length > 1) {
    084            artifactShortName = splitArtifactId[1];
    085        } else if (splitArtifactId.length == 1) {
    086            artifactShortName = splitArtifactId[0];
    087        }
    088        // ".+\\d$" matches a non-empty string that terminates in a digit {0-9}.
    089        if (artifactShortName.matches(".+\\d$")) {
    090            artifactShortName = artifactShortName.substring(0, artifactShortName.length() - 1);
    091        }
    092        final String artifactIdWithFirstLetterscapitalized =
    093                StringUtils.capitalize(wordCommons)
    094                        + "-"
    095                        + artifactShortName.toUpperCase();
    096        final VelocityContext context = new VelocityContext();
    097        context.internalPut("artifactIdWithFirstLetterscapitalized", artifactIdWithFirstLetterscapitalized);
    098        context.internalPut("artifactShortName", artifactShortName.toUpperCase());
    099        context.internalPut("artifactId", artifactId);
    100        context.internalPut("version", version);
    101        context.internalPut("siteUrl", siteUrl);
    102        template.merge(context, writer);
    103        return writer;
    104    }
    105
    106    /**
    107     * A builder class for instantiation of the {@link ReadmeHtmlVelocityDelegate}.
    108     */
    109    public static class ReadmeHtmlVelocityDelegateBuilder {
    110        /** The maven artifactId to use in the <code>README.vm</code> template. */
    111        private String artifactId;
    112        /** The maven version to use in the <code>README.vm</code> template. */
    113        private String version;
    114        /** The site url to use in the <code>README.vm</code> template. */
    115        private String siteUrl;
    116
    117        /**
    118         * Private constructor for using the builder through the {@link ReadmeHtmlVelocityDelegate#builder()}
    119         * method.
    120         */
    121        private ReadmeHtmlVelocityDelegateBuilder() {
    122        }
    123
    124        /**
    125         * Adds the artifactId to the {@link ReadmeHtmlVelocityDelegate}.
    126         * @param artifactId the {@link String} representing the maven artifactId.
    127         * @return the builder to continue building.
    128         */
    129        public ReadmeHtmlVelocityDelegateBuilder withArtifactId(final String artifactId) {
    130            this.artifactId = artifactId;
    131            return this;
    132        }
    133
    134        /**
    135         * Adds the version to the {@link ReadmeHtmlVelocityDelegate}.
    136         * @param version the maven version.
    137         * @return the builder to continue building.
    138         */
    139        public ReadmeHtmlVelocityDelegateBuilder withVersion(final String version) {
    140            this.version = version;
    141            return this;
    142        }
    143
    144        /**
    145         * Adds the siteUrl to the {@link ReadmeHtmlVelocityDelegate}.
    146         * @param siteUrl the site url to be used in the <code>README.html</code>
    147         * @return the builder to continue building.
    148         */
    149        public ReadmeHtmlVelocityDelegateBuilder withSiteUrl(final String siteUrl) {
    150            this.siteUrl = siteUrl;
    151            return this;
    152        }
    153
    154        /**
    155         * Builds up the {@link ReadmeHtmlVelocityDelegate} from the previously set parameters.
    156         * @return a new {@link ReadmeHtmlVelocityDelegate}.
    157         */
    158        public ReadmeHtmlVelocityDelegate build() {
    159            return new ReadmeHtmlVelocityDelegate(this.artifactId, this.version, this.siteUrl);
    160        }
    161    }
    162}