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 |
51
|
jones
|
package edu.ucsb.nceas.metacat;
|
13 |
|
|
|
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 |
|
|
|
27 |
31
|
jones
|
/**
|
28 |
|
|
* A database aware Class implementing callback bethods for the SAX parser to
|
29 |
|
|
* call when processing the XML stream and generating events
|
30 |
|
|
*/
|
31 |
29
|
jones
|
public class DBSAXHandler extends DefaultXMLDocumentHandler
|
32 |
17
|
jones
|
{
|
33 |
|
|
|
34 |
|
|
private boolean debug = false;
|
35 |
|
|
private boolean stackCreated = false;
|
36 |
|
|
private Stack elementStack;
|
37 |
|
|
private Connection conn = null;
|
38 |
|
|
|
39 |
31
|
jones
|
/** Construct an instance of the handler class
|
40 |
|
|
*
|
41 |
|
|
* @param conn the JDBC connection to which information is written
|
42 |
|
|
*/
|
43 |
17
|
jones
|
public DBSAXHandler(Connection conn)
|
44 |
|
|
{
|
45 |
|
|
this.conn = conn;
|
46 |
|
|
|
47 |
|
|
// Create the stack for keeping track of element context
|
48 |
|
|
// if it doesn't already exist
|
49 |
|
|
if (!stackCreated) {
|
50 |
|
|
elementStack = new Stack();
|
51 |
|
|
stackCreated = true;
|
52 |
|
|
}
|
53 |
|
|
|
54 |
|
|
}
|
55 |
|
|
|
56 |
31
|
jones
|
/** SAX Handler that is called at the start of each XML element */
|
57 |
17
|
jones
|
public void startElement(NSName name, SAXAttrList atts) throws SAXException
|
58 |
|
|
{
|
59 |
|
|
|
60 |
|
|
// Use the methods getQualifiedName(), getLocalName(), getNamespace()
|
61 |
|
|
// and getExpandedName() in NSName interface to get Namespace
|
62 |
|
|
// information.
|
63 |
|
|
|
64 |
|
|
String qName;
|
65 |
|
|
String localName;
|
66 |
|
|
String nsName;
|
67 |
|
|
String expName;
|
68 |
18
|
jones
|
DBSAXElement parentElement;
|
69 |
17
|
jones
|
DBSAXElement currentElement;
|
70 |
|
|
|
71 |
|
|
qName = name.getQualifiedName();
|
72 |
|
|
localName = name.getLocalName();
|
73 |
|
|
nsName = name.getNamespace();
|
74 |
|
|
expName = name.getExpandedName();
|
75 |
|
|
|
76 |
18
|
jones
|
// Get a reference to the parent element for the id
|
77 |
|
|
long parent_id;
|
78 |
|
|
try {
|
79 |
|
|
parentElement = (DBSAXElement)elementStack.peek();
|
80 |
|
|
parent_id = parentElement.getElementID();
|
81 |
|
|
} catch (EmptyStackException e) {
|
82 |
|
|
parent_id = 0;
|
83 |
|
|
}
|
84 |
|
|
|
85 |
17
|
jones
|
// Create the current element representation
|
86 |
18
|
jones
|
currentElement = new DBSAXElement(conn, localName, parent_id);
|
87 |
17
|
jones
|
|
88 |
|
|
// Add all of the attributes
|
89 |
|
|
for (int i=0; i<atts.getLength(); i++)
|
90 |
|
|
{
|
91 |
|
|
|
92 |
|
|
// Use the methods getQualifiedName(), getLocalName(), getNamespace()
|
93 |
|
|
// and getExpandedName() in SAXAttrList interface to get Namespace
|
94 |
|
|
// information.
|
95 |
|
|
|
96 |
|
|
qName = atts.getQualifiedName(i);
|
97 |
|
|
localName = atts.getLocalName(i);
|
98 |
|
|
nsName = atts.getNamespace(i);
|
99 |
|
|
expName = atts.getExpandedName(i);
|
100 |
|
|
|
101 |
|
|
// You can get the type and value of the attributes either
|
102 |
|
|
// by index or by the Qualified Name.
|
103 |
|
|
|
104 |
|
|
String type = atts.getType(qName);
|
105 |
|
|
String value = atts.getValue(qName);
|
106 |
|
|
|
107 |
|
|
currentElement.setAttribute(localName, value);
|
108 |
|
|
}
|
109 |
|
|
|
110 |
|
|
// Add the element to the stack, so that any text data can be
|
111 |
|
|
// added as it is encountered
|
112 |
|
|
elementStack.push(currentElement);
|
113 |
|
|
|
114 |
|
|
}
|
115 |
|
|
|
116 |
31
|
jones
|
/** SAX Handler that is called for each XML text node */
|
117 |
17
|
jones
|
public void characters(char[] cbuf, int start, int len)
|
118 |
|
|
{
|
119 |
|
|
DBSAXElement currentElement = (DBSAXElement)elementStack.peek();
|
120 |
|
|
currentElement.appendContent(cbuf,start,len);
|
121 |
|
|
}
|
122 |
|
|
|
123 |
31
|
jones
|
/**
|
124 |
|
|
* SAX Handler that is called for each XML text node that is Ignorable
|
125 |
|
|
* white space
|
126 |
|
|
*/
|
127 |
17
|
jones
|
public void ignorableWhitespace(char[] cbuf, int start, int len)
|
128 |
|
|
{
|
129 |
|
|
}
|
130 |
|
|
|
131 |
31
|
jones
|
/** SAX Handler that is called at the end of each XML element */
|
132 |
17
|
jones
|
public void endElement(NSName name) throws SAXException
|
133 |
|
|
{
|
134 |
|
|
// Use the methods getQualifiedName(), getLocalName(), getNamespace()
|
135 |
|
|
// and getExpandedName() in NSName interface to get Namespace
|
136 |
|
|
// information.
|
137 |
|
|
String expName = name.getExpandedName();
|
138 |
|
|
|
139 |
18
|
jones
|
// Get the element from the stack
|
140 |
17
|
jones
|
DBSAXElement currentElement = (DBSAXElement)elementStack.pop();
|
141 |
18
|
jones
|
|
142 |
|
|
// Write the content of the element to the database
|
143 |
|
|
currentElement.writeContentToDB();
|
144 |
17
|
jones
|
}
|
145 |
|
|
|
146 |
|
|
/** Debug routine */
|
147 |
|
|
private void db(int flag) {
|
148 |
|
|
if (debug) {
|
149 |
|
|
System.err.println("DEBUG POSITION " + flag);
|
150 |
|
|
}
|
151 |
|
|
}
|
152 |
|
|
}
|