OSDN Git Service

ecdee00687a0cb2fea355de08c727ef757ec104f
[nxt-jsp/lejos_nxj.git] / nxtOSEK / lejos_nxj / src / java / jtools / js / tinyvm / TinyVM.java
1 package js.tinyvm;
2
3 import java.io.FileNotFoundException;
4 import java.io.FileOutputStream;
5 import java.io.IOException;
6 import java.io.OutputStream;
7
8 import js.common.CLIToolProgressMonitor;
9 import js.common.ToolProgressMonitor;
10 import js.tinyvm.util.TinyVMCommandLineParser;
11
12 import org.apache.commons.cli.CommandLine;
13
14 /**
15  * Tiny VM.
16  */
17 public class TinyVM extends TinyVMTool
18 {
19         private TinyVMCommandLineParser fParser;
20         
21    /**
22     * Main entry point for command line usage.
23     * 
24     * @param args command line
25     */
26    public static void main (String[] args)
27    {
28       try
29       {
30          TinyVM tinyVM = new TinyVM(new CLIToolProgressMonitor());
31          tinyVM.start(args);
32       }
33       catch (TinyVMException e)
34       {
35          System.err.println(e.getMessage());
36          System.exit(1);
37       }
38    }
39
40    /**
41     * Constructor.
42     */
43    public TinyVM (ToolProgressMonitor monitor)
44    {
45       super(monitor);
46       fParser = new TinyVMCommandLineParser();
47    }
48
49    /**
50     * Execute tiny vm.
51     * 
52     * @param args command line
53     * @throws TinyVMException
54     */
55    public void start (String[] args) throws TinyVMException
56    {
57       assert args != null: "Precondition: args != null";
58
59       CommandLine commandLine = fParser.parse(args);
60
61       // options
62       boolean verbose = commandLine.hasOption("v");
63       String classpath = commandLine.getOptionValue("cp");
64       String output = commandLine.getOptionValue("o");
65       boolean all = commandLine.hasOption("a");
66       boolean bigEndian = "be".equalsIgnoreCase(commandLine
67          .getOptionValue("wo"));
68
69       // files
70       String[] classes = commandLine.getArgs();
71
72       ((CLIToolProgressMonitor) getProgressMonitor()).setVerbose(verbose);
73
74       OutputStream stream = null;
75       try
76       {
77          stream = output == null
78             ? (OutputStream) System.out
79             : (OutputStream) new FileOutputStream(output);
80          link(classpath, classes, all, stream, bigEndian);
81       }
82       catch (FileNotFoundException e)
83       {
84          throw new TinyVMException(e.getMessage(), e);
85       }
86       finally
87       {
88          if (stream instanceof FileOutputStream)
89          {
90             try
91             {
92                stream.close();
93             }
94             catch (IOException e)
95             {
96                throw new TinyVMException(e);
97             }
98          }
99       }
100    }
101
102 }