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 /**
36 * A builder class for instantiation of the {@link ReadmeHtmlVelocityDelegate}.
37 */
38 public static class ReadmeHtmlVelocityDelegateBuilder {
39 /** The maven artifactId to use in the <code>README.vm</code> template. */
40 private String artifactId;
41 /** The maven version to use in the <code>README.vm</code> template. */
42 private String version;
43 /** The site url to use in the <code>README.vm</code> template. */
44 private String siteUrl;
45
46 /**
47 * Private constructor for using the builder through the {@link ReadmeHtmlVelocityDelegate#builder()}
48 * method.
49 */
50 private ReadmeHtmlVelocityDelegateBuilder() {
51 }
52
53 /**
54 * Builds up the {@link ReadmeHtmlVelocityDelegate} from the previously set parameters.
55 * @return a new {@link ReadmeHtmlVelocityDelegate}.
56 */
57 public ReadmeHtmlVelocityDelegate build() {
58 return new ReadmeHtmlVelocityDelegate(this.artifactId, this.version, this.siteUrl);
59 }
60
61 /**
62 * Adds the artifactId to the {@link ReadmeHtmlVelocityDelegate}.
63 * @param artifactId the {@link String} representing the maven artifactId.
64 * @return the builder to continue building.
65 */
66 public ReadmeHtmlVelocityDelegateBuilder withArtifactId(final String artifactId) {
67 this.artifactId = artifactId;
68 return this;
69 }
70
71 /**
72 * Adds the siteUrl to the {@link ReadmeHtmlVelocityDelegate}.
73 * @param siteUrl the site url to be used in the <code>README.html</code>
74 * @return the builder to continue building.
75 */
76 public ReadmeHtmlVelocityDelegateBuilder withSiteUrl(final String siteUrl) {
77 this.siteUrl = siteUrl;
78 return this;
79 }
80
81 /**
82 * Adds the version to the {@link ReadmeHtmlVelocityDelegate}.
83 * @param version the maven version.
84 * @return the builder to continue building.
85 */
86 public ReadmeHtmlVelocityDelegateBuilder withVersion(final String version) {
87 this.version = version;
88 return this;
89 }
90 }
91 /** The location of the velocity template for this class. */
92 private static final String TEMPLATE = "resources/org/apache/commons/release/plugin"
93 + "/velocity/README.vm";
94 /**
95 * Gets the {@link ReadmeHtmlVelocityDelegateBuilder} for constructing the {@link ReadmeHtmlVelocityDelegate}.
96 *
97 * @return the {@link ReadmeHtmlVelocityDelegateBuilder}.
98 */
99 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 }