📄 .#maptojava.java.1.26
字号:
package org.ephman.abra.tools;import org.ephman.xml.*;import org.ephman.abra.tools.generators.*;import org.ephman.abra.tools.plugins.*;import java.io.*;import java.util.*;import org.ephman.utils.*;/** * read an XML file representing a Java to SQL mapping for * then generate classes in the current directory (or use -outdir) * ! make sure to give fully qualified class name ie * <code>com.foo.bar.database.User</code> in the map file.. * default output is current directory or use -outdir switch * to turn off schema or class generation use the switch -no... * * * @author Paul M. Bethe * @version 0.0.2 */public class MapToJava extends XmlProcessor { public static final String VERSION_STRING = "0.9.8"; public MapToJava (String odir, boolean schema, boolean classes, boolean factories, boolean procs, boolean useAbraParser) { this (odir, schema, classes, factories, useAbraParser); this.useProcs = procs; } public MapToJava (String odir, boolean schema, boolean classes, boolean factories, boolean useAbraParser) { super (useAbraParser); outdir = odir; this.makeSchema = schema; this.makeClasses = classes; this.makeFactories = factories; } public static String fileSeperator = "/"; //System.getProperty ("file.seperator"); //'/';bb protected static String rootString = "/"; protected static String windowsRootString = ":"; protected static boolean isTopLevelDirectory (String fname) { boolean result = fname.startsWith(rootString); if (!result) { // maybe it's a windows directory result = (fname.length() > 1) && (fname.indexOf (windowsRootString)) != -1; } return result; } protected boolean makeSchema; protected boolean makeClasses; // protected boolean makeValidators; public static boolean makeFactories; protected boolean useProcs; protected String toImplement = ""; protected String commonMapFile; public static String outdir; protected static String outSchema = ""; protected static String facImp = "FactoryBase"; public static void printHelp () { System.out.println ("Usage: java com.tradinglinx.tools.xml.MapToJava map-file"); System.out.println ("\toptions:\n\t\t -outdir <directory>"); System.out.println ("\t\t-noclasses (turnoff class generation)"); System.out.println ("\t\t-schema <file-name> (generate schema to the specified schema-file)"); System.out.println ("\t\t-verify (just verify XML, generate nothing)"); System.out.println ("\t\t-factories (make database factories using Java->sql map)"); System.out.println ("\t\t-procs (use stored procedures/functions instead of dyn SQL) "); System.out.println ("\t\t-xp (use the Abra JavaCup based Xml mini-parser instead of Xerces) "); System.out.println ("\t\t-validation generate validators for each class"); System.out.println ("\t\t-props <prop-file> use this prop file for everything"); System.out.println ("\t\t-verbose to get messages about what is being generated"); System.out.println ("\t\t-mega to get every single message (a lot) about what is being generated"); System.out.println ("\t\t-supress to print only fatal errors/ nothing else"); System.out.println ("\t\t-help to get this print out"); System.out.println ("\n\tDEPRECATED: \n"); System.out.println ("\t\t-implements <class-name> (going away now xml-tags in map-files)"); } public static void main (String[] argv) { System.out.println ("Abra generator v" + VERSION_STRING); boolean valid = true; if (argv.length < 1 || (argv.length == 1 && argv[0].startsWith ("-"))) { printHelp (); } else { /* try to run */ Vector mapList = new Vector (); // String mapName = ""; boolean makeSchema = false; boolean makeClasses = true; boolean makeValidators = false; boolean makeFactories = false; boolean procs = false; boolean useAbraParser = false; String toImp = ""; String outdir = System.getProperties().getProperty("user.dir"); if (argv.length == 1) mapList.addElement (argv[0]); else { // more than one argument for (int i = 0; i < argv.length; i++) { if (argv[i].equals ("-outdir") && i+1 < argv.length) { i++; // look at data.. if (isTopLevelDirectory (argv[i])) outdir = argv[i]; else outdir += fileSeperator + argv[i]; } else if (argv[i].equals ("-common") && i+1 < argv.length) { i++; // look at data.. // commonMapFile = argv[i]; } else if (argv[i].equals ("-schema") && i+1 < argv.length) { makeSchema = true; i++; // look at data.. outSchema = argv[i]; } else if (argv[i].equals ("-verbose")) { Debugger.init (Debugger.VERBOSE); } else if (argv[i].equals ("-supress")) { Debugger.init (Debugger.ERROR); } else if (argv[i].equals ("-mega")) { Debugger.init (Debugger.ALL); } else if (argv[i].equals ("-procs")) { procs=true; if (i + 1 < argv.length && !argv[i+1].startsWith ("-")) { i++; SchemaGenerator.dbPackageName = argv[i]; } } else if (argv[i].equals ("-xp")) { // use xml parser (Abra) useAbraParser = true; } else if (argv[i].equals ("-help")) { printHelp (); System.exit (0); } else if (argv[i].equals ("-factories")) { makeFactories = true; } else if (argv[i].equals ("-validation")) makeValidators = true; else if (argv[i].equals ("-noclasses")) makeClasses = false; else if (argv[i].equals ("-props")) { try { File f = new File (argv[i+1]); Properties props = new Properties (); props.load (new FileInputStream (f)); makeClasses = !(new Boolean(props.getProperty("noclasses", "false")).booleanValue()); outSchema = props.getProperty("schema"); if (Checks.exists (outSchema)) makeSchema = true; makeFactories = new Boolean(props.getProperty("factories", "false")).booleanValue(); procs = new Boolean(props.getProperty("procs", "false")).booleanValue(); makeValidators = new Boolean(props.getProperty("validate", "false")).booleanValue(); if (new Boolean(props.getProperty("useAbraParser", "false")).booleanValue()) useAbraParser = true; String mapFiles = props.getProperty("files", ""); String tmp = props.getProperty("outdir", ""); if (isTopLevelDirectory(outdir)) outdir = tmp; mapList.addAll (Tokenizer.tokenize (mapFiles, ";", "'")); i = argv.length; } catch (Exception e) {} } else if (argv[i].equals ("-verify")) { makeClasses = false; makeSchema = false; makeFactories = false; } else // must be map name mapList.addElement(argv[i]); } } try { MapToJava mtj = new MapToJava (outdir, makeSchema, makeClasses, makeFactories, procs, useAbraParser); generatorHandler = new PluginGeneratorHandler (); if (makeValidators) generatorHandler.registerGenerator (new ValidatorGenerator ()); if (useAbraParser) Debugger.trace ("Using Abra XMLParser", Debugger.SHORT); for (int i =0; i < mapList.size(); i++) { String mapName = (String)mapList.elementAt(i); Debugger.trace ("***Mapping file: " + mapName, Debugger.SHORT); mtj.applyRules(mtj.mapXMLFile (mapName,false), mapName); } if (mapList.size() > 0) mtj.generateAll(); } catch (XmlException e) { e.printStackTrace (); valid = false; } catch (SchemaException se) { System.err.println("*** Invalid schema:" + se.toString() + "\nerror= " + se.getMessage()); se.printStackTrace(); valid = false; } catch (Exception e) { System.err.println("*** Parsing failed:" + e.toString() + "\nerror= " + e.getMessage()); e.printStackTrace(); valid = false; } if (valid && mapList.size() > 0) { try { Debugger.trace ("Success", Debugger.SHORT); if (!(makeClasses || makeSchema)) Debugger.trace ("XML verified", Debugger.SHORT); if (makeClasses) Debugger.trace ("Classes generated in " + outdir, Debugger.SHORT); if (makeSchema) { Debugger.trace ("Schema file '" + outSchema + "' written", Debugger.SHORT); } } catch (SchemaException se) { // non expected } } else System.exit (1); } } protected JClass currentClass = null; protected JField currentField = null; private static PluginGeneratorHandler generatorHandler; protected HashMap classTree = new HashMap (); private boolean inFieldNode = false; public HashMap getClassTree () { return this.classTree; } public void addMapFile (String filePath) throws Exception { // Do not validate XML - we have not DTD applyRules (mapXMLFile (filePath, false), filePath); } public static PropertyTable propertyTable = null; String PROPERTY_TAG = "property"; String GENERATOR_TAG = "generator"; String CLASS_IMPLEMENTS_TAG = "class-implements"; // all classes implement these String CLASS_GENERATOR_TAG = "class-generator"; HashSet ignoreTags = new HashSet (); { ignoreTags.add (PROPERTY_TAG); ignoreTags.add (GENERATOR_TAG); ignoreTags.add (CLASS_IMPLEMENTS_TAG); ignoreTags.add (CLASS_GENERATOR_TAG); } void addGenerators (Vector gens, String mapFile) throws SchemaException { for (int i=0; gens.size () > i; i++) { XmlNode n = (XmlNode)gens.elementAt (i); addGenerator (n, mapFile); } } void addGenerator (XmlNode n, String mapFile) throws SchemaException { String className = getAttribute (n, "name"); Generator p = null; try { p = (Generator)Class.forName(className).newInstance (); } catch (Exception e) { e.printStackTrace (); throw new SchemaException (e.getMessage (), mapFile); } generatorHandler.registerGenerator (p); if (p instanceof ClassGenerator) TypeMapper.setObjectMap (((ClassGenerator)p).getTypeMap ()); Debugger.trace ("Registered generator " + className, Debugger.SHORT); } protected String getAttribute (XmlNode thisNode, String attrName) { return propertyTable.replace (thisNode.getAttribute (attrName)); } protected void applyRules (XmlNode thisNode, String mapName) throws XmlException, IOException, SchemaException { String thisName = thisNode.getName ();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -