1 |
17
|
jones
|
/**
|
2 |
|
|
* Name: DBSAXHandler.java
|
3 |
|
|
* Purpose: A Class that handles the SAX XML events as they
|
4 |
|
|
* are generated from XML documents
|
5 |
35
|
jones
|
* Copyright: 2000 Regents of the University of California and the
|
6 |
|
|
* National Center for Ecological Analysis and Synthesis
|
7 |
17
|
jones
|
* Authors: Matt Jones
|
8 |
|
|
*
|
9 |
|
|
* Version: '$Id$'
|
10 |
|
|
*/
|
11 |
|
|
|
12 |
74
|
jones
|
package edu.ucsb.nceas.metacat;
|
13 |
51
|
jones
|
|
14 |
17
|
jones
|
import org.xml.sax.*;
|
15 |
|
|
|
16 |
|
|
import java.sql.*;
|
17 |
|
|
import java.util.Stack;
|
18 |
18
|
jones
|
import java.util.EmptyStackException;
|
19 |
17
|
jones
|
|
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 |
72
|
bojilova
|
import oracle.xml.parser.v2.DTD;
|
27 |
17
|
jones
|
|
28 |
31
|
jones
|
/**
|
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 |
29
|
jones
|
public class DBSAXHandler extends DefaultXMLDocumentHandler
|
33 |
17
|
jones
|
{
|
34 |
|
|
|
35 |
72
|
bojilova
|
static int elementNo = 0;
|
36 |
|
|
private String docname;
|
37 |
|
|
private String doctype;
|
38 |
|
|
private String systemid;
|
39 |
17
|
jones
|
private boolean debug = false;
|
40 |
|
|
private boolean stackCreated = false;
|
41 |
|
|
private Stack elementStack;
|
42 |
|
|
private Connection conn = null;
|
43 |
|
|
|
44 |
31
|
jones
|
/** Construct an instance of the handler class
|
45 |
|
|
*
|
46 |
|
|
* @param conn the JDBC connection to which information is written
|
47 |
|
|
*/
|
48 |
17
|
jones
|
public DBSAXHandler(Connection conn)
|
49 |
|
|
{
|
50 |
|
|
this.conn = conn;
|
51 |
|
|
|
52 |
|
|
// Create the stack for keeping track of element context
|
53 |
|
|
// if it doesn't already exist
|
54 |
|
|
if (!stackCreated) {
|
55 |
|
|
elementStack = new Stack();
|
56 |
|
|
stackCreated = true;
|
57 |
|
|
}
|
58 |
|
|
|
59 |
|
|
}
|
60 |
|
|
|
61 |
72
|
bojilova
|
/** SAX Handler that receives notification of beginning of the document */
|
62 |
|
|
public void startDocument() throws SAXException
|
63 |
|
|
{
|
64 |
|
|
System.out.println("start Document");
|
65 |
|
|
}
|
66 |
|
|
|
67 |
|
|
/** SAX Handler that receives notification of end of the document */
|
68 |
|
|
public void endDocument() throws SAXException
|
69 |
|
|
{
|
70 |
|
|
System.out.println("end Document");
|
71 |
|
|
}
|
72 |
|
|
|
73 |
|
|
/** SAX Handler that receives notification of DTD. Sets the DTD */
|
74 |
|
|
public void setDoctype(DTD dtd) throws SAXException
|
75 |
|
|
{
|
76 |
|
|
// here is a bug: dtd.getPublicId() and dtd.getSustemId() return null.
|
77 |
|
|
docname = dtd.getName();
|
78 |
|
|
doctype = dtd.getPublicId();
|
79 |
|
|
systemid = dtd.getSystemId();
|
80 |
|
|
System.out.println("DOCTYPE: " + docname);
|
81 |
|
|
System.out.println("DOCTYPE: " + doctype);
|
82 |
|
|
System.out.println("DOCTYPE: " + systemid);
|
83 |
|
|
}
|
84 |
|
|
|
85 |
|
|
/** SAX Handler that receives notification of end of DTD
|
86 |
|
|
* All events in DTDHandler about all unparsed entities and the event in EntityResolver for the DTD file declaration
|
87 |
|
|
* appear between setDoctype and endDoctype.
|
88 |
|
|
* The rest of parsable external entities inside DTD file appear later in the elements from where they are referred to. */
|
89 |
|
|
public void endDoctype() throws SAXException
|
90 |
|
|
{
|
91 |
|
|
System.out.println("end of DOCTYPE");
|
92 |
|
|
if (doctype == null)
|
93 |
|
|
doctype = DBEntityResolver.doctype;
|
94 |
|
|
}
|
95 |
|
|
|
96 |
31
|
jones
|
/** SAX Handler that is called at the start of each XML element */
|
97 |
17
|
jones
|
public void startElement(NSName name, SAXAttrList atts) throws SAXException
|
98 |
|
|
{
|
99 |
|
|
|
100 |
|
|
// Use the methods getQualifiedName(), getLocalName(), getNamespace()
|
101 |
|
|
// and getExpandedName() in NSName interface to get Namespace
|
102 |
|
|
// information.
|
103 |
|
|
|
104 |
|
|
String qName;
|
105 |
|
|
String localName;
|
106 |
|
|
String nsName;
|
107 |
|
|
String expName;
|
108 |
18
|
jones
|
DBSAXElement parentElement;
|
109 |
17
|
jones
|
DBSAXElement currentElement;
|
110 |
|
|
|
111 |
|
|
qName = name.getQualifiedName();
|
112 |
|
|
localName = name.getLocalName();
|
113 |
|
|
nsName = name.getNamespace();
|
114 |
|
|
expName = name.getExpandedName();
|
115 |
|
|
|
116 |
72
|
bojilova
|
elementNo++;
|
117 |
|
|
if ((elementNo == 1) && (doctype == null))
|
118 |
|
|
throw new SAXException("No DOCTYPE declaration or PUBLIC ID provided");
|
119 |
18
|
jones
|
// Get a reference to the parent element for the id
|
120 |
|
|
long parent_id;
|
121 |
72
|
bojilova
|
int nodeIndex;
|
122 |
18
|
jones
|
try {
|
123 |
|
|
parentElement = (DBSAXElement)elementStack.peek();
|
124 |
|
|
parent_id = parentElement.getElementID();
|
125 |
72
|
bojilova
|
nodeIndex = parentElement.incChildNum();
|
126 |
18
|
jones
|
} catch (EmptyStackException e) {
|
127 |
|
|
parent_id = 0;
|
128 |
72
|
bojilova
|
nodeIndex = 0;
|
129 |
18
|
jones
|
}
|
130 |
|
|
|
131 |
17
|
jones
|
// Create the current element representation
|
132 |
72
|
bojilova
|
currentElement = new DBSAXElement(conn, localName, parent_id, nodeIndex);
|
133 |
|
|
// go to create document definition in the db
|
134 |
|
|
// call once after insertion of the first element
|
135 |
|
|
if (parent_id == 0) {
|
136 |
|
|
long rootnodeid = currentElement.getElementID();
|
137 |
|
|
new DBSAXDocument(conn, rootnodeid, docname, doctype);
|
138 |
|
|
}
|
139 |
17
|
jones
|
|
140 |
|
|
// Add all of the attributes
|
141 |
|
|
for (int i=0; i<atts.getLength(); i++)
|
142 |
|
|
{
|
143 |
|
|
|
144 |
|
|
// Use the methods getQualifiedName(), getLocalName(), getNamespace()
|
145 |
|
|
// and getExpandedName() in SAXAttrList interface to get Namespace
|
146 |
|
|
// information.
|
147 |
|
|
|
148 |
|
|
qName = atts.getQualifiedName(i);
|
149 |
|
|
localName = atts.getLocalName(i);
|
150 |
|
|
nsName = atts.getNamespace(i);
|
151 |
|
|
expName = atts.getExpandedName(i);
|
152 |
|
|
|
153 |
|
|
// You can get the type and value of the attributes either
|
154 |
|
|
// by index or by the Qualified Name.
|
155 |
|
|
|
156 |
|
|
String type = atts.getType(qName);
|
157 |
|
|
String value = atts.getValue(qName);
|
158 |
|
|
|
159 |
|
|
currentElement.setAttribute(localName, value);
|
160 |
|
|
}
|
161 |
|
|
|
162 |
|
|
// Add the element to the stack, so that any text data can be
|
163 |
|
|
// added as it is encountered
|
164 |
|
|
elementStack.push(currentElement);
|
165 |
|
|
|
166 |
|
|
}
|
167 |
|
|
|
168 |
31
|
jones
|
/** SAX Handler that is called for each XML text node */
|
169 |
17
|
jones
|
public void characters(char[] cbuf, int start, int len)
|
170 |
|
|
{
|
171 |
|
|
DBSAXElement currentElement = (DBSAXElement)elementStack.peek();
|
172 |
|
|
currentElement.appendContent(cbuf,start,len);
|
173 |
|
|
}
|
174 |
|
|
|
175 |
31
|
jones
|
/**
|
176 |
|
|
* SAX Handler that is called for each XML text node that is Ignorable
|
177 |
|
|
* white space
|
178 |
|
|
*/
|
179 |
17
|
jones
|
public void ignorableWhitespace(char[] cbuf, int start, int len)
|
180 |
|
|
{
|
181 |
|
|
}
|
182 |
|
|
|
183 |
72
|
bojilova
|
/** SAX Handler called once for each comment found:
|
184 |
|
|
* node that comment may occur before or after the root element.
|
185 |
|
|
* For now works only for comments after the root element. */
|
186 |
|
|
public void comment(String data) throws SAXException
|
187 |
|
|
{
|
188 |
|
|
if (elementNo > 0) {
|
189 |
|
|
DBSAXElement currentElement = (DBSAXElement)elementStack.peek();
|
190 |
|
|
currentElement.writeCommentToDB(data);
|
191 |
|
|
}
|
192 |
|
|
}
|
193 |
|
|
|
194 |
31
|
jones
|
/** SAX Handler that is called at the end of each XML element */
|
195 |
17
|
jones
|
public void endElement(NSName name) throws SAXException
|
196 |
|
|
{
|
197 |
|
|
// Use the methods getQualifiedName(), getLocalName(), getNamespace()
|
198 |
|
|
// and getExpandedName() in NSName interface to get Namespace
|
199 |
|
|
// information.
|
200 |
|
|
String expName = name.getExpandedName();
|
201 |
|
|
|
202 |
18
|
jones
|
// Get the element from the stack
|
203 |
17
|
jones
|
DBSAXElement currentElement = (DBSAXElement)elementStack.pop();
|
204 |
18
|
jones
|
|
205 |
|
|
// Write the content of the element to the database
|
206 |
|
|
currentElement.writeContentToDB();
|
207 |
17
|
jones
|
}
|
208 |
|
|
|
209 |
|
|
/** Debug routine */
|
210 |
|
|
private void db(int flag) {
|
211 |
|
|
if (debug) {
|
212 |
|
|
System.err.println("DEBUG POSITION " + flag);
|
213 |
|
|
}
|
214 |
|
|
}
|
215 |
|
|
}
|