View Javadoc
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  
19  import java.io.Writer;
20  
21  import org.apache.commons.lang3.StringUtils;
22  import org.apache.velocity.Template;
23  import org.apache.velocity.VelocityContext;
24  import org.apache.velocity.app.VelocityEngine;
25  import org.apache.velocity.runtime.RuntimeConstants;
26  import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
27  
28  /**
29   * This class' purpose is to generate the <code>README.html</code> that moves along with the
30   * release for the sake of downloading the release from the distribution area.
31   *
32   * @since 1.3
33   */
34  public class ReadmeHtmlVelocityDelegate {
35      /** The location of the velocity template for this class. */
36      private static final String TEMPLATE = "resources/org/apache/commons/release/plugin"
37                                           + "/velocity/README.vm";
38      /** This is supposed to represent the maven artifactId. */
39      private final String artifactId;
40      /** This is supposed to represent the maven version of the release. */
41      private final String version;
42      /** The url of the site that gets set into the <code>README.html</code>. */
43      private final String siteUrl;
44  
45      /**
46       * The private constructor to be used by the {@link ReadmeHtmlVelocityDelegateBuilder}.
47       *
48       * @param artifactId sets the {@link ReadmeHtmlVelocityDelegate#artifactId}.
49       * @param version sets the {@link ReadmeHtmlVelocityDelegate#version}.
50       * @param siteUrl sets the {@link ReadmeHtmlVelocityDelegate#siteUrl}.
51       */
52      private ReadmeHtmlVelocityDelegate(final String artifactId, final String version, final String siteUrl) {
53          this.artifactId = artifactId;
54          this.version = version;
55          this.siteUrl = siteUrl;
56      }
57  
58      /**
59       * Gets the {@link ReadmeHtmlVelocityDelegateBuilder} for constructing the {@link ReadmeHtmlVelocityDelegate}.
60       *
61       * @return the {@link ReadmeHtmlVelocityDelegateBuilder}.
62       */
63      public static ReadmeHtmlVelocityDelegateBuilder builder() {
64          return new ReadmeHtmlVelocityDelegateBuilder();
65      }
66  
67      /**
68       * Renders the <code>README.vm</code> velocity template with the variables constructed with the
69       * {@link ReadmeHtmlVelocityDelegateBuilder}.
70       *
71       * @param writer is the {@link Writer} to which we wish to render the <code>README.vm</code> template.
72       * @return a reference to the {@link Writer} passed in.
73       */
74      public Writer render(final Writer writer) {
75          final VelocityEngine ve = new VelocityEngine();
76          ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
77          ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
78          ve.init();
79          final Template template = ve.getTemplate(TEMPLATE);
80          final String[] splitArtifactId = artifactId.split("-");
81          final String wordCommons = "commons";
82          String artifactShortName = "";
83          if (splitArtifactId.length > 1) {
84              artifactShortName = splitArtifactId[1];
85          } else if (splitArtifactId.length == 1) {
86              artifactShortName = splitArtifactId[0];
87          }
88          // ".+\\d$" matches a non-empty string that terminates in a digit {0-9}.
89          if (artifactShortName.matches(".+\\d$")) {
90              artifactShortName = artifactShortName.substring(0, artifactShortName.length() - 1);
91          }
92          final String artifactIdWithFirstLetterscapitalized =
93                  StringUtils.capitalize(wordCommons)
94                          + "-"
95                          + artifactShortName.toUpperCase();
96          final VelocityContext context = new VelocityContext();
97          context.internalPut("artifactIdWithFirstLetterscapitalized", artifactIdWithFirstLetterscapitalized);
98          context.internalPut("artifactShortName", artifactShortName.toUpperCase());
99          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 }