Revision 15
Added by Matt Jones over 24 years ago
DBWriter.java | ||
---|---|---|
47 | 47 |
// Open a connection to the database |
48 | 48 |
conn = openDBConnection( |
49 | 49 |
"oracle.jdbc.driver.OracleDriver", |
50 |
"jdbc:oracle:thin:@penelope.nceas.ucsb.edu:1526:DEV",
|
|
50 |
"jdbc:oracle:thin:@localhost:test",
|
|
51 | 51 |
argv[2], argv[3]); |
52 | 52 |
|
53 | 53 |
// |
src/edu/ucsb/nceas/metacat/DBWriter.java | ||
---|---|---|
1 |
/** |
|
2 |
* Name: DBSAXWriter.java |
|
3 |
* Purpose: A Class that reads in an XML text document and |
|
4 |
* write its contents to a database connection using SAX |
|
5 |
* Institution: National Center for Ecological Analysis and Synthesis |
|
6 |
* Copyright: 2000 |
|
7 |
* Authors: Matt Jones |
|
8 |
* |
|
9 |
* Version: '$Id$' |
|
10 |
*/ |
|
11 |
|
|
12 |
import org.xml.sax.*; |
|
13 |
|
|
14 |
import java.io.*; |
|
15 |
import java.net.URL; |
|
16 |
import java.net.MalformedURLException; |
|
17 |
import java.sql.*; |
|
18 |
import java.util.Stack; |
|
19 |
|
|
20 |
// Extensions to the SAX Interfaces for Namespace support. |
|
21 |
import oracle.xml.parser.v2.XMLDocumentHandler; |
|
22 |
import oracle.xml.parser.v2.DefaultXMLDocumentHandler; |
|
23 |
import oracle.xml.parser.v2.NSName; |
|
24 |
import oracle.xml.parser.v2.SAXAttrList; |
|
25 |
|
|
26 |
import oracle.xml.parser.v2.SAXParser; |
|
27 |
|
|
28 |
public class DBSAXWriter { |
|
29 |
|
|
30 |
static String defaultDB = "jdbc:oracle:thin:@localhost:1521:test"; |
|
31 |
private Connection conn = null; |
|
32 |
|
|
33 |
static public void main(String[] args) { |
|
34 |
|
|
35 |
if (args.length < 3) |
|
36 |
{ |
|
37 |
System.err.println("Wrong number of arguments!!!"); |
|
38 |
System.err.println("USAGE: java DBSAXWriter " + |
|
39 |
"<filename> <user> <password> [dbstring]"); |
|
40 |
return; |
|
41 |
} else { |
|
42 |
try { |
|
43 |
|
|
44 |
String filename = args[0]; |
|
45 |
String user = args[1]; |
|
46 |
String password = args[2]; |
|
47 |
String dbstring = null; |
|
48 |
|
|
49 |
if (args.length <= 3) { |
|
50 |
dbstring = defaultDB; |
|
51 |
} else { |
|
52 |
dbstring = args[3]; |
|
53 |
} |
|
54 |
|
|
55 |
new DBSAXWriter(filename, user, password, dbstring); |
|
56 |
} catch (Exception e) { |
|
57 |
System.err.println("EXCEPTION HANDLING REQUIRED"); |
|
58 |
System.err.println(e.getMessage()); |
|
59 |
e.printStackTrace(System.err); |
|
60 |
} |
|
61 |
} |
|
62 |
} |
|
63 |
|
|
64 |
private DBSAXWriter( String filename, String user, |
|
65 |
String password, String dbstring) |
|
66 |
throws IOException, |
|
67 |
SQLException, |
|
68 |
ClassNotFoundException |
|
69 |
{ |
|
70 |
|
|
71 |
// Open a connection to the database |
|
72 |
conn = openDBConnection( |
|
73 |
"oracle.jdbc.driver.OracleDriver", |
|
74 |
dbstring, user, password); |
|
75 |
|
|
76 |
// |
|
77 |
// Set up the SAX document handlers for parsing |
|
78 |
// |
|
79 |
|
|
80 |
try { |
|
81 |
|
|
82 |
// Use the XMLDocumentHandler interface for namespace support |
|
83 |
// instead of org.xml.sax.DocumentHandler |
|
84 |
XMLDocumentHandler xmlDocHandler = new XMLDocumentHandlerImpl(); |
|
85 |
|
|
86 |
// For all the other interface use the default provided by |
|
87 |
// Handler base |
|
88 |
HandlerBase defHandler = new HandlerBase(); |
|
89 |
|
|
90 |
// Get an instance of the parser |
|
91 |
SAXParser parser = new SAXParser(); |
|
92 |
|
|
93 |
// Set Handlers in the parser |
|
94 |
// Set the DocumentHandler to XMLDocumentHandler |
|
95 |
parser.setDocumentHandler(xmlDocHandler); |
|
96 |
|
|
97 |
// Set the other Handler to the defHandler |
|
98 |
parser.setErrorHandler(defHandler); |
|
99 |
parser.setEntityResolver(defHandler); |
|
100 |
parser.setDTDHandler(defHandler); |
|
101 |
|
|
102 |
try |
|
103 |
{ |
|
104 |
parser.parse(fileToURL(new File(filename)).toString()); |
|
105 |
} |
|
106 |
catch (SAXParseException e) |
|
107 |
{ |
|
108 |
System.err.println(filename + ": " + e.getMessage()); |
|
109 |
} |
|
110 |
catch (SAXException e) |
|
111 |
{ |
|
112 |
System.err.println(filename + ": " + e.getMessage()); |
|
113 |
} |
|
114 |
} |
|
115 |
catch (Exception e) |
|
116 |
{ |
|
117 |
System.err.println(e.toString()); |
|
118 |
} |
|
119 |
|
|
120 |
} |
|
121 |
|
|
122 |
private Connection openDBConnection(String dbDriver, String connection, |
|
123 |
String user, String password) |
|
124 |
throws SQLException, ClassNotFoundException { |
|
125 |
// Load the Oracle JDBC driver |
|
126 |
Class.forName (dbDriver); |
|
127 |
|
|
128 |
// Connect to the database |
|
129 |
Connection conn = |
|
130 |
DriverManager.getConnection( connection, user, password); |
|
131 |
return conn; |
|
132 |
} |
|
133 |
|
|
134 |
|
|
135 |
static public URL fileToURL(File file) |
|
136 |
{ |
|
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 |
} |
|
151 |
} |
|
152 |
|
|
153 |
} |
|
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 |
|
|
160 |
class XMLDocumentHandlerImpl extends DefaultXMLDocumentHandler |
|
161 |
{ |
|
162 |
|
|
163 |
boolean debug = false; |
|
164 |
boolean stackCreated = false; |
|
165 |
private Stack elementStack; |
|
166 |
|
|
167 |
public void XMLDocumentHandlerImpl() |
|
168 |
{ |
|
169 |
} |
|
170 |
|
|
171 |
|
|
172 |
public void startElement(NSName name, SAXAttrList atts) throws SAXException |
|
173 |
{ |
|
174 |
|
|
175 |
// Use the methods getQualifiedName(), getLocalName(), getNamespace() |
|
176 |
// and getExpandedName() in NSName interface to get Namespace |
|
177 |
// information. |
|
178 |
|
|
179 |
String qName; |
|
180 |
String localName; |
|
181 |
String nsName; |
|
182 |
String expName; |
|
183 |
DBSAXElement currentElement; |
|
184 |
|
|
185 |
qName = name.getQualifiedName(); |
|
186 |
localName = name.getLocalName(); |
|
187 |
nsName = name.getNamespace(); |
|
188 |
expName = name.getExpandedName(); |
|
189 |
|
|
190 |
// Create the current element representation |
|
191 |
currentElement = new DBSAXElement(1, localName, false, 0); |
|
192 |
System.out.println("Element created:" + currentElement.getTagName()); |
|
193 |
|
|
194 |
// Add all of the attributes |
|
195 |
for (int i=0; i<atts.getLength(); i++) |
|
196 |
{ |
|
197 |
|
|
198 |
// Use the methods getQualifiedName(), getLocalName(), getNamespace() |
|
199 |
// and getExpandedName() in SAXAttrList interface to get Namespace |
|
200 |
// information. |
|
201 |
|
|
202 |
qName = atts.getQualifiedName(i); |
|
203 |
localName = atts.getLocalName(i); |
|
204 |
nsName = atts.getNamespace(i); |
|
205 |
expName = atts.getExpandedName(i); |
|
206 |
|
|
207 |
// You can get the type and value of the attributes either |
|
208 |
// by index or by the Qualified Name. |
|
209 |
|
|
210 |
String type = atts.getType(qName); |
|
211 |
String value = atts.getValue(qName); |
|
212 |
|
|
213 |
currentElement.setAttribute(localName, value); |
|
214 |
|
|
215 |
System.out.println(" Added ATTRIBUTE Name:" + localName); |
|
216 |
System.out.println(" Type:" + type); |
|
217 |
System.out.println(" Value:" + value); |
|
218 |
System.out.println(); |
|
219 |
|
|
220 |
} |
|
221 |
|
|
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 |
// Add the element to the stack, so that any text data can be |
|
230 |
// added as it is encountered |
|
231 |
elementStack.push(currentElement); |
|
232 |
|
|
233 |
} |
|
234 |
|
|
235 |
public void characters(char[] cbuf, int start, int len) |
|
236 |
{ |
|
237 |
DBSAXElement currentElement = (DBSAXElement)elementStack.peek(); |
|
238 |
currentElement.appendContent(cbuf,start,len); |
|
239 |
System.out.println(" Characters: " + new String(cbuf,start,len)); |
|
240 |
System.out.println(" Appended: " + currentElement.getContent()); |
|
241 |
} |
|
242 |
|
|
243 |
public void ignorableWhitespace(char[] cbuf, int start, int len) |
|
244 |
{ |
|
245 |
System.out.println("IgnorableWhiteSpace"); |
|
246 |
} |
|
247 |
|
|
248 |
public void endElement(NSName name) throws SAXException |
|
249 |
{ |
|
250 |
// Use the methods getQualifiedName(), getLocalName(), getNamespace() |
|
251 |
// and getExpandedName() in NSName interface to get Namespace |
|
252 |
// information. |
|
253 |
|
|
254 |
String expName = name.getExpandedName(); |
|
255 |
|
|
256 |
// Remove the element from the stack |
|
257 |
DBSAXElement currentElement = (DBSAXElement)elementStack.pop(); |
|
258 |
|
|
259 |
System.out.println("Final content: " + currentElement.getContent()); |
|
260 |
System.out.println("Closing element: " + expName); |
|
261 |
} |
|
262 |
|
|
263 |
public void db(int flag) { |
|
264 |
if (debug) { |
|
265 |
System.err.println("DEBUG POSITION " + flag); |
|
266 |
} |
|
267 |
} |
|
268 |
} |
|
0 | 269 |
src/edu/ucsb/nceas/metacat/DBSAXNode.java | ||
---|---|---|
1 |
/** |
|
2 |
* Name: DBSAXElement.java |
|
3 |
* Purpose: A Class that represents an XML element and its contents |
|
4 |
* Institution: National Center for Ecological Analysis and Synthesis |
|
5 |
* Copyright: 2000 |
|
6 |
* Authors: Matt Jones |
|
7 |
* |
|
8 |
* Version: '$Id$' |
|
9 |
*/ |
|
10 |
|
|
11 |
//package project; |
|
12 |
|
|
13 |
import java.sql.*; |
|
14 |
import java.io.IOException; |
|
15 |
import java.util.Hashtable; |
|
16 |
import java.util.Enumeration; |
|
17 |
|
|
18 |
public class DBSAXElement { |
|
19 |
|
|
20 |
private long element_id; |
|
21 |
private String tagname; |
|
22 |
private StringBuffer content; |
|
23 |
private boolean isEmpty; |
|
24 |
private long parent_id; |
|
25 |
private Hashtable attributes; |
|
26 |
|
|
27 |
public DBSAXElement (long element_id, String tagname, |
|
28 |
boolean isEmpty, long parent_id) { |
|
29 |
this.element_id = element_id; |
|
30 |
this.tagname = tagname; |
|
31 |
this.isEmpty = isEmpty; |
|
32 |
this.parent_id = parent_id; |
|
33 |
content = new StringBuffer(); |
|
34 |
attributes = new Hashtable(); |
|
35 |
}; |
|
36 |
|
|
37 |
// used by JTree to display this node |
|
38 |
public String toString () |
|
39 |
{ |
|
40 |
StringBuffer value = new StringBuffer (); |
|
41 |
value.append ('<'); |
|
42 |
value.append (getTagName ()); |
|
43 |
value.append (getAttributes ().toString ()); |
|
44 |
value.append ('>'); |
|
45 |
return value.toString (); |
|
46 |
} |
|
47 |
|
|
48 |
/** Get the name of this element */ |
|
49 |
public String getTagName() { return tagname; } |
|
50 |
|
|
51 |
/** Get the attributes as a string */ |
|
52 |
public String getAttributes() { |
|
53 |
StringBuffer buf = new StringBuffer(); |
|
54 |
String attName = null; |
|
55 |
String attValue = null; |
|
56 |
|
|
57 |
Enumeration attList = attributes.keys(); |
|
58 |
while (attList.hasMoreElements()) { |
|
59 |
attName = (String)attList.nextElement(); |
|
60 |
attValue = (String)attributes.get(attName); |
|
61 |
buf.append(" ").append(attName).append("=").append(attValue); |
|
62 |
} |
|
63 |
return buf.toString(); |
|
64 |
} |
|
65 |
|
|
66 |
/** Add a new attribute to this element, or set its value */ |
|
67 |
public void setAttribute(String attName, String attValue) { |
|
68 |
if (attName != null) { |
|
69 |
attributes.put(attName, attValue); |
|
70 |
} else { |
|
71 |
System.err.println("Attribute name must not be null!"); |
|
72 |
} |
|
73 |
} |
|
74 |
|
|
75 |
/** Get an attribute value by name */ |
|
76 |
public String getAttribute(String attName) { |
|
77 |
return (String)attributes.get(attName); |
|
78 |
} |
|
79 |
|
|
80 |
/** Append to the content of the element */ |
|
81 |
public void appendContent(char[] cbuf, int start, int len) { |
|
82 |
this.content.append( cbuf, start, len ); |
|
83 |
} |
|
84 |
|
|
85 |
/** Append to the content of the element */ |
|
86 |
public void appendContent(String new_content) { |
|
87 |
this.content.append( new_content ); |
|
88 |
} |
|
89 |
|
|
90 |
/** Get the content of the element */ |
|
91 |
public String getContent() { |
|
92 |
return this.content.toString(); |
|
93 |
} |
|
94 |
} |
|
0 | 95 |
src/edu/ucsb/nceas/metacat/DBSAXElement.java | ||
---|---|---|
1 |
/** |
|
2 |
* Name: DBSAXElement.java |
|
3 |
* Purpose: A Class that represents an XML element and its contents |
|
4 |
* Institution: National Center for Ecological Analysis and Synthesis |
|
5 |
* Copyright: 2000 |
|
6 |
* Authors: Matt Jones |
|
7 |
* |
|
8 |
* Version: '$Id$' |
|
9 |
*/ |
|
10 |
|
|
11 |
//package project; |
|
12 |
|
|
13 |
import java.sql.*; |
|
14 |
import java.io.IOException; |
|
15 |
import java.util.Hashtable; |
|
16 |
import java.util.Enumeration; |
|
17 |
|
|
18 |
public class DBSAXElement { |
|
19 |
|
|
20 |
private long element_id; |
|
21 |
private String tagname; |
|
22 |
private StringBuffer content; |
|
23 |
private boolean isEmpty; |
|
24 |
private long parent_id; |
|
25 |
private Hashtable attributes; |
|
26 |
|
|
27 |
public DBSAXElement (long element_id, String tagname, |
|
28 |
boolean isEmpty, long parent_id) { |
|
29 |
this.element_id = element_id; |
|
30 |
this.tagname = tagname; |
|
31 |
this.isEmpty = isEmpty; |
|
32 |
this.parent_id = parent_id; |
|
33 |
content = new StringBuffer(); |
|
34 |
attributes = new Hashtable(); |
|
35 |
}; |
|
36 |
|
|
37 |
// used by JTree to display this node |
|
38 |
public String toString () |
|
39 |
{ |
|
40 |
StringBuffer value = new StringBuffer (); |
|
41 |
value.append ('<'); |
|
42 |
value.append (getTagName ()); |
|
43 |
value.append (getAttributes ().toString ()); |
|
44 |
value.append ('>'); |
|
45 |
return value.toString (); |
|
46 |
} |
|
47 |
|
|
48 |
/** Get the name of this element */ |
|
49 |
public String getTagName() { return tagname; } |
|
50 |
|
|
51 |
/** Get the attributes as a string */ |
|
52 |
public String getAttributes() { |
|
53 |
StringBuffer buf = new StringBuffer(); |
|
54 |
String attName = null; |
|
55 |
String attValue = null; |
|
56 |
|
|
57 |
Enumeration attList = attributes.keys(); |
|
58 |
while (attList.hasMoreElements()) { |
|
59 |
attName = (String)attList.nextElement(); |
|
60 |
attValue = (String)attributes.get(attName); |
|
61 |
buf.append(" ").append(attName).append("=").append(attValue); |
|
62 |
} |
|
63 |
return buf.toString(); |
|
64 |
} |
|
65 |
|
|
66 |
/** Add a new attribute to this element, or set its value */ |
|
67 |
public void setAttribute(String attName, String attValue) { |
|
68 |
if (attName != null) { |
|
69 |
attributes.put(attName, attValue); |
|
70 |
} else { |
|
71 |
System.err.println("Attribute name must not be null!"); |
|
72 |
} |
|
73 |
} |
|
74 |
|
|
75 |
/** Get an attribute value by name */ |
|
76 |
public String getAttribute(String attName) { |
|
77 |
return (String)attributes.get(attName); |
|
78 |
} |
|
79 |
|
|
80 |
/** Append to the content of the element */ |
|
81 |
public void appendContent(char[] cbuf, int start, int len) { |
|
82 |
this.content.append( cbuf, start, len ); |
|
83 |
} |
|
84 |
|
|
85 |
/** Append to the content of the element */ |
|
86 |
public void appendContent(String new_content) { |
|
87 |
this.content.append( new_content ); |
|
88 |
} |
|
89 |
|
|
90 |
/** Get the content of the element */ |
|
91 |
public String getContent() { |
|
92 |
return this.content.toString(); |
|
93 |
} |
|
94 |
} |
|
0 | 95 |
src/edu/ucsb/nceas/metacat/DBSAXWriter.java | ||
---|---|---|
1 |
/** |
|
2 |
* Name: DBSAXWriter.java |
|
3 |
* Purpose: A Class that reads in an XML text document and |
|
4 |
* write its contents to a database connection using SAX |
|
5 |
* Institution: National Center for Ecological Analysis and Synthesis |
|
6 |
* Copyright: 2000 |
|
7 |
* Authors: Matt Jones |
|
8 |
* |
|
9 |
* Version: '$Id$' |
|
10 |
*/ |
|
11 |
|
|
12 |
import org.xml.sax.*; |
|
13 |
|
|
14 |
import java.io.*; |
|
15 |
import java.net.URL; |
|
16 |
import java.net.MalformedURLException; |
|
17 |
import java.sql.*; |
|
18 |
import java.util.Stack; |
|
19 |
|
|
20 |
// Extensions to the SAX Interfaces for Namespace support. |
|
21 |
import oracle.xml.parser.v2.XMLDocumentHandler; |
|
22 |
import oracle.xml.parser.v2.DefaultXMLDocumentHandler; |
|
23 |
import oracle.xml.parser.v2.NSName; |
|
24 |
import oracle.xml.parser.v2.SAXAttrList; |
|
25 |
|
|
26 |
import oracle.xml.parser.v2.SAXParser; |
|
27 |
|
|
28 |
public class DBSAXWriter { |
|
29 |
|
|
30 |
static String defaultDB = "jdbc:oracle:thin:@localhost:1521:test"; |
|
31 |
private Connection conn = null; |
|
32 |
|
|
33 |
static public void main(String[] args) { |
|
34 |
|
|
35 |
if (args.length < 3) |
|
36 |
{ |
|
37 |
System.err.println("Wrong number of arguments!!!"); |
|
38 |
System.err.println("USAGE: java DBSAXWriter " + |
|
39 |
"<filename> <user> <password> [dbstring]"); |
|
40 |
return; |
|
41 |
} else { |
|
42 |
try { |
|
43 |
|
|
44 |
String filename = args[0]; |
|
45 |
String user = args[1]; |
|
46 |
String password = args[2]; |
|
47 |
String dbstring = null; |
|
48 |
|
|
49 |
if (args.length <= 3) { |
|
50 |
dbstring = defaultDB; |
|
51 |
} else { |
|
52 |
dbstring = args[3]; |
|
53 |
} |
|
54 |
|
|
55 |
new DBSAXWriter(filename, user, password, dbstring); |
|
56 |
} catch (Exception e) { |
|
57 |
System.err.println("EXCEPTION HANDLING REQUIRED"); |
|
58 |
System.err.println(e.getMessage()); |
|
59 |
e.printStackTrace(System.err); |
|
60 |
} |
|
61 |
} |
|
62 |
} |
|
63 |
|
|
64 |
private DBSAXWriter( String filename, String user, |
|
65 |
String password, String dbstring) |
|
66 |
throws IOException, |
|
67 |
SQLException, |
|
68 |
ClassNotFoundException |
|
69 |
{ |
|
70 |
|
|
71 |
// Open a connection to the database |
|
72 |
conn = openDBConnection( |
|
73 |
"oracle.jdbc.driver.OracleDriver", |
|
74 |
dbstring, user, password); |
|
75 |
|
|
76 |
// |
|
77 |
// Set up the SAX document handlers for parsing |
|
78 |
// |
|
79 |
|
|
80 |
try { |
|
81 |
|
|
82 |
// Use the XMLDocumentHandler interface for namespace support |
|
83 |
// instead of org.xml.sax.DocumentHandler |
|
84 |
XMLDocumentHandler xmlDocHandler = new XMLDocumentHandlerImpl(); |
|
85 |
|
|
86 |
// For all the other interface use the default provided by |
|
87 |
// Handler base |
|
88 |
HandlerBase defHandler = new HandlerBase(); |
|
89 |
|
|
90 |
// Get an instance of the parser |
|
91 |
SAXParser parser = new SAXParser(); |
|
92 |
|
|
93 |
// Set Handlers in the parser |
|
94 |
// Set the DocumentHandler to XMLDocumentHandler |
|
95 |
parser.setDocumentHandler(xmlDocHandler); |
|
96 |
|
|
97 |
// Set the other Handler to the defHandler |
|
98 |
parser.setErrorHandler(defHandler); |
|
99 |
parser.setEntityResolver(defHandler); |
|
100 |
parser.setDTDHandler(defHandler); |
|
101 |
|
|
102 |
try |
|
103 |
{ |
|
104 |
parser.parse(fileToURL(new File(filename)).toString()); |
|
105 |
} |
|
106 |
catch (SAXParseException e) |
|
107 |
{ |
|
108 |
System.err.println(filename + ": " + e.getMessage()); |
|
109 |
} |
|
110 |
catch (SAXException e) |
|
111 |
{ |
|
112 |
System.err.println(filename + ": " + e.getMessage()); |
|
113 |
} |
|
114 |
} |
|
115 |
catch (Exception e) |
|
116 |
{ |
|
117 |
System.err.println(e.toString()); |
|
118 |
} |
|
119 |
|
|
120 |
} |
|
121 |
|
|
122 |
private Connection openDBConnection(String dbDriver, String connection, |
|
123 |
String user, String password) |
|
124 |
throws SQLException, ClassNotFoundException { |
|
125 |
// Load the Oracle JDBC driver |
|
126 |
Class.forName (dbDriver); |
|
127 |
|
|
128 |
// Connect to the database |
|
129 |
Connection conn = |
|
130 |
DriverManager.getConnection( connection, user, password); |
|
131 |
return conn; |
|
132 |
} |
|
133 |
|
|
134 |
|
|
135 |
static public URL fileToURL(File file) |
|
136 |
{ |
|
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 |
} |
|
151 |
} |
|
152 |
|
|
153 |
} |
|
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 |
|
|
160 |
class XMLDocumentHandlerImpl extends DefaultXMLDocumentHandler |
|
161 |
{ |
|
162 |
|
|
163 |
boolean debug = false; |
|
164 |
boolean stackCreated = false; |
|
165 |
private Stack elementStack; |
|
166 |
|
|
167 |
public void XMLDocumentHandlerImpl() |
|
168 |
{ |
|
169 |
} |
|
170 |
|
|
171 |
|
|
172 |
public void startElement(NSName name, SAXAttrList atts) throws SAXException |
|
173 |
{ |
|
174 |
|
|
175 |
// Use the methods getQualifiedName(), getLocalName(), getNamespace() |
|
176 |
// and getExpandedName() in NSName interface to get Namespace |
|
177 |
// information. |
|
178 |
|
|
179 |
String qName; |
|
180 |
String localName; |
|
181 |
String nsName; |
|
182 |
String expName; |
|
183 |
DBSAXElement currentElement; |
|
184 |
|
|
185 |
qName = name.getQualifiedName(); |
|
186 |
localName = name.getLocalName(); |
|
187 |
nsName = name.getNamespace(); |
|
188 |
expName = name.getExpandedName(); |
|
189 |
|
|
190 |
// Create the current element representation |
|
191 |
currentElement = new DBSAXElement(1, localName, false, 0); |
|
192 |
System.out.println("Element created:" + currentElement.getTagName()); |
|
193 |
|
|
194 |
// Add all of the attributes |
|
195 |
for (int i=0; i<atts.getLength(); i++) |
|
196 |
{ |
|
197 |
|
|
198 |
// Use the methods getQualifiedName(), getLocalName(), getNamespace() |
|
199 |
// and getExpandedName() in SAXAttrList interface to get Namespace |
|
200 |
// information. |
|
201 |
|
|
202 |
qName = atts.getQualifiedName(i); |
|
203 |
localName = atts.getLocalName(i); |
|
204 |
nsName = atts.getNamespace(i); |
|
205 |
expName = atts.getExpandedName(i); |
|
206 |
|
|
207 |
// You can get the type and value of the attributes either |
|
208 |
// by index or by the Qualified Name. |
|
209 |
|
|
210 |
String type = atts.getType(qName); |
|
211 |
String value = atts.getValue(qName); |
|
212 |
|
|
213 |
currentElement.setAttribute(localName, value); |
|
214 |
|
|
215 |
System.out.println(" Added ATTRIBUTE Name:" + localName); |
|
216 |
System.out.println(" Type:" + type); |
|
217 |
System.out.println(" Value:" + value); |
|
218 |
System.out.println(); |
|
219 |
|
|
220 |
} |
|
221 |
|
|
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 |
// Add the element to the stack, so that any text data can be |
|
230 |
// added as it is encountered |
|
231 |
elementStack.push(currentElement); |
|
232 |
|
|
233 |
} |
|
234 |
|
|
235 |
public void characters(char[] cbuf, int start, int len) |
|
236 |
{ |
|
237 |
DBSAXElement currentElement = (DBSAXElement)elementStack.peek(); |
|
238 |
currentElement.appendContent(cbuf,start,len); |
|
239 |
System.out.println(" Characters: " + new String(cbuf,start,len)); |
|
240 |
System.out.println(" Appended: " + currentElement.getContent()); |
|
241 |
} |
|
242 |
|
|
243 |
public void ignorableWhitespace(char[] cbuf, int start, int len) |
|
244 |
{ |
|
245 |
System.out.println("IgnorableWhiteSpace"); |
|
246 |
} |
|
247 |
|
|
248 |
public void endElement(NSName name) throws SAXException |
|
249 |
{ |
|
250 |
// Use the methods getQualifiedName(), getLocalName(), getNamespace() |
|
251 |
// and getExpandedName() in NSName interface to get Namespace |
|
252 |
// information. |
|
253 |
|
|
254 |
String expName = name.getExpandedName(); |
|
255 |
|
|
256 |
// Remove the element from the stack |
|
257 |
DBSAXElement currentElement = (DBSAXElement)elementStack.pop(); |
|
258 |
|
|
259 |
System.out.println("Final content: " + currentElement.getContent()); |
|
260 |
System.out.println("Closing element: " + expName); |
|
261 |
} |
|
262 |
|
|
263 |
public void db(int flag) { |
|
264 |
if (debug) { |
|
265 |
System.err.println("DEBUG POSITION " + flag); |
|
266 |
} |
|
267 |
} |
|
268 |
} |
|
0 | 269 |
DBSAXElement.java | ||
---|---|---|
1 |
/** |
|
2 |
* Name: DBSAXElement.java |
|
3 |
* Purpose: A Class that represents an XML element and its contents |
|
4 |
* Institution: National Center for Ecological Analysis and Synthesis |
|
5 |
* Copyright: 2000 |
|
6 |
* Authors: Matt Jones |
|
7 |
* |
|
8 |
* Version: '$Id$' |
|
9 |
*/ |
|
10 |
|
|
11 |
//package project; |
|
12 |
|
|
13 |
import java.sql.*; |
|
14 |
import java.io.IOException; |
|
15 |
import java.util.Hashtable; |
|
16 |
import java.util.Enumeration; |
|
17 |
|
|
18 |
public class DBSAXElement { |
|
19 |
|
|
20 |
private long element_id; |
|
21 |
private String tagname; |
|
22 |
private StringBuffer content; |
|
23 |
private boolean isEmpty; |
|
24 |
private long parent_id; |
|
25 |
private Hashtable attributes; |
|
26 |
|
|
27 |
public DBSAXElement (long element_id, String tagname, |
|
28 |
boolean isEmpty, long parent_id) { |
|
29 |
this.element_id = element_id; |
|
30 |
this.tagname = tagname; |
|
31 |
this.isEmpty = isEmpty; |
|
32 |
this.parent_id = parent_id; |
|
33 |
content = new StringBuffer(); |
|
34 |
attributes = new Hashtable(); |
|
35 |
}; |
|
36 |
|
|
37 |
// used by JTree to display this node |
|
38 |
public String toString () |
|
39 |
{ |
|
40 |
StringBuffer value = new StringBuffer (); |
|
41 |
value.append ('<'); |
|
42 |
value.append (getTagName ()); |
|
43 |
value.append (getAttributes ().toString ()); |
|
44 |
value.append ('>'); |
|
45 |
return value.toString (); |
|
46 |
} |
|
47 |
|
|
48 |
/** Get the name of this element */ |
|
49 |
public String getTagName() { return tagname; } |
|
50 |
|
|
51 |
/** Get the attributes as a string */ |
|
52 |
public String getAttributes() { |
|
53 |
StringBuffer buf = new StringBuffer(); |
|
54 |
String attName = null; |
|
55 |
String attValue = null; |
|
56 |
|
|
57 |
Enumeration attList = attributes.keys(); |
|
58 |
while (attList.hasMoreElements()) { |
|
59 |
attName = (String)attList.nextElement(); |
|
60 |
attValue = (String)attributes.get(attName); |
|
61 |
buf.append(" ").append(attName).append("=").append(attValue); |
|
62 |
} |
|
63 |
return buf.toString(); |
|
64 |
} |
|
65 |
|
|
66 |
/** Add a new attribute to this element, or set its value */ |
|
67 |
public void setAttribute(String attName, String attValue) { |
|
68 |
if (attName != null) { |
|
69 |
attributes.put(attName, attValue); |
|
70 |
} else { |
|
71 |
System.err.println("Attribute name must not be null!"); |
|
72 |
} |
|
73 |
} |
|
74 |
|
|
75 |
/** Get an attribute value by name */ |
|
76 |
public String getAttribute(String attName) { |
|
77 |
return (String)attributes.get(attName); |
|
78 |
} |
|
79 |
|
|
80 |
/** Append to the content of the element */ |
|
81 |
public void appendContent(char[] cbuf, int start, int len) { |
|
82 |
this.content.append( cbuf, start, len ); |
|
83 |
} |
|
84 |
|
|
85 |
/** Append to the content of the element */ |
|
86 |
public void appendContent(String new_content) { |
|
87 |
this.content.append( new_content ); |
|
88 |
} |
|
89 |
|
|
90 |
/** Get the content of the element */ |
|
91 |
public String getContent() { |
|
92 |
return this.content.toString(); |
|
93 |
} |
|
94 |
} |
|
0 | 95 |
Makefile | ||
---|---|---|
1 | 1 |
# For Win32, make 'SEP' be a semicolon |
2 | 2 |
SEP=: |
3 | 3 |
#CPATH=../../xml.jar$(SEP)$(JAVA_HOME)/lib/classes.zip |
4 |
CPATH=. |
|
4 |
CPATH=/home/httpd/servlets/xsql/lib/xmlparserv2.jar$(SEP)$(ORACLE_HOME)/jdbc/lib/classes111.zip$(SEP).
|
|
5 | 5 |
|
6 |
default: |
|
6 |
default: orasax |
|
7 |
|
|
8 |
orasax: |
|
7 | 9 |
javac -classpath "$(CPATH)" \ |
10 |
DBSAXWriter.java \ |
|
11 |
DBSAXElement.java |
|
12 |
|
|
13 |
sundom: |
|
14 |
javac -classpath "$(CPATH)" \ |
|
8 | 15 |
DBWriter.java \ |
9 | 16 |
DBElement.java |
10 | 17 |
|
11 |
#java -classpath "$(CPATH)" DBWriter DBElement.props file.xml username password |
|
18 |
test: |
|
19 |
java -cp $(CPATH) DBSAXWriter test.xml jones kinkaj0u |
|
12 | 20 |
|
21 |
test1: |
|
22 |
java -cp $(CPATH) DBSAXWriter test.xml jones kinkaj0u \ |
|
23 |
jdbc:oracle:thin:@24.237.22.245:1521:test |
|
24 |
|
|
25 |
test2: |
|
26 |
java -cp $(CPATH) DBSAXWriter test.xml jones kinkaj0u \ |
|
27 |
jdbc:oracle:thin:@localhost:1521:test |
|
28 |
|
|
13 | 29 |
clean: |
14 | 30 |
-rm -f *.class Log |
DBSAXWriter.java | ||
---|---|---|
1 |
/** |
|
2 |
* Name: DBSAXWriter.java |
|
3 |
* Purpose: A Class that reads in an XML text document and |
|
4 |
* write its contents to a database connection using SAX |
|
5 |
* Institution: National Center for Ecological Analysis and Synthesis |
|
6 |
* Copyright: 2000 |
|
7 |
* Authors: Matt Jones |
|
8 |
* |
|
9 |
* Version: '$Id$' |
|
10 |
*/ |
|
11 |
|
|
12 |
import org.xml.sax.*; |
|
13 |
|
|
14 |
import java.io.*; |
|
15 |
import java.net.URL; |
|
16 |
import java.net.MalformedURLException; |
|
17 |
import java.sql.*; |
|
18 |
import java.util.Stack; |
|
19 |
|
|
20 |
// Extensions to the SAX Interfaces for Namespace support. |
|
21 |
import oracle.xml.parser.v2.XMLDocumentHandler; |
|
22 |
import oracle.xml.parser.v2.DefaultXMLDocumentHandler; |
|
23 |
import oracle.xml.parser.v2.NSName; |
|
24 |
import oracle.xml.parser.v2.SAXAttrList; |
|
25 |
|
|
26 |
import oracle.xml.parser.v2.SAXParser; |
|
27 |
|
|
28 |
public class DBSAXWriter { |
|
29 |
|
|
30 |
static String defaultDB = "jdbc:oracle:thin:@localhost:1521:test"; |
|
31 |
private Connection conn = null; |
|
32 |
|
|
33 |
static public void main(String[] args) { |
|
34 |
|
|
35 |
if (args.length < 3) |
|
36 |
{ |
|
37 |
System.err.println("Wrong number of arguments!!!"); |
|
38 |
System.err.println("USAGE: java DBSAXWriter " + |
|
39 |
"<filename> <user> <password> [dbstring]"); |
|
40 |
return; |
|
41 |
} else { |
|
42 |
try { |
|
43 |
|
|
44 |
String filename = args[0]; |
|
45 |
String user = args[1]; |
|
46 |
String password = args[2]; |
|
47 |
String dbstring = null; |
|
48 |
|
|
49 |
if (args.length <= 3) { |
|
50 |
dbstring = defaultDB; |
|
51 |
} else { |
|
52 |
dbstring = args[3]; |
|
53 |
} |
|
54 |
|
|
55 |
new DBSAXWriter(filename, user, password, dbstring); |
|
56 |
} catch (Exception e) { |
|
57 |
System.err.println("EXCEPTION HANDLING REQUIRED"); |
|
58 |
System.err.println(e.getMessage()); |
|
59 |
e.printStackTrace(System.err); |
|
60 |
} |
|
61 |
} |
|
62 |
} |
|
63 |
|
|
64 |
private DBSAXWriter( String filename, String user, |
|
65 |
String password, String dbstring) |
|
66 |
throws IOException, |
|
67 |
SQLException, |
|
68 |
ClassNotFoundException |
|
69 |
{ |
|
70 |
|
|
71 |
// Open a connection to the database |
|
72 |
conn = openDBConnection( |
|
73 |
"oracle.jdbc.driver.OracleDriver", |
|
74 |
dbstring, user, password); |
|
75 |
|
|
76 |
// |
|
77 |
// Set up the SAX document handlers for parsing |
|
78 |
// |
|
79 |
|
|
80 |
try { |
|
81 |
|
|
82 |
// Use the XMLDocumentHandler interface for namespace support |
|
83 |
// instead of org.xml.sax.DocumentHandler |
|
84 |
XMLDocumentHandler xmlDocHandler = new XMLDocumentHandlerImpl(); |
|
85 |
|
|
86 |
// For all the other interface use the default provided by |
|
87 |
// Handler base |
|
88 |
HandlerBase defHandler = new HandlerBase(); |
|
89 |
|
|
90 |
// Get an instance of the parser |
|
91 |
SAXParser parser = new SAXParser(); |
|
92 |
|
|
93 |
// Set Handlers in the parser |
|
94 |
// Set the DocumentHandler to XMLDocumentHandler |
|
95 |
parser.setDocumentHandler(xmlDocHandler); |
|
96 |
|
|
97 |
// Set the other Handler to the defHandler |
|
98 |
parser.setErrorHandler(defHandler); |
|
99 |
parser.setEntityResolver(defHandler); |
|
100 |
parser.setDTDHandler(defHandler); |
|
101 |
|
|
102 |
try |
|
103 |
{ |
|
104 |
parser.parse(fileToURL(new File(filename)).toString()); |
|
105 |
} |
|
106 |
catch (SAXParseException e) |
|
107 |
{ |
|
108 |
System.err.println(filename + ": " + e.getMessage()); |
|
109 |
} |
|
110 |
catch (SAXException e) |
|
111 |
{ |
|
112 |
System.err.println(filename + ": " + e.getMessage()); |
|
113 |
} |
|
114 |
} |
|
115 |
catch (Exception e) |
|
116 |
{ |
|
117 |
System.err.println(e.toString()); |
|
118 |
} |
|
119 |
|
|
120 |
} |
|
121 |
|
|
122 |
private Connection openDBConnection(String dbDriver, String connection, |
|
123 |
String user, String password) |
|
124 |
throws SQLException, ClassNotFoundException { |
|
125 |
// Load the Oracle JDBC driver |
|
126 |
Class.forName (dbDriver); |
|
127 |
|
|
128 |
// Connect to the database |
|
129 |
Connection conn = |
|
130 |
DriverManager.getConnection( connection, user, password); |
|
131 |
return conn; |
|
132 |
} |
|
133 |
|
|
134 |
|
|
135 |
static public URL fileToURL(File file) |
|
136 |
{ |
|
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 |
} |
|
151 |
} |
|
152 |
|
|
153 |
} |
|
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 |
|
|
160 |
class XMLDocumentHandlerImpl extends DefaultXMLDocumentHandler |
|
161 |
{ |
|
162 |
|
|
163 |
boolean debug = false; |
|
164 |
boolean stackCreated = false; |
|
165 |
private Stack elementStack; |
|
166 |
|
|
167 |
public void XMLDocumentHandlerImpl() |
|
168 |
{ |
|
169 |
} |
|
170 |
|
|
171 |
|
|
172 |
public void startElement(NSName name, SAXAttrList atts) throws SAXException |
|
173 |
{ |
|
174 |
|
|
175 |
// Use the methods getQualifiedName(), getLocalName(), getNamespace() |
|
176 |
// and getExpandedName() in NSName interface to get Namespace |
|
177 |
// information. |
|
178 |
|
|
179 |
String qName; |
|
180 |
String localName; |
|
181 |
String nsName; |
|
182 |
String expName; |
|
183 |
DBSAXElement currentElement; |
|
184 |
|
|
185 |
qName = name.getQualifiedName(); |
|
186 |
localName = name.getLocalName(); |
|
187 |
nsName = name.getNamespace(); |
|
188 |
expName = name.getExpandedName(); |
|
189 |
|
|
190 |
// Create the current element representation |
|
191 |
currentElement = new DBSAXElement(1, localName, false, 0); |
|
192 |
System.out.println("Element created:" + currentElement.getTagName()); |
|
193 |
|
|
194 |
// Add all of the attributes |
|
195 |
for (int i=0; i<atts.getLength(); i++) |
|
196 |
{ |
|
197 |
|
|
198 |
// Use the methods getQualifiedName(), getLocalName(), getNamespace() |
|
199 |
// and getExpandedName() in SAXAttrList interface to get Namespace |
|
200 |
// information. |
|
201 |
|
|
202 |
qName = atts.getQualifiedName(i); |
|
203 |
localName = atts.getLocalName(i); |
|
204 |
nsName = atts.getNamespace(i); |
|
205 |
expName = atts.getExpandedName(i); |
|
206 |
|
|
207 |
// You can get the type and value of the attributes either |
|
208 |
// by index or by the Qualified Name. |
|
209 |
|
|
210 |
String type = atts.getType(qName); |
|
211 |
String value = atts.getValue(qName); |
|
212 |
|
|
213 |
currentElement.setAttribute(localName, value); |
|
214 |
|
|
215 |
System.out.println(" Added ATTRIBUTE Name:" + localName); |
|
216 |
System.out.println(" Type:" + type); |
|
217 |
System.out.println(" Value:" + value); |
|
218 |
System.out.println(); |
|
219 |
|
|
220 |
} |
|
221 |
|
|
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 |
// Add the element to the stack, so that any text data can be |
|
230 |
// added as it is encountered |
|
231 |
elementStack.push(currentElement); |
|
232 |
|
|
233 |
} |
|
234 |
|
|
235 |
public void characters(char[] cbuf, int start, int len) |
|
236 |
{ |
|
237 |
DBSAXElement currentElement = (DBSAXElement)elementStack.peek(); |
|
238 |
currentElement.appendContent(cbuf,start,len); |
|
239 |
System.out.println(" Characters: " + new String(cbuf,start,len)); |
|
240 |
System.out.println(" Appended: " + currentElement.getContent()); |
|
241 |
} |
|
242 |
|
|
243 |
public void ignorableWhitespace(char[] cbuf, int start, int len) |
|
244 |
{ |
|
245 |
System.out.println("IgnorableWhiteSpace"); |
|
246 |
} |
|
247 |
|
|
248 |
public void endElement(NSName name) throws SAXException |
|
249 |
{ |
|
250 |
// Use the methods getQualifiedName(), getLocalName(), getNamespace() |
|
251 |
// and getExpandedName() in NSName interface to get Namespace |
|
252 |
// information. |
|
253 |
|
|
254 |
String expName = name.getExpandedName(); |
|
255 |
|
|
256 |
// Remove the element from the stack |
|
257 |
DBSAXElement currentElement = (DBSAXElement)elementStack.pop(); |
|
258 |
|
|
259 |
System.out.println("Final content: " + currentElement.getContent()); |
|
260 |
System.out.println("Closing element: " + expName); |
|
261 |
} |
|
262 |
|
|
263 |
public void db(int flag) { |
|
264 |
if (debug) { |
|
265 |
System.err.println("DEBUG POSITION " + flag); |
|
266 |
} |
|
267 |
} |
|
268 |
} |
|
0 | 269 |
test.xml | ||
---|---|---|
1 |
<?xml version="1.0"?> |
|
2 |
<testroot> |
|
3 |
<a testatt="attvalue1"> |
|
4 |
<b>Value1</b> |
|
5 |
<c newatt="89766">Value2</c> |
|
6 |
</a> |
|
7 |
</testroot> |
|
0 | 8 |
Also available in: Unified diff
Creating database loading scripts that utilize the oracle SAX parser