KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > generator > Main


1 /*
2  * Copyright 2003, 2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15
16  */

17 package org.apache.ws.jaxme.generator;
18
19 import java.io.File JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.List JavaDoc;
22
23 import org.apache.ws.jaxme.generator.impl.GeneratorImpl;
24 import org.apache.ws.jaxme.generator.sg.SGFactoryChain;
25 import org.apache.ws.jaxme.generator.sg.impl.JAXBSchemaReader;
26
27
28 /** <p>A command line interface for the JaxMe source generator.</p>
29  * <p><em>Implementation note</em>: If you update this class, you
30  * should consider updating the following files and classes as
31  * well:
32  * <ul>
33  * <li>{@link Generator}</li>
34  * <li>{@link XJCTask}</li>
35  * <li>docs/Reference.html</li>
36  * </ul></p>
37  *
38  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
39  */

40 public class Main {
41   /** <p>Prints the Usage message and exits with error status.</p>
42    */

43   public static void Usage(String JavaDoc msg) {
44     if (msg != null) {
45       System.err.println(msg);
46       System.err.println();
47     }
48     java.io.PrintStream JavaDoc ps = System.err;
49     ps.println("Usage: " + Main.class.getName() + " <inputfile> [options]");
50     ps.println();
51     ps.println("Reads a schema definition from the given <inputfile>");
52     ps.println("Possible options are:");
53     ps.println(" --force Force overwriting files");
54     ps.println(" --schemaReader=<class> Sets the SchemaReader class; defaults to");
55     ps.println(" " + JAXBSchemaReader.class.getName());
56     ps.println(" --sgFactoryChain=<class> Adds an instance of <class> to the");
57     ps.println(" generation process.");
58     ps.println(" --logFile=<filename> Sets a logfile for debugging purposes.");
59     ps.println(" By default System.err is used.");
60     ps.println(" --logLevel=<level> Sets the default logging level.");
61     ps.println(" Possible levels are fatal, error (default),");
62     ps.println(" warn, info and debug");
63     ps.println(" --option=<name=value> Sets the option <name> to the given <value>.");
64     ps.println(" --package=<name> Sets the package of the generated sources to");
65     ps.println(" <name>.");
66     ps.println(" --target=<dir> Sets the directory where to generate");
67     ps.println(" sources. By default the current directory");
68     ps.println(" is used.");
69     ps.println(" --validating Turns the XML parsers validation on.");
70     System.exit(1);
71   }
72
73   /**
74    * @param args the command line arguments
75    */

76   public static void main (String JavaDoc args[]) throws Exception JavaDoc {
77     java.io.File JavaDoc schemaFile = null;
78     Generator g = new GeneratorImpl();
79     String JavaDoc schemaReaderClass = JAXBSchemaReader.class.getName();
80     String JavaDoc target = null;
81     String JavaDoc logFile = null;
82     String JavaDoc logLevel = null;
83     String JavaDoc packageName = null;
84     List JavaDoc sgFactoryChains = new ArrayList JavaDoc();
85
86     for (int i = 0; i < args.length; i++) {
87       String JavaDoc arg = args[i];
88       if (arg.equals("--")) {
89         for (int j = i; j < args.length; j++) {
90           if (schemaFile != null) {
91             Usage("Only one input file may be specified.");
92           }
93           schemaFile = new java.io.File JavaDoc(args[j]);
94         }
95         break;
96       }
97       if (arg.startsWith("--")) {
98         arg = arg.substring(1);
99       }
100       if (arg.startsWith("-")) {
101         arg = arg.substring(1);
102         int optIndex = arg.indexOf('=');
103         String JavaDoc opt = null;
104         if (optIndex > 0) {
105           opt = arg.substring(optIndex+1);
106           arg = arg.substring(0, optIndex);
107         }
108         if (arg.equalsIgnoreCase("force")) {
109             g.setForcingOverwrite(true);
110         } else if (arg.equalsIgnoreCase("schemaReader")) {
111           if (logLevel != null) {
112             Usage("The option " + arg + " may be used only once.");
113           }
114           if (opt == null) {
115             if (i == args.length) {
116               Usage("Missing argument for option " + arg);
117             }
118             opt = args[++i];
119           }
120           schemaReaderClass = opt;
121         } else if (arg.equalsIgnoreCase("logFile")) {
122           if (logFile != null) {
123             Usage("The option " + arg + " may be used only once.");
124           }
125           if (opt == null) {
126             if (i == args.length) {
127               Usage("Missing argument for option " + arg);
128             }
129             opt = args[++i];
130           }
131           logFile = opt;
132         } else if (arg.equalsIgnoreCase("package")) {
133           if (packageName != null) {
134             Usage("The option " + arg + " may be used only once.");
135           }
136           if (opt == null) {
137             if (i == args.length) {
138               Usage("Missing argument for option " + arg);
139             }
140             opt = args[++i];
141           }
142           packageName = opt;
143           g.setProperty("jaxme.package.name", packageName);
144         } else if (arg.equalsIgnoreCase("logLevel")) {
145           if (logLevel != null) {
146             Usage("The option " + arg + " may be used only once.");
147           }
148           if (opt == null) {
149             if (i == args.length) {
150               Usage("Missing argument for option " + arg);
151             }
152             opt = args[++i];
153           }
154           logLevel = opt;
155         } else if (arg.equalsIgnoreCase("target")) {
156           if (target != null) {
157             Usage("The option " + arg + " may be used only once.");
158           }
159           if (opt == null) {
160             if (i == args.length) {
161               Usage("Missing argument for option " + arg);
162             }
163             opt = args[++i];
164           }
165           target = opt;
166         } else if (arg.equalsIgnoreCase("option")) {
167           if (opt == null) {
168             if (i == args.length) {
169               Usage("Missing argument for option " + arg);
170             }
171             opt = args[++i];
172           }
173           int offset = opt.indexOf('=');
174           if (offset < 1) {
175             System.err.println("Failed to parse option definition " + opt);
176             System.err.println("Must be like --option=name=value or");
177             System.err.println("--option=name=target=value");
178             System.err.println();
179             Usage(null);
180           }
181           String JavaDoc optName = opt.substring(0, offset);
182           String JavaDoc optValue = opt.substring(offset+1);
183           g.setProperty(optName, optValue);
184         } else if (arg.equalsIgnoreCase("validating")) {
185           g.setValidating(true);
186         } else if ("sgFactoryChain".equals(arg)) {
187           if (opt == null) {
188             if (i == args.length) {
189               Usage("Missing argument for option " + arg);
190             }
191             opt = args[++i];
192           }
193           Class JavaDoc c = null;
194           try {
195             c = Class.forName(opt);
196           } catch (ClassNotFoundException JavaDoc e) {
197             ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
198             if (cl != null) {
199               try {
200                 c = cl.loadClass(opt);
201               } catch (ClassNotFoundException JavaDoc f) {
202               }
203             }
204             if (c != null) {
205               System.err.println("Failed to load class " + opt);
206               System.exit(1);
207             }
208           }
209           if (!SGFactoryChain.class.isAssignableFrom(c)) {
210             System.err.println("The SGFactoryChain implementation " + c.getName() +
211                                " is not implementing " + SGFactoryChain.class.getName());
212           }
213           sgFactoryChains.add(c);
214         } else if (arg.equalsIgnoreCase("h") || arg.equalsIgnoreCase("help") ||
215                    arg.equalsIgnoreCase("?")) {
216           Usage(null);
217         } else {
218           Usage("Unknown option name: " + arg);
219         }
220       } else if (schemaFile != null) {
221         Usage("Only one input file may be specified.");
222        } else {
223          schemaFile = new java.io.File JavaDoc(args[i]);
224        }
225     }
226
227     if (schemaFile == null) {
228       Usage("A Schema file must be specified");
229     }
230
231     SchemaReader sr = null;
232     try {
233       Class JavaDoc c = Class.forName(schemaReaderClass);
234       sr = (SchemaReader) c.newInstance();
235       g.setSchemaReader(sr);
236       sr.setGenerator(g);
237     } catch (ClassNotFoundException JavaDoc e) {
238       System.err.println("Could not find SchemaReader class " + schemaReaderClass);
239       System.exit(1);
240     } catch (ClassCastException JavaDoc e) {
241       System.err.println("Class " + schemaReaderClass +
242                          " is not implementing " + SchemaReader.class.getName());
243       System.exit(1);
244     } catch (InstantiationException JavaDoc e) {
245       System.err.println("Failed to instantiate SchemaReader class " + schemaReaderClass);
246       System.exit(1);
247     } catch (IllegalAccessException JavaDoc e) {
248       System.err.println("Illegal access to SchemaReader class " + schemaReaderClass);
249       System.exit(1);
250     }
251
252     if (sgFactoryChains.size() > 0) {
253       if (!(sr instanceof JAXBSchemaReader)) {
254         System.err.println("Additional instances of " + SGFactoryChain.class.getName()
255                            + " may be specified only, if the schema reader is an instance of "
256                            + JAXBSchemaReader.class.getName());
257         System.exit(1);
258       }
259       JAXBSchemaReader jsr = (JAXBSchemaReader) sr;
260       for (int i = 0; i < sgFactoryChains.size(); i++) {
261         jsr.addSGFactoryChain((Class JavaDoc) sgFactoryChains.get(i));
262       }
263     }
264
265     if (target != null) {
266       g.setTargetDirectory(new File JavaDoc(target));
267     }
268
269     try {
270       g.generate(schemaFile);
271     } catch (Exception JavaDoc e) {
272       e.printStackTrace();
273       System.exit(1);
274     }
275
276     System.exit(0); // Explicit System.exit, so that hsqldb closes nicely
277
}
278 }
279
Popular Tags