1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.deduced.generator.java; |
17 | |
|
18 | |
import org.apache.commons.cli.CommandLine; |
19 | |
import org.apache.commons.cli.CommandLineParser; |
20 | |
import org.apache.commons.cli.HelpFormatter; |
21 | |
import org.apache.commons.cli.Option; |
22 | |
import org.apache.commons.cli.Options; |
23 | |
import org.apache.commons.cli.ParseException; |
24 | |
import org.apache.commons.cli.PosixParser; |
25 | |
import org.apache.commons.lang.StringUtils; |
26 | |
import org.deduced.AbstractPropertyCollection; |
27 | |
import org.deduced.DeducedModelLayerExtension; |
28 | |
import org.deduced.PropertyCollection; |
29 | |
import org.deduced.framework.FrameworkLoader; |
30 | |
import org.deduced.utilities.LogUtilities; |
31 | |
import org.deduced.utilities.SystemExit; |
32 | |
import org.deduced.utilities.SystemExitImplementation; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | 1 | public class CommandLineJavaGenerator |
40 | |
{ |
41 | |
|
42 | 1 | private static SystemExit EXIT = new SystemExitImplementation(); |
43 | |
|
44 | |
|
45 | |
public static final String HELP_OPTION_SHORT_NAME = "h"; |
46 | |
|
47 | |
|
48 | |
public static final String OUTPUT_OPTION_SHORT_NAME = "o"; |
49 | |
|
50 | |
|
51 | |
public static final String INPUT_OPTION_SHORT_NAME = "i"; |
52 | |
|
53 | |
|
54 | |
public static final String IGNORING_XML_LOADING_ERROR_NAME = "x"; |
55 | |
|
56 | |
|
57 | |
public static final String DEPENDENCY_OPTION_SHORT_NAME = "d"; |
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
public static void main( |
65 | |
String[] arguments) |
66 | |
{ |
67 | 2 | LogUtilities.readLoggerConfigurationOnce(true, new String[]{ |
68 | |
"logging.properties", "org/deduced/generator/logging.properties" }); |
69 | 2 | if (!runGeneratorBasedOnArguments(arguments)) |
70 | |
{ |
71 | 1 | getExit().exit(-1); |
72 | |
} |
73 | 2 | } |
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
public static boolean runGeneratorBasedOnArguments( |
82 | |
String[] arguments) |
83 | |
{ |
84 | 2 | boolean returnValue = false; |
85 | 2 | FrameworkLoader.createBaseSchemaModel(); |
86 | 2 | AbstractPropertyCollection |
87 | |
.setCheckDeleteMode(AbstractPropertyCollection.CHECK_DELETE_MODE_MANUAL); |
88 | |
|
89 | 2 | Options createOptions = createOptions(); |
90 | |
|
91 | 2 | GeneratorOptions options = new GeneratorOptions(); |
92 | 2 | if (readOptions(arguments, createOptions, options)) |
93 | |
{ |
94 | 1 | returnValue = executeActionBasedOnOption(options); |
95 | |
} |
96 | 2 | return returnValue; |
97 | |
} |
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
public static boolean executeActionBasedOnOption( |
108 | |
GeneratorOptions options) |
109 | |
{ |
110 | 4 | boolean retVal = true; |
111 | 4 | if (options.isShowHelp()) |
112 | |
{ |
113 | 2 | printHelp(); |
114 | |
} |
115 | |
else |
116 | |
{ |
117 | 2 | retVal = runGenerator(options); |
118 | |
} |
119 | 4 | return retVal; |
120 | |
} |
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
public static boolean runGenerator( |
129 | |
GeneratorOptions options) |
130 | |
{ |
131 | 12 | boolean retVal = false; |
132 | |
try |
133 | |
{ |
134 | 12 | DeducedModelLayerExtension hub = |
135 | |
FrameworkLoader |
136 | |
.createApplicationModel("Deduced Generator Application"); |
137 | |
|
138 | 12 | if (StringUtils.isBlank(options.getInputFileName())) |
139 | |
{ |
140 | 2 | throw new IllegalArgumentException("No input file specified"); |
141 | |
} |
142 | |
|
143 | 10 | String[] dependencyFileNameList = |
144 | |
options.getDependencyFileNameList(); |
145 | 10 | boolean ignoringXmlLoadingErrors = |
146 | |
options.isIgnoringXmlLoadingErrors(); |
147 | 10 | if (dependencyFileNameList != null) |
148 | |
{ |
149 | 8 | for (String currentDependency : dependencyFileNameList) |
150 | |
{ |
151 | 5 | if (!StringUtils.isEmpty(currentDependency)) |
152 | |
{ |
153 | 5 | JavaGeneratorUtilities.loadModelFromXmlFile( |
154 | |
currentDependency, hub, ignoringXmlLoadingErrors); |
155 | |
} |
156 | |
} |
157 | |
} |
158 | 8 | PropertyCollection<?, ?> schemaModelToGenerate = null; |
159 | 8 | String inputFileName = options.getInputFileName(); |
160 | |
|
161 | 8 | schemaModelToGenerate = |
162 | |
JavaGeneratorUtilities.loadModelFromXmlFile(inputFileName, hub, |
163 | |
ignoringXmlLoadingErrors); |
164 | |
|
165 | 4 | JavaSchemaGenerator javaGenerator = |
166 | |
new JavaSchemaGeneratorImplementation(); |
167 | |
|
168 | 4 | javaGenerator.generateSchemaFromModel( |
169 | |
options.getOutputFolderName(), schemaModelToGenerate); |
170 | |
|
171 | 4 | retVal = true; |
172 | |
} |
173 | 8 | catch (Exception e) |
174 | |
{ |
175 | 8 | handleError(e, "Failed to generate files"); |
176 | 8 | printHelp(); |
177 | 4 | } |
178 | |
|
179 | 12 | return retVal; |
180 | |
} |
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
private static void handleError( |
189 | |
Exception error, String errorContext) |
190 | |
{ |
191 | 12 | String errorMessage = errorContext + " : " + error.getMessage(); |
192 | 12 | setLastError(errorMessage); |
193 | 12 | System.err.println(errorMessage); |
194 | 12 | error.printStackTrace(); |
195 | 12 | } |
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | 1 | private static String LAST_ERROR = null; |
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
public static boolean readOptions( |
213 | |
String[] arguments, Options createOptions, GeneratorOptions options) |
214 | |
{ |
215 | |
try |
216 | |
{ |
217 | 10 | CommandLineParser parser = new PosixParser(); |
218 | 10 | CommandLine cmd = parser.parse(createOptions, arguments); |
219 | |
|
220 | 6 | options.setShowHelp(cmd.hasOption(HELP_OPTION_SHORT_NAME)); |
221 | 6 | options.setOutputFolderName(cmd |
222 | |
.getOptionValue(OUTPUT_OPTION_SHORT_NAME)); |
223 | 6 | options.setInputFileName(cmd |
224 | |
.getOptionValue(INPUT_OPTION_SHORT_NAME)); |
225 | 6 | options.setDependencyFileNameList(cmd |
226 | |
.getOptionValues(DEPENDENCY_OPTION_SHORT_NAME)); |
227 | 6 | options.setIgnoringXmlLoadingErrors(cmd |
228 | |
.hasOption(IGNORING_XML_LOADING_ERROR_NAME)); |
229 | |
} |
230 | 4 | catch (ParseException ex) |
231 | |
{ |
232 | 4 | handleError(ex, "Parsing command line arguments failed."); |
233 | 4 | printHelp(); |
234 | |
|
235 | 4 | return false; |
236 | 6 | } |
237 | 6 | return true; |
238 | |
} |
239 | |
|
240 | |
|
241 | |
|
242 | |
|
243 | |
public static void printHelp() |
244 | |
{ |
245 | 14 | printHelp(createOptions()); |
246 | 14 | } |
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | |
|
252 | |
|
253 | |
public static void printHelp( |
254 | |
Options options) |
255 | |
{ |
256 | 15 | HelpFormatter formatter = new HelpFormatter(); |
257 | 15 | formatter.printHelp("javagenerator", options); |
258 | 15 | } |
259 | |
|
260 | |
|
261 | |
|
262 | |
|
263 | |
|
264 | |
|
265 | |
public static Options createOptions() |
266 | |
{ |
267 | 18 | Options retVal = new Options(); |
268 | |
|
269 | 18 | retVal.addOption(createInputOption()); |
270 | 18 | retVal.addOption(createOutputOption()); |
271 | 18 | retVal.addOption(createDependenciesOption()); |
272 | 18 | retVal.addOption(createHelpOption()); |
273 | 18 | retVal.addOption(createIgnoringXmlLoadingErrorOption()); |
274 | |
|
275 | 18 | return retVal; |
276 | |
} |
277 | |
|
278 | |
|
279 | |
|
280 | |
|
281 | |
|
282 | |
|
283 | |
public static Option createHelpOption() |
284 | |
{ |
285 | 18 | Option helpOption = |
286 | |
new Option(HELP_OPTION_SHORT_NAME, "Print this message."); |
287 | |
|
288 | |
|
289 | 18 | helpOption.setLongOpt("help"); |
290 | |
|
291 | 18 | return helpOption; |
292 | |
} |
293 | |
|
294 | |
|
295 | |
|
296 | |
|
297 | |
|
298 | |
|
299 | |
public static Option createOutputOption() |
300 | |
{ |
301 | 18 | Option dependenciesOption = |
302 | |
new Option( |
303 | |
OUTPUT_OPTION_SHORT_NAME, |
304 | |
"Output folder name where the generated files will be created. " |
305 | |
+ "If the folder doesn't exist, the generator will create it. " |
306 | |
+ "This option isn't mandatory. If it isn't specified, the output will be " |
307 | |
+ "in the current folder."); |
308 | |
|
309 | |
|
310 | 18 | dependenciesOption.setLongOpt("output"); |
311 | 18 | dependenciesOption.setArgs(1); |
312 | 18 | dependenciesOption.setArgName("FOLDER"); |
313 | |
|
314 | 18 | return dependenciesOption; |
315 | |
} |
316 | |
|
317 | |
|
318 | |
|
319 | |
|
320 | |
|
321 | |
|
322 | |
public static Option createInputOption() |
323 | |
{ |
324 | 18 | Option inputOption = |
325 | |
new Option( |
326 | |
INPUT_OPTION_SHORT_NAME, |
327 | |
"Input file name. This should point to a valid XML file that contains " |
328 | |
+ "a deduced schema layer. Use quotes to enter file names with blank spaces. " |
329 | |
+ "This option is mandatory."); |
330 | |
|
331 | |
|
332 | 18 | inputOption.setLongOpt("input"); |
333 | 18 | inputOption.setArgs(1); |
334 | 18 | inputOption.setArgName("FILE"); |
335 | |
|
336 | 18 | return inputOption; |
337 | |
} |
338 | |
|
339 | |
|
340 | |
|
341 | |
|
342 | |
|
343 | |
|
344 | |
public static Option createIgnoringXmlLoadingErrorOption() |
345 | |
{ |
346 | 18 | Option inputOption = |
347 | |
new Option(IGNORING_XML_LOADING_ERROR_NAME, |
348 | |
"Ignore XML loading errors encountered while loading the input file or " |
349 | |
+ "it's dependencies. Default setting is false."); |
350 | |
|
351 | |
|
352 | 18 | inputOption.setLongOpt("ignoreXmlLoadingErrors"); |
353 | |
|
354 | 18 | return inputOption; |
355 | |
} |
356 | |
|
357 | |
|
358 | |
|
359 | |
|
360 | |
|
361 | |
|
362 | |
public static Option createDependenciesOption() |
363 | |
{ |
364 | 18 | Option dependenciesOption = |
365 | |
new Option( |
366 | |
DEPENDENCY_OPTION_SHORT_NAME, |
367 | |
"List of dependent schema files required to generate the current schema. " |
368 | |
+ "Use a list of file names separated by a semicolon. The files have to " |
369 | |
+ "be in order of dependency. Example : " |
370 | |
+ "--dependencies=\"file1.xml;file2.xml\""); |
371 | |
|
372 | |
|
373 | 18 | dependenciesOption.setLongOpt("dependencies"); |
374 | 18 | dependenciesOption.setArgs(Option.UNLIMITED_VALUES); |
375 | 18 | dependenciesOption.setValueSeparator(';'); |
376 | 18 | dependenciesOption.setArgName("FILES"); |
377 | |
|
378 | 18 | return dependenciesOption; |
379 | |
} |
380 | |
|
381 | |
|
382 | |
|
383 | |
|
384 | |
public static void setExit( |
385 | |
SystemExit setExit) |
386 | |
{ |
387 | 9 | EXIT = setExit; |
388 | 9 | } |
389 | |
|
390 | |
|
391 | |
|
392 | |
|
393 | |
public static SystemExit getExit() |
394 | |
{ |
395 | 1 | return EXIT; |
396 | |
} |
397 | |
|
398 | |
|
399 | |
|
400 | |
|
401 | |
public static void setLastError( |
402 | |
String setLastError) |
403 | |
{ |
404 | 12 | LAST_ERROR = setLastError; |
405 | 12 | } |
406 | |
|
407 | |
|
408 | |
|
409 | |
|
410 | |
public static String getLastError() |
411 | |
{ |
412 | 2 | return LAST_ERROR; |
413 | |
} |
414 | |
} |