1 package jimm.datavision.testdata; 2 import java.io.File ; 3 import javax.xml.parsers.SAXParserFactory ; 4 import org.xml.sax.*; 5 import org.xml.sax.helpers.DefaultHandler ; 6 7 13 14 public abstract class SchemaGen extends DefaultHandler { 15 16 protected String type; 17 protected int size; 18 protected boolean notNull; 19 protected boolean primaryKey; 20 protected boolean isFirstColumn; 21 22 public void run(String schemaXMLFile) { 23 try { 24 SAXParserFactory.newInstance().newSAXParser() 25 .parse(new File (schemaXMLFile), this); 26 } 27 catch (Exception e) { 28 e.printStackTrace(); 29 System.exit(1); 30 } 31 } 32 33 public void startElement(final String namespaceURI, final String localName, 34 final String qName, final Attributes attributes) 35 throws SAXException 36 { 37 String tagName = localName; 38 if (tagName == null || tagName.length() == 0) 39 tagName = qName; 40 41 if ("table".equals(tagName)) table(attributes); 42 else if ("column".equals(tagName)) column(attributes); 43 } 44 45 public void endElement(final String namespaceURI, final String localName, 46 final String qName) 47 throws SAXException 48 { 49 String tagName = localName; 50 if (tagName == null || tagName.length() == 0) 51 tagName = qName; 52 53 if ("table".equals(tagName)) endTable(); 54 } 55 56 62 protected void table(Attributes attributes) { 63 makeTable(attributes.getValue("name")); 64 isFirstColumn = true; 65 } 66 67 74 protected void column(Attributes attributes) { 75 String name = attributes.getValue("name"); 76 String type = attributes.getValue("type"); 77 String sizeStr = attributes.getValue("size"); 78 int size = 0; 79 if (sizeStr != null) 80 size = Integer.parseInt(sizeStr); 81 boolean notNull = (attributes.getValue("not-null") != null); 82 boolean primaryKey = (attributes.getValue("primary-key") != null); 83 84 if (!isFirstColumn) 85 System.out.print(","); 86 System.out.println(); 87 System.out.print("\t"); 88 89 printColumn(name, type, size, notNull, primaryKey); 90 isFirstColumn = false; 91 } 92 93 97 protected abstract void makeTable(String tableName); 98 99 protected String printableName(String name) { 100 if (name.indexOf(' ') >= 0 || !name.equals(name.toLowerCase())) 101 return "\"" + name + "\""; 102 else 103 return name; 104 } 105 106 109 protected abstract void endTable(); 110 111 115 protected void printColumn(String columnName, String type, int size, 116 boolean notNull, boolean primaryKey) 117 { 118 printColumnName(columnName); 119 printType(type, size); 120 if (notNull) printNotNull(); 121 if (primaryKey) printPrimaryKey(); 122 } 123 124 128 protected void printColumnName(String columnName) { 129 System.out.print(printableName(columnName)); 130 } 131 132 135 protected abstract void printType(String type, int size); 136 137 140 protected abstract void printNotNull(); 141 142 145 protected abstract void printPrimaryKey(); 146 147 } 148 | Popular Tags |