1 | package org.etsi.t3q; |
---|
2 | |
---|
3 | import java.io.File; |
---|
4 | |
---|
5 | import java.util.ArrayList; |
---|
6 | import java.util.Comparator; |
---|
7 | import java.util.HashMap; |
---|
8 | import java.util.List; |
---|
9 | |
---|
10 | import org.apache.commons.cli.CommandLine; |
---|
11 | import org.apache.commons.cli.CommandLineParser; |
---|
12 | import org.apache.commons.cli.GnuParser; |
---|
13 | import org.apache.commons.cli.HelpFormatter; |
---|
14 | import org.apache.commons.cli.Option; |
---|
15 | import org.apache.commons.cli.ParseException; |
---|
16 | import org.etsi.t3q.config.QualityCheckProfile; |
---|
17 | import org.etsi.t3q.config.T3QConfig; |
---|
18 | import org.etsi.t3q.config.T3QOptionsHandler; |
---|
19 | import org.etsi.t3q.visitor.T3QVisitor; |
---|
20 | import org.etsi.common.InputInterface; |
---|
21 | import org.etsi.common.MiscTools; |
---|
22 | import org.etsi.common.configuration.ConfigTools; |
---|
23 | import org.etsi.common.exceptions.TerminationException; |
---|
24 | import org.etsi.common.logging.LoggingInterface.LogLevel; |
---|
25 | import org.etsi.common.logging.LoggingInterface.MessageClass; |
---|
26 | |
---|
27 | import antlr.MismatchedTokenException; |
---|
28 | import antlr.RecognitionException; |
---|
29 | import antlr.TokenStreamException; |
---|
30 | import de.ugoe.cs.swe.trex.core.analyzer.rfparser.LocationAST; |
---|
31 | import de.ugoe.cs.swe.trex.core.analyzer.rfparser.TTCN3Analyzer; |
---|
32 | import de.ugoe.cs.swe.trex.core.analyzer.rfparser.TTCN3AnalyzerFlyweightFactory; |
---|
33 | import de.ugoe.cs.swe.trex.core.analyzer.rfparser.TTCN3Parser; |
---|
34 | import de.ugoe.cs.swe.trex.core.formatter.TTCN3Formatter; |
---|
35 | import de.ugoe.cs.swe.trex.core.visitor.TTCN3BehaviorException; |
---|
36 | import de.ugoe.cs.swe.trex.core.visitor.TTCN3ParserException; |
---|
37 | |
---|
38 | public class T3Q { |
---|
39 | |
---|
40 | private static String versionNumber = "v1.0.4"; |
---|
41 | //set during automated server builds |
---|
42 | private static String buildStamp = "---BUILD_STAMP---"; |
---|
43 | public static QualityCheckProfile activeProfile = null; |
---|
44 | private String configurationClassName = T3QConfig.class.getName(); |
---|
45 | private String configurationProfileClassName = QualityCheckProfile.class.getName(); |
---|
46 | private String configurationFilename; |
---|
47 | private String selectedProfileName = null; |
---|
48 | private static LogLevel logLevel = LogLevel.INFORMATION; |
---|
49 | |
---|
50 | private HashMap<String, String> argsMap = new HashMap<String, String>(); |
---|
51 | private ArrayList<String> inputPaths = new ArrayList<String>(); |
---|
52 | private String destinationPath = null; |
---|
53 | |
---|
54 | private HashMap<String, Integer> linesOfCodeMap = new HashMap<String, Integer>(); |
---|
55 | private int totalLoc = 0; |
---|
56 | |
---|
57 | private boolean generateNewConfiguration = false; |
---|
58 | |
---|
59 | private boolean formattingEnabled = false; |
---|
60 | //TODO: statistics shall be ideally based on XSLT, meaning the output shall be transformed into XML |
---|
61 | //TODO: also add exception checking for the profiles |
---|
62 | public T3Q() { |
---|
63 | } |
---|
64 | |
---|
65 | // -------------------------------------------------------------------------- |
---|
66 | |
---|
67 | public void showHelp() { |
---|
68 | System.out.println("\nHelp:"); |
---|
69 | // TODO: update help |
---|
70 | // TODO: transfer to t3d as well |
---|
71 | // System.out.println(" t3q[.bat] [options] (path | filename)+"); |
---|
72 | // System.out.println(""); |
---|
73 | // System.out.println(" Options (specify in any order): "); |
---|
74 | // System.out.println(" --help - prints this screen"); |
---|
75 | // System.out |
---|
76 | // .println(" --profile [profilename] - allows manual profile selection, overriding the selected default profile"); |
---|
77 | // // System.out.println(" --output [path] "); |
---|
78 | // System.out.println(""); |
---|
79 | |
---|
80 | HelpFormatter formatter = new HelpFormatter(); |
---|
81 | formatter.setOptionComparator(new Comparator<Option>() { |
---|
82 | |
---|
83 | @Override |
---|
84 | public int compare(Option o1, Option o2) { |
---|
85 | if (o1.getId() > o2.getId()) { |
---|
86 | return 1; |
---|
87 | } else { |
---|
88 | return -1; |
---|
89 | } |
---|
90 | } |
---|
91 | }); |
---|
92 | // formatter.setWidth(120); |
---|
93 | formatter.setSyntaxPrefix(" Usage: "); |
---|
94 | formatter.printHelp("t3q [options] (filename | path)+", " Options: (in any order, config is required)", |
---|
95 | new T3QOptionsHandler().getOptions(), ""); |
---|
96 | System.out.println(""); |
---|
97 | } |
---|
98 | |
---|
99 | // -------------------------------------------------------------------------- |
---|
100 | private void showDebugInfo(String[] args) { |
---|
101 | if (getLogLevel().equals(LogLevel.DEBUG)) { |
---|
102 | System.out.println("==================ARGS:==================="); |
---|
103 | for (int a = 0; a < args.length; a++) { |
---|
104 | System.out.println(" " + args[a]); |
---|
105 | } |
---|
106 | System.out.println("==================PROPERTIES:============="); |
---|
107 | for(Object key : System.getProperties().keySet()) { |
---|
108 | System.out.println(" "+key.toString() + " : "+System.getProperty(key.toString())); |
---|
109 | } |
---|
110 | |
---|
111 | System.out.println("=========================================="); |
---|
112 | } |
---|
113 | } |
---|
114 | |
---|
115 | // -------------------------------------------------------------------------- |
---|
116 | //TODO: Revise and reorganize for reusability |
---|
117 | public void run(String[] args) { |
---|
118 | System.out.println("T3Q " + getVersionNumber()); |
---|
119 | System.out.println("Build " + getBuildStamp()); |
---|
120 | System.out.println(" TTCN-3 version supported: " |
---|
121 | + TTCN3Parser.getSupportedVersion()); |
---|
122 | System.out.println("=========================================="); |
---|
123 | try { |
---|
124 | if (handleCommandLineArguments(args) == false) { |
---|
125 | return; |
---|
126 | } |
---|
127 | showDebugInfo(args); |
---|
128 | if (selectedProfileName != null) { |
---|
129 | handleConfig(selectedProfileName); |
---|
130 | } else { |
---|
131 | handleConfig(null); |
---|
132 | } |
---|
133 | } catch (TerminationException e) { |
---|
134 | // TODO: handle exception |
---|
135 | System.out.println("ERRORING OUT!"); |
---|
136 | } |
---|
137 | |
---|
138 | TTCN3Parser.disableStatementBlockCompatibilityMode(); |
---|
139 | TTCN3Parser.enableStatementBlockCompatibilityMode(); |
---|
140 | |
---|
141 | List<String> ttcn3Resources = new InputInterface(T3Q.activeProfile.getResourceExtensionsRegExp(), T3Q.activeProfile.getProjectExtension(), |
---|
142 | T3Q.activeProfile.isSettingRecursiveProcessing()).getInputFromParameterList(inputPaths); |
---|
143 | ttcn3Resources = InputInterface.filterAbsoluteDuplicates(ttcn3Resources); |
---|
144 | |
---|
145 | |
---|
146 | if (ttcn3Resources.isEmpty()) { |
---|
147 | // Terminate |
---|
148 | System.out.println("No ttcn3 files found!"); |
---|
149 | showHelp(); |
---|
150 | return; |
---|
151 | } |
---|
152 | |
---|
153 | long startTime = System.currentTimeMillis(); |
---|
154 | TTCN3Analyzer analyzer = null; |
---|
155 | |
---|
156 | try { |
---|
157 | System.out.println("Parsing files..."); |
---|
158 | for (int i = 0; i < ttcn3Resources.size(); i++) { |
---|
159 | String resourcePath = ttcn3Resources.get(i); |
---|
160 | analyzeFile(resourcePath); |
---|
161 | TTCN3AnalyzerFlyweightFactory analyzerFactory = TTCN3AnalyzerFlyweightFactory |
---|
162 | .getInstance(); |
---|
163 | analyzer = analyzerFactory.getTTCN3Analyzer(ttcn3Resources |
---|
164 | .get(i)); |
---|
165 | if (analyzer.getExceptions().size() > 0) { |
---|
166 | String exceptionMessage = "Error while parsing file " |
---|
167 | + analyzer.getFilename(); |
---|
168 | for (int i1 = 0; i1 < analyzer.getExceptions().size(); i1++) { |
---|
169 | String className = analyzer.getExceptions().get(i1).getStackTrace()[0].getClassName(); |
---|
170 | String methodName = analyzer.getExceptions().get(i1).getStackTrace()[0].getMethodName(); |
---|
171 | //moved from below, migrate to t3d as well |
---|
172 | String lineMessage = "Line " |
---|
173 | + analyzer.getExceptions().get(i1).getLine() |
---|
174 | + ": " |
---|
175 | + analyzer.getExceptions().get(i1).getMessage(); |
---|
176 | ; |
---|
177 | // if (className.substring(className.lastIndexOf(".")+1).equals("TTCN3Parser")){ |
---|
178 | exceptionMessage+="\n "+className.substring(className.lastIndexOf(".")+1)+" : "+methodName +"\n "+lineMessage; |
---|
179 | // } |
---|
180 | } |
---|
181 | //TODO: adapt to T3D as well |
---|
182 | String tree = ""; |
---|
183 | tree = LocationAST.dumpTree((LocationAST)analyzer.getParser().getAST(),1,tree); |
---|
184 | if (getLogLevel().equals(LogLevel.DEBUG)) { |
---|
185 | exceptionMessage+="\n" + |
---|
186 | "Parse-tree trace:\n" + |
---|
187 | tree; |
---|
188 | } |
---|
189 | if (T3Q.activeProfile.isSettingAbortOnError()) { |
---|
190 | throw new TTCN3ParserException(exceptionMessage); |
---|
191 | } else { |
---|
192 | try { |
---|
193 | throw new TTCN3ParserException(exceptionMessage); |
---|
194 | } catch (TTCN3ParserException e) { |
---|
195 | System.err.println(e.getLocalizedMessage()); |
---|
196 | // e.printStackTrace(); |
---|
197 | } |
---|
198 | } |
---|
199 | } |
---|
200 | } |
---|
201 | long endTime = System.currentTimeMillis(); |
---|
202 | long elapsed = endTime - startTime; |
---|
203 | double elapsedMinutes = ((double) elapsed) / 1000.0 / 60.0; |
---|
204 | System.out.println("Done parsing in " + elapsed + "ms (" |
---|
205 | + MiscTools.doubleToString(elapsedMinutes) + " minutes)."); |
---|
206 | |
---|
207 | if (T3Q.activeProfile.isStatShowLOC()) { |
---|
208 | System.out.println("Total lines of code parsed: " + totalLoc); |
---|
209 | } |
---|
210 | |
---|
211 | TTCN3AnalyzerFlyweightFactory analyzerFactory = TTCN3AnalyzerFlyweightFactory |
---|
212 | .getInstance(); |
---|
213 | |
---|
214 | System.out.println("Postprocessing..."); |
---|
215 | startTime = System.currentTimeMillis(); |
---|
216 | analyzerFactory.postProcess(); |
---|
217 | endTime = System.currentTimeMillis(); |
---|
218 | elapsed = endTime - startTime; |
---|
219 | elapsedMinutes = ((double) elapsed) / 1000.0 / 60.0; |
---|
220 | System.out.println("Done processing in " + elapsed + "ms (" |
---|
221 | + MiscTools.doubleToString(elapsedMinutes) + " minutes)."); |
---|
222 | startTime = System.currentTimeMillis(); |
---|
223 | |
---|
224 | System.out.println("=========================================="); |
---|
225 | //TODO: up to here mostly identical, make reusable |
---|
226 | |
---|
227 | //TODO: core functionality, encapsulate and extract |
---|
228 | for (int i = 0; i < ttcn3Resources.size(); i++) { |
---|
229 | if (MiscTools.regExpMatch(activeProfile.getIgnoredResourceRegExp() , ttcn3Resources.get(i))){ |
---|
230 | System.out.println("Analyzing: " + ttcn3Resources.get(i) + " (Skipped) "); //TODO: may need to use analyzer.getFilename for consistency |
---|
231 | } else { |
---|
232 | analyzer = analyzerFactory.getTTCN3Analyzer(ttcn3Resources |
---|
233 | .get(i)); |
---|
234 | System.out.println("Analyzing: " + analyzer.getFilename()); |
---|
235 | T3QVisitor visitor = new T3QVisitor(); |
---|
236 | visitor.setFilename(analyzer.getFilename()); |
---|
237 | visitor.acceptDFS((LocationAST) analyzer.getParser().getAST()); |
---|
238 | } |
---|
239 | } |
---|
240 | endTime = System.currentTimeMillis(); |
---|
241 | elapsed = endTime - startTime; |
---|
242 | elapsedMinutes = ((double) elapsed) / 1000.0 / 60.0; |
---|
243 | System.out.println("Quality checks finished in " + elapsed + "ms (" |
---|
244 | + MiscTools.doubleToString(elapsedMinutes) + " minutes)."); |
---|
245 | |
---|
246 | //TODO: custom functionality |
---|
247 | analyzer = handleFormatter(ttcn3Resources, analyzer, |
---|
248 | analyzerFactory); |
---|
249 | if (T3Q.activeProfile.isStatShowLOC()) { |
---|
250 | System.out |
---|
251 | .println("Total lines of code processed: " + totalLoc); |
---|
252 | } |
---|
253 | |
---|
254 | if (T3Q.activeProfile.isStatShowSummary()) { |
---|
255 | System.out.println("Brief statistics summary of occurences in message classes:"); |
---|
256 | for (MessageClass m : MessageClass.values()) { |
---|
257 | System.out.println("\t" + m.getDescription() + " : " |
---|
258 | + m.getOccurenceCount()); |
---|
259 | } |
---|
260 | } |
---|
261 | |
---|
262 | } catch (TTCN3BehaviorException e) { |
---|
263 | System.err.println(e.getLocalizedMessage()); |
---|
264 | } catch (TTCN3ParserException e) { |
---|
265 | // Default setting where processing is terminated in the event of a |
---|
266 | // parsing error |
---|
267 | System.err.println(e.getLocalizedMessage()); |
---|
268 | // TODO: Isolate different steps and implement a recovery mechanism: |
---|
269 | |
---|
270 | } |
---|
271 | //TODO: move to common, use in T3D as well |
---|
272 | if (getLogLevel().equals(LogLevel.DEBUG)) { |
---|
273 | int mb = 1024*1024; |
---|
274 | Runtime.getRuntime().maxMemory(); |
---|
275 | Runtime runtime = Runtime.getRuntime(); |
---|
276 | System.out.println(); |
---|
277 | System.out.println("============Memory statistics:==========="); |
---|
278 | // Print used memory |
---|
279 | System.out.println("Used Memory:\t" |
---|
280 | + (runtime.totalMemory() - runtime.freeMemory()) / mb+" MB"); |
---|
281 | // Print free memory |
---|
282 | System.out.println("Free Memory:\t" + runtime.freeMemory() / mb+" MB"); |
---|
283 | // Print total available memory |
---|
284 | System.out.println("Total Memory:\t" + runtime.totalMemory() / mb+" MB"); |
---|
285 | // Print maximum available memory |
---|
286 | System.out.println("Max Memory:\t" + runtime.maxMemory() / mb+" MB"); |
---|
287 | } |
---|
288 | } |
---|
289 | |
---|
290 | |
---|
291 | //TODO: copy back output handling tricks from T3D |
---|
292 | private TTCN3Analyzer handleFormatter(List<String> ttcn3Resources, |
---|
293 | TTCN3Analyzer analyzer, |
---|
294 | TTCN3AnalyzerFlyweightFactory analyzerFactory) { |
---|
295 | long startTime; |
---|
296 | long endTime; |
---|
297 | long elapsed; |
---|
298 | double elapsedMinutes; |
---|
299 | if (this.isFormattingEnabled()) { |
---|
300 | // TODO: consider ways to avoid reparsing (performing the comment |
---|
301 | // collection during the first parse) |
---|
302 | System.out.println("=========================================="); |
---|
303 | System.out.println("Formatting Code..."); |
---|
304 | startTime = System.currentTimeMillis(); |
---|
305 | |
---|
306 | TTCN3Formatter formatter = new TTCN3Formatter(); |
---|
307 | |
---|
308 | for (int i = 0; i < ttcn3Resources.size(); i++) { |
---|
309 | if (MiscTools.regExpMatch(activeProfile.getIgnoredResourceRegExp() , ttcn3Resources.get(i))){ |
---|
310 | System.out.println(" Formatting file: " |
---|
311 | + ttcn3Resources.get(i) + " (Skipped) "); |
---|
312 | } else { |
---|
313 | analyzer = analyzerFactory.getTTCN3Analyzer(ttcn3Resources |
---|
314 | .get(i)); |
---|
315 | System.out.println(" Formatting file: " |
---|
316 | + analyzer.getFilename()); |
---|
317 | |
---|
318 | try { |
---|
319 | |
---|
320 | String resourcePath = ttcn3Resources.get(i); |
---|
321 | |
---|
322 | String source = MiscTools.readFile(resourcePath); |
---|
323 | |
---|
324 | String formatted = formatter.formatTTCN3Source(source, |
---|
325 | T3Q.activeProfile.getFormattingParameters()); |
---|
326 | |
---|
327 | // calculate the target path for the current resource |
---|
328 | |
---|
329 | // TODO: look into it and fix it -> is it not fixed now? |
---|
330 | // reason is: absolute paths are difficult to handle |
---|
331 | // String subPath = resourcePath; |
---|
332 | // String subPath = "/" + new File(resourcePath).getName(); |
---|
333 | |
---|
334 | String canonicalResourcePath = new File(resourcePath).getCanonicalPath(); |
---|
335 | String canonicalWorkingPath = new File(".").getCanonicalPath(); |
---|
336 | |
---|
337 | String destinationSubPath = canonicalResourcePath; |
---|
338 | |
---|
339 | //TODO: make this subtraction optional and document the feature |
---|
340 | //CF: Manuel's remark and my answer - it introduces a dependency on the working path which may not be easily understood and has at least be documented properly |
---|
341 | if (canonicalResourcePath.startsWith(canonicalWorkingPath)){ |
---|
342 | destinationSubPath = canonicalResourcePath.substring(canonicalWorkingPath.length()); |
---|
343 | } |
---|
344 | |
---|
345 | if (destinationSubPath.substring(0,3).endsWith(":\\")){ |
---|
346 | destinationSubPath=destinationSubPath.substring(2); |
---|
347 | } |
---|
348 | |
---|
349 | String outputPathArg = new File(getDestinationPath()).getPath(); // also |
---|
350 | // strips |
---|
351 | // the |
---|
352 | // slash |
---|
353 | // if |
---|
354 | // present |
---|
355 | String outputPath = outputPathArg + destinationSubPath; |
---|
356 | |
---|
357 | |
---|
358 | |
---|
359 | // System.out.println("****************************"); |
---|
360 | // System.out.println("ABSOLUTE: \t\t"+canonicalResourcePath); |
---|
361 | // System.out.println("WORKING: \t\t"+canonicalWorkingPath); |
---|
362 | // System.out.println("ResourcePath: \t\t"+resourcePath); |
---|
363 | // System.out.println("SubPath: \t\t"+destinationSubPath); |
---|
364 | // System.out.println("OutputPath: \t\t"+outputPath); |
---|
365 | // System.out.println("****************************"); |
---|
366 | // System.out.println(formatted); |
---|
367 | // System.out.println(outputPath); |
---|
368 | MiscTools.writeFile(outputPath, formatted); |
---|
369 | System.out.println("File \""+outputPath+"\" written successfully!"); |
---|
370 | |
---|
371 | } catch (RecognitionException e1) { |
---|
372 | |
---|
373 | String exceptionMessage = "Error while formatting file (recognition) " |
---|
374 | + analyzer.getFilename(); |
---|
375 | String className = e1.getStackTrace()[0].getClassName(); |
---|
376 | String methodName = e1.getStackTrace()[0].getMethodName(); |
---|
377 | String lineMessage = "Line " |
---|
378 | + e1.getLine() |
---|
379 | + ": " |
---|
380 | + e1.getMessage(); |
---|
381 | |
---|
382 | exceptionMessage+="\n "+className.substring(className.lastIndexOf(".")+1)+" : "+methodName +"\n "+lineMessage; |
---|
383 | //TODO: adapt to T3D as well? |
---|
384 | |
---|
385 | System.err.println(exceptionMessage); |
---|
386 | |
---|
387 | |
---|
388 | } catch (TokenStreamException e) { |
---|
389 | System.err.println("Token stream exception:"); |
---|
390 | e.printStackTrace(); |
---|
391 | } catch (Exception e) { |
---|
392 | System.err.println("Exception while processing "+ttcn3Resources.get(i)+" :"); |
---|
393 | e.printStackTrace(); |
---|
394 | } |
---|
395 | } |
---|
396 | |
---|
397 | } |
---|
398 | endTime = System.currentTimeMillis(); |
---|
399 | elapsed = endTime - startTime; |
---|
400 | elapsedMinutes = ((double) elapsed) / 1000.0 / 60.0; |
---|
401 | System.out.println("Code formatting finished in " + elapsed |
---|
402 | + "ms (" + MiscTools.doubleToString(elapsedMinutes) + " minutes)."); |
---|
403 | } |
---|
404 | return analyzer; |
---|
405 | } |
---|
406 | //TODO: DUPLICATED IN T3D |
---|
407 | private boolean handleCommandLineArguments(String[] args) throws TerminationException { |
---|
408 | //parseCommandLineArguments(args); |
---|
409 | CommandLine commandLine = parseCommandLineArguments(args); |
---|
410 | if (commandLine == null) { |
---|
411 | return false; |
---|
412 | } |
---|
413 | String arguments[] = evaluateCommandLineOptions(commandLine); |
---|
414 | |
---|
415 | if (commandLine.hasOption("help")) { |
---|
416 | showHelp(); |
---|
417 | return false; |
---|
418 | } |
---|
419 | |
---|
420 | if (commandLine.hasOption("config") && arguments.length < 1) { |
---|
421 | System.out.println("ERROR: Missing input location(s)"); |
---|
422 | showHelp(); |
---|
423 | return false; |
---|
424 | } |
---|
425 | |
---|
426 | for (String arg : arguments) { |
---|
427 | //TODO: add validity checks |
---|
428 | inputPaths.add(arg); |
---|
429 | } |
---|
430 | return true; |
---|
431 | } |
---|
432 | |
---|
433 | // -------------------------------------------------------------------------- |
---|
434 | //TODO: DUPLICATED IN T3D |
---|
435 | private CommandLine parseCommandLineArguments(String[] args) { |
---|
436 | CommandLineParser parser = new GnuParser(); |
---|
437 | T3QOptionsHandler optionsHandler = new T3QOptionsHandler(); |
---|
438 | CommandLine commandLine = null; |
---|
439 | try { |
---|
440 | commandLine = parser.parse(optionsHandler.getOptions(), args); |
---|
441 | } catch (ParseException e) { |
---|
442 | System.out.println("ERROR: " + e.getMessage() ); |
---|
443 | showHelp(); |
---|
444 | } |
---|
445 | return commandLine; |
---|
446 | } |
---|
447 | |
---|
448 | // -------------------------------------------------------------------------- |
---|
449 | //TODO: DUPLICATED MOSTLY IN T3D |
---|
450 | private String[] evaluateCommandLineOptions(CommandLine commandLine) throws TerminationException { |
---|
451 | if (commandLine.hasOption("generate-config")) { |
---|
452 | this.setConfigurationFilename(commandLine.getOptionValue("generate-config")); |
---|
453 | this.setGenerateNewConfiguration(true); |
---|
454 | } else if (commandLine.hasOption("config")) { |
---|
455 | this.setConfigurationFilename(commandLine.getOptionValue("config")); |
---|
456 | } else { |
---|
457 | System.out.println("ERROR: No configuration file selected!"); |
---|
458 | showHelp(); |
---|
459 | throw new TerminationException(""); |
---|
460 | } |
---|
461 | |
---|
462 | if (commandLine.hasOption("profile")) { |
---|
463 | this.setSelectedProfileName(commandLine.getOptionValue("profile")); |
---|
464 | } |
---|
465 | if (commandLine.hasOption("format")){ |
---|
466 | this.setFormattingEnabled(true); |
---|
467 | } |
---|
468 | if (commandLine.hasOption("verbosity")){ |
---|
469 | this.selectLogLevel(commandLine.getOptionValue("verbosity")); |
---|
470 | } |
---|
471 | if (commandLine.hasOption("output-path")){ |
---|
472 | this.setDestinationPath(commandLine.getOptionValue("output-path")); |
---|
473 | } |
---|
474 | return commandLine.getArgs(); |
---|
475 | } |
---|
476 | |
---|
477 | // -------------------------------------------------------------------------- |
---|
478 | |
---|
479 | //TODO: THIS SHALL BE DEPRECATED NOW |
---|
480 | // private void parseCommandLineArguments(String[] args) { |
---|
481 | // String key = ""; |
---|
482 | // String value = ""; |
---|
483 | // //targetPath = ""; |
---|
484 | // |
---|
485 | // boolean lastKey = false; |
---|
486 | // for (int i = 0; i < args.length; i++) { |
---|
487 | // if (args[i].startsWith("--")) { |
---|
488 | // key = args[i].replaceAll("--", "").toLowerCase(); |
---|
489 | // |
---|
490 | // if (lastKey) { |
---|
491 | // argsMap.put(key, "true"); |
---|
492 | // key = null; |
---|
493 | // value = null; |
---|
494 | // lastKey = false; |
---|
495 | // } |
---|
496 | // |
---|
497 | // lastKey = true; |
---|
498 | // } else { |
---|
499 | // value = args[i]; |
---|
500 | // if ((key != null) && (argsMap.get(key) == null) |
---|
501 | // && (key.length() > 0)) { |
---|
502 | // argsMap.put(key, value); |
---|
503 | // key = null; |
---|
504 | // value = null; |
---|
505 | // } else { |
---|
506 | // inputPaths.add(value); |
---|
507 | // } |
---|
508 | // lastKey = false; |
---|
509 | // } |
---|
510 | // } |
---|
511 | // |
---|
512 | // if (key != null) { |
---|
513 | // if ((argsMap.get(key) == null) && (key.length() > 0)) { |
---|
514 | // argsMap.put(key, "true"); |
---|
515 | // } |
---|
516 | // } |
---|
517 | // } |
---|
518 | |
---|
519 | // -------------------------------------------------------------------------- |
---|
520 | //TODO: DUPLICATED MOSTLY IN T3D |
---|
521 | private void handleConfig(String specifiedProfile) throws TerminationException { |
---|
522 | ConfigTools configTools = new ConfigTools(configurationClassName, configurationProfileClassName); |
---|
523 | configTools.setToolVersion(getVersionNumber()); |
---|
524 | |
---|
525 | try { |
---|
526 | if (isGenerateNewConfiguration()) { |
---|
527 | configTools.initializeNewDefaultConfig(getConfigurationFilename()); |
---|
528 | System.exit(0); |
---|
529 | } else { |
---|
530 | configTools.loadConfig(getConfigurationFilename()); |
---|
531 | activeProfile = (QualityCheckProfile) configTools.selectProfile(specifiedProfile); |
---|
532 | if (this.getDestinationPath()==null) { |
---|
533 | setDestinationPath(T3Q.activeProfile.getPathFormattedOutputPath()); |
---|
534 | } |
---|
535 | } |
---|
536 | } catch (InstantiationException e) { |
---|
537 | throw new TerminationException("ERROR: Instantiation problems encountered while loading configuration profile. "+e.getMessage()); |
---|
538 | } catch (IllegalAccessException e) { |
---|
539 | throw new TerminationException("ERROR: Instantiation problems encountered while loading configuration profile. "+e.getMessage()); |
---|
540 | } catch (ClassNotFoundException e) { |
---|
541 | throw new TerminationException("ERROR: Instantiation problems encountered while loading configuration profile. "+e.getMessage()); |
---|
542 | } |
---|
543 | |
---|
544 | if (!isProfileVersionValid()) { |
---|
545 | System.out.println("\nERROR: Selected profile \"" + activeProfile.getProfileName() |
---|
546 | + "\" has a mismatching or no version (required: \""+getVersionNumber()+"\").\n" + |
---|
547 | " Consider upgrading the profile by transfering the relevant parts to an auto-generated profile or selecting a different profile.\n"); |
---|
548 | throw new TerminationException(""); |
---|
549 | } |
---|
550 | } |
---|
551 | //TODO: DUPLICATED IN T3D |
---|
552 | private boolean isProfileVersionValid() { |
---|
553 | if (activeProfile.getProfileVersion() != null && activeProfile.getProfileVersion().equals(getVersionNumber())){ |
---|
554 | return true; |
---|
555 | } else { |
---|
556 | return false; |
---|
557 | } |
---|
558 | } |
---|
559 | //TODO: DUPLICATED IN T3D |
---|
560 | private void selectLogLevel(String logLevel) throws TerminationException{ |
---|
561 | boolean selected = false; |
---|
562 | String possibleValues =""; |
---|
563 | for (LogLevel l : LogLevel.values()){ |
---|
564 | if (l.toString().equals(logLevel)){ |
---|
565 | setLogLevel(l); |
---|
566 | selected = true; |
---|
567 | System.out.println("Selected log level \""+logLevel+"\""); |
---|
568 | } |
---|
569 | if (!l.toString().equals("FIXME") && !l.toString().equals("DEBUG")) |
---|
570 | possibleValues+=l.toString()+", "; |
---|
571 | } |
---|
572 | if (!selected){ |
---|
573 | System.out.println("\nERROR: No valid log level provided! Possible values are (in ascending inclusive order): "+ possibleValues.substring(0,possibleValues.length()-2)+"."); |
---|
574 | throw new TerminationException(""); |
---|
575 | } |
---|
576 | |
---|
577 | } |
---|
578 | |
---|
579 | |
---|
580 | // // -------------------------------------------------------------------------- |
---|
581 | // //TODO: should be obsolete now? |
---|
582 | // List<String> findTTCN3Resources(String directory) { |
---|
583 | // List<String> files = new LinkedList<String>(); |
---|
584 | // |
---|
585 | // File f = new File(directory); |
---|
586 | // |
---|
587 | // File[] fileNames = f.listFiles(new FileFilter() { |
---|
588 | // public boolean accept(File pathname) { |
---|
589 | // if (pathname.getPath().endsWith(".ttcn3") |
---|
590 | // || pathname.getPath().endsWith(".ttcn") |
---|
591 | // || pathname.getPath().endsWith(".3mp")) |
---|
592 | // return true; |
---|
593 | // return false; |
---|
594 | // } |
---|
595 | // }); |
---|
596 | // |
---|
597 | // for (int i = 0; i < fileNames.length; i++) { |
---|
598 | // files.add(fileNames[i].getPath()); |
---|
599 | // } |
---|
600 | // |
---|
601 | // File[] directories = f.listFiles(new FileFilter() { |
---|
602 | // public boolean accept(File pathname) { |
---|
603 | // if (pathname.isDirectory()) |
---|
604 | // return true; |
---|
605 | // return false; |
---|
606 | // } |
---|
607 | // }); |
---|
608 | // |
---|
609 | // if (T3Q.activeProfile.isSettingRecursiveProcessing()) { |
---|
610 | // for (int i = 0; i < directories.length; i++) { |
---|
611 | // files.addAll(findTTCN3Resources(directories[i].getPath())); |
---|
612 | // } |
---|
613 | // } |
---|
614 | // |
---|
615 | // return files; |
---|
616 | // } |
---|
617 | |
---|
618 | // -------------------------------------------------------------------------- |
---|
619 | //TODO: DUPLICATE IN T3D |
---|
620 | private TTCN3Analyzer analyzeFile(String filename) { |
---|
621 | TTCN3AnalyzerFlyweightFactory analyzerFactory = TTCN3AnalyzerFlyweightFactory |
---|
622 | .getInstance(); |
---|
623 | analyzerFactory.setStandaloneUsage(true); |
---|
624 | System.out.print(" Parsing file: " + filename); |
---|
625 | |
---|
626 | String code = MiscTools.readFile(filename); |
---|
627 | int loc = MiscTools.getLOC(filename); |
---|
628 | linesOfCodeMap.put(filename, loc); |
---|
629 | totalLoc += loc; |
---|
630 | |
---|
631 | System.out.println(" (LOC: " + linesOfCodeMap.get(filename) + ") ..."); |
---|
632 | long startTime = System.currentTimeMillis(); |
---|
633 | |
---|
634 | TTCN3Analyzer analyzer = analyzerFactory.getTTCN3Analyzer(filename, |
---|
635 | code); |
---|
636 | try { |
---|
637 | analyzer.analyze(); |
---|
638 | } catch (MismatchedTokenException e) { |
---|
639 | e.printStackTrace(); |
---|
640 | } catch (RecognitionException e) { |
---|
641 | e.printStackTrace(); |
---|
642 | } catch (TokenStreamException e) { |
---|
643 | e.printStackTrace(); |
---|
644 | } catch (Exception e) { |
---|
645 | e.printStackTrace(); |
---|
646 | } |
---|
647 | long endTime = System.currentTimeMillis(); |
---|
648 | long elapsed = endTime - startTime; |
---|
649 | double elapsedMinutes = ((double) elapsed) / 1000.0 / 60.0; |
---|
650 | |
---|
651 | System.out.println(" ...done in " + elapsed + "ms (" |
---|
652 | + MiscTools.doubleToString(elapsedMinutes) + " minutes)."); |
---|
653 | |
---|
654 | return analyzer; |
---|
655 | } |
---|
656 | |
---|
657 | // -------------------------------------------------------------------------- |
---|
658 | //TODO: DUPLICATED IN T3D |
---|
659 | public static void setVersionNumber(String versionNumber) { |
---|
660 | T3Q.versionNumber = versionNumber; |
---|
661 | } |
---|
662 | //TODO: DUPLICATED IN T3D |
---|
663 | public static String getVersionNumber() { |
---|
664 | return versionNumber; |
---|
665 | } |
---|
666 | |
---|
667 | public static void main(String[] args) { |
---|
668 | try{ |
---|
669 | T3Q tool = new T3Q(); |
---|
670 | tool.run(args); |
---|
671 | } catch (Exception e) { |
---|
672 | if (getLogLevel()==LogLevel.DEBUG){ |
---|
673 | e.printStackTrace(); |
---|
674 | |
---|
675 | } else { |
---|
676 | String stacktrace = ""; |
---|
677 | for (StackTraceElement ste : e.getStackTrace()){ |
---|
678 | stacktrace+="\n "+ste.toString(); |
---|
679 | } |
---|
680 | System.err.println("ERROR: A problem occurred while running T3Q" + |
---|
681 | "\n Problem type: " + |
---|
682 | e + |
---|
683 | "\n Stacktrace:" + |
---|
684 | stacktrace + |
---|
685 | "\n Run T3Q with --verbosity=DEBUG for a more detailed report" ); |
---|
686 | } |
---|
687 | } |
---|
688 | } |
---|
689 | //TODO: DUPLICATED IN T3D |
---|
690 | public void setConfigurationFilename(String configurationFilename) { |
---|
691 | this.configurationFilename = configurationFilename; |
---|
692 | } |
---|
693 | //TODO: DUPLICATED IN T3D |
---|
694 | public String getConfigurationFilename() { |
---|
695 | return configurationFilename; |
---|
696 | } |
---|
697 | //TODO: DUPLICATED IN T3D |
---|
698 | public static void setLogLevel(LogLevel logLevel) { |
---|
699 | T3Q.logLevel = logLevel; |
---|
700 | } |
---|
701 | //TODO: DUPLICATED IN T3D |
---|
702 | public static LogLevel getLogLevel() { |
---|
703 | return logLevel; |
---|
704 | } |
---|
705 | //TODO: DUPLICATED IN T3D |
---|
706 | public void setSelectedProfileName(String selectedProfileName) { |
---|
707 | this.selectedProfileName = selectedProfileName; |
---|
708 | } |
---|
709 | //TODO: DUPLICATED IN T3D |
---|
710 | public String getSelectedProfileName() { |
---|
711 | return selectedProfileName; |
---|
712 | } |
---|
713 | |
---|
714 | public void setFormattingEnabled(boolean formattingEnabled) { |
---|
715 | this.formattingEnabled = formattingEnabled; |
---|
716 | } |
---|
717 | |
---|
718 | public boolean isFormattingEnabled() { |
---|
719 | return formattingEnabled; |
---|
720 | } |
---|
721 | |
---|
722 | public void setGenerateNewConfiguration(boolean generateNewConfiguration) { |
---|
723 | this.generateNewConfiguration = generateNewConfiguration; |
---|
724 | } |
---|
725 | |
---|
726 | public boolean isGenerateNewConfiguration() { |
---|
727 | return generateNewConfiguration; |
---|
728 | } |
---|
729 | |
---|
730 | public void setDestinationPath(String destinationPath) { |
---|
731 | this.destinationPath = destinationPath; |
---|
732 | } |
---|
733 | |
---|
734 | public String getDestinationPath() { |
---|
735 | return destinationPath; |
---|
736 | } |
---|
737 | |
---|
738 | public static void setBuildStamp(String buildStamp) { |
---|
739 | T3Q.buildStamp = buildStamp; |
---|
740 | } |
---|
741 | |
---|
742 | public static String getBuildStamp() { |
---|
743 | return buildStamp; |
---|
744 | } |
---|
745 | |
---|
746 | } |
---|