KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jimm > datavision > testdata > SchemaGen


1 package jimm.datavision.testdata;
2 import java.io.File JavaDoc;
3 import javax.xml.parsers.SAXParserFactory JavaDoc;
4 import org.xml.sax.*;
5 import org.xml.sax.helpers.DefaultHandler JavaDoc;
6
7 /**
8  * Skeleton for creating a schema.sql file by reading an XML description
9  * of a schema.
10  *
11  * @author Jim Menard, <a HREF="mailto:jimm@io.com">jimm@io.com</a>
12  */

13
14 public abstract class SchemaGen extends DefaultHandler JavaDoc {
15
16 protected String JavaDoc type;
17 protected int size;
18 protected boolean notNull;
19 protected boolean primaryKey;
20 protected boolean isFirstColumn;
21
22 public void run(String JavaDoc schemaXMLFile) {
23     try {
24     SAXParserFactory.newInstance().newSAXParser()
25         .parse(new File JavaDoc(schemaXMLFile), this);
26     }
27     catch (Exception JavaDoc e) {
28     e.printStackTrace();
29     System.exit(1);
30     }
31 }
32
33 public void startElement(final String JavaDoc namespaceURI, final String JavaDoc localName,
34              final String JavaDoc qName, final Attributes attributes)
35     throws SAXException
36 {
37     String JavaDoc 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 JavaDoc namespaceURI, final String JavaDoc localName,
46                final String JavaDoc qName)
47     throws SAXException
48 {
49     String JavaDoc tagName = localName;
50     if (tagName == null || tagName.length() == 0)
51         tagName = qName;
52
53     if ("table".equals(tagName)) endTable();
54 }
55
56 /**
57  * Parses a table XML tag and calls <code>makeTable</code>.
58  *
59  * @param attributes XML element attributes
60  * @see #makeTable
61  */

62 protected void table(Attributes attributes) {
63     makeTable(attributes.getValue("name"));
64     isFirstColumn = true;
65 }
66
67 /**
68  * Parses a column XML tag and calls <code>printColumn</code>. Also handles
69  * commas and indentation.
70  *
71  * @param attributes XML element attributes
72  * @see #makeTable
73  */

74 protected void column(Attributes attributes) {
75     String JavaDoc name = attributes.getValue("name");
76     String JavaDoc type = attributes.getValue("type");
77     String JavaDoc 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 /**
94  * Outputs the SQL needed to create a database table. Optionally prints
95  * the SQL needed to destroy the table first.
96  */

97 protected abstract void makeTable(String JavaDoc tableName);
98
99 protected String JavaDoc printableName(String JavaDoc name) {
100     if (name.indexOf(' ') >= 0 || !name.equals(name.toLowerCase()))
101     return "\"" + name + "\"";
102     else
103     return name;
104 }
105
106 /**
107  * Outputs the SQL needed to close a create table statement.
108  */

109 protected abstract void endTable();
110
111 /**
112  * Prints the SQL needed to create a database column within a create table
113  * statement.
114  */

115 protected void printColumn(String JavaDoc columnName, String JavaDoc 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 /**
125  * Prints the column name, taking into account case, blanks, and other
126  * possibly funky things.
127  */

128 protected void printColumnName(String JavaDoc columnName) {
129     System.out.print(printableName(columnName));
130 }
131
132 /**
133  * Prints the SQL that defines a column's type.
134  */

135 protected abstract void printType(String JavaDoc type, int size);
136
137 /**
138  * Prints the SQL that defines a column as NOT NULL.
139  */

140 protected abstract void printNotNull();
141
142 /**
143  * Prints the SQL that defines a column as a primary key.
144  */

145 protected abstract void printPrimaryKey();
146
147 }
148
Popular Tags