1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.deduced.generator.java; |
17 | |
|
18 | |
import java.io.ByteArrayOutputStream; |
19 | |
import java.io.IOException; |
20 | |
import java.util.List; |
21 | |
import java.util.logging.Level; |
22 | |
import java.util.logging.Logger; |
23 | |
|
24 | |
import org.apache.commons.lang.StringUtils; |
25 | |
import org.deduced.DeducedModelLayer; |
26 | |
import org.deduced.DeducedSchemaLayer; |
27 | |
import org.deduced.DeducedUtilities; |
28 | |
import org.deduced.OrderedPropertyListType; |
29 | |
import org.deduced.PropertyCollection; |
30 | |
import org.deduced.PropertyCollectionType; |
31 | |
import org.deduced.framework.DeducedModelLayerExtensionImplementation; |
32 | |
import org.deduced.framework.FrameworkUtilities; |
33 | |
import org.deduced.generator.VelocityGenerator; |
34 | |
import org.deduced.utilities.FileUtilities; |
35 | |
import org.deduced.xml.XmlFileParser; |
36 | |
import org.deduced.xml.XmlFileParserImplementation; |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | 2 | public class JavaGeneratorUtilities |
44 | |
{ |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | 1 | private static final Logger LOGGER = Logger |
50 | |
.getLogger(JavaGeneratorUtilities.class.getName()); |
51 | |
|
52 | |
|
53 | 1 | private static XmlFileParser XML_PARSER = new XmlFileParserImplementation(); |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
public static final String JAVA_INTERFACE_TEMPLATE = |
59 | |
"org/deduced/generator/java/JavaInterface.vm"; |
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
public static final String JAVA_IMPLEMENTATION_TEMPLATE = |
65 | |
"org/deduced/generator/java/JavaImplementation.vm"; |
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
public static final String JAVA_FRAMEWORK_TEMPLATE = |
71 | |
"org/deduced/generator/java/JavaFramework.vm"; |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
private static final String JAVA_ORDERED_PROPERTY_LIST_INTERFACE_TEMPLATE = |
77 | |
"org/deduced/generator/java/JavaOrderedPropertyListInterface.vm"; |
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
private static final String JAVA_ORDERED_PROPERTY_LIST_IMPLEMENTATION_TEMPLATE = |
83 | |
"org/deduced/generator/java/JavaOrderedPropertyListImplementation.vm"; |
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
public static void generateImplementationForType( |
95 | |
PropertyCollection<?, ?> type, String outputFolderName) |
96 | |
throws IOException |
97 | |
{ |
98 | 12 | validateTypePackageName(type); |
99 | 12 | String implementationFileName = |
100 | |
JavaNamingUtilities.getImplementationFileName(type); |
101 | 12 | String implementationFolderName = |
102 | |
JavaNamingUtilities.getImplementationFolderName(type); |
103 | |
|
104 | 12 | String folderName = |
105 | |
FileUtilities.mergeFolderName(outputFolderName, |
106 | |
implementationFolderName); |
107 | |
|
108 | 12 | String javaImplementationTemplate = JAVA_IMPLEMENTATION_TEMPLATE; |
109 | |
|
110 | 12 | boolean isOrderedPropertyListType = isOrderedPropertyListType(type); |
111 | |
|
112 | 12 | if (isOrderedPropertyListType) |
113 | |
{ |
114 | 0 | javaImplementationTemplate = |
115 | |
JAVA_ORDERED_PROPERTY_LIST_IMPLEMENTATION_TEMPLATE; |
116 | |
} |
117 | |
|
118 | 12 | generateType(type, javaImplementationTemplate, folderName, |
119 | |
implementationFileName); |
120 | 12 | } |
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
private static void generateType( |
134 | |
PropertyCollection<?, ?> type, String template, String folderName, |
135 | |
String fileName) throws IOException |
136 | |
{ |
137 | 24 | if (LOGGER.isLoggable(Level.FINE)) |
138 | |
{ |
139 | 8 | LOGGER.log(Level.FINE, |
140 | |
"Generating : " + DeducedUtilities.getCollectionName(type) |
141 | |
+ " using template : " + template); |
142 | |
} |
143 | |
|
144 | 24 | ByteArrayOutputStream outStream = new ByteArrayOutputStream(); |
145 | |
|
146 | 24 | VelocityGenerator generator = new VelocityGenerator(); |
147 | 24 | generator.setOutputStream(outStream); |
148 | 24 | generator.setTemplateName(template); |
149 | 24 | generator.getContext().put("type", type); |
150 | 24 | fillCommonGeneratorContext(generator); |
151 | |
|
152 | 24 | generator.generate(); |
153 | |
|
154 | 24 | FileUtilities.forceCreateDirectory(folderName); |
155 | 24 | String output = FileUtilities.mergeFolderName(folderName, fileName); |
156 | |
|
157 | 24 | FileUtilities.writeFileContent(output, outStream.toByteArray()); |
158 | |
|
159 | 24 | outStream.close(); |
160 | 24 | } |
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
public static void generateFrameworkForSchema( |
173 | |
PropertyCollection<?, ?> model, PropertyCollection<?, ?> schema, |
174 | |
String outputFolderName) throws IOException |
175 | |
{ |
176 | 14 | String template = JAVA_FRAMEWORK_TEMPLATE; |
177 | 14 | String schemaName = DeducedUtilities.getCollectionName(schema); |
178 | 14 | String frameworkFileName = |
179 | |
JavaNamingUtilities.getSchemaClassName(schemaName) + ".java"; |
180 | |
|
181 | 14 | String frameworkFolderName = |
182 | |
JavaNamingUtilities.getSchemaFolderName(schemaName); |
183 | |
|
184 | 14 | String folderName = |
185 | |
FileUtilities |
186 | |
.mergeFolderName(outputFolderName, frameworkFolderName); |
187 | |
|
188 | 14 | if (LOGGER.isLoggable(Level.FINE)) |
189 | |
{ |
190 | 5 | LOGGER.log(Level.FINE, "Generating : " + schemaName |
191 | |
+ " using template : " + template); |
192 | |
} |
193 | |
|
194 | 14 | ByteArrayOutputStream outStream = new ByteArrayOutputStream(); |
195 | |
|
196 | 14 | VelocityGenerator generator = new VelocityGenerator(); |
197 | 14 | generator.setOutputStream(outStream); |
198 | 14 | generator.setTemplateName(template); |
199 | 14 | generator.getContext().put("schema", schema); |
200 | 14 | generator.getContext().put("model", model); |
201 | 14 | fillCommonGeneratorContext(generator); |
202 | |
|
203 | 14 | generator.generate(); |
204 | |
|
205 | 14 | FileUtilities.forceCreateDirectory(folderName); |
206 | 14 | String output = |
207 | |
FileUtilities.mergeFolderName(folderName, frameworkFileName); |
208 | |
|
209 | 14 | FileUtilities.writeFileContent(output, outStream.toByteArray()); |
210 | |
|
211 | 14 | outStream.close(); |
212 | 14 | } |
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
public static void generateInterfaceForType( |
223 | |
PropertyCollection<?, ?> type, String outputFolderName) |
224 | |
throws IOException |
225 | |
{ |
226 | 12 | validateTypePackageName(type); |
227 | 12 | String interfaceFileName = |
228 | |
JavaNamingUtilities.getInterfaceFileName(type); |
229 | 12 | String interfaceFolderName = |
230 | |
JavaNamingUtilities.getInterfaceFolderName(type); |
231 | |
|
232 | 12 | String folderName = |
233 | |
FileUtilities |
234 | |
.mergeFolderName(outputFolderName, interfaceFolderName); |
235 | |
|
236 | 12 | String javaInterfaceTemplate = JAVA_INTERFACE_TEMPLATE; |
237 | |
|
238 | 12 | boolean isOrderedPropertyListType = isOrderedPropertyListType(type); |
239 | |
|
240 | 12 | if (isOrderedPropertyListType) |
241 | |
{ |
242 | 0 | javaInterfaceTemplate = |
243 | |
JAVA_ORDERED_PROPERTY_LIST_INTERFACE_TEMPLATE; |
244 | |
} |
245 | |
|
246 | 12 | generateType(type, javaInterfaceTemplate, folderName, interfaceFileName); |
247 | 12 | } |
248 | |
|
249 | |
|
250 | |
|
251 | |
|
252 | |
|
253 | |
|
254 | |
|
255 | |
|
256 | |
public static boolean isOrderedPropertyListType( |
257 | |
PropertyCollection<?, ?> type) |
258 | |
{ |
259 | 24 | PropertyCollectionType orderedPropertyListType = |
260 | |
OrderedPropertyListType.ORDERED_PROPERTY_LIST_TYPE_TYPE |
261 | |
.getPropertyCollectionType(); |
262 | |
|
263 | 24 | PropertyCollection<?, ?> typeOfTheCurrentType = type.type(); |
264 | |
|
265 | 24 | boolean isOrderedPropertyListType = |
266 | |
DeducedUtilities.isInstanceOf(typeOfTheCurrentType, |
267 | |
orderedPropertyListType); |
268 | 24 | return isOrderedPropertyListType; |
269 | |
} |
270 | |
|
271 | |
|
272 | |
|
273 | |
|
274 | |
|
275 | |
|
276 | |
public static void validateTypePackageName( |
277 | |
PropertyCollection<?, ?> type) |
278 | |
{ |
279 | 36 | String interfacePackageName = |
280 | |
JavaNamingUtilities.getInterfacePackageName(type); |
281 | 36 | if (StringUtils.isEmpty(interfacePackageName)) |
282 | |
{ |
283 | 8 | throw new IllegalArgumentException( |
284 | |
"Invalid empty package name for type : " |
285 | |
+ DeducedUtilities.getCollectionName(type)); |
286 | |
} |
287 | 28 | } |
288 | |
|
289 | |
|
290 | |
|
291 | |
|
292 | |
|
293 | |
|
294 | |
|
295 | |
private static void fillCommonGeneratorContext( |
296 | |
VelocityGenerator generator) |
297 | |
{ |
298 | 38 | generator.getContext().put("javaNamingUtilities", |
299 | |
JavaNamingUtilities.DEFAULT); |
300 | 38 | generator.getContext().put("deducedUtilities", new DeducedUtilities()); |
301 | 38 | } |
302 | |
|
303 | |
|
304 | |
|
305 | |
|
306 | |
|
307 | |
|
308 | |
|
309 | |
|
310 | |
|
311 | |
|
312 | |
|
313 | |
|
314 | |
public static void generateFilesForSchema( |
315 | |
PropertyCollection<?, ?> model, PropertyCollection<?, ?> schema, |
316 | |
String outputFolderName) throws IOException |
317 | |
{ |
318 | 10 | List<PropertyCollection<?, ?>> propertyCollectionTypeListFromSchema = |
319 | |
JavaNamingUtilities.getPropertyCollectionTypeListFromSchema(schema); |
320 | |
|
321 | 10 | generateFrameworkForSchema(model, schema, outputFolderName); |
322 | |
|
323 | 10 | for (PropertyCollection<?, ?> currentType : propertyCollectionTypeListFromSchema) |
324 | |
{ |
325 | 8 | generateImplementationForType(currentType, outputFolderName); |
326 | 8 | generateInterfaceForType(currentType, outputFolderName); |
327 | |
} |
328 | 10 | } |
329 | |
|
330 | |
|
331 | |
|
332 | |
|
333 | |
|
334 | |
|
335 | |
|
336 | |
|
337 | |
|
338 | |
|
339 | |
|
340 | |
public static void generateSchemaFromModel( |
341 | |
String outputFolderName, PropertyCollection<?, ?> schemaModelToGenerate) |
342 | |
throws IOException |
343 | |
{ |
344 | 6 | PropertyCollection<?, ?> collectionList = |
345 | |
(PropertyCollection<?, ?>) schemaModelToGenerate |
346 | |
.getPropertyValue(DeducedModelLayer.COLLECTION_LIST_INSTANCE |
347 | |
.getKey()); |
348 | |
|
349 | 6 | List<?> schemaValueList = collectionList.asValueList(); |
350 | 6 | for (Object object : schemaValueList) |
351 | |
{ |
352 | 10 | PropertyCollection<?, ?> possibleSchema = |
353 | |
(PropertyCollection<?, ?>) object; |
354 | |
|
355 | 10 | if (DeducedUtilities.isInstanceOf(possibleSchema.type(), |
356 | |
DeducedSchemaLayer.DEDUCED_SCHEMA_LAYER_TYPE |
357 | |
.getPropertyCollectionType())) |
358 | |
{ |
359 | 8 | JavaGeneratorUtilities.generateFilesForSchema( |
360 | |
schemaModelToGenerate, possibleSchema, outputFolderName); |
361 | |
} |
362 | 10 | } |
363 | 6 | } |
364 | |
|
365 | |
|
366 | |
|
367 | |
|
368 | |
|
369 | |
|
370 | |
|
371 | |
|
372 | |
|
373 | |
|
374 | |
|
375 | |
|
376 | |
|
377 | |
|
378 | |
|
379 | |
@SuppressWarnings("unchecked") |
380 | |
public static PropertyCollection<?, ?> loadModelFromXmlFile( |
381 | |
String xmlFileName, PropertyCollection<?, ?> generatorApplicationModel, |
382 | |
boolean ignoreXmlErrors) throws Exception |
383 | |
{ |
384 | 19 | DeducedModelLayerExtensionImplementation newSchemaModel = |
385 | |
FrameworkUtilities.createDynamicModelLayer(); |
386 | |
|
387 | 19 | XmlFileParser xmlParser = getXmlParser(); |
388 | |
|
389 | 19 | xmlParser.readModelLayer(xmlFileName, newSchemaModel, |
390 | |
generatorApplicationModel, ignoreXmlErrors); |
391 | |
|
392 | 9 | Object collectionListKey = |
393 | |
DeducedModelLayer.COLLECTION_LIST_INSTANCE.getKey(); |
394 | |
|
395 | 9 | PropertyCollection collectionList = |
396 | |
(PropertyCollection) generatorApplicationModel |
397 | |
.getPropertyValue(collectionListKey); |
398 | |
|
399 | 9 | collectionList.addProperty(null, null, newSchemaModel); |
400 | |
|
401 | 9 | return newSchemaModel; |
402 | |
} |
403 | |
|
404 | |
|
405 | |
|
406 | |
|
407 | |
public static void setXmlParser( |
408 | |
XmlFileParser setXmlParser) |
409 | |
{ |
410 | 0 | XML_PARSER = setXmlParser; |
411 | 0 | } |
412 | |
|
413 | |
|
414 | |
|
415 | |
|
416 | |
public static XmlFileParser getXmlParser() |
417 | |
{ |
418 | 19 | return XML_PARSER; |
419 | |
} |
420 | |
|
421 | |
} |