View Javadoc

1   package net.sf.quarrel;
2   
3   import net.sf.quarrel.dot.ModelRenderer;
4   import net.sf.quarrel.file.FileElement;
5   import net.sf.quarrel.file.StreamParser;
6   import net.sf.quarrel.uml.Model;
7   import net.sf.quarrel.uml.ModelBuilder;
8   import net.sf.quarrel.uml.ModelBuilder2;
9   import org.apache.commons.cli.CommandLineParser;
10  import org.apache.commons.cli.GnuParser;
11  import org.apache.commons.cli.HelpFormatter;
12  import org.apache.commons.cli.Option;
13  import org.apache.commons.cli.Options;
14  import org.apache.commons.cli.ParseException;
15  
16  import java.io.File;
17  import java.io.FileInputStream;
18  import java.io.FileOutputStream;
19  import java.io.OutputStreamWriter;
20  import java.io.PrintWriter;
21  import java.util.Iterator;
22  
23  /***
24   * The class which exposes the Quarrel functionality to the command line.
25   */
26  public class CommandLine {
27  
28  
29      /***
30       * The main method called from the command line.
31       *
32       * @param args the Strings passed in from the command line.
33       */
34      public static void main(String[] args) {
35  
36          // todo : is it meaningful to document the command line paramters here?
37          Options options = buildCommandLineOptions();
38          CommandLineParser parser = new GnuParser();
39  
40          String sourcePath = null;
41          String targetPath = null;
42  
43          final HelpFormatter helpFormatter = new HelpFormatter();
44          helpFormatter.setWidth(80);
45  
46          try {
47              final org.apache.commons.cli.CommandLine line = parser.parse(options, args);
48  
49              if (line.hasOption("s")) {
50                  sourcePath = line.getOptionValue("s").trim();
51              }
52  
53              if (line.hasOption("t")) {
54                  targetPath = line.getOptionValue("t").trim();
55              }
56  
57  
58          } catch (ParseException e) {
59              System.out.println("");
60              System.out.println(e.getMessage());
61              System.out.println("");
62              helpFormatter.printHelp("java " + CommandLine.class.getName(), options);
63              System.out.println("");
64              System.exit(1);
65          }
66  
67          try {
68  
69              final File inputFile = new File(sourcePath);
70  
71  
72              File targetFile;
73  
74              if (targetPath == null) {
75                  targetFile = inputFile.getParentFile();
76              } else {
77                  targetFile = new File(targetPath);
78              }
79  
80              if (targetFile.isDirectory()) {
81                  String newFile = targetFile.getAbsolutePath() + File.separatorChar + inputFile.getName() + ".dot";
82                  targetFile = new File(newFile);
83              }
84  
85              System.out.println("writing DOT to " + targetFile.getAbsolutePath());
86  
87              targetFile.getParentFile().mkdirs();
88  
89              final FileInputStream fis = new FileInputStream(inputFile);
90  
91              // todo extract class for the balance of this method, need for Ant
92  
93              final StreamParser streamParser = new StreamParser(fis);
94              streamParser.parse();
95  
96              fis.close();
97  
98              final ModelBuilder builder = new ModelBuilder2();
99  
100             final Iterator elements = streamParser.elements();
101 
102             FileElement element;
103             while (elements.hasNext()) {
104                 element = (FileElement) elements.next();
105                 builder.add(element);
106             }
107 
108             final Model model = builder.getModel();
109             final ModelRenderer renderer = new ModelRenderer();
110             final String dotString = renderer.getDotString(model);
111 
112             final FileOutputStream fos = new FileOutputStream(targetFile);
113 
114             final PrintWriter writer = new PrintWriter(new OutputStreamWriter(fos));
115 
116             writer.println(dotString);
117 
118             writer.flush();
119             writer.close();
120 
121         } catch (Exception e) {
122             e.printStackTrace();
123         }
124 
125     }
126 
127     /***
128      * Build an object that represents the command line options.
129      *
130      * @return an object which models the command line options.
131      */
132     private static Options buildCommandLineOptions() {
133 
134         Option help = new Option("help", "print this message");
135 
136         Option source = new Option("s", true, "the UAS formated file to read");
137         source.setArgName("filename");
138         source.setLongOpt("source");
139         source.setRequired(true);
140         source.setValueSeparator(' ');
141         source.setArgs(1);
142 
143         Option target = new Option("t", true, "the target directory or file for the generated DOT file");
144         target.setArgName("target");
145         target.setRequired(false);
146         target.setArgs(1);
147 
148 
149         final Options options = new Options();
150 
151         options.addOption(help);
152         options.addOption(source);
153         options.addOption(target);
154 
155         return options;
156 
157     }
158 
159 }