ReadmeHtmlVelocityDelegate.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.release.plugin.velocity;

  18. import java.io.Writer;

  19. import org.apache.commons.lang3.StringUtils;
  20. import org.apache.velocity.Template;
  21. import org.apache.velocity.VelocityContext;
  22. import org.apache.velocity.app.VelocityEngine;
  23. import org.apache.velocity.runtime.RuntimeConstants;
  24. import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;

  25. /**
  26.  * This class' purpose is to generate the <code>README.html</code> that moves along with the
  27.  * release for the sake of downloading the release from the distribution area.
  28.  *
  29.  * @since 1.3
  30.  */
  31. public class ReadmeHtmlVelocityDelegate {
  32.     /** The location of the velocity template for this class. */
  33.     private static final String TEMPLATE = "resources/org/apache/commons/release/plugin"
  34.                                          + "/velocity/README.vm";
  35.     /** This is supposed to represent the maven artifactId. */
  36.     private final String artifactId;
  37.     /** This is supposed to represent the maven version of the release. */
  38.     private final String version;
  39.     /** The url of the site that gets set into the <code>README.html</code>. */
  40.     private final String siteUrl;

  41.     /**
  42.      * The private constructor to be used by the {@link ReadmeHtmlVelocityDelegateBuilder}.
  43.      *
  44.      * @param artifactId sets the {@link ReadmeHtmlVelocityDelegate#artifactId}.
  45.      * @param version sets the {@link ReadmeHtmlVelocityDelegate#version}.
  46.      * @param siteUrl sets the {@link ReadmeHtmlVelocityDelegate#siteUrl}.
  47.      */
  48.     private ReadmeHtmlVelocityDelegate(final String artifactId, final String version, final String siteUrl) {
  49.         this.artifactId = artifactId;
  50.         this.version = version;
  51.         this.siteUrl = siteUrl;
  52.     }

  53.     /**
  54.      * Gets the {@link ReadmeHtmlVelocityDelegateBuilder} for constructing the {@link ReadmeHtmlVelocityDelegate}.
  55.      *
  56.      * @return the {@link ReadmeHtmlVelocityDelegateBuilder}.
  57.      */
  58.     public static ReadmeHtmlVelocityDelegateBuilder builder() {
  59.         return new ReadmeHtmlVelocityDelegateBuilder();
  60.     }

  61.     /**
  62.      * Renders the <code>README.vm</code> velocity template with the variables constructed with the
  63.      * {@link ReadmeHtmlVelocityDelegateBuilder}.
  64.      *
  65.      * @param writer is the {@link Writer} to which we wish to render the <code>README.vm</code> template.
  66.      * @return a reference to the {@link Writer} passed in.
  67.      */
  68.     public Writer render(final Writer writer) {
  69.         final VelocityEngine ve = new VelocityEngine();
  70.         ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
  71.         ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
  72.         ve.init();
  73.         final Template template = ve.getTemplate(TEMPLATE);
  74.         final String[] splitArtifactId = artifactId.split("-");
  75.         final String wordCommons = "commons";
  76.         String artifactShortName = "";
  77.         if (splitArtifactId.length > 1) {
  78.             artifactShortName = splitArtifactId[1];
  79.         } else if (splitArtifactId.length == 1) {
  80.             artifactShortName = splitArtifactId[0];
  81.         }
  82.         // ".+\\d$" matches a non-empty string that terminates in a digit {0-9}.
  83.         if (artifactShortName.matches(".+\\d$")) {
  84.             artifactShortName = artifactShortName.substring(0, artifactShortName.length() - 1);
  85.         }
  86.         final String artifactIdWithFirstLetterscapitalized =
  87.                 StringUtils.capitalize(wordCommons)
  88.                         + "-"
  89.                         + artifactShortName.toUpperCase();
  90.         final VelocityContext context = new VelocityContext();
  91.         context.internalPut("artifactIdWithFirstLetterscapitalized", artifactIdWithFirstLetterscapitalized);
  92.         context.internalPut("artifactShortName", artifactShortName.toUpperCase());
  93.         context.internalPut("artifactId", artifactId);
  94.         context.internalPut("version", version);
  95.         context.internalPut("siteUrl", siteUrl);
  96.         template.merge(context, writer);
  97.         return writer;
  98.     }

  99.     /**
  100.      * A builder class for instantiation of the {@link ReadmeHtmlVelocityDelegate}.
  101.      */
  102.     public static class ReadmeHtmlVelocityDelegateBuilder {
  103.         /** The maven artifactId to use in the <code>README.vm</code> template. */
  104.         private String artifactId;
  105.         /** The maven version to use in the <code>README.vm</code> template. */
  106.         private String version;
  107.         /** The site url to use in the <code>README.vm</code> template. */
  108.         private String siteUrl;

  109.         /**
  110.          * Private constructor for using the builder through the {@link ReadmeHtmlVelocityDelegate#builder()}
  111.          * method.
  112.          */
  113.         private ReadmeHtmlVelocityDelegateBuilder() {
  114.         }

  115.         /**
  116.          * Adds the artifactId to the {@link ReadmeHtmlVelocityDelegate}.
  117.          * @param artifactId the {@link String} representing the maven artifactId.
  118.          * @return the builder to continue building.
  119.          */
  120.         public ReadmeHtmlVelocityDelegateBuilder withArtifactId(final String artifactId) {
  121.             this.artifactId = artifactId;
  122.             return this;
  123.         }

  124.         /**
  125.          * Adds the version to the {@link ReadmeHtmlVelocityDelegate}.
  126.          * @param version the maven version.
  127.          * @return the builder to continue building.
  128.          */
  129.         public ReadmeHtmlVelocityDelegateBuilder withVersion(final String version) {
  130.             this.version = version;
  131.             return this;
  132.         }

  133.         /**
  134.          * Adds the siteUrl to the {@link ReadmeHtmlVelocityDelegate}.
  135.          * @param siteUrl the site url to be used in the <code>README.html</code>
  136.          * @return the builder to continue building.
  137.          */
  138.         public ReadmeHtmlVelocityDelegateBuilder withSiteUrl(final String siteUrl) {
  139.             this.siteUrl = siteUrl;
  140.             return this;
  141.         }

  142.         /**
  143.          * Builds up the {@link ReadmeHtmlVelocityDelegate} from the previously set parameters.
  144.          * @return a new {@link ReadmeHtmlVelocityDelegate}.
  145.          */
  146.         public ReadmeHtmlVelocityDelegate build() {
  147.             return new ReadmeHtmlVelocityDelegate(this.artifactId, this.version, this.siteUrl);
  148.         }
  149.     }
  150. }