1 |
6
|
jones
|
/**
|
2 |
|
|
* Name: DBWriter.java
|
3 |
|
|
* Purpose: A Class that reads in an XML text document and
|
4 |
|
|
* write its contents to a database connection
|
5 |
35
|
jones
|
* Copyright: 2000 Regents of the University of California and the
|
6 |
|
|
* National Center for Ecological Analysis and Synthesis
|
7 |
6
|
jones
|
* Authors: Matt Jones
|
8 |
|
|
*
|
9 |
|
|
* Version: '$Id$'
|
10 |
|
|
*/
|
11 |
|
|
|
12 |
|
|
//package edu.ucsb.nceas.dbwriter;
|
13 |
|
|
|
14 |
|
|
import java.io.File;
|
15 |
|
|
import java.io.FileInputStream;
|
16 |
|
|
import java.io.IOException;
|
17 |
|
|
import java.io.InputStream;
|
18 |
|
|
import java.net.URL;
|
19 |
|
|
import java.util.Properties;
|
20 |
7
|
jones
|
import java.sql.*;
|
21 |
6
|
jones
|
|
22 |
|
|
import org.xml.sax.ErrorHandler;
|
23 |
|
|
import org.xml.sax.InputSource;
|
24 |
|
|
import org.xml.sax.SAXException;
|
25 |
|
|
import org.xml.sax.SAXParseException;
|
26 |
|
|
|
27 |
|
|
import org.w3c.dom.Element;
|
28 |
|
|
import org.w3c.dom.Node;
|
29 |
|
|
import org.w3c.dom.NodeList;
|
30 |
|
|
|
31 |
|
|
import com.sun.xml.tree.XmlDocument;
|
32 |
|
|
import com.sun.xml.tree.XmlDocumentBuilder;
|
33 |
|
|
import com.sun.xml.tree.SimpleElementFactory;
|
34 |
|
|
import com.sun.xml.parser.Resolver;
|
35 |
|
|
import com.sun.xml.parser.ValidatingParser;
|
36 |
|
|
|
37 |
|
|
|
38 |
|
|
public class DBWriter
|
39 |
|
|
{
|
40 |
|
|
private XmlDocument doc;
|
41 |
8
|
jones
|
private Connection conn = null;
|
42 |
6
|
jones
|
|
43 |
7
|
jones
|
// Constructer to init the class
|
44 |
|
|
public DBWriter(String argv[])
|
45 |
|
|
throws SAXException, SQLException, IOException, ClassNotFoundException {
|
46 |
|
|
|
47 |
8
|
jones
|
// Open a connection to the database
|
48 |
7
|
jones
|
conn = openDBConnection(
|
49 |
|
|
"oracle.jdbc.driver.OracleDriver",
|
50 |
15
|
jones
|
"jdbc:oracle:thin:@localhost:test",
|
51 |
9
|
jones
|
argv[2], argv[3]);
|
52 |
7
|
jones
|
|
53 |
8
|
jones
|
//
|
54 |
|
|
// Load the document, using the appropriate custom
|
55 |
|
|
// DOM elements.
|
56 |
|
|
//
|
57 |
|
|
XmlDocument doc = createDocument (
|
58 |
|
|
Resolver.createInputSource (new File (argv [1])),
|
59 |
|
|
new FileInputStream (argv [0]),
|
60 |
|
|
new ErrorPrinter ()
|
61 |
|
|
);
|
62 |
|
|
|
63 |
|
|
DBElement root;
|
64 |
|
|
|
65 |
|
|
root = (DBElement)doc.getDocumentElement();
|
66 |
|
|
root.normalize();
|
67 |
11
|
jones
|
root.writeXmlToDB(conn, 0);
|
68 |
7
|
jones
|
}
|
69 |
|
|
|
70 |
6
|
jones
|
public static void main (String argv [])
|
71 |
|
|
{
|
72 |
|
|
try {
|
73 |
9
|
jones
|
if (argv.length != 4) {
|
74 |
|
|
System.err.println ("usage: java DBWriter "
|
75 |
|
|
+ "DBElement.props somefile.xml username password");
|
76 |
6
|
jones
|
System.exit (1);
|
77 |
|
|
}
|
78 |
|
|
|
79 |
7
|
jones
|
new DBWriter(argv);
|
80 |
6
|
jones
|
|
81 |
|
|
} catch (Throwable t) {
|
82 |
|
|
t.printStackTrace (System.out);
|
83 |
|
|
}
|
84 |
|
|
}
|
85 |
|
|
|
86 |
|
|
static private XmlDocument createDocument (
|
87 |
|
|
InputSource input,
|
88 |
|
|
InputStream propsStream,
|
89 |
|
|
ErrorHandler errorHandler
|
90 |
|
|
) throws SAXException, IOException
|
91 |
|
|
{
|
92 |
|
|
try {
|
93 |
|
|
XmlDocumentBuilder builder = new XmlDocumentBuilder ();
|
94 |
|
|
ValidatingParser parser = new ValidatingParser ();
|
95 |
|
|
|
96 |
|
|
//
|
97 |
|
|
// Configure the builder to create the right elements.
|
98 |
|
|
//
|
99 |
|
|
{
|
100 |
|
|
Properties props;
|
101 |
|
|
SimpleElementFactory factory;
|
102 |
|
|
|
103 |
|
|
props = new Properties ();
|
104 |
|
|
props.load (propsStream);
|
105 |
|
|
factory = new SimpleElementFactory ();
|
106 |
|
|
factory.addMapping (props, DBWriter.class.getClassLoader ());
|
107 |
|
|
builder.setElementFactory (factory);
|
108 |
|
|
}
|
109 |
|
|
|
110 |
|
|
//
|
111 |
|
|
// Configure the parser
|
112 |
|
|
//
|
113 |
|
|
parser.setErrorHandler (errorHandler);
|
114 |
|
|
parser.setEntityResolver (new Resolver ());
|
115 |
|
|
parser.setDocumentHandler (builder);
|
116 |
|
|
parser.parse (input);
|
117 |
|
|
|
118 |
|
|
// Look at how the different versions print themselvs...
|
119 |
|
|
if (false) {
|
120 |
|
|
builder.getDocument ().write (System.out);
|
121 |
|
|
System.exit (0);
|
122 |
|
|
}
|
123 |
|
|
|
124 |
|
|
return builder.getDocument ();
|
125 |
|
|
|
126 |
|
|
} catch (SAXParseException e) {
|
127 |
|
|
// already reported
|
128 |
|
|
throw e;
|
129 |
|
|
|
130 |
|
|
} catch (SAXException e) {
|
131 |
|
|
e.printStackTrace (System.out);
|
132 |
|
|
throw e;
|
133 |
|
|
|
134 |
|
|
} catch (IOException e) {
|
135 |
|
|
e.printStackTrace (System.out);
|
136 |
|
|
throw e;
|
137 |
|
|
}
|
138 |
|
|
}
|
139 |
7
|
jones
|
|
140 |
|
|
private Connection openDBConnection(String dbDriver, String connection,
|
141 |
|
|
String user, String password)
|
142 |
|
|
throws SQLException, ClassNotFoundException {
|
143 |
|
|
// Load the Oracle JDBC driver
|
144 |
9
|
jones
|
Class.forName (dbDriver);
|
145 |
6
|
jones
|
|
146 |
7
|
jones
|
// Connect to the database
|
147 |
|
|
// You can put a database name after the @ sign in the connection URL.
|
148 |
|
|
Connection conn = DriverManager.getConnection(
|
149 |
9
|
jones
|
connection, user, password);
|
150 |
7
|
jones
|
return conn;
|
151 |
|
|
}
|
152 |
|
|
|
153 |
6
|
jones
|
static class ErrorPrinter implements ErrorHandler
|
154 |
|
|
{
|
155 |
|
|
private void message (String level, SAXParseException e)
|
156 |
|
|
{
|
157 |
|
|
System.out.print ("** ");
|
158 |
|
|
System.out.println (level);
|
159 |
|
|
System.out.print (" URI = ");
|
160 |
|
|
System.out.print (e.getSystemId ());
|
161 |
|
|
System.out.print (" Line = ");
|
162 |
|
|
System.out.println (e.getLineNumber ());
|
163 |
|
|
System.out.print (" Message = ");
|
164 |
|
|
System.out.println (e.getMessage ());
|
165 |
|
|
|
166 |
|
|
/*
|
167 |
|
|
if (e.getException () != null)
|
168 |
|
|
e.getException ().printStackTrace (System.out);
|
169 |
|
|
else
|
170 |
|
|
e.printStackTrace (System.out);
|
171 |
|
|
*/
|
172 |
|
|
}
|
173 |
|
|
|
174 |
|
|
public void error (SAXParseException e)
|
175 |
|
|
{
|
176 |
|
|
// normally a validity error
|
177 |
|
|
message ("Error (recoverable)", e);
|
178 |
|
|
}
|
179 |
|
|
|
180 |
|
|
public void warning (SAXParseException e)
|
181 |
|
|
{
|
182 |
|
|
message ("Warning", e);
|
183 |
|
|
}
|
184 |
|
|
|
185 |
|
|
public void fatalError (SAXParseException e)
|
186 |
|
|
{
|
187 |
|
|
message ("FATAL ERROR", e);
|
188 |
|
|
}
|
189 |
|
|
}
|
190 |
|
|
|
191 |
|
|
}
|