1 17 package org.apache.ws.jaxme.generator; 18 19 import java.io.File ; 20 import java.util.ArrayList ; 21 import java.util.List ; 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 40 public class Main { 41 43 public static void Usage(String msg) { 44 if (msg != null) { 45 System.err.println(msg); 46 System.err.println(); 47 } 48 java.io.PrintStream 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 76 public static void main (String args[]) throws Exception { 77 java.io.File schemaFile = null; 78 Generator g = new GeneratorImpl(); 79 String schemaReaderClass = JAXBSchemaReader.class.getName(); 80 String target = null; 81 String logFile = null; 82 String logLevel = null; 83 String packageName = null; 84 List sgFactoryChains = new ArrayList (); 85 86 for (int i = 0; i < args.length; i++) { 87 String 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 (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 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 optName = opt.substring(0, offset); 182 String 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 c = null; 194 try { 195 c = Class.forName(opt); 196 } catch (ClassNotFoundException e) { 197 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 198 if (cl != null) { 199 try { 200 c = cl.loadClass(opt); 201 } catch (ClassNotFoundException 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 (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 c = Class.forName(schemaReaderClass); 234 sr = (SchemaReader) c.newInstance(); 235 g.setSchemaReader(sr); 236 sr.setGenerator(g); 237 } catch (ClassNotFoundException e) { 238 System.err.println("Could not find SchemaReader class " + schemaReaderClass); 239 System.exit(1); 240 } catch (ClassCastException e) { 241 System.err.println("Class " + schemaReaderClass + 242 " is not implementing " + SchemaReader.class.getName()); 243 System.exit(1); 244 } catch (InstantiationException e) { 245 System.err.println("Failed to instantiate SchemaReader class " + schemaReaderClass); 246 System.exit(1); 247 } catch (IllegalAccessException 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 ) sgFactoryChains.get(i)); 262 } 263 } 264 265 if (target != null) { 266 g.setTargetDirectory(new File (target)); 267 } 268 269 try { 270 g.generate(schemaFile); 271 } catch (Exception e) { 272 e.printStackTrace(); 273 System.exit(1); 274 } 275 276 System.exit(0); } 278 } 279 | Popular Tags |