1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
30
31
32
33
34 public class ReadmeHtmlVelocityDelegate {
35
36 private static final String TEMPLATE = "resources/org/apache/commons/release/plugin"
37 + "/velocity/README.vm";
38
39 private final String artifactId;
40
41 private final String version;
42
43 private final String siteUrl;
44
45
46
47
48
49
50
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
60
61
62
63 public static ReadmeHtmlVelocityDelegateBuilder builder() {
64 return new ReadmeHtmlVelocityDelegateBuilder();
65 }
66
67
68
69
70
71
72
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
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
108
109 public static class ReadmeHtmlVelocityDelegateBuilder {
110
111 private String artifactId;
112
113 private String version;
114
115 private String siteUrl;
116
117
118
119
120
121 private ReadmeHtmlVelocityDelegateBuilder() {
122 }
123
124
125
126
127
128
129 public ReadmeHtmlVelocityDelegateBuilder withArtifactId(final String artifactId) {
130 this.artifactId = artifactId;
131 return this;
132 }
133
134
135
136
137
138
139 public ReadmeHtmlVelocityDelegateBuilder withVersion(final String version) {
140 this.version = version;
141 return this;
142 }
143
144
145
146
147
148
149 public ReadmeHtmlVelocityDelegateBuilder withSiteUrl(final String siteUrl) {
150 this.siteUrl = siteUrl;
151 return this;
152 }
153
154
155
156
157
158 public ReadmeHtmlVelocityDelegate build() {
159 return new ReadmeHtmlVelocityDelegate(this.artifactId, this.version, this.siteUrl);
160 }
161 }
162 }