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 = splitArtifactId[0];
082        String artifactShortName = splitArtifactId[1];
083        String artifactIdWithFirstLetterscapitalized =
084                StringUtils.capitalize(wordCommons)
085                        + "-"
086                        + artifactShortName.toUpperCase();
087        VelocityContext context = new VelocityContext();
088        context.internalPut("artifactIdWithFirstLetterscapitalized", artifactIdWithFirstLetterscapitalized);
089        context.internalPut("artifactShortName", artifactShortName.toUpperCase());
090        context.internalPut("artifactId", artifactId);
091        context.internalPut("version", version);
092        context.internalPut("siteUrl", siteUrl);
093        template.merge(context, writer);
094        return writer;
095    }
096
097    /**
098     * A builder class for instantiation of the {@link ReadmeHtmlVelocityDelegate}.
099     */
100    public static class ReadmeHtmlVelocityDelegateBuilder {
101        /** The maven artifactId to use in the <code>README.vm</code> template. */
102        private String artifactId;
103        /** The maven version to use in the <code>README.vm</code> template. */
104        private String version;
105        /** The site url to use in the <code>README.vm</code> template. */
106        private String siteUrl;
107
108        /**
109         * Private constructor for using the builder through the {@link ReadmeHtmlVelocityDelegate#builder()}
110         * method.
111         */
112        private ReadmeHtmlVelocityDelegateBuilder() {
113            super();
114        }
115
116        /**
117         * Adds the artifactId to the {@link ReadmeHtmlVelocityDelegate}.
118         * @param artifactId the {@link String} representing the maven artifactId.
119         * @return the builder to continue building.
120         */
121        public ReadmeHtmlVelocityDelegateBuilder withArtifactId(String artifactId) {
122            this.artifactId = artifactId;
123            return this;
124        }
125
126        /**
127         * Adds the version to the {@link ReadmeHtmlVelocityDelegate}.
128         * @param version the maven version.
129         * @return the builder to continue building.
130         */
131        public ReadmeHtmlVelocityDelegateBuilder withVersion(String version) {
132            this.version = version;
133            return this;
134        }
135
136        /**
137         * Adds the siteUrl to the {@link ReadmeHtmlVelocityDelegate}.
138         * @param siteUrl the site url to be used in the <code>README.html</code>
139         * @return the builder to continue building.
140         */
141        public ReadmeHtmlVelocityDelegateBuilder withSiteUrl(String siteUrl) {
142            this.siteUrl = siteUrl;
143            return this;
144        }
145
146        /**
147         * Builds up the {@link ReadmeHtmlVelocityDelegate} from the previously set parameters.
148         * @return a new {@link ReadmeHtmlVelocityDelegate}.
149         */
150        public ReadmeHtmlVelocityDelegate build() {
151            return new ReadmeHtmlVelocityDelegate(this.artifactId, this.version, this.siteUrl);
152        }
153    }
154}