Revision 16
Added by Matt Jones over 24 years ago
src/edu/ucsb/nceas/metacat/DBWriter.java | ||
---|---|---|
67 | 67 |
SQLException, |
68 | 68 |
ClassNotFoundException |
69 | 69 |
{ |
70 |
|
|
71 | 70 |
// Open a connection to the database |
72 | 71 |
conn = openDBConnection( |
73 | 72 |
"oracle.jdbc.driver.OracleDriver", |
74 | 73 |
dbstring, user, password); |
75 | 74 |
|
75 |
|
|
76 | 76 |
// |
77 | 77 |
// Set up the SAX document handlers for parsing |
78 | 78 |
// |
... | ... | |
81 | 81 |
|
82 | 82 |
// Use the XMLDocumentHandler interface for namespace support |
83 | 83 |
// instead of org.xml.sax.DocumentHandler |
84 |
XMLDocumentHandler xmlDocHandler = new XMLDocumentHandlerImpl(); |
|
84 |
XMLDocumentHandler xmlDocHandler = new XMLDocumentHandlerImpl(conn);
|
|
85 | 85 |
|
86 | 86 |
// For all the other interface use the default provided by |
87 | 87 |
// Handler base |
... | ... | |
119 | 119 |
|
120 | 120 |
} |
121 | 121 |
|
122 |
private Connection openDBConnection(String dbDriver, String connection,
|
|
122 |
private Connection openDBConnection(String dbDriver, String connection, |
|
123 | 123 |
String user, String password) |
124 | 124 |
throws SQLException, ClassNotFoundException { |
125 |
// Load the Oracle JDBC driver
|
|
126 |
Class.forName (dbDriver);
|
|
125 |
// Load the Oracle JDBC driver |
|
126 |
Class.forName (dbDriver); |
|
127 | 127 |
|
128 |
// Connect to the database |
|
129 |
Connection conn = |
|
130 |
DriverManager.getConnection( connection, user, password); |
|
131 |
return conn; |
|
132 |
} |
|
128 |
// Connect to the database |
|
129 |
Connection conn = DriverManager.getConnection( connection, user, password); |
|
130 |
return conn; |
|
131 |
} |
|
133 | 132 |
|
134 |
|
|
135 |
static public URL fileToURL(File file) |
|
133 |
static public URL fileToURL(File file) |
|
136 | 134 |
{ |
137 |
String path = file.getAbsolutePath(); |
|
138 |
String fSep = System.getProperty("file.separator"); |
|
139 |
if (fSep != null && fSep.length() == 1) |
|
140 |
path = path.replace(fSep.charAt(0), '/'); |
|
141 |
if (path.length() > 0 && path.charAt(0) != '/') |
|
142 |
path = '/' + path; |
|
143 |
try { |
|
144 |
return new URL("file", null, path); |
|
145 |
} |
|
146 |
catch (java.net.MalformedURLException e) { |
|
147 |
/* According to the spec this could only happen if the file |
|
148 |
protocol were not recognized. */ |
|
149 |
throw new Error("unexpected MalformedURLException"); |
|
150 |
} |
|
135 |
String path = file.getAbsolutePath();
|
|
136 |
String fSep = System.getProperty("file.separator");
|
|
137 |
if (fSep != null && fSep.length() == 1)
|
|
138 |
path = path.replace(fSep.charAt(0), '/');
|
|
139 |
if (path.length() > 0 && path.charAt(0) != '/')
|
|
140 |
path = '/' + path;
|
|
141 |
try {
|
|
142 |
return new URL("file", null, path);
|
|
143 |
}
|
|
144 |
catch (java.net.MalformedURLException e) {
|
|
145 |
/* According to the spec this could only happen if the file
|
|
146 |
protocol were not recognized. */
|
|
147 |
throw new Error("unexpected MalformedURLException");
|
|
148 |
}
|
|
151 | 149 |
} |
152 | 150 |
|
153 | 151 |
} |
154 |
/*********************************************************************** |
|
155 |
Implementation of XMLDocumentHandler interface. Only the new |
|
156 |
startElement and endElement interfaces are implemented here. All other |
|
157 |
interfaces are implemented in the class HandlerBase. |
|
158 |
**********************************************************************/ |
|
159 | 152 |
|
153 |
/**************************************************************** |
|
154 |
* Implementation of XMLDocumentHandler interface. * |
|
155 |
* Other interfaces are implemented in the class HandlerBase. * |
|
156 |
****************************************************************/ |
|
157 |
|
|
160 | 158 |
class XMLDocumentHandlerImpl extends DefaultXMLDocumentHandler |
161 | 159 |
{ |
162 | 160 |
|
163 |
boolean debug = false; |
|
164 |
boolean stackCreated = false; |
|
165 |
private Stack elementStack; |
|
161 |
private boolean debug = false; |
|
162 |
private boolean stackCreated = false; |
|
163 |
private Stack elementStack; |
|
164 |
private Connection conn = null; |
|
166 | 165 |
|
167 |
public void XMLDocumentHandlerImpl()
|
|
166 |
public XMLDocumentHandlerImpl(Connection conn)
|
|
168 | 167 |
{ |
168 |
System.out.println("\nINITIALIZING HANDLER....\n"); |
|
169 |
|
|
170 |
this.conn = conn; |
|
171 |
|
|
172 |
// Create the stack for keeping track of element context |
|
173 |
// if it doesn't already exist |
|
174 |
if (!stackCreated) { |
|
175 |
elementStack = new Stack(); |
|
176 |
stackCreated = true; |
|
177 |
} |
|
178 |
|
|
169 | 179 |
} |
170 |
|
|
171 |
|
|
180 |
|
|
172 | 181 |
public void startElement(NSName name, SAXAttrList atts) throws SAXException |
173 | 182 |
{ |
174 | 183 |
|
... | ... | |
188 | 197 |
expName = name.getExpandedName(); |
189 | 198 |
|
190 | 199 |
// Create the current element representation |
191 |
currentElement = new DBSAXElement(1, localName, false, 0); |
|
200 |
currentElement = new DBSAXElement(conn, 1, localName, false, 0);
|
|
192 | 201 |
System.out.println("Element created:" + currentElement.getTagName()); |
193 | 202 |
|
194 | 203 |
// Add all of the attributes |
195 | 204 |
for (int i=0; i<atts.getLength(); i++) |
196 | 205 |
{ |
197 | 206 |
|
198 |
// Use the methods getQualifiedName(), getLocalName(), getNamespace() |
|
199 |
// and getExpandedName() in SAXAttrList interface to get Namespace |
|
200 |
// information. |
|
207 |
// Use the methods getQualifiedName(), getLocalName(), getNamespace()
|
|
208 |
// and getExpandedName() in SAXAttrList interface to get Namespace
|
|
209 |
// information.
|
|
201 | 210 |
|
202 | 211 |
qName = atts.getQualifiedName(i); |
203 | 212 |
localName = atts.getLocalName(i); |
... | ... | |
219 | 228 |
|
220 | 229 |
} |
221 | 230 |
|
222 |
// Create the stack for keeping track of element context |
|
223 |
// if it doesn't already exist |
|
224 |
if (!stackCreated) { |
|
225 |
elementStack = new Stack(); |
|
226 |
stackCreated = true; |
|
227 |
} |
|
228 |
|
|
229 | 231 |
// Add the element to the stack, so that any text data can be |
230 | 232 |
// added as it is encountered |
231 | 233 |
elementStack.push(currentElement); |
... | ... | |
260 | 262 |
System.out.println("Closing element: " + expName); |
261 | 263 |
} |
262 | 264 |
|
263 |
public void db(int flag) { |
|
265 |
/** Debug routine */ |
|
266 |
private void db(int flag) { |
|
264 | 267 |
if (debug) { |
265 | 268 |
System.err.println("DEBUG POSITION " + flag); |
266 | 269 |
} |
src/edu/ucsb/nceas/metacat/DBSAXNode.java | ||
---|---|---|
23 | 23 |
private boolean isEmpty; |
24 | 24 |
private long parent_id; |
25 | 25 |
private Hashtable attributes; |
26 |
private Connection conn; |
|
26 | 27 |
|
27 |
public DBSAXElement (long element_id, String tagname, |
|
28 |
public DBSAXElement (Connection conn, long element_id, String tagname,
|
|
28 | 29 |
boolean isEmpty, long parent_id) { |
29 |
this.element_id = element_id; |
|
30 |
this.conn = conn; |
|
31 |
this.element_id = assignElementID(); |
|
32 |
|
|
30 | 33 |
this.tagname = tagname; |
31 | 34 |
this.isEmpty = isEmpty; |
32 | 35 |
this.parent_id = parent_id; |
33 | 36 |
content = new StringBuffer(); |
34 | 37 |
attributes = new Hashtable(); |
38 |
writeElementToDB(); |
|
39 |
|
|
40 |
System.out.println("******ELEMENT_ID: " + this.element_id); |
|
35 | 41 |
}; |
36 | 42 |
|
43 |
private long assignElementID() { |
|
44 |
long maxid=0; |
|
45 |
Statement stmt; |
|
46 |
try { |
|
47 |
stmt = conn.createStatement(); |
|
48 |
stmt.execute("SELECT MAX(nodeid) FROM xml_elements"); |
|
49 |
try { |
|
50 |
ResultSet rs = stmt.getResultSet(); |
|
51 |
try { |
|
52 |
boolean tableHasRows = rs.next(); |
|
53 |
if (tableHasRows) { |
|
54 |
try { |
|
55 |
maxid = rs.getInt(1); |
|
56 |
} catch (SQLException e) { |
|
57 |
System.out.println("Error with getInt: " + e.getMessage()); |
|
58 |
} |
|
59 |
} |
|
60 |
} catch (SQLException e) { |
|
61 |
System.out.println("Error with next: " + e.getMessage()); |
|
62 |
} |
|
63 |
} catch (SQLException e) { |
|
64 |
System.out.println("Error with getrset: " + e.getMessage()); |
|
65 |
} |
|
66 |
stmt.close(); |
|
67 |
} catch (SQLException e) { |
|
68 |
System.out.println("Error getting id: " + e.getMessage()); |
|
69 |
} |
|
70 |
|
|
71 |
// assign a new ID number |
|
72 |
return (maxid + 1); |
|
73 |
} |
|
74 |
|
|
75 |
private void writeElementToDB() { |
|
76 |
try { |
|
77 |
PreparedStatement pstmt; |
|
78 |
if (parent_id != 0) { |
|
79 |
pstmt = conn.prepareStatement( |
|
80 |
"INSERT INTO xml_elements(nodeid, nodename, parentnodeid) " + |
|
81 |
"VALUES (?, ?, ?)"); |
|
82 |
} else { |
|
83 |
pstmt = conn.prepareStatement( |
|
84 |
"INSERT INTO xml_elements(nodeid, nodename) " + |
|
85 |
"VALUES (?, ?)"); |
|
86 |
} |
|
87 |
|
|
88 |
// Bind the values to the query |
|
89 |
pstmt.setLong(1, element_id); // The first ? is for NODEID |
|
90 |
pstmt.setString(2, getTagName());// The second ? is for NODENAME |
|
91 |
if (parent_id != 0) { |
|
92 |
pstmt.setLong(3, parent_id); |
|
93 |
} |
|
94 |
// Do the insertion |
|
95 |
pstmt.execute(); |
|
96 |
pstmt.close(); |
|
97 |
|
|
98 |
} catch (SQLException e) { |
|
99 |
System.out.println(e.getMessage()); |
|
100 |
} |
|
101 |
} |
|
102 |
|
|
37 | 103 |
// used by JTree to display this node |
38 | 104 |
public String toString () |
39 | 105 |
{ |
src/edu/ucsb/nceas/metacat/DBSAXElement.java | ||
---|---|---|
23 | 23 |
private boolean isEmpty; |
24 | 24 |
private long parent_id; |
25 | 25 |
private Hashtable attributes; |
26 |
private Connection conn; |
|
26 | 27 |
|
27 |
public DBSAXElement (long element_id, String tagname, |
|
28 |
public DBSAXElement (Connection conn, long element_id, String tagname,
|
|
28 | 29 |
boolean isEmpty, long parent_id) { |
29 |
this.element_id = element_id; |
|
30 |
this.conn = conn; |
|
31 |
this.element_id = assignElementID(); |
|
32 |
|
|
30 | 33 |
this.tagname = tagname; |
31 | 34 |
this.isEmpty = isEmpty; |
32 | 35 |
this.parent_id = parent_id; |
33 | 36 |
content = new StringBuffer(); |
34 | 37 |
attributes = new Hashtable(); |
38 |
writeElementToDB(); |
|
39 |
|
|
40 |
System.out.println("******ELEMENT_ID: " + this.element_id); |
|
35 | 41 |
}; |
36 | 42 |
|
43 |
private long assignElementID() { |
|
44 |
long maxid=0; |
|
45 |
Statement stmt; |
|
46 |
try { |
|
47 |
stmt = conn.createStatement(); |
|
48 |
stmt.execute("SELECT MAX(nodeid) FROM xml_elements"); |
|
49 |
try { |
|
50 |
ResultSet rs = stmt.getResultSet(); |
|
51 |
try { |
|
52 |
boolean tableHasRows = rs.next(); |
|
53 |
if (tableHasRows) { |
|
54 |
try { |
|
55 |
maxid = rs.getInt(1); |
|
56 |
} catch (SQLException e) { |
|
57 |
System.out.println("Error with getInt: " + e.getMessage()); |
|
58 |
} |
|
59 |
} |
|
60 |
} catch (SQLException e) { |
|
61 |
System.out.println("Error with next: " + e.getMessage()); |
|
62 |
} |
|
63 |
} catch (SQLException e) { |
|
64 |
System.out.println("Error with getrset: " + e.getMessage()); |
|
65 |
} |
|
66 |
stmt.close(); |
|
67 |
} catch (SQLException e) { |
|
68 |
System.out.println("Error getting id: " + e.getMessage()); |
|
69 |
} |
|
70 |
|
|
71 |
// assign a new ID number |
|
72 |
return (maxid + 1); |
|
73 |
} |
|
74 |
|
|
75 |
private void writeElementToDB() { |
|
76 |
try { |
|
77 |
PreparedStatement pstmt; |
|
78 |
if (parent_id != 0) { |
|
79 |
pstmt = conn.prepareStatement( |
|
80 |
"INSERT INTO xml_elements(nodeid, nodename, parentnodeid) " + |
|
81 |
"VALUES (?, ?, ?)"); |
|
82 |
} else { |
|
83 |
pstmt = conn.prepareStatement( |
|
84 |
"INSERT INTO xml_elements(nodeid, nodename) " + |
|
85 |
"VALUES (?, ?)"); |
|
86 |
} |
|
87 |
|
|
88 |
// Bind the values to the query |
|
89 |
pstmt.setLong(1, element_id); // The first ? is for NODEID |
|
90 |
pstmt.setString(2, getTagName());// The second ? is for NODENAME |
|
91 |
if (parent_id != 0) { |
|
92 |
pstmt.setLong(3, parent_id); |
|
93 |
} |
|
94 |
// Do the insertion |
|
95 |
pstmt.execute(); |
|
96 |
pstmt.close(); |
|
97 |
|
|
98 |
} catch (SQLException e) { |
|
99 |
System.out.println(e.getMessage()); |
|
100 |
} |
|
101 |
} |
|
102 |
|
|
37 | 103 |
// used by JTree to display this node |
38 | 104 |
public String toString () |
39 | 105 |
{ |
src/edu/ucsb/nceas/metacat/DBSAXWriter.java | ||
---|---|---|
67 | 67 |
SQLException, |
68 | 68 |
ClassNotFoundException |
69 | 69 |
{ |
70 |
|
|
71 | 70 |
// Open a connection to the database |
72 | 71 |
conn = openDBConnection( |
73 | 72 |
"oracle.jdbc.driver.OracleDriver", |
74 | 73 |
dbstring, user, password); |
75 | 74 |
|
75 |
|
|
76 | 76 |
// |
77 | 77 |
// Set up the SAX document handlers for parsing |
78 | 78 |
// |
... | ... | |
81 | 81 |
|
82 | 82 |
// Use the XMLDocumentHandler interface for namespace support |
83 | 83 |
// instead of org.xml.sax.DocumentHandler |
84 |
XMLDocumentHandler xmlDocHandler = new XMLDocumentHandlerImpl(); |
|
84 |
XMLDocumentHandler xmlDocHandler = new XMLDocumentHandlerImpl(conn);
|
|
85 | 85 |
|
86 | 86 |
// For all the other interface use the default provided by |
87 | 87 |
// Handler base |
... | ... | |
119 | 119 |
|
120 | 120 |
} |
121 | 121 |
|
122 |
private Connection openDBConnection(String dbDriver, String connection,
|
|
122 |
private Connection openDBConnection(String dbDriver, String connection, |
|
123 | 123 |
String user, String password) |
124 | 124 |
throws SQLException, ClassNotFoundException { |
125 |
// Load the Oracle JDBC driver
|
|
126 |
Class.forName (dbDriver);
|
|
125 |
// Load the Oracle JDBC driver |
|
126 |
Class.forName (dbDriver); |
|
127 | 127 |
|
128 |
// Connect to the database |
|
129 |
Connection conn = |
|
130 |
DriverManager.getConnection( connection, user, password); |
|
131 |
return conn; |
|
132 |
} |
|
128 |
// Connect to the database |
|
129 |
Connection conn = DriverManager.getConnection( connection, user, password); |
|
130 |
return conn; |
|
131 |
} |
|
133 | 132 |
|
134 |
|
|
135 |
static public URL fileToURL(File file) |
|
133 |
static public URL fileToURL(File file) |
|
136 | 134 |
{ |
137 |
String path = file.getAbsolutePath(); |
|
138 |
String fSep = System.getProperty("file.separator"); |
|
139 |
if (fSep != null && fSep.length() == 1) |
|
140 |
path = path.replace(fSep.charAt(0), '/'); |
|
141 |
if (path.length() > 0 && path.charAt(0) != '/') |
|
142 |
path = '/' + path; |
|
143 |
try { |
|
144 |
return new URL("file", null, path); |
|
145 |
} |
|
146 |
catch (java.net.MalformedURLException e) { |
|
147 |
/* According to the spec this could only happen if the file |
|
148 |
protocol were not recognized. */ |
|
149 |
throw new Error("unexpected MalformedURLException"); |
|
150 |
} |
|
135 |
String path = file.getAbsolutePath();
|
|
136 |
String fSep = System.getProperty("file.separator");
|
|
137 |
if (fSep != null && fSep.length() == 1)
|
|
138 |
path = path.replace(fSep.charAt(0), '/');
|
|
139 |
if (path.length() > 0 && path.charAt(0) != '/')
|
|
140 |
path = '/' + path;
|
|
141 |
try {
|
|
142 |
return new URL("file", null, path);
|
|
143 |
}
|
|
144 |
catch (java.net.MalformedURLException e) {
|
|
145 |
/* According to the spec this could only happen if the file
|
|
146 |
protocol were not recognized. */
|
|
147 |
throw new Error("unexpected MalformedURLException");
|
|
148 |
}
|
|
151 | 149 |
} |
152 | 150 |
|
153 | 151 |
} |
154 |
/*********************************************************************** |
|
155 |
Implementation of XMLDocumentHandler interface. Only the new |
|
156 |
startElement and endElement interfaces are implemented here. All other |
|
157 |
interfaces are implemented in the class HandlerBase. |
|
158 |
**********************************************************************/ |
|
159 | 152 |
|
153 |
/**************************************************************** |
|
154 |
* Implementation of XMLDocumentHandler interface. * |
|
155 |
* Other interfaces are implemented in the class HandlerBase. * |
|
156 |
****************************************************************/ |
|
157 |
|
|
160 | 158 |
class XMLDocumentHandlerImpl extends DefaultXMLDocumentHandler |
161 | 159 |
{ |
162 | 160 |
|
163 |
boolean debug = false; |
|
164 |
boolean stackCreated = false; |
|
165 |
private Stack elementStack; |
|
161 |
private boolean debug = false; |
|
162 |
private boolean stackCreated = false; |
|
163 |
private Stack elementStack; |
|
164 |
private Connection conn = null; |
|
166 | 165 |
|
167 |
public void XMLDocumentHandlerImpl()
|
|
166 |
public XMLDocumentHandlerImpl(Connection conn)
|
|
168 | 167 |
{ |
168 |
System.out.println("\nINITIALIZING HANDLER....\n"); |
|
169 |
|
|
170 |
this.conn = conn; |
|
171 |
|
|
172 |
// Create the stack for keeping track of element context |
|
173 |
// if it doesn't already exist |
|
174 |
if (!stackCreated) { |
|
175 |
elementStack = new Stack(); |
|
176 |
stackCreated = true; |
|
177 |
} |
|
178 |
|
|
169 | 179 |
} |
170 |
|
|
171 |
|
|
180 |
|
|
172 | 181 |
public void startElement(NSName name, SAXAttrList atts) throws SAXException |
173 | 182 |
{ |
174 | 183 |
|
... | ... | |
188 | 197 |
expName = name.getExpandedName(); |
189 | 198 |
|
190 | 199 |
// Create the current element representation |
191 |
currentElement = new DBSAXElement(1, localName, false, 0); |
|
200 |
currentElement = new DBSAXElement(conn, 1, localName, false, 0);
|
|
192 | 201 |
System.out.println("Element created:" + currentElement.getTagName()); |
193 | 202 |
|
194 | 203 |
// Add all of the attributes |
195 | 204 |
for (int i=0; i<atts.getLength(); i++) |
196 | 205 |
{ |
197 | 206 |
|
198 |
// Use the methods getQualifiedName(), getLocalName(), getNamespace() |
|
199 |
// and getExpandedName() in SAXAttrList interface to get Namespace |
|
200 |
// information. |
|
207 |
// Use the methods getQualifiedName(), getLocalName(), getNamespace()
|
|
208 |
// and getExpandedName() in SAXAttrList interface to get Namespace
|
|
209 |
// information.
|
|
201 | 210 |
|
202 | 211 |
qName = atts.getQualifiedName(i); |
203 | 212 |
localName = atts.getLocalName(i); |
... | ... | |
219 | 228 |
|
220 | 229 |
} |
221 | 230 |
|
222 |
// Create the stack for keeping track of element context |
|
223 |
// if it doesn't already exist |
|
224 |
if (!stackCreated) { |
|
225 |
elementStack = new Stack(); |
|
226 |
stackCreated = true; |
|
227 |
} |
|
228 |
|
|
229 | 231 |
// Add the element to the stack, so that any text data can be |
230 | 232 |
// added as it is encountered |
231 | 233 |
elementStack.push(currentElement); |
... | ... | |
260 | 262 |
System.out.println("Closing element: " + expName); |
261 | 263 |
} |
262 | 264 |
|
263 |
public void db(int flag) { |
|
265 |
/** Debug routine */ |
|
266 |
private void db(int flag) { |
|
264 | 267 |
if (debug) { |
265 | 268 |
System.err.println("DEBUG POSITION " + flag); |
266 | 269 |
} |
DBSAXElement.java | ||
---|---|---|
23 | 23 |
private boolean isEmpty; |
24 | 24 |
private long parent_id; |
25 | 25 |
private Hashtable attributes; |
26 |
private Connection conn; |
|
26 | 27 |
|
27 |
public DBSAXElement (long element_id, String tagname, |
|
28 |
public DBSAXElement (Connection conn, long element_id, String tagname,
|
|
28 | 29 |
boolean isEmpty, long parent_id) { |
29 |
this.element_id = element_id; |
|
30 |
this.conn = conn; |
|
31 |
this.element_id = assignElementID(); |
|
32 |
|
|
30 | 33 |
this.tagname = tagname; |
31 | 34 |
this.isEmpty = isEmpty; |
32 | 35 |
this.parent_id = parent_id; |
33 | 36 |
content = new StringBuffer(); |
34 | 37 |
attributes = new Hashtable(); |
38 |
writeElementToDB(); |
|
39 |
|
|
40 |
System.out.println("******ELEMENT_ID: " + this.element_id); |
|
35 | 41 |
}; |
36 | 42 |
|
43 |
private long assignElementID() { |
|
44 |
long maxid=0; |
|
45 |
Statement stmt; |
|
46 |
try { |
|
47 |
stmt = conn.createStatement(); |
|
48 |
stmt.execute("SELECT MAX(nodeid) FROM xml_elements"); |
|
49 |
try { |
|
50 |
ResultSet rs = stmt.getResultSet(); |
|
51 |
try { |
|
52 |
boolean tableHasRows = rs.next(); |
|
53 |
if (tableHasRows) { |
|
54 |
try { |
|
55 |
maxid = rs.getInt(1); |
|
56 |
} catch (SQLException e) { |
|
57 |
System.out.println("Error with getInt: " + e.getMessage()); |
|
58 |
} |
|
59 |
} |
|
60 |
} catch (SQLException e) { |
|
61 |
System.out.println("Error with next: " + e.getMessage()); |
|
62 |
} |
|
63 |
} catch (SQLException e) { |
|
64 |
System.out.println("Error with getrset: " + e.getMessage()); |
|
65 |
} |
|
66 |
stmt.close(); |
|
67 |
} catch (SQLException e) { |
|
68 |
System.out.println("Error getting id: " + e.getMessage()); |
|
69 |
} |
|
70 |
|
|
71 |
// assign a new ID number |
|
72 |
return (maxid + 1); |
|
73 |
} |
|
74 |
|
|
75 |
private void writeElementToDB() { |
|
76 |
try { |
|
77 |
PreparedStatement pstmt; |
|
78 |
if (parent_id != 0) { |
|
79 |
pstmt = conn.prepareStatement( |
|
80 |
"INSERT INTO xml_elements(nodeid, nodename, parentnodeid) " + |
|
81 |
"VALUES (?, ?, ?)"); |
|
82 |
} else { |
|
83 |
pstmt = conn.prepareStatement( |
|
84 |
"INSERT INTO xml_elements(nodeid, nodename) " + |
|
85 |
"VALUES (?, ?)"); |
|
86 |
} |
|
87 |
|
|
88 |
// Bind the values to the query |
|
89 |
pstmt.setLong(1, element_id); // The first ? is for NODEID |
|
90 |
pstmt.setString(2, getTagName());// The second ? is for NODENAME |
|
91 |
if (parent_id != 0) { |
|
92 |
pstmt.setLong(3, parent_id); |
|
93 |
} |
|
94 |
// Do the insertion |
|
95 |
pstmt.execute(); |
|
96 |
pstmt.close(); |
|
97 |
|
|
98 |
} catch (SQLException e) { |
|
99 |
System.out.println(e.getMessage()); |
|
100 |
} |
|
101 |
} |
|
102 |
|
|
37 | 103 |
// used by JTree to display this node |
38 | 104 |
public String toString () |
39 | 105 |
{ |
DBSAXWriter.java | ||
---|---|---|
67 | 67 |
SQLException, |
68 | 68 |
ClassNotFoundException |
69 | 69 |
{ |
70 |
|
|
71 | 70 |
// Open a connection to the database |
72 | 71 |
conn = openDBConnection( |
73 | 72 |
"oracle.jdbc.driver.OracleDriver", |
74 | 73 |
dbstring, user, password); |
75 | 74 |
|
75 |
|
|
76 | 76 |
// |
77 | 77 |
// Set up the SAX document handlers for parsing |
78 | 78 |
// |
... | ... | |
81 | 81 |
|
82 | 82 |
// Use the XMLDocumentHandler interface for namespace support |
83 | 83 |
// instead of org.xml.sax.DocumentHandler |
84 |
XMLDocumentHandler xmlDocHandler = new XMLDocumentHandlerImpl(); |
|
84 |
XMLDocumentHandler xmlDocHandler = new XMLDocumentHandlerImpl(conn);
|
|
85 | 85 |
|
86 | 86 |
// For all the other interface use the default provided by |
87 | 87 |
// Handler base |
... | ... | |
119 | 119 |
|
120 | 120 |
} |
121 | 121 |
|
122 |
private Connection openDBConnection(String dbDriver, String connection,
|
|
122 |
private Connection openDBConnection(String dbDriver, String connection, |
|
123 | 123 |
String user, String password) |
124 | 124 |
throws SQLException, ClassNotFoundException { |
125 |
// Load the Oracle JDBC driver
|
|
126 |
Class.forName (dbDriver);
|
|
125 |
// Load the Oracle JDBC driver |
|
126 |
Class.forName (dbDriver); |
|
127 | 127 |
|
128 |
// Connect to the database |
|
129 |
Connection conn = |
|
130 |
DriverManager.getConnection( connection, user, password); |
|
131 |
return conn; |
|
132 |
} |
|
128 |
// Connect to the database |
|
129 |
Connection conn = DriverManager.getConnection( connection, user, password); |
|
130 |
return conn; |
|
131 |
} |
|
133 | 132 |
|
134 |
|
|
135 |
static public URL fileToURL(File file) |
|
133 |
static public URL fileToURL(File file) |
|
136 | 134 |
{ |
137 |
String path = file.getAbsolutePath(); |
|
138 |
String fSep = System.getProperty("file.separator"); |
|
139 |
if (fSep != null && fSep.length() == 1) |
|
140 |
path = path.replace(fSep.charAt(0), '/'); |
|
141 |
if (path.length() > 0 && path.charAt(0) != '/') |
|
142 |
path = '/' + path; |
|
143 |
try { |
|
144 |
return new URL("file", null, path); |
|
145 |
} |
|
146 |
catch (java.net.MalformedURLException e) { |
|
147 |
/* According to the spec this could only happen if the file |
|
148 |
protocol were not recognized. */ |
|
149 |
throw new Error("unexpected MalformedURLException"); |
|
150 |
} |
|
135 |
String path = file.getAbsolutePath();
|
|
136 |
String fSep = System.getProperty("file.separator");
|
|
137 |
if (fSep != null && fSep.length() == 1)
|
|
138 |
path = path.replace(fSep.charAt(0), '/');
|
|
139 |
if (path.length() > 0 && path.charAt(0) != '/')
|
|
140 |
path = '/' + path;
|
|
141 |
try {
|
|
142 |
return new URL("file", null, path);
|
|
143 |
}
|
|
144 |
catch (java.net.MalformedURLException e) {
|
|
145 |
/* According to the spec this could only happen if the file
|
|
146 |
protocol were not recognized. */
|
|
147 |
throw new Error("unexpected MalformedURLException");
|
|
148 |
}
|
|
151 | 149 |
} |
152 | 150 |
|
153 | 151 |
} |
154 |
/*********************************************************************** |
|
155 |
Implementation of XMLDocumentHandler interface. Only the new |
|
156 |
startElement and endElement interfaces are implemented here. All other |
|
157 |
interfaces are implemented in the class HandlerBase. |
|
158 |
**********************************************************************/ |
|
159 | 152 |
|
153 |
/**************************************************************** |
|
154 |
* Implementation of XMLDocumentHandler interface. * |
|
155 |
* Other interfaces are implemented in the class HandlerBase. * |
|
156 |
****************************************************************/ |
|
157 |
|
|
160 | 158 |
class XMLDocumentHandlerImpl extends DefaultXMLDocumentHandler |
161 | 159 |
{ |
162 | 160 |
|
163 |
boolean debug = false; |
|
164 |
boolean stackCreated = false; |
|
165 |
private Stack elementStack; |
|
161 |
private boolean debug = false; |
|
162 |
private boolean stackCreated = false; |
|
163 |
private Stack elementStack; |
|
164 |
private Connection conn = null; |
|
166 | 165 |
|
167 |
public void XMLDocumentHandlerImpl()
|
|
166 |
public XMLDocumentHandlerImpl(Connection conn)
|
|
168 | 167 |
{ |
168 |
System.out.println("\nINITIALIZING HANDLER....\n"); |
|
169 |
|
|
170 |
this.conn = conn; |
|
171 |
|
|
172 |
// Create the stack for keeping track of element context |
|
173 |
// if it doesn't already exist |
|
174 |
if (!stackCreated) { |
|
175 |
elementStack = new Stack(); |
|
176 |
stackCreated = true; |
|
177 |
} |
|
178 |
|
|
169 | 179 |
} |
170 |
|
|
171 |
|
|
180 |
|
|
172 | 181 |
public void startElement(NSName name, SAXAttrList atts) throws SAXException |
173 | 182 |
{ |
174 | 183 |
|
... | ... | |
188 | 197 |
expName = name.getExpandedName(); |
189 | 198 |
|
190 | 199 |
// Create the current element representation |
191 |
currentElement = new DBSAXElement(1, localName, false, 0); |
|
200 |
currentElement = new DBSAXElement(conn, 1, localName, false, 0);
|
|
192 | 201 |
System.out.println("Element created:" + currentElement.getTagName()); |
193 | 202 |
|
194 | 203 |
// Add all of the attributes |
195 | 204 |
for (int i=0; i<atts.getLength(); i++) |
196 | 205 |
{ |
197 | 206 |
|
198 |
// Use the methods getQualifiedName(), getLocalName(), getNamespace() |
|
199 |
// and getExpandedName() in SAXAttrList interface to get Namespace |
|
200 |
// information. |
|
207 |
// Use the methods getQualifiedName(), getLocalName(), getNamespace()
|
|
208 |
// and getExpandedName() in SAXAttrList interface to get Namespace
|
|
209 |
// information.
|
|
201 | 210 |
|
202 | 211 |
qName = atts.getQualifiedName(i); |
203 | 212 |
localName = atts.getLocalName(i); |
... | ... | |
219 | 228 |
|
220 | 229 |
} |
221 | 230 |
|
222 |
// Create the stack for keeping track of element context |
|
223 |
// if it doesn't already exist |
|
224 |
if (!stackCreated) { |
|
225 |
elementStack = new Stack(); |
|
226 |
stackCreated = true; |
|
227 |
} |
|
228 |
|
|
229 | 231 |
// Add the element to the stack, so that any text data can be |
230 | 232 |
// added as it is encountered |
231 | 233 |
elementStack.push(currentElement); |
... | ... | |
260 | 262 |
System.out.println("Closing element: " + expName); |
261 | 263 |
} |
262 | 264 |
|
263 |
public void db(int flag) { |
|
265 |
/** Debug routine */ |
|
266 |
private void db(int flag) { |
|
264 | 267 |
if (debug) { |
265 | 268 |
System.err.println("DEBUG POSITION " + flag); |
266 | 269 |
} |
Also available in: Unified diff
Added code to write element to DB using SAX parser model