    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    /**
    036     * A builder class for instantiation of the {@link ReadmeHtmlVelocityDelegate}.
    037     */
    038    public static class ReadmeHtmlVelocityDelegateBuilder {
    039        /** The maven artifactId to use in the <code>README.vm</code> template. */
    040        private String artifactId;
    041        /** The maven version to use in the <code>README.vm</code> template. */
    042        private String version;
    043        /** The site url to use in the <code>README.vm</code> template. */
    044        private String siteUrl;
    045
    046        /**
    047         * Private constructor for using the builder through the {@link ReadmeHtmlVelocityDelegate#builder()}
    048         * method.
    049         */
    050        private ReadmeHtmlVelocityDelegateBuilder() {
    051        }
    052
    053        /**
    054         * Builds up the {@link ReadmeHtmlVelocityDelegate} from the previously set parameters.
    055         * @return a new {@link ReadmeHtmlVelocityDelegate}.
    056         */
    057        public ReadmeHtmlVelocityDelegate build() {
    058            return new ReadmeHtmlVelocityDelegate(this.artifactId, this.version, this.siteUrl);
    059        }
    060
    061        /**
    062         * Adds the artifactId to the {@link ReadmeHtmlVelocityDelegate}.
    063         * @param artifactId the {@link String} representing the maven artifactId.
    064         * @return the builder to continue building.
    065         */
    066        public ReadmeHtmlVelocityDelegateBuilder withArtifactId(final String artifactId) {
    067            this.artifactId = artifactId;
    068            return this;
    069        }
    070
    071        /**
    072         * Adds the siteUrl to the {@link ReadmeHtmlVelocityDelegate}.
    073         * @param siteUrl the site url to be used in the <code>README.html</code>
    074         * @return the builder to continue building.
    075         */
    076        public ReadmeHtmlVelocityDelegateBuilder withSiteUrl(final String siteUrl) {
    077            this.siteUrl = siteUrl;
    078            return this;
    079        }
    080
    081        /**
    082         * Adds the version to the {@link ReadmeHtmlVelocityDelegate}.
    083         * @param version the maven version.
    084         * @return the builder to continue building.
    085         */
    086        public ReadmeHtmlVelocityDelegateBuilder withVersion(final String version) {
    087            this.version = version;
    088            return this;
    089        }
    090    }
    091    /** The location of the velocity template for this class. */
    092    private static final String TEMPLATE = "resources/org/apache/commons/release/plugin"
    093                                         + "/velocity/README.vm";
    094    /**
    095     * Gets the {@link ReadmeHtmlVelocityDelegateBuilder} for constructing the {@link ReadmeHtmlVelocityDelegate}.
    096     *
    097     * @return the {@link ReadmeHtmlVelocityDelegateBuilder}.
    098     */
    099    public static ReadmeHtmlVelocityDelegateBuilder builder() {
    100        return new ReadmeHtmlVelocityDelegateBuilder();
    101    }
    102    /** This is supposed to represent the maven artifactId. */
    103    private final String artifactId;
    104
    105    /** This is supposed to represent the maven version of the release. */
    106    private final String version;
    107
    108    /** The url of the site that gets set into the <code>README.html</code>. */
    109    private final String siteUrl;
    110
    111    /**
    112     * The private constructor to be used by the {@link ReadmeHtmlVelocityDelegateBuilder}.
    113     *
    114     * @param artifactId sets the {@link ReadmeHtmlVelocityDelegate#artifactId}.
    115     * @param version sets the {@link ReadmeHtmlVelocityDelegate#version}.
    116     * @param siteUrl sets the {@link ReadmeHtmlVelocityDelegate#siteUrl}.
    117     */
    118    private ReadmeHtmlVelocityDelegate(final String artifactId, final String version, final String siteUrl) {
    119        this.artifactId = artifactId;
    120        this.version = version;
    121        this.siteUrl = siteUrl;
    122    }
    123
    124    /**
    125     * Renders the <code>README.vm</code> velocity template with the variables constructed with the
    126     * {@link ReadmeHtmlVelocityDelegateBuilder}.
    127     *
    128     * @param writer is the {@link Writer} to which we wish to render the <code>README.vm</code> template.
    129     * @return a reference to the {@link Writer} passed in.
    130     */
    131    public Writer render(final Writer writer) {
    132        final VelocityEngine ve = new VelocityEngine();
    133        ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
    134        ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
    135        ve.init();
    136        final Template template = ve.getTemplate(TEMPLATE);
    137        final String[] splitArtifactId = artifactId.split("-");
    138        final String wordCommons = "commons";
    139        String artifactShortName = "";
    140        if (splitArtifactId.length > 1) {
    141            artifactShortName = splitArtifactId[1];
    142        } else if (splitArtifactId.length == 1) {
    143            artifactShortName = splitArtifactId[0];
    144        }
    145        // ".+\\d$" matches a non-empty string that terminates in a digit {0-9}.
    146        if (artifactShortName.matches(".+\\d$")) {
    147            artifactShortName = artifactShortName.substring(0, artifactShortName.length() - 1);
    148        }
    149        final String artifactIdWithFirstLetterscapitalized =
    150                StringUtils.capitalize(wordCommons)
    151                        + "-"
    152                        + artifactShortName.toUpperCase();
    153        final VelocityContext context = new VelocityContext();
    154        context.internalPut("artifactIdWithFirstLetterscapitalized", artifactIdWithFirstLetterscapitalized);
    155        context.internalPut("artifactShortName", artifactShortName.toUpperCase());
    156        context.internalPut("artifactId", artifactId);
    157        context.internalPut("version", version);
    158        context.internalPut("siteUrl", siteUrl);
    159        template.merge(context, writer);
    160        return writer;
    161    }
    162}