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 |
|
|
* Institution: National Center for Ecological Analysis and Synthesis
|
6 |
|
|
* Copyright: 1998
|
7 |
|
|
* 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 |
|
|
"jdbc:oracle:thin:@penelope.nceas.ucsb.edu:1526:DEV",
|
51 |
|
|
"jones", "kinkaj0u");
|
52 |
|
|
|
53 |
8
|
jones
|
// THIS IS TEMPORARY
|
54 |
7
|
jones
|
// Prepare a statement to cleanup the emp table
|
55 |
8
|
jones
|
/*
|
56 |
7
|
jones
|
Statement stmt = conn.createStatement ();
|
57 |
|
|
try {
|
58 |
|
|
stmt.execute ("delete from xml_elements where nodeid = 1500");
|
59 |
|
|
} catch (SQLException e) {
|
60 |
|
|
// Ignore an error here
|
61 |
|
|
}
|
62 |
8
|
jones
|
*/
|
63 |
|
|
// THIS IS TEMPORARY
|
64 |
7
|
jones
|
// Prepare to insert new names in the Elements table
|
65 |
8
|
jones
|
/*
|
66 |
7
|
jones
|
PreparedStatement pstmt = conn.prepareStatement(
|
67 |
|
|
"insert into xml_elements (nodeid, nodename) values (?, ?)");
|
68 |
|
|
|
69 |
8
|
jones
|
// Add an element as element number 1500
|
70 |
7
|
jones
|
pstmt.setInt (1, 1500); // The first ? is for NODEID
|
71 |
|
|
pstmt.setString (2, "eml-variable");// The second ? is for NODENAME
|
72 |
|
|
// Do the insertion
|
73 |
|
|
pstmt.execute ();
|
74 |
8
|
jones
|
*/
|
75 |
|
|
|
76 |
|
|
//
|
77 |
|
|
// Load the document, using the appropriate custom
|
78 |
|
|
// DOM elements.
|
79 |
|
|
//
|
80 |
|
|
XmlDocument doc = createDocument (
|
81 |
|
|
Resolver.createInputSource (new File (argv [1])),
|
82 |
|
|
new FileInputStream (argv [0]),
|
83 |
|
|
new ErrorPrinter ()
|
84 |
|
|
);
|
85 |
|
|
|
86 |
|
|
DBElement root;
|
87 |
|
|
|
88 |
|
|
root = (DBElement)doc.getDocumentElement();
|
89 |
|
|
root.normalize();
|
90 |
|
|
root.writeXmlToDB(conn);
|
91 |
7
|
jones
|
}
|
92 |
|
|
|
93 |
6
|
jones
|
//
|
94 |
|
|
// usage: main someprops.props someplay.xml
|
95 |
|
|
//
|
96 |
|
|
public static void main (String argv [])
|
97 |
|
|
{
|
98 |
|
|
try {
|
99 |
|
|
if (argv.length != 2) {
|
100 |
|
|
System.err.println ("usage: java project.DBWriter "
|
101 |
|
|
+ "someprops.props someplay.xml");
|
102 |
|
|
System.exit (1);
|
103 |
|
|
}
|
104 |
|
|
|
105 |
7
|
jones
|
new DBWriter(argv);
|
106 |
6
|
jones
|
|
107 |
|
|
} catch (Throwable t) {
|
108 |
|
|
t.printStackTrace (System.out);
|
109 |
|
|
}
|
110 |
|
|
}
|
111 |
|
|
|
112 |
|
|
static private XmlDocument createDocument (
|
113 |
|
|
InputSource input,
|
114 |
|
|
InputStream propsStream,
|
115 |
|
|
ErrorHandler errorHandler
|
116 |
|
|
) throws SAXException, IOException
|
117 |
|
|
{
|
118 |
|
|
try {
|
119 |
|
|
XmlDocumentBuilder builder = new XmlDocumentBuilder ();
|
120 |
|
|
ValidatingParser parser = new ValidatingParser ();
|
121 |
|
|
|
122 |
|
|
//
|
123 |
|
|
// Configure the builder to create the right elements.
|
124 |
|
|
//
|
125 |
|
|
{
|
126 |
|
|
Properties props;
|
127 |
|
|
SimpleElementFactory factory;
|
128 |
|
|
|
129 |
|
|
props = new Properties ();
|
130 |
|
|
props.load (propsStream);
|
131 |
|
|
factory = new SimpleElementFactory ();
|
132 |
|
|
factory.addMapping (props, DBWriter.class.getClassLoader ());
|
133 |
|
|
builder.setElementFactory (factory);
|
134 |
|
|
}
|
135 |
|
|
|
136 |
|
|
//
|
137 |
|
|
// Configure the parser
|
138 |
|
|
//
|
139 |
|
|
parser.setErrorHandler (errorHandler);
|
140 |
|
|
parser.setEntityResolver (new Resolver ());
|
141 |
|
|
parser.setDocumentHandler (builder);
|
142 |
|
|
parser.parse (input);
|
143 |
|
|
|
144 |
|
|
// Look at how the different versions print themselvs...
|
145 |
|
|
if (false) {
|
146 |
|
|
builder.getDocument ().write (System.out);
|
147 |
|
|
System.exit (0);
|
148 |
|
|
}
|
149 |
|
|
|
150 |
|
|
return builder.getDocument ();
|
151 |
|
|
|
152 |
|
|
} catch (SAXParseException e) {
|
153 |
|
|
// already reported
|
154 |
|
|
throw e;
|
155 |
|
|
|
156 |
|
|
} catch (SAXException e) {
|
157 |
|
|
e.printStackTrace (System.out);
|
158 |
|
|
throw e;
|
159 |
|
|
|
160 |
|
|
} catch (IOException e) {
|
161 |
|
|
e.printStackTrace (System.out);
|
162 |
|
|
throw e;
|
163 |
|
|
}
|
164 |
|
|
}
|
165 |
7
|
jones
|
|
166 |
|
|
private Connection openDBConnection(String dbDriver, String connection,
|
167 |
|
|
String user, String password)
|
168 |
|
|
throws SQLException, ClassNotFoundException {
|
169 |
|
|
// Load the Oracle JDBC driver
|
170 |
|
|
Class.forName ("oracle.jdbc.driver.OracleDriver");
|
171 |
6
|
jones
|
|
172 |
7
|
jones
|
// Connect to the database
|
173 |
|
|
// You can put a database name after the @ sign in the connection URL.
|
174 |
|
|
Connection conn = DriverManager.getConnection(
|
175 |
|
|
"jdbc:oracle:thin:@penelope.nceas.ucsb.edu:1526:DEV",
|
176 |
|
|
"jones", "kinkaj0u");
|
177 |
|
|
return conn;
|
178 |
|
|
}
|
179 |
|
|
|
180 |
6
|
jones
|
static class ErrorPrinter implements ErrorHandler
|
181 |
|
|
{
|
182 |
|
|
private void message (String level, SAXParseException e)
|
183 |
|
|
{
|
184 |
|
|
System.out.print ("** ");
|
185 |
|
|
System.out.println (level);
|
186 |
|
|
System.out.print (" URI = ");
|
187 |
|
|
System.out.print (e.getSystemId ());
|
188 |
|
|
System.out.print (" Line = ");
|
189 |
|
|
System.out.println (e.getLineNumber ());
|
190 |
|
|
System.out.print (" Message = ");
|
191 |
|
|
System.out.println (e.getMessage ());
|
192 |
|
|
|
193 |
|
|
/*
|
194 |
|
|
if (e.getException () != null)
|
195 |
|
|
e.getException ().printStackTrace (System.out);
|
196 |
|
|
else
|
197 |
|
|
e.printStackTrace (System.out);
|
198 |
|
|
*/
|
199 |
|
|
}
|
200 |
|
|
|
201 |
|
|
public void error (SAXParseException e)
|
202 |
|
|
{
|
203 |
|
|
// normally a validity error
|
204 |
|
|
message ("Error (recoverable)", e);
|
205 |
|
|
}
|
206 |
|
|
|
207 |
|
|
public void warning (SAXParseException e)
|
208 |
|
|
{
|
209 |
|
|
message ("Warning", e);
|
210 |
|
|
}
|
211 |
|
|
|
212 |
|
|
public void fatalError (SAXParseException e)
|
213 |
|
|
{
|
214 |
|
|
message ("FATAL ERROR", e);
|
215 |
|
|
}
|
216 |
|
|
}
|
217 |
|
|
|
218 |
|
|
}
|