001: package jimm.datavision.testdata;
002:
003: import java.io.File;
004: import javax.xml.parsers.SAXParserFactory;
005: import org.xml.sax.*;
006: import org.xml.sax.helpers.DefaultHandler;
007:
008: /**
009: * Skeleton for creating a schema.sql file by reading an XML description
010: * of a schema.
011: *
012: * @author Jim Menard, <a href="mailto:jimm@io.com">jimm@io.com</a>
013: */
014:
015: public abstract class SchemaGen extends DefaultHandler {
016:
017: protected String type;
018: protected int size;
019: protected boolean notNull;
020: protected boolean primaryKey;
021: protected boolean isFirstColumn;
022:
023: public void run(String schemaXMLFile) {
024: try {
025: SAXParserFactory.newInstance().newSAXParser().parse(
026: new File(schemaXMLFile), this );
027: } catch (Exception e) {
028: e.printStackTrace();
029: System.exit(1);
030: }
031: }
032:
033: public void startElement(final String namespaceURI,
034: final String localName, final String qName,
035: final Attributes attributes) throws SAXException {
036: String tagName = localName;
037: if (tagName == null || tagName.length() == 0)
038: tagName = qName;
039:
040: if ("table".equals(tagName))
041: table(attributes);
042: else if ("column".equals(tagName))
043: column(attributes);
044: }
045:
046: public void endElement(final String namespaceURI,
047: final String localName, final String qName)
048: throws SAXException {
049: String tagName = localName;
050: if (tagName == null || tagName.length() == 0)
051: tagName = qName;
052:
053: if ("table".equals(tagName))
054: endTable();
055: }
056:
057: /**
058: * Parses a table XML tag and calls <code>makeTable</code>.
059: *
060: * @param attributes XML element attributes
061: * @see #makeTable
062: */
063: protected void table(Attributes attributes) {
064: makeTable(attributes.getValue("name"));
065: isFirstColumn = true;
066: }
067:
068: /**
069: * Parses a column XML tag and calls <code>printColumn</code>. Also handles
070: * commas and indentation.
071: *
072: * @param attributes XML element attributes
073: * @see #makeTable
074: */
075: protected void column(Attributes attributes) {
076: String name = attributes.getValue("name");
077: String type = attributes.getValue("type");
078: String sizeStr = attributes.getValue("size");
079: int size = 0;
080: if (sizeStr != null)
081: size = Integer.parseInt(sizeStr);
082: boolean notNull = (attributes.getValue("not-null") != null);
083: boolean primaryKey = (attributes.getValue("primary-key") != null);
084:
085: if (!isFirstColumn)
086: System.out.print(",");
087: System.out.println();
088: System.out.print("\t");
089:
090: printColumn(name, type, size, notNull, primaryKey);
091: isFirstColumn = false;
092: }
093:
094: /**
095: * Outputs the SQL needed to create a database table. Optionally prints
096: * the SQL needed to destroy the table first.
097: */
098: protected abstract void makeTable(String tableName);
099:
100: protected String printableName(String name) {
101: if (name.indexOf(' ') >= 0 || !name.equals(name.toLowerCase()))
102: return "\"" + name + "\"";
103: else
104: return name;
105: }
106:
107: /**
108: * Outputs the SQL needed to close a create table statement.
109: */
110: protected abstract void endTable();
111:
112: /**
113: * Prints the SQL needed to create a database column within a create table
114: * statement.
115: */
116: protected void printColumn(String columnName, String type,
117: int size, boolean notNull, boolean primaryKey) {
118: printColumnName(columnName);
119: printType(type, size);
120: if (notNull)
121: printNotNull();
122: if (primaryKey)
123: printPrimaryKey();
124: }
125:
126: /**
127: * Prints the column name, taking into account case, blanks, and other
128: * possibly funky things.
129: */
130: protected void printColumnName(String columnName) {
131: System.out.print(printableName(columnName));
132: }
133:
134: /**
135: * Prints the SQL that defines a column's type.
136: */
137: protected abstract void printType(String type, int size);
138:
139: /**
140: * Prints the SQL that defines a column as NOT NULL.
141: */
142: protected abstract void printNotNull();
143:
144: /**
145: * Prints the SQL that defines a column as a primary key.
146: */
147: protected abstract void printPrimaryKey();
148:
149: }
|