-
Notifications
You must be signed in to change notification settings - Fork 17
/
AbstractModelloGenerator.java
279 lines (224 loc) · 9.07 KB
/
AbstractModelloGenerator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package org.codehaus.modello.plugin;
/*
* Copyright (c) 2004, Codehaus.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do
* so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import javax.inject.Inject;
import java.io.File;
import java.io.FilterWriter;
import java.io.IOException;
import java.io.Writer;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import org.codehaus.modello.ModelloException;
import org.codehaus.modello.ModelloParameterConstants;
import org.codehaus.modello.ModelloRuntimeException;
import org.codehaus.modello.model.Model;
import org.codehaus.modello.model.ModelAssociation;
import org.codehaus.modello.model.ModelClass;
import org.codehaus.modello.model.ModelDefault;
import org.codehaus.modello.model.ModelField;
import org.codehaus.modello.model.Version;
import org.codehaus.plexus.build.BuildContext;
import org.codehaus.plexus.util.StringUtils;
import org.codehaus.plexus.util.io.CachingWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author <a href="mailto:jason@modello.org">Jason van Zyl</a>
* @author <a href="mailto:evenisse@codehaus.org">Emmanuel Venisse</a>
*/
public abstract class AbstractModelloGenerator implements ModelloGenerator {
private final Logger logger = LoggerFactory.getLogger(getClass());
private Model model;
private File outputDirectory;
private Version generatedVersion;
private boolean packageWithVersion;
private String encoding;
private String licenseText;
@Inject
private BuildContext buildContext;
protected Logger getLogger() {
return logger;
}
protected void initialize(Model model, Properties parameters) throws ModelloException {
this.model = model;
outputDirectory = new File(getParameter(parameters, ModelloParameterConstants.OUTPUT_DIRECTORY));
String version = getParameter(parameters, ModelloParameterConstants.VERSION);
generatedVersion = new Version(version);
packageWithVersion = Boolean.valueOf(getParameter(parameters, ModelloParameterConstants.PACKAGE_WITH_VERSION));
encoding = parameters.getProperty(ModelloParameterConstants.ENCODING);
licenseText = parameters.getProperty(ModelloParameterConstants.LICENSE_TEXT);
}
protected Model getModel() {
return model;
}
protected Version getGeneratedVersion() {
return generatedVersion;
}
protected boolean isPackageWithVersion() {
return packageWithVersion;
}
public File getOutputDirectory() {
return outputDirectory;
}
protected String getEncoding() {
return encoding;
}
protected String getHeader() {
String header = getLicenseHeader();
if (header == null) {
return getGeneratedHeader();
}
header += "\n";
header += getGeneratedHeader();
return header;
}
private String getGeneratedHeader() {
String version = getClass().getPackage().getImplementationVersion();
return "=================== DO NOT EDIT THIS FILE ====================\n"
+ "Generated by Modello" + ((version == null) ? "" : (' ' + version)) + ",\n"
+ "any modifications will be overwritten.\n"
+ "==============================================================";
}
private String getLicenseHeader() {
return licenseText;
}
protected boolean isClassInModel(String fieldType, Model model) {
try {
return model.getClass(fieldType, generatedVersion) != null;
} catch (Exception e) {
}
return false;
}
/**
* Return the child fields of this class.
* @param modelClass current class
* @return the list of fields of this class
*/
protected List<ModelField> getFieldsForClass(ModelClass modelClass) {
List<ModelField> fields = new ArrayList<ModelField>();
while (modelClass != null) {
fields.addAll(modelClass.getFields(getGeneratedVersion()));
String superClass = modelClass.getSuperClass();
if (superClass != null) {
modelClass = getModel().getClass(superClass, getGeneratedVersion());
} else {
modelClass = null;
}
}
return fields;
}
protected boolean isInnerAssociation(ModelField field) {
return field instanceof ModelAssociation && isClassInModel(((ModelAssociation) field).getTo(), getModel());
}
protected boolean isMap(String fieldType) {
return ModelDefault.MAP.equals(fieldType) || ModelDefault.PROPERTIES.equals(fieldType);
}
protected boolean isCollection(String fieldType) {
return ModelDefault.LIST.equals(fieldType) || ModelDefault.SET.equals(fieldType);
}
protected String capitalise(String str) {
if (StringUtils.isEmpty(str)) {
return str;
}
return new StringBuilder(str.length())
.append(Character.toTitleCase(str.charAt(0)))
.append(str.substring(1))
.toString();
}
public static String singular(String name) {
if (StringUtils.isEmpty(name)) {
return name;
}
if (name.endsWith("ies")) {
return name.substring(0, name.length() - 3) + "y";
} else if (name.endsWith("es") && name.endsWith("ches")) {
return name.substring(0, name.length() - 2);
} else if (name.endsWith("xes")) {
return name.substring(0, name.length() - 2);
} else if (name.endsWith("s") && (name.length() != 1)) {
return name.substring(0, name.length() - 1);
}
return name;
}
public static String uncapitalise(String str) {
if (StringUtils.isEmpty(str)) {
return str;
}
return new StringBuilder(str.length())
.append(Character.toLowerCase(str.charAt(0)))
.append(str.substring(1))
.toString();
}
// ----------------------------------------------------------------------
// Text utils
// ----------------------------------------------------------------------
protected boolean isEmpty(String string) {
return string == null || string.trim().length() == 0;
}
// ----------------------------------------------------------------------
// Parameter utils
// ----------------------------------------------------------------------
/**
* @deprecated Use {@link #getParameter(Properties, String)} instead
* @param name parameter name
* @param parameters the properties
* @return the parameter value
*/
@Deprecated
protected String getParameter(String name, Properties parameters) {
return getParameter(parameters, name);
}
protected String getParameter(Properties parameters, String name) {
String value = parameters.getProperty(name);
if (value == null) {
throw new ModelloRuntimeException("Missing parameter '" + name + "'.");
}
return value;
}
protected String getParameter(Properties parameters, String name, String defaultValue) {
return parameters.getProperty(name, defaultValue);
}
protected BuildContext getBuildContext() {
return buildContext;
}
protected Writer newWriter(Path path) throws IOException {
Charset charset = getEncoding() != null ? Charset.forName(getEncoding()) : Charset.defaultCharset();
return newWriter(path, charset);
}
protected Writer newWriter(Path path, Charset charset) throws IOException {
CachingWriter cachingWriter = new CachingWriter(path, charset);
return new FilterWriter(cachingWriter) {
@Override
public void close() throws IOException {
super.close();
if (cachingWriter.isModified()) {
getBuildContext().refresh(path.toFile());
} else {
getLogger().debug("The contents of the file " + path + " matches, skipping writing file.");
}
}
};
}
}