    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;
    020import org.apache.commons.lang3.StringUtils;
    021import org.apache.velocity.Template;
    022import org.apache.velocity.VelocityContext;
    023import org.apache.velocity.app.VelocityEngine;
    024import org.apache.velocity.runtime.RuntimeConstants;
    025import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
    026
    027/**
    028 * This class' purpose is to generate the <code>README.html</code> that moves along with the
    029 * release for the sake of downloading the release from the distribution area.
    030 *
    031 * @author chtompki
    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(String artifactId, String version, 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(Writer writer) {
    075        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        Template template = ve.getTemplate(TEMPLATE);
    080        String[] splitArtifactId = artifactId.split("-");
    081        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        String artifactIdWithFirstLetterscapitalized =
    093                StringUtils.capitalize(wordCommons)
    094                        + "-"
    095                        + artifactShortName.toUpperCase();
    096        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            super();
    123        }
    124
    125        /**
    126         * Adds the artifactId to the {@link ReadmeHtmlVelocityDelegate}.
    127         * @param artifactId the {@link String} representing the maven artifactId.
    128         * @return the builder to continue building.
    129         */
    130        public ReadmeHtmlVelocityDelegateBuilder withArtifactId(String artifactId) {
    131            this.artifactId = artifactId;
    132            return this;
    133        }
    134
    135        /**
    136         * Adds the version to the {@link ReadmeHtmlVelocityDelegate}.
    137         * @param version the maven version.
    138         * @return the builder to continue building.
    139         */
    140        public ReadmeHtmlVelocityDelegateBuilder withVersion(String version) {
    141            this.version = version;
    142            return this;
    143        }
    144
    145        /**
    146         * Adds the siteUrl to the {@link ReadmeHtmlVelocityDelegate}.
    147         * @param siteUrl the site url to be used in the <code>README.html</code>
    148         * @return the builder to continue building.
    149         */
    150        public ReadmeHtmlVelocityDelegateBuilder withSiteUrl(String siteUrl) {
    151            this.siteUrl = siteUrl;
    152            return this;
    153        }
    154
    155        /**
    156         * Builds up the {@link ReadmeHtmlVelocityDelegate} from the previously set parameters.
    157         * @return a new {@link ReadmeHtmlVelocityDelegate}.
    158         */
    159        public ReadmeHtmlVelocityDelegate build() {
    160            return new ReadmeHtmlVelocityDelegate(this.artifactId, this.version, this.siteUrl);
    161        }
    162    }
    163}