1 |
15
|
jones
|
/**
|
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 |
35
|
jones
|
* Copyright: 2000 Regents of the University of California and the
|
6 |
|
|
* National Center for Ecological Analysis and Synthesis
|
7 |
15
|
jones
|
* Authors: Matt Jones
|
8 |
|
|
*
|
9 |
|
|
* Version: '$Id$'
|
10 |
|
|
*/
|
11 |
|
|
|
12 |
51
|
jones
|
package edu.ucsb.nceas.metacat;
|
13 |
|
|
|
14 |
15
|
jones
|
import org.xml.sax.*;
|
15 |
|
|
|
16 |
|
|
import java.io.*;
|
17 |
|
|
import java.net.URL;
|
18 |
|
|
import java.net.MalformedURLException;
|
19 |
|
|
import java.sql.*;
|
20 |
|
|
import java.util.Stack;
|
21 |
|
|
|
22 |
|
|
// Extensions to the SAX Interfaces for Namespace support.
|
23 |
|
|
import oracle.xml.parser.v2.XMLDocumentHandler;
|
24 |
|
|
import oracle.xml.parser.v2.DefaultXMLDocumentHandler;
|
25 |
|
|
import oracle.xml.parser.v2.NSName;
|
26 |
|
|
import oracle.xml.parser.v2.SAXAttrList;
|
27 |
|
|
|
28 |
|
|
import oracle.xml.parser.v2.SAXParser;
|
29 |
|
|
|
30 |
31
|
jones
|
/**
|
31 |
|
|
* A Class that reads in an XML text document and
|
32 |
|
|
* write its contents to a database connection using SAX
|
33 |
|
|
*/
|
34 |
15
|
jones
|
public class DBSAXWriter {
|
35 |
|
|
|
36 |
|
|
static String defaultDB = "jdbc:oracle:thin:@localhost:1521:test";
|
37 |
|
|
private Connection conn = null;
|
38 |
|
|
|
39 |
31
|
jones
|
/**
|
40 |
|
|
* the main routine used to test the DBSAXWriter utility.
|
41 |
|
|
*
|
42 |
|
|
* Usage: java DBSAXWriter <filename> <user> <password> [dbstring]
|
43 |
|
|
*
|
44 |
|
|
* @param filename the filename to be loaded into the database
|
45 |
|
|
* @param user the username to use for the database connection
|
46 |
|
|
* @param password the password to use for the database connection
|
47 |
|
|
* @param dbstring the connection info to use for the database connection
|
48 |
|
|
*/
|
49 |
15
|
jones
|
static public void main(String[] args) {
|
50 |
|
|
|
51 |
|
|
if (args.length < 3)
|
52 |
|
|
{
|
53 |
|
|
System.err.println("Wrong number of arguments!!!");
|
54 |
|
|
System.err.println("USAGE: java DBSAXWriter " +
|
55 |
|
|
"<filename> <user> <password> [dbstring]");
|
56 |
|
|
return;
|
57 |
|
|
} else {
|
58 |
|
|
try {
|
59 |
|
|
|
60 |
|
|
String filename = args[0];
|
61 |
|
|
String user = args[1];
|
62 |
|
|
String password = args[2];
|
63 |
|
|
String dbstring = null;
|
64 |
|
|
|
65 |
|
|
if (args.length <= 3) {
|
66 |
|
|
dbstring = defaultDB;
|
67 |
|
|
} else {
|
68 |
|
|
dbstring = args[3];
|
69 |
|
|
}
|
70 |
|
|
|
71 |
50
|
jones
|
// Open a connection to the database
|
72 |
|
|
Connection dbconn = MetaCatUtil.openDBConnection(
|
73 |
|
|
"oracle.jdbc.driver.OracleDriver",
|
74 |
|
|
dbstring, user, password);
|
75 |
|
|
|
76 |
|
|
new DBSAXWriter(filename, dbconn);
|
77 |
30
|
jones
|
System.out.println("Document processing finished for: " + filename);
|
78 |
|
|
|
79 |
15
|
jones
|
} catch (Exception e) {
|
80 |
|
|
System.err.println("EXCEPTION HANDLING REQUIRED");
|
81 |
|
|
System.err.println(e.getMessage());
|
82 |
|
|
e.printStackTrace(System.err);
|
83 |
|
|
}
|
84 |
|
|
}
|
85 |
|
|
}
|
86 |
|
|
|
87 |
31
|
jones
|
/**
|
88 |
|
|
* construct a new instance of the class to write an XML file to the database
|
89 |
|
|
*
|
90 |
|
|
* @param filename the filename to be loaded into the database
|
91 |
50
|
jones
|
* @param conn the database connection to which to write the XML file
|
92 |
31
|
jones
|
*/
|
93 |
55
|
jones
|
public DBSAXWriter( Reader xml, Connection conn)
|
94 |
|
|
throws IOException,
|
95 |
|
|
SQLException,
|
96 |
|
|
ClassNotFoundException
|
97 |
|
|
{
|
98 |
|
|
this.conn = conn;
|
99 |
|
|
|
100 |
|
|
//try {
|
101 |
|
|
|
102 |
|
|
try {
|
103 |
|
|
SAXParser parser = initializeParser(conn);
|
104 |
|
|
parser.parse(xml);
|
105 |
|
|
} catch (SAXParseException e) {
|
106 |
|
|
System.err.println(e.getMessage());
|
107 |
|
|
} catch (SAXException e) {
|
108 |
|
|
System.err.println(e.getMessage());
|
109 |
|
|
}
|
110 |
|
|
//} catch (Exception e) {
|
111 |
|
|
//System.err.println(e.toString());
|
112 |
|
|
//}
|
113 |
|
|
}
|
114 |
|
|
|
115 |
50
|
jones
|
public DBSAXWriter( String filename, Connection conn)
|
116 |
15
|
jones
|
throws IOException,
|
117 |
|
|
SQLException,
|
118 |
|
|
ClassNotFoundException
|
119 |
55
|
jones
|
{
|
120 |
50
|
jones
|
this.conn = conn;
|
121 |
55
|
jones
|
FileReader xmlfile = new FileReader(new File(filename).toString());
|
122 |
15
|
jones
|
try {
|
123 |
55
|
jones
|
SAXParser parser = initializeParser(conn);
|
124 |
|
|
parser.parse(xmlfile);
|
125 |
|
|
} catch (SAXParseException e) {
|
126 |
|
|
System.err.println(e.getMessage());
|
127 |
|
|
} catch (SAXException e) {
|
128 |
|
|
System.err.println(e.getMessage());
|
129 |
|
|
}
|
130 |
|
|
}
|
131 |
|
|
|
132 |
|
|
private SAXParser initializeParser(Connection conn) {
|
133 |
|
|
SAXParser parser = null;
|
134 |
|
|
//
|
135 |
|
|
// Set up the SAX document handlers for parsing
|
136 |
|
|
//
|
137 |
|
|
try {
|
138 |
|
|
// Use the XMLDocumentHandler interface for namespace support
|
139 |
|
|
// instead of org.xml.sax.DocumentHandler
|
140 |
|
|
XMLDocumentHandler xmlDocHandler = new DBSAXHandler(conn);
|
141 |
15
|
jones
|
|
142 |
55
|
jones
|
// For all the other interface use the default provided by
|
143 |
|
|
// Handler base
|
144 |
|
|
HandlerBase defHandler = new HandlerBase();
|
145 |
15
|
jones
|
|
146 |
55
|
jones
|
// Get an instance of the parser
|
147 |
|
|
parser = new SAXParser();
|
148 |
15
|
jones
|
|
149 |
55
|
jones
|
// Set Handlers in the parser
|
150 |
|
|
// Set the DocumentHandler to XMLDocumentHandler
|
151 |
|
|
parser.setDocumentHandler(xmlDocHandler);
|
152 |
15
|
jones
|
|
153 |
55
|
jones
|
// Set the other Handler to the defHandler
|
154 |
|
|
parser.setErrorHandler(defHandler);
|
155 |
|
|
parser.setEntityResolver(defHandler);
|
156 |
|
|
parser.setDTDHandler(defHandler);
|
157 |
15
|
jones
|
|
158 |
55
|
jones
|
} catch (Exception e) {
|
159 |
|
|
System.err.println(e.toString());
|
160 |
|
|
}
|
161 |
15
|
jones
|
|
162 |
55
|
jones
|
return parser;
|
163 |
|
|
}
|
164 |
15
|
jones
|
|
165 |
55
|
jones
|
/** Utility method to convert a file handle into a URL */
|
166 |
|
|
static public URL fileToURL(File file)
|
167 |
|
|
{
|
168 |
16
|
jones
|
String path = file.getAbsolutePath();
|
169 |
|
|
String fSep = System.getProperty("file.separator");
|
170 |
|
|
if (fSep != null && fSep.length() == 1)
|
171 |
|
|
path = path.replace(fSep.charAt(0), '/');
|
172 |
|
|
if (path.length() > 0 && path.charAt(0) != '/')
|
173 |
|
|
path = '/' + path;
|
174 |
|
|
try {
|
175 |
|
|
return new URL("file", null, path);
|
176 |
|
|
}
|
177 |
|
|
catch (java.net.MalformedURLException e) {
|
178 |
|
|
/* According to the spec this could only happen if the file
|
179 |
|
|
protocol were not recognized. */
|
180 |
|
|
throw new Error("unexpected MalformedURLException");
|
181 |
|
|
}
|
182 |
15
|
jones
|
}
|
183 |
|
|
}
|