/** * Called by {@link #getDefault} to load the configuration the * first time. Will not complain about inability to load * configuration file from one of the default directories, but if * it cannot find the JAR's configuration file, will throw a * run time exception. */ protected static void load() { _default = Configuration.loadProperties( Property.ResourceName, Property.FileName); // Parse XML namespace and package list _nspackages = new Hashtable(); String prop = _default.getProperty( Property.NamespacePackages, ""); StringTokenizer tokens = new StringTokenizer(prop, ","); while(tokens.hasMoreTokens()) { String token = tokens.nextToken(); int comma = token.indexOf('='); if(comma==-1) continue; String ns = token.substring(0,comma).trim(); String javaPackage = token.substring(comma+1).trim(); _nspackages.put(ns, javaPackage); } //-- bound properties prop = _default.getProperty( Property.BOUND_PROPERTIES, ""); _boundProperties = prop.equalsIgnoreCase("true"); //-- Equals method? prop = _default.getProperty( Property.EqualsMethod, ""); _equalsMethod = prop.equalsIgnoreCase("true"); initBindingType(); } //-- load
/** * Creates Java Source code (Object model) for the given XML Schema * @param InputSource - the InputSource representing the XML schema. * @param packageName the package for the generated source files **/ public void generateSource(InputSource source, String packageName) { //-- get default parser from Configuration Parser parser = null; try { parser = Configuration.getParser(); } catch(RuntimeException rte) {} if (parser == null) { System.out.println("fatal error: unable to create SAX parser."); return; } SchemaUnmarshaller schemaUnmarshaller = null; try { schemaUnmarshaller = new SchemaUnmarshaller(); } catch (SAXException e) { // can never happen since a SAXException is thrown // when we are dealing with an included schema e.printStackTrace(); } parser.setDocumentHandler(schemaUnmarshaller); parser.setErrorHandler(schemaUnmarshaller); try { parser.parse(source); } catch(java.io.IOException ioe) { System.out.println("error reading XML Schema file"); return; } catch(org.xml.sax.SAXException sx) { Exception except = sx.getException(); if (except == null) except = sx; if (except instanceof SAXParseException) { SAXParseException spe = (SAXParseException)except; System.out.println("SAXParseException: " + spe); System.out.print(" - occured at line "); System.out.print(spe.getLineNumber()); System.out.print(", column "); System.out.println(spe.getColumnNumber()); } else except.printStackTrace(); return; } Schema schema = schemaUnmarshaller.getSchema(); generateSource(schema, packageName); } //-- generateSource
/** * main class used for command line invocation * @param args the String[] consisting of the command line arguments **/ public static void main(String[] args) { CommandLineOptions allOptions = new CommandLineOptions(); //-- filename flag allOptions.addFlag("i", "filename", "Sets the input filename"); //-- package name flag allOptions.addFlag("package", "package-name", "Sets the package name", true); //-- destination directory String desc = "Sets the destination output directory"; allOptions.addFlag("dest", "dest-dir", desc, true); //-- line break flag desc = "Sets the line separator style for the desired platform"; allOptions.addFlag("line-separator", "( unix | mac | win)", desc, true); //-- Force flag desc = "Suppresses non fatal warnings, such as overwriting files."; allOptions.addFlag("f", "", desc, true); //-- Help flag desc = "Displays this help screen."; allOptions.addFlag("h", "", desc, true); //-- verbose flag desc = "Prints out additional messages when creaing source"; allOptions.addFlag("verbose", "", desc, true); //-- no descriptors flag desc = "Disables the generation of the Class descriptors"; allOptions.addFlag("nodesc", "", desc, true); //-- source generator types name flag desc = "Sets the source generator types name (SGTypeFactory)"; allOptions.addFlag("types", "types", desc, true); //-- XXX maintained temporarily allOptions.addFlag("type-factory", "classname", "", true); //-- no marshalling framework methods desc = "Disables the generation of the methods specific to the XML marshalling framework"; allOptions.addFlag("nomarshall","",desc,true); //-- implements org.exolab.castor.tests.CastroTestable? desc = "Implements some specific methods to allow the generated classes to be used with Castor Testing Framework"; allOptions.addFlag("testable","",desc,true); //-- Process the specified command line options Properties options = allOptions.getOptions(args); //-- check for help option if (options.getProperty("h") != null) { PrintWriter pw = new PrintWriter(System.out, true); allOptions.printHelp(pw); pw.flush(); return; } String schemaFilename = options.getProperty("i"); String packageName = options.getProperty("package"); String lineSepStyle = options.getProperty("line-separator"); boolean force = (options.getProperty("f") != null); String typeFactory = options.getProperty("types"); boolean verbose = (options.getProperty("verbose") != null); if (schemaFilename == null) { System.out.println(appName); allOptions.printUsage(new PrintWriter(System.out)); return; } // -- XXX maintained temporarily if (typeFactory == null) typeFactory = options.getProperty("type-factory"); String lineSep = System.getProperty("line.separator"); if (lineSepStyle != null) { if ("win".equals(lineSepStyle)) { System.out.println(" - using Windows style line separation."); lineSep = "\r\n"; } else if ("unix".equals(lineSepStyle)) { System.out.println(" - using UNIX style line separation."); lineSep = "\n"; } else if ("mac".equals(lineSepStyle)) { System.out.println(" - using Macintosh style line separation."); lineSep = "\r"; } else { System.out.print("- invalid option for line-separator: "); System.out.println(lineSepStyle); System.out.println("-- using default line separator for this platform"); } } SourceGenerator sgen = null; if (typeFactory != null) { typeFactory = Configuration.getProperty("org.exolab.castor.builder.type." + typeFactory.toLowerCase(),typeFactory); try { sgen = new SourceGenerator((FieldInfoFactory)Class.forName(typeFactory).newInstance()); } catch(Exception x) { System.out.print("- invalid option for types: "); System.out.println(typeFactory); System.out.println(x); System.out.println("-- using default source generator types"); sgen = new SourceGenerator(); // default } } else { sgen = new SourceGenerator(); // default } sgen.setDestDir(options.getProperty("dest")); sgen.setLineSeparator(lineSep); sgen.setSuppressNonFatalWarnings(force); sgen.setVerbose(verbose); if (force) System.out.println("-- Suppressing non fatal warnings."); if (options.getProperty("nodesc") != null) { sgen.setDescriptorCreation(false); System.out.print("-- "); System.out.println(DISABLE_DESCRIPTORS_MSG); } if (options.getProperty("nomarshall") != null) { sgen.setCreateMarshalMethods(false); System.out.print("-- "); System.out.println(DISABLE_MARSHALL_MSG); } if (options.getProperty("testable") != null) { sgen.setTestable(true); System.out.print("-- "); System.out.println(CASTOR_TESTABLE_MSG); } try { sgen.generateSource(schemaFilename, packageName); } catch(java.io.FileNotFoundException fne) { System.out.println("unable to open XML schema file"); return; } } //-- main