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 |
|
|
* Institution: National Center for Ecological Analysis and Synthesis
|
6 |
|
|
* Copyright: 2000
|
7 |
|
|
* Authors: Matt Jones
|
8 |
|
|
*
|
9 |
|
|
* Version: '$Id$'
|
10 |
|
|
*/
|
11 |
|
|
|
12 |
|
|
import org.xml.sax.*;
|
13 |
|
|
|
14 |
|
|
import java.sql.*;
|
15 |
|
|
import java.util.Stack;
|
16 |
|
|
|
17 |
|
|
// Extensions to the SAX Interfaces for Namespace support.
|
18 |
|
|
import oracle.xml.parser.v2.DefaultXMLDocumentHandler;
|
19 |
|
|
import oracle.xml.parser.v2.NSName;
|
20 |
|
|
import oracle.xml.parser.v2.SAXAttrList;
|
21 |
|
|
|
22 |
|
|
import oracle.xml.parser.v2.SAXParser;
|
23 |
|
|
|
24 |
|
|
class DBSAXHandler extends DefaultXMLDocumentHandler
|
25 |
|
|
{
|
26 |
|
|
|
27 |
|
|
private boolean debug = false;
|
28 |
|
|
private boolean stackCreated = false;
|
29 |
|
|
private Stack elementStack;
|
30 |
|
|
private Connection conn = null;
|
31 |
|
|
|
32 |
|
|
public DBSAXHandler(Connection conn)
|
33 |
|
|
{
|
34 |
|
|
System.out.println("\nINITIALIZING HANDLER....\n");
|
35 |
|
|
|
36 |
|
|
this.conn = conn;
|
37 |
|
|
|
38 |
|
|
// Create the stack for keeping track of element context
|
39 |
|
|
// if it doesn't already exist
|
40 |
|
|
if (!stackCreated) {
|
41 |
|
|
elementStack = new Stack();
|
42 |
|
|
stackCreated = true;
|
43 |
|
|
}
|
44 |
|
|
|
45 |
|
|
}
|
46 |
|
|
|
47 |
|
|
public void startElement(NSName name, SAXAttrList atts) throws SAXException
|
48 |
|
|
{
|
49 |
|
|
|
50 |
|
|
// Use the methods getQualifiedName(), getLocalName(), getNamespace()
|
51 |
|
|
// and getExpandedName() in NSName interface to get Namespace
|
52 |
|
|
// information.
|
53 |
|
|
|
54 |
|
|
String qName;
|
55 |
|
|
String localName;
|
56 |
|
|
String nsName;
|
57 |
|
|
String expName;
|
58 |
|
|
DBSAXElement currentElement;
|
59 |
|
|
|
60 |
|
|
qName = name.getQualifiedName();
|
61 |
|
|
localName = name.getLocalName();
|
62 |
|
|
nsName = name.getNamespace();
|
63 |
|
|
expName = name.getExpandedName();
|
64 |
|
|
|
65 |
|
|
// Create the current element representation
|
66 |
|
|
currentElement = new DBSAXElement(conn, 1, localName, false, 0);
|
67 |
|
|
System.out.println("Element created:" + currentElement.getTagName());
|
68 |
|
|
|
69 |
|
|
// Add all of the attributes
|
70 |
|
|
for (int i=0; i<atts.getLength(); i++)
|
71 |
|
|
{
|
72 |
|
|
|
73 |
|
|
// Use the methods getQualifiedName(), getLocalName(), getNamespace()
|
74 |
|
|
// and getExpandedName() in SAXAttrList interface to get Namespace
|
75 |
|
|
// information.
|
76 |
|
|
|
77 |
|
|
qName = atts.getQualifiedName(i);
|
78 |
|
|
localName = atts.getLocalName(i);
|
79 |
|
|
nsName = atts.getNamespace(i);
|
80 |
|
|
expName = atts.getExpandedName(i);
|
81 |
|
|
|
82 |
|
|
// You can get the type and value of the attributes either
|
83 |
|
|
// by index or by the Qualified Name.
|
84 |
|
|
|
85 |
|
|
String type = atts.getType(qName);
|
86 |
|
|
String value = atts.getValue(qName);
|
87 |
|
|
|
88 |
|
|
currentElement.setAttribute(localName, value);
|
89 |
|
|
|
90 |
|
|
System.out.println(" Added ATTRIBUTE Name:" + localName);
|
91 |
|
|
System.out.println(" Type:" + type);
|
92 |
|
|
System.out.println(" Value:" + value);
|
93 |
|
|
System.out.println();
|
94 |
|
|
|
95 |
|
|
}
|
96 |
|
|
|
97 |
|
|
// Add the element to the stack, so that any text data can be
|
98 |
|
|
// added as it is encountered
|
99 |
|
|
elementStack.push(currentElement);
|
100 |
|
|
|
101 |
|
|
}
|
102 |
|
|
|
103 |
|
|
public void characters(char[] cbuf, int start, int len)
|
104 |
|
|
{
|
105 |
|
|
DBSAXElement currentElement = (DBSAXElement)elementStack.peek();
|
106 |
|
|
currentElement.appendContent(cbuf,start,len);
|
107 |
|
|
System.out.println(" Characters: " + new String(cbuf,start,len));
|
108 |
|
|
System.out.println(" Appended: " + currentElement.getContent());
|
109 |
|
|
}
|
110 |
|
|
|
111 |
|
|
public void ignorableWhitespace(char[] cbuf, int start, int len)
|
112 |
|
|
{
|
113 |
|
|
System.out.println("IgnorableWhiteSpace");
|
114 |
|
|
}
|
115 |
|
|
|
116 |
|
|
public void endElement(NSName name) throws SAXException
|
117 |
|
|
{
|
118 |
|
|
// Use the methods getQualifiedName(), getLocalName(), getNamespace()
|
119 |
|
|
// and getExpandedName() in NSName interface to get Namespace
|
120 |
|
|
// information.
|
121 |
|
|
|
122 |
|
|
String expName = name.getExpandedName();
|
123 |
|
|
|
124 |
|
|
// Remove the element from the stack
|
125 |
|
|
DBSAXElement currentElement = (DBSAXElement)elementStack.pop();
|
126 |
|
|
|
127 |
|
|
System.out.println("Final content: " + currentElement.getContent());
|
128 |
|
|
System.out.println("Closing element: " + expName);
|
129 |
|
|
}
|
130 |
|
|
|
131 |
|
|
/** Debug routine */
|
132 |
|
|
private void db(int flag) {
|
133 |
|
|
if (debug) {
|
134 |
|
|
System.err.println("DEBUG POSITION " + flag);
|
135 |
|
|
}
|
136 |
|
|
}
|
137 |
|
|
}
|