1
|
/**
|
2
|
* Name: DBSAXHandler.java
|
3
|
* Purpose: A Class that handles the SAX XML events as they
|
4
|
* are generated from XML documents
|
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: DBSAXHandler.java 149 2000-06-12 19:52:10Z bojilova $'
|
10
|
*/
|
11
|
|
12
|
package edu.ucsb.nceas.metacat;
|
13
|
|
14
|
import org.xml.sax.*;
|
15
|
|
16
|
import java.sql.*;
|
17
|
import java.util.Stack;
|
18
|
import java.util.EmptyStackException;
|
19
|
|
20
|
// Extensions to the SAX Interfaces for Namespace support.
|
21
|
import oracle.xml.parser.v2.DefaultXMLDocumentHandler;
|
22
|
import oracle.xml.parser.v2.NSName;
|
23
|
import oracle.xml.parser.v2.SAXAttrList;
|
24
|
|
25
|
import oracle.xml.parser.v2.SAXParser;
|
26
|
import oracle.xml.parser.v2.DTD;
|
27
|
|
28
|
/**
|
29
|
* A database aware Class implementing callback bethods for the SAX parser to
|
30
|
* call when processing the XML stream and generating events
|
31
|
*/
|
32
|
public class DBSAXHandler extends DefaultXMLDocumentHandler {
|
33
|
|
34
|
static int elementNo = 0;
|
35
|
static String docname = null;
|
36
|
//private String docID;
|
37
|
//private String rootNodeID;
|
38
|
private String doctype;
|
39
|
private String systemid;
|
40
|
private boolean debug = false;
|
41
|
private boolean stackCreated = false;
|
42
|
private Stack nodeStack;
|
43
|
private Connection conn = null;
|
44
|
private DBSAXDocument currentDocument;
|
45
|
private DBSAXNode rootNode;
|
46
|
|
47
|
/** Construct an instance of the handler class
|
48
|
*
|
49
|
* @param conn the JDBC connection to which information is written
|
50
|
*/
|
51
|
public DBSAXHandler(Connection conn) {
|
52
|
this.conn = conn;
|
53
|
|
54
|
// Create the stack for keeping track of node context
|
55
|
// if it doesn't already exist
|
56
|
if (!stackCreated) {
|
57
|
nodeStack = new Stack();
|
58
|
stackCreated = true;
|
59
|
}
|
60
|
|
61
|
}
|
62
|
|
63
|
/** SAX Handler that receives notification of beginning of the document */
|
64
|
public void startDocument() throws SAXException {
|
65
|
if (debug) {
|
66
|
System.out.println("start Document");
|
67
|
}
|
68
|
// Create the document node represantation as root
|
69
|
rootNode = new DBSAXNode(conn, docname);
|
70
|
// Add the node to the stack, so that any text data can be
|
71
|
// added as it is encountered
|
72
|
nodeStack.push(rootNode);
|
73
|
}
|
74
|
|
75
|
/** SAX Handler that receives notification of end of the document */
|
76
|
public void endDocument() throws SAXException {
|
77
|
currentDocument.setTitleFromChildElement();
|
78
|
if (debug) {
|
79
|
System.out.println("end Document");
|
80
|
}
|
81
|
}
|
82
|
|
83
|
/** SAX Handler that receives notification of DTD. Sets the DTD */
|
84
|
public void setDoctype(DTD dtd) throws SAXException {
|
85
|
// here is a bug: dtd.getPublicId() and dtd.getSustemId()
|
86
|
// always return null.
|
87
|
docname = dtd.getName();
|
88
|
doctype = dtd.getPublicId();
|
89
|
systemid = dtd.getSystemId();
|
90
|
if (debug) {
|
91
|
System.out.println("DOCNAME: " + docname);
|
92
|
System.out.println("DOCTYPE: " + doctype);
|
93
|
System.out.println(" SYSID: " + systemid);
|
94
|
}
|
95
|
}
|
96
|
|
97
|
/**
|
98
|
* SAX Handler that receives notification of end of DTD
|
99
|
* All events in DTDHandler about all unparsed entities and the
|
100
|
* event in EntityResolver for the DTD file declaration appear
|
101
|
* between setDoctype and endDoctype. The rest of parsable external
|
102
|
* entities inside DTD file appear later in the elements from where
|
103
|
* they are referred to.
|
104
|
*/
|
105
|
public void endDoctype() throws SAXException {
|
106
|
if (debug) {
|
107
|
System.out.println("end of DOCTYPE");
|
108
|
}
|
109
|
}
|
110
|
|
111
|
/** SAX Handler that is called at the start of each XML element */
|
112
|
public void startElement(NSName name, SAXAttrList atts) throws SAXException {
|
113
|
|
114
|
// Use the methods getQualifiedName(), getLocalName(), getNamespace()
|
115
|
// and getExpandedName() in NSName interface to get Namespace
|
116
|
// information.
|
117
|
|
118
|
String qName;
|
119
|
String localName;
|
120
|
String nsName;
|
121
|
String expName;
|
122
|
DBSAXNode parentNode = null;
|
123
|
DBSAXNode currentNode = null;
|
124
|
|
125
|
qName = name.getQualifiedName();
|
126
|
localName = name.getLocalName();
|
127
|
nsName = name.getNamespace();
|
128
|
expName = name.getExpandedName();
|
129
|
|
130
|
elementNo++;
|
131
|
// Get a reference to the parent node for the id
|
132
|
try {
|
133
|
parentNode = (DBSAXNode)nodeStack.peek();
|
134
|
} catch (EmptyStackException e) {
|
135
|
}
|
136
|
|
137
|
// Document representation that points to the root document node
|
138
|
if (elementNo == 1) {
|
139
|
// If no DOCTYPE declaration: docname = root element name
|
140
|
if (docname == null) {
|
141
|
docname = localName;
|
142
|
} else if (doctype == null) {
|
143
|
doctype = DBEntityResolver.doctype;
|
144
|
}
|
145
|
//DBSAXNode documentNode = (DBSAXNode)nodeStack.peek();
|
146
|
rootNode.writeNodename(docname);
|
147
|
rootNode.writeRootNodeID(rootNode.getNodeID());
|
148
|
currentDocument = new DBSAXDocument(conn, rootNode.getNodeID(),
|
149
|
docname, doctype);
|
150
|
rootNode.writeDocID(currentDocument.getDocID());
|
151
|
}
|
152
|
|
153
|
// Create the current node representation
|
154
|
currentNode = new DBSAXNode(conn, localName, parentNode, rootNode, currentDocument);
|
155
|
|
156
|
// Add all of the attributes
|
157
|
for (int i=0; i<atts.getLength(); i++)
|
158
|
{
|
159
|
|
160
|
// Use the methods getQualifiedName(), getLocalName(), getNamespace()
|
161
|
// and getExpandedName() in SAXAttrList interface to get Namespace
|
162
|
// information.
|
163
|
|
164
|
qName = atts.getQualifiedName(i);
|
165
|
localName = atts.getLocalName(i);
|
166
|
nsName = atts.getNamespace(i);
|
167
|
expName = atts.getExpandedName(i);
|
168
|
|
169
|
// You can get the type and value of the attributes either
|
170
|
// by index or by the Qualified Name.
|
171
|
|
172
|
String type = atts.getType(qName);
|
173
|
String value = atts.getValue(qName);
|
174
|
|
175
|
currentNode.setAttribute(localName, value);
|
176
|
}
|
177
|
|
178
|
// Add the node to the stack, so that any text data can be
|
179
|
// added as it is encountered
|
180
|
nodeStack.push(currentNode);
|
181
|
|
182
|
}
|
183
|
|
184
|
/** SAX Handler that is called for each XML text node */
|
185
|
public void characters(char[] cbuf, int start, int len) {
|
186
|
DBSAXNode currentNode = (DBSAXNode)nodeStack.peek();
|
187
|
String data = new String(cbuf, start, len);
|
188
|
|
189
|
// Write the content of the node to the database
|
190
|
currentNode.writeChildNodeToDB("TEXT", null, data);
|
191
|
}
|
192
|
|
193
|
/**
|
194
|
* SAX Handler that is called for each XML text node that is Ignorable
|
195
|
* white space
|
196
|
*/
|
197
|
public void ignorableWhitespace(char[] cbuf, int start, int len) {
|
198
|
}
|
199
|
|
200
|
/**
|
201
|
* SAX Handler called once for each comment found:
|
202
|
* node that comment may occur before or after the root element.
|
203
|
*/
|
204
|
public void comment(String data) throws SAXException {
|
205
|
DBSAXNode currentNode = (DBSAXNode)nodeStack.peek();
|
206
|
currentNode.writeChildNodeToDB("COMMENT", null, data);
|
207
|
}
|
208
|
|
209
|
/**
|
210
|
* SAX Handler called once for each processing instruction found:
|
211
|
* node that PI may occur before or after the root element.
|
212
|
*/
|
213
|
public void processingInstruction(String target, String data)
|
214
|
throws SAXException {
|
215
|
DBSAXNode currentNode = (DBSAXNode)nodeStack.peek();
|
216
|
currentNode.writeChildNodeToDB("PI", target, data);
|
217
|
}
|
218
|
|
219
|
/** SAX Handler that is called at the end of each XML element */
|
220
|
public void endElement(NSName name) throws SAXException {
|
221
|
// Use the methods getQualifiedName(), getLocalName(), getNamespace()
|
222
|
// and getExpandedName() in NSName interface to get Namespace
|
223
|
// information.
|
224
|
String expName = name.getExpandedName();
|
225
|
|
226
|
// Get the node from the stack
|
227
|
DBSAXNode currentNode = (DBSAXNode)nodeStack.pop();
|
228
|
}
|
229
|
|
230
|
/** Debug routine */
|
231
|
private void db(int flag) {
|
232
|
if (debug) {
|
233
|
System.err.println("DEBUG POSITION " + flag);
|
234
|
}
|
235
|
}
|
236
|
}
|