1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package com.legstar.cob2xsd; |
12 | |
|
13 | |
import java.io.File; |
14 | |
import java.io.FileInputStream; |
15 | |
import java.io.FileNotFoundException; |
16 | |
import java.io.IOException; |
17 | |
import java.io.InputStream; |
18 | |
import java.util.Properties; |
19 | |
|
20 | |
import org.apache.commons.cli.CommandLine; |
21 | |
import org.apache.commons.cli.CommandLineParser; |
22 | |
import org.apache.commons.cli.HelpFormatter; |
23 | |
import org.apache.commons.cli.Option; |
24 | |
import org.apache.commons.cli.Options; |
25 | |
import org.apache.commons.cli.PosixParser; |
26 | |
import org.apache.commons.io.FileUtils; |
27 | |
import org.apache.commons.io.FilenameUtils; |
28 | |
import org.apache.commons.logging.Log; |
29 | |
import org.apache.commons.logging.LogFactory; |
30 | |
|
31 | |
import com.legstar.antlr.RecognizerException; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | 6 | public class Cob2XsdMain { |
45 | |
|
46 | |
|
47 | |
private static final String VERSION_FILE_NAME = "/com/legstar/cob2xsd/version.properties"; |
48 | |
|
49 | |
|
50 | |
private static final String DEFAULT_INPUT_FOLDER = "cobol"; |
51 | |
|
52 | |
|
53 | |
private static final String DEFAULT_OUTPUT_FOLDER = "schema"; |
54 | |
|
55 | |
|
56 | |
private static final String DEFAULT_CONFIG_FILE = "conf/cob2xsd.properties"; |
57 | |
|
58 | |
|
59 | |
private File _configFile; |
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
private File _input; |
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
private File _output; |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
private boolean _appendBaseFileNameToNamespace; |
78 | |
|
79 | |
|
80 | |
private Cob2XsdModel _model; |
81 | |
|
82 | |
|
83 | 1 | public static final String LS = System.getProperty("line.separator"); |
84 | |
|
85 | |
|
86 | 6 | private final Log _log = LogFactory.getLog(getClass()); |
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
public static void main(final String[] args) { |
92 | 0 | Cob2XsdMain main = new Cob2XsdMain(); |
93 | 0 | main.execute(args); |
94 | 0 | } |
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
public void execute(final String[] args) { |
105 | |
try { |
106 | 2 | Options options = createOptions(); |
107 | 2 | if (collectOptions(options, args)) { |
108 | 2 | setDefaults(); |
109 | 2 | loadModel(); |
110 | 2 | execute(getInput(), getOutput()); |
111 | |
} |
112 | 0 | } catch (Exception e) { |
113 | 0 | _log.error("COBOL to Xsd translation failure", e); |
114 | 0 | throw new RuntimeException(e); |
115 | 2 | } |
116 | 2 | } |
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
protected boolean collectOptions(final Options options, final String[] args) |
130 | |
throws Exception { |
131 | 6 | if (args != null && args.length > 0) { |
132 | 5 | CommandLineParser parser = new PosixParser(); |
133 | 5 | CommandLine line = parser.parse(options, args); |
134 | 4 | return processLine(line, options); |
135 | |
} |
136 | 1 | return true; |
137 | |
} |
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
protected void setDefaults() { |
143 | 2 | if (getConfigFile() == null) { |
144 | 0 | setConfigFile(DEFAULT_CONFIG_FILE); |
145 | |
} |
146 | 2 | if (getInput() == null) { |
147 | 0 | setInput(DEFAULT_INPUT_FOLDER); |
148 | |
} |
149 | 2 | if (getOutput() == null) { |
150 | 0 | setOutput(DEFAULT_OUTPUT_FOLDER); |
151 | |
} |
152 | 2 | } |
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
protected void loadModel() throws XsdGenerationException { |
161 | |
try { |
162 | 2 | if (getConfigFile() == null) { |
163 | 0 | _model = new Cob2XsdModel(); |
164 | |
} else { |
165 | 2 | Properties config = new Properties(); |
166 | 2 | config.load(new FileInputStream(getConfigFile())); |
167 | 2 | _model = new Cob2XsdModel(config); |
168 | |
} |
169 | 0 | } catch (FileNotFoundException e) { |
170 | 0 | throw new XsdGenerationException(e); |
171 | 0 | } catch (IOException e) { |
172 | 0 | throw new XsdGenerationException(e); |
173 | 2 | } |
174 | 2 | } |
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
protected void produceHelp(final Options options) throws Exception { |
181 | 1 | HelpFormatter formatter = new HelpFormatter(); |
182 | 1 | String version = getVersion(); |
183 | 1 | formatter.printHelp( |
184 | |
"java -jar legstar-cob2xsd-" |
185 | |
+ version.substring(0, version.indexOf(' ')) |
186 | |
+ "-exe.jar followed by:", options); |
187 | 1 | } |
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
protected Options createOptions() { |
193 | 6 | Options options = new Options(); |
194 | |
|
195 | 6 | Option version = new Option("v", "version", false, |
196 | |
"print the version information and exit"); |
197 | 6 | options.addOption(version); |
198 | |
|
199 | 6 | Option help = new Option("h", "help", false, |
200 | |
"print the options available"); |
201 | 6 | options.addOption(help); |
202 | |
|
203 | 6 | Option configFile = new Option("c", "config", true, |
204 | |
"path to configuration file"); |
205 | 6 | options.addOption(configFile); |
206 | |
|
207 | 6 | Option input = new Option("i", "input", true, |
208 | |
"file or folder holding the COBOL code to translate." |
209 | |
+ " Name is relative or absolute"); |
210 | 6 | options.addOption(input); |
211 | |
|
212 | 6 | Option output = new Option("o", "output", true, |
213 | |
"folder or file receiving the translated XML schema"); |
214 | 6 | options.addOption(output); |
215 | |
|
216 | 6 | Option appendBaseFileNameToNamespace = new Option("a", |
217 | |
"appendBaseFileNameToNamespace", false, |
218 | |
"add input base file name to namespace"); |
219 | 6 | options.addOption(appendBaseFileNameToNamespace); |
220 | |
|
221 | 6 | return options; |
222 | |
} |
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | |
protected boolean processLine(final CommandLine line, final Options options) |
233 | |
throws Exception { |
234 | 4 | if (line.hasOption("version")) { |
235 | 0 | System.out.println("version " + getVersion()); |
236 | 0 | return false; |
237 | |
} |
238 | 4 | if (line.hasOption("help")) { |
239 | 1 | produceHelp(options); |
240 | 1 | return false; |
241 | |
} |
242 | 3 | if (line.hasOption("config")) { |
243 | 2 | setConfigFile(line.getOptionValue("config").trim()); |
244 | |
} |
245 | 3 | if (line.hasOption("input")) { |
246 | 3 | setInput(line.getOptionValue("input").trim()); |
247 | |
} |
248 | 2 | if (line.hasOption("output")) { |
249 | 2 | setOutput(line.getOptionValue("output").trim()); |
250 | |
} |
251 | |
|
252 | 2 | if (line.hasOption("appendBaseFileNameToNamespace")) { |
253 | 1 | _appendBaseFileNameToNamespace = true; |
254 | |
} |
255 | |
|
256 | 2 | return true; |
257 | |
} |
258 | |
|
259 | |
|
260 | |
|
261 | |
|
262 | |
|
263 | |
|
264 | |
|
265 | |
|
266 | |
|
267 | |
protected void execute(final File input, final File target) |
268 | |
throws XsdGenerationException { |
269 | |
|
270 | |
try { |
271 | 2 | _log.info("Started translation from COBOL to XML Schema"); |
272 | 2 | _log.info("Taking COBOL from : " + input); |
273 | 2 | _log.info("Output XML Schema to : " + target); |
274 | 2 | _log.info("Options in effect : " + getModel().toString()); |
275 | 2 | _log.info("Append base file name : " |
276 | |
+ _appendBaseFileNameToNamespace); |
277 | |
|
278 | 2 | if (input.isFile()) { |
279 | 2 | if (FilenameUtils.getExtension(target.getPath()).length() == 0) { |
280 | 0 | FileUtils.forceMkdir(target); |
281 | |
} |
282 | 2 | translate(input, target); |
283 | |
} else { |
284 | 0 | FileUtils.forceMkdir(target); |
285 | 0 | for (File cobolFile : input.listFiles()) { |
286 | 0 | if (cobolFile.isFile()) { |
287 | 0 | translate(cobolFile, target); |
288 | |
} |
289 | |
} |
290 | |
} |
291 | 2 | _log.info("Finished translation"); |
292 | 0 | } catch (IOException e) { |
293 | 0 | throw new XsdGenerationException(e); |
294 | 2 | } |
295 | |
|
296 | 2 | } |
297 | |
|
298 | |
|
299 | |
|
300 | |
|
301 | |
|
302 | |
|
303 | |
|
304 | |
|
305 | |
protected void translate(final File cobolFile, final File target) |
306 | |
throws XsdGenerationException { |
307 | |
try { |
308 | 2 | Cob2XsdIO cob2XsdIO = new Cob2XsdIO(getModel()); |
309 | 2 | cob2XsdIO.translate(cobolFile, target, |
310 | |
_appendBaseFileNameToNamespace); |
311 | 0 | } catch (RecognizerException e) { |
312 | 0 | throw new XsdGenerationException(e); |
313 | 2 | } |
314 | 2 | } |
315 | |
|
316 | |
|
317 | |
|
318 | |
|
319 | |
|
320 | |
|
321 | |
|
322 | |
protected String getVersion() throws IOException { |
323 | 1 | InputStream stream = null; |
324 | |
try { |
325 | 1 | Properties version = new Properties(); |
326 | 1 | stream = Cob2XsdMain.class.getResourceAsStream(VERSION_FILE_NAME); |
327 | 1 | version.load(stream); |
328 | 1 | return version.getProperty("version"); |
329 | |
} finally { |
330 | 1 | if (stream != null) { |
331 | 1 | stream.close(); |
332 | |
} |
333 | |
} |
334 | |
} |
335 | |
|
336 | |
|
337 | |
|
338 | |
|
339 | |
public File getConfigFile() { |
340 | 6 | return _configFile; |
341 | |
} |
342 | |
|
343 | |
|
344 | |
|
345 | |
|
346 | |
|
347 | |
|
348 | |
|
349 | |
|
350 | |
public void setConfigFile(final String config) { |
351 | 2 | if (config == null) { |
352 | 0 | throw (new IllegalArgumentException( |
353 | |
"You must provide a configuration file")); |
354 | |
} |
355 | 2 | File file = new File(config); |
356 | 2 | if (file.exists()) { |
357 | 2 | if (file.isDirectory()) { |
358 | 0 | throw new IllegalArgumentException("Folder " + config |
359 | |
+ " is not a configuration file"); |
360 | |
} |
361 | |
} else { |
362 | 0 | if (config.equals(DEFAULT_CONFIG_FILE)) { |
363 | 0 | file = null; |
364 | |
} else { |
365 | 0 | throw new IllegalArgumentException("Configuration file " |
366 | |
+ config + " not found"); |
367 | |
} |
368 | |
} |
369 | 2 | setConfigFile(file); |
370 | 2 | } |
371 | |
|
372 | |
|
373 | |
|
374 | |
|
375 | |
public void setConfigFile(final File configFile) { |
376 | 2 | _configFile = configFile; |
377 | 2 | } |
378 | |
|
379 | |
|
380 | |
|
381 | |
|
382 | |
|
383 | |
|
384 | |
public void setInput(final String input) { |
385 | 3 | if (input == null) { |
386 | 0 | throw (new IllegalArgumentException( |
387 | |
"You must provide a COBOL source folder or file")); |
388 | |
} |
389 | 3 | File file = new File(input); |
390 | 3 | if (file.exists()) { |
391 | 2 | if (file.isDirectory() && file.list().length == 0) { |
392 | 0 | throw new IllegalArgumentException("Folder " + input |
393 | |
+ " is empty"); |
394 | |
} |
395 | |
} else { |
396 | 1 | throw new IllegalArgumentException("Input file or folder " + input |
397 | |
+ " not found"); |
398 | |
} |
399 | 2 | _input = file; |
400 | 2 | } |
401 | |
|
402 | |
|
403 | |
|
404 | |
|
405 | |
|
406 | |
|
407 | |
public void setOutput(final String output) { |
408 | 2 | if (output == null) { |
409 | 0 | throw (new IllegalArgumentException( |
410 | |
"You must provide a target directory or file")); |
411 | |
} |
412 | 2 | _output = new File(output); |
413 | 2 | } |
414 | |
|
415 | |
|
416 | |
|
417 | |
|
418 | |
|
419 | |
|
420 | |
public Cob2XsdModel getModel() { |
421 | 4 | return _model; |
422 | |
} |
423 | |
|
424 | |
|
425 | |
|
426 | |
|
427 | |
public File getInput() { |
428 | 4 | return _input; |
429 | |
} |
430 | |
|
431 | |
|
432 | |
|
433 | |
|
434 | |
public File getOutput() { |
435 | 4 | return _output; |
436 | |
} |
437 | |
|
438 | |
} |