1
|
/**
|
2
|
* Name: DBWriter.java
|
3
|
* Purpose: A Class that reads in an XML text document and
|
4
|
* write its contents to a database connection using SAX
|
5
|
* Copyright: 2000 Regents of the University of California and the
|
6
|
* National Center for Ecological Analysis and Synthesis
|
7
|
* Authors: Matt Jones
|
8
|
*
|
9
|
* Version: '$Id: DBWriter.java 186 2000-06-23 19:09:56Z jones $'
|
10
|
*/
|
11
|
|
12
|
package edu.ucsb.nceas.metacat;
|
13
|
|
14
|
import java.io.*;
|
15
|
import java.sql.*;
|
16
|
import java.util.Stack;
|
17
|
|
18
|
import org.xml.sax.AttributeList;
|
19
|
import org.xml.sax.ContentHandler;
|
20
|
import org.xml.sax.DTDHandler;
|
21
|
import org.xml.sax.EntityResolver;
|
22
|
import org.xml.sax.ErrorHandler;
|
23
|
import org.xml.sax.InputSource;
|
24
|
import org.xml.sax.XMLReader;
|
25
|
import org.xml.sax.SAXException;
|
26
|
import org.xml.sax.SAXParseException;
|
27
|
import org.xml.sax.helpers.XMLReaderFactory;
|
28
|
|
29
|
/**
|
30
|
* A Class that reads in an XML text document and
|
31
|
* write its contents to a database connection using SAX
|
32
|
*/
|
33
|
public class DBWriter {
|
34
|
|
35
|
private Connection conn = null;
|
36
|
private String parserName = null;
|
37
|
|
38
|
/** Default parser name. */
|
39
|
private static final String
|
40
|
DEFAULT_PARSER = "org.apache.xerces.parsers.SAXParser";
|
41
|
//DEFAULT_PARSER = "oracle.xml.parser.v2.SAXParser";
|
42
|
|
43
|
/**
|
44
|
* the main routine used to test the DBWriter utility.
|
45
|
* <p>
|
46
|
* Usage: java DBWriter <filename>
|
47
|
*
|
48
|
* @param filename the filename to be loaded into the database
|
49
|
*/
|
50
|
static public void main(String[] args) {
|
51
|
|
52
|
if (args.length < 1)
|
53
|
{
|
54
|
System.err.println("Wrong number of arguments!!!");
|
55
|
System.err.println("USAGE: java DBWriter <filename>");
|
56
|
return;
|
57
|
} else {
|
58
|
try {
|
59
|
|
60
|
String filename = args[0];
|
61
|
|
62
|
// Open a connection to the database
|
63
|
MetaCatUtil util = new MetaCatUtil();
|
64
|
Connection dbconn = util.openDBConnection();
|
65
|
|
66
|
new DBWriter(filename, dbconn, DEFAULT_PARSER);
|
67
|
System.out.println("Document processing finished for: " + filename);
|
68
|
|
69
|
} catch (Exception e) {
|
70
|
System.err.println("EXCEPTION HANDLING REQUIRED");
|
71
|
System.err.println(e.getMessage());
|
72
|
e.printStackTrace(System.err);
|
73
|
}
|
74
|
}
|
75
|
}
|
76
|
|
77
|
/**
|
78
|
* construct a new instance of the class to write an XML file to the database
|
79
|
*
|
80
|
* @param xml the xml stream to be loaded into the database
|
81
|
* @param conn the database connection to which to write the XML file
|
82
|
* @param parserName the name of a SAX2 compliant parser class
|
83
|
*/
|
84
|
public DBWriter( Reader xml, Connection conn, String parserName)
|
85
|
throws IOException,
|
86
|
SQLException,
|
87
|
ClassNotFoundException {
|
88
|
this.conn = conn;
|
89
|
this.parserName = parserName;
|
90
|
|
91
|
try {
|
92
|
XMLReader parser = initializeParser();
|
93
|
conn.setAutoCommit(false);
|
94
|
parser.parse(new InputSource(xml));
|
95
|
conn.commit();
|
96
|
conn.setAutoCommit(true);
|
97
|
} catch (SAXParseException e) {
|
98
|
conn.rollback();
|
99
|
System.err.println(e.getMessage());
|
100
|
} catch (SAXException e) {
|
101
|
conn.rollback();
|
102
|
System.err.println(e.getMessage());
|
103
|
} catch (Exception e) {
|
104
|
conn.rollback();
|
105
|
System.err.println(e.toString());
|
106
|
}
|
107
|
}
|
108
|
|
109
|
/**
|
110
|
* construct a new instance of the class to write an XML file to the database
|
111
|
*
|
112
|
* @param filename the filename to be loaded into the database
|
113
|
* @param conn the database connection to which to write the XML file
|
114
|
* @param parserName the name of a SAX2 compliant parser class
|
115
|
*/
|
116
|
public DBWriter( String filename, Connection conn, String parserName)
|
117
|
throws IOException,
|
118
|
SQLException,
|
119
|
ClassNotFoundException {
|
120
|
this(new FileReader(new File(filename).toString()), conn, parserName);
|
121
|
}
|
122
|
|
123
|
private XMLReader initializeParser() {
|
124
|
XMLReader parser = null;
|
125
|
//
|
126
|
// Set up the SAX document handlers for parsing
|
127
|
//
|
128
|
try {
|
129
|
ContentHandler xmlDocHandler = new DBSAXHandler(conn);
|
130
|
EntityResolver xmlEntityResolver = new DBEntityResolver(conn);
|
131
|
DTDHandler xmlDTDHandler = new DBDTDHandler(conn);
|
132
|
|
133
|
// Get an instance of the parser
|
134
|
parser = XMLReaderFactory.createXMLReader(parserName);
|
135
|
|
136
|
// Turn off validation
|
137
|
parser.setFeature("http://xml.org/sax/features/validation", false);
|
138
|
|
139
|
// Set Handlers in the parser
|
140
|
parser.setProperty("http://xml.org/sax/properties/declaration-handler",
|
141
|
xmlDocHandler);
|
142
|
parser.setProperty("http://xml.org/sax/properties/lexical-handler",
|
143
|
xmlDocHandler);
|
144
|
parser.setContentHandler(xmlDocHandler);
|
145
|
parser.setEntityResolver(xmlEntityResolver);
|
146
|
parser.setDTDHandler(xmlDTDHandler);
|
147
|
parser.setErrorHandler((ErrorHandler)xmlDocHandler);
|
148
|
|
149
|
} catch (Exception e) {
|
150
|
System.err.println(e.toString());
|
151
|
}
|
152
|
|
153
|
return parser;
|
154
|
}
|
155
|
}
|