View Javadoc

1   package net.sf.tomp.xtcl;
2   
3   import org.apache.commons.cli.CommandLine;
4   import org.apache.commons.cli.Option;
5   import org.apache.commons.cli.OptionBuilder;
6   import org.apache.commons.cli.Options;
7   import org.apache.commons.cli.PosixParser;
8   
9   import java.io.File;
10  
11  /***
12   * Main XTCL entry point, allows execution of XTCL script from files or
13   * interactively command-by-command from comman line.
14   *
15   * @author tomp
16   * @version 2003-10-15
17   */
18  public class Main
19  {
20      /***
21       * Main XTCL entry point, allows execution of XTCL script from files or
22       * interactively command-by-command from comman line.
23       *
24       * @param args the command line arguments
25       * @throws Exception if execution fails
26       */
27      public static void main(String[] args)
28          throws Exception
29      {
30          // Create Context w/System.err as the error output
31          Context context = new Context();
32  
33          context.setOut(System.err);
34  
35          // Get its Compiler
36          Compiler comp = context.getCompiler();
37  
38          // puts the System.out as standard $out variable
39          context.put("$out", System.out);
40  
41          Command cs = null;
42          int returnCode = 0;
43  
44          OptionBuilder.hasArgs(1);
45          OptionBuilder.withLongOpt("script-file");
46          OptionBuilder.withDescription("run the specified script file");
47          OptionBuilder.withArgName("scriptfile");
48  
49          Option oTdb = OptionBuilder.create('f');
50  
51          OptionBuilder.hasArgs(1);
52          OptionBuilder.withLongOpt("document-builder-factory");
53          OptionBuilder.withDescription(
54              "use the specified DocumentBuilderFactory");
55          OptionBuilder.withArgName("DocumentBuilderFactory_classname");
56  
57          Option oDBF = OptionBuilder.create('b');
58  
59          OptionBuilder.hasArgs(1);
60          OptionBuilder.withLongOpt("uri-resolver");
61          OptionBuilder.withDescription("use the specified URIResolver");
62          OptionBuilder.withArgName("URIResolver_classname");
63  
64          Option oUR = OptionBuilder.create('u');
65  
66          OptionBuilder.hasArgs(1);
67          OptionBuilder.withLongOpt("resolving-reader");
68          OptionBuilder.withDescription("use the specified resolving XMLReader");
69          OptionBuilder.withArgName("ResolvingXMLReader_classname");
70  
71          Option oRR = OptionBuilder.create('r');
72  
73          OptionBuilder.withLongOpt("quiet");
74          OptionBuilder.withDescription("provide no info on processing");
75  
76          Option oVerb = OptionBuilder.create('q');
77  
78          OptionBuilder.withLongOpt("interactive");
79          OptionBuilder.withDescription("enter interactive mode");
80  
81          Option oInt = OptionBuilder.create('i');
82  
83          Options opts = new Options();
84  
85          opts.addOption(oTdb);
86          opts.addOption(oDBF);
87          opts.addOption(oUR);
88          opts.addOption(oRR);
89          opts.addOption(oVerb);
90          opts.addOption(oInt);
91  
92          CommandLine cl = new PosixParser().parse(opts, args);
93  
94          comp.getContext().setVerbose(!cl.hasOption('q'));
95  
96          // no args - commands will be entered to the stdin
97          if (args.length == 0)
98          {
99              //returnCode = interpreter.execute();
100             if (comp.getContext().isVerbose())
101             {
102                 System.out.println(
103                     "XTCL - command line mode. Enter the program:");
104             }
105 
106             cs = comp.compile(new CommandLineReader());
107         }
108         else
109         {
110             // -f reads commands from file
111             if (cl.hasOption('f'))
112             {
113                 File f = new File(cl.getOptionValue('f'));
114 
115                 if (comp.getContext().isVerbose())
116                 {
117                     System.out.println("XTCL - program from file '" + f + "'");
118                 }
119 
120                 cs = comp.compile(new CommandLineReader(f));
121 
122                 // -i enters interactive mode
123             }
124             else if (cl.hasOption('i'))
125             {
126                 if (comp.getContext().isVerbose())
127                 {
128                     System.out.println(
129                         "XTCL - interactive mode. Enter the commands:");
130                 }
131 
132                 new InteractiveExecutor(context).interactive();
133 
134                 // else: commands read from CL
135             }
136             else if (cl.hasOption('b'))
137             {
138                 System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
139                     cl.getOptionValue('b'));
140             }
141             else if (cl.hasOption('u'))
142             {
143                 Context.setUriResolverClass(Class.forName(cl.getOptionValue('u')));
144             }
145             else if (cl.hasOption('r'))
146             {
147                 Context.setResolvingXMLReaderClass(Class.forName(
148                         cl.getOptionValue('r')));
149             }
150             else
151             {
152                 if (comp.getContext().isVerbose())
153                 {
154                     System.out.println("XTCL - command read from command line:");
155                 }
156 
157                 cs = comp.compile(args, new CommandLineReader());
158             }
159         }
160 
161         // WARM UP
162 
163         /*
164          * returnCode = exec.execute(cs); returnCode = exec.execute(cs);
165          * returnCode = exec.execute(cs); returnCode = exec.execute(cs);
166          * returnCode = exec.execute(cs); context.setOut(System.out);
167          */
168 
169         // execution time measurement
170         long start = System.currentTimeMillis();
171 
172         returnCode = comp.getContext().execute(cs);
173 
174         long end = System.currentTimeMillis();
175 
176         if (comp.getContext().isVerbose())
177         {
178             System.out.println("Execution time: " + (end - start) + " ms.");
179         }
180 
181         System.exit(returnCode);
182     }
183 }
184 
185 
186 /*
187  * The contents of this file are subject to the Mozilla Public License Version
188  * 1.1 (the "License"); you may not use this file except in compliance with the
189  * License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
190  * Software distributed under the License is distributed on an "AS IS" basis,
191  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
192  * the specific language governing rights and limitations under the License. The
193  * Original Code is: all this file. The Initial Developer of the Original Code
194  * is: Tomas Pitner, Masaryk University in Brno, Czech Republic. Contributor(s):
195  */