Revision 17
Added by Matt Jones almost 25 years ago
DBSAXHandler.java | ||
---|---|---|
1 |
/** |
|
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 |
} |
|
0 | 138 |
src/edu/ucsb/nceas/metacat/DBSAXHandler.java | ||
---|---|---|
1 |
/** |
|
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 |
} |
|
0 | 138 |
src/edu/ucsb/nceas/metacat/DBWriter.java | ||
---|---|---|
81 | 81 |
|
82 | 82 |
// Use the XMLDocumentHandler interface for namespace support |
83 | 83 |
// instead of org.xml.sax.DocumentHandler |
84 |
XMLDocumentHandler xmlDocHandler = new XMLDocumentHandlerImpl(conn);
|
|
84 |
XMLDocumentHandler xmlDocHandler = new DBSAXHandler(conn);
|
|
85 | 85 |
|
86 | 86 |
// For all the other interface use the default provided by |
87 | 87 |
// Handler base |
... | ... | |
149 | 149 |
} |
150 | 150 |
|
151 | 151 |
} |
152 |
|
|
153 |
/**************************************************************** |
|
154 |
* Implementation of XMLDocumentHandler interface. * |
|
155 |
* Other interfaces are implemented in the class HandlerBase. * |
|
156 |
****************************************************************/ |
|
157 |
|
|
158 |
class XMLDocumentHandlerImpl extends DefaultXMLDocumentHandler |
|
159 |
{ |
|
160 |
|
|
161 |
private boolean debug = false; |
|
162 |
private boolean stackCreated = false; |
|
163 |
private Stack elementStack; |
|
164 |
private Connection conn = null; |
|
165 |
|
|
166 |
public XMLDocumentHandlerImpl(Connection conn) |
|
167 |
{ |
|
168 |
System.out.println("\nINITIALIZING HANDLER....\n"); |
|
169 |
|
|
170 |
this.conn = conn; |
|
171 |
|
|
172 |
// Create the stack for keeping track of element context |
|
173 |
// if it doesn't already exist |
|
174 |
if (!stackCreated) { |
|
175 |
elementStack = new Stack(); |
|
176 |
stackCreated = true; |
|
177 |
} |
|
178 |
|
|
179 |
} |
|
180 |
|
|
181 |
public void startElement(NSName name, SAXAttrList atts) throws SAXException |
|
182 |
{ |
|
183 |
|
|
184 |
// Use the methods getQualifiedName(), getLocalName(), getNamespace() |
|
185 |
// and getExpandedName() in NSName interface to get Namespace |
|
186 |
// information. |
|
187 |
|
|
188 |
String qName; |
|
189 |
String localName; |
|
190 |
String nsName; |
|
191 |
String expName; |
|
192 |
DBSAXElement currentElement; |
|
193 |
|
|
194 |
qName = name.getQualifiedName(); |
|
195 |
localName = name.getLocalName(); |
|
196 |
nsName = name.getNamespace(); |
|
197 |
expName = name.getExpandedName(); |
|
198 |
|
|
199 |
// Create the current element representation |
|
200 |
currentElement = new DBSAXElement(conn, 1, localName, false, 0); |
|
201 |
System.out.println("Element created:" + currentElement.getTagName()); |
|
202 |
|
|
203 |
// Add all of the attributes |
|
204 |
for (int i=0; i<atts.getLength(); i++) |
|
205 |
{ |
|
206 |
|
|
207 |
// Use the methods getQualifiedName(), getLocalName(), getNamespace() |
|
208 |
// and getExpandedName() in SAXAttrList interface to get Namespace |
|
209 |
// information. |
|
210 |
|
|
211 |
qName = atts.getQualifiedName(i); |
|
212 |
localName = atts.getLocalName(i); |
|
213 |
nsName = atts.getNamespace(i); |
|
214 |
expName = atts.getExpandedName(i); |
|
215 |
|
|
216 |
// You can get the type and value of the attributes either |
|
217 |
// by index or by the Qualified Name. |
|
218 |
|
|
219 |
String type = atts.getType(qName); |
|
220 |
String value = atts.getValue(qName); |
|
221 |
|
|
222 |
currentElement.setAttribute(localName, value); |
|
223 |
|
|
224 |
System.out.println(" Added ATTRIBUTE Name:" + localName); |
|
225 |
System.out.println(" Type:" + type); |
|
226 |
System.out.println(" Value:" + value); |
|
227 |
System.out.println(); |
|
228 |
|
|
229 |
} |
|
230 |
|
|
231 |
// Add the element to the stack, so that any text data can be |
|
232 |
// added as it is encountered |
|
233 |
elementStack.push(currentElement); |
|
234 |
|
|
235 |
} |
|
236 |
|
|
237 |
public void characters(char[] cbuf, int start, int len) |
|
238 |
{ |
|
239 |
DBSAXElement currentElement = (DBSAXElement)elementStack.peek(); |
|
240 |
currentElement.appendContent(cbuf,start,len); |
|
241 |
System.out.println(" Characters: " + new String(cbuf,start,len)); |
|
242 |
System.out.println(" Appended: " + currentElement.getContent()); |
|
243 |
} |
|
244 |
|
|
245 |
public void ignorableWhitespace(char[] cbuf, int start, int len) |
|
246 |
{ |
|
247 |
System.out.println("IgnorableWhiteSpace"); |
|
248 |
} |
|
249 |
|
|
250 |
public void endElement(NSName name) throws SAXException |
|
251 |
{ |
|
252 |
// Use the methods getQualifiedName(), getLocalName(), getNamespace() |
|
253 |
// and getExpandedName() in NSName interface to get Namespace |
|
254 |
// information. |
|
255 |
|
|
256 |
String expName = name.getExpandedName(); |
|
257 |
|
|
258 |
// Remove the element from the stack |
|
259 |
DBSAXElement currentElement = (DBSAXElement)elementStack.pop(); |
|
260 |
|
|
261 |
System.out.println("Final content: " + currentElement.getContent()); |
|
262 |
System.out.println("Closing element: " + expName); |
|
263 |
} |
|
264 |
|
|
265 |
/** Debug routine */ |
|
266 |
private void db(int flag) { |
|
267 |
if (debug) { |
|
268 |
System.err.println("DEBUG POSITION " + flag); |
|
269 |
} |
|
270 |
} |
|
271 |
} |
src/edu/ucsb/nceas/metacat/DBSAXWriter.java | ||
---|---|---|
81 | 81 |
|
82 | 82 |
// Use the XMLDocumentHandler interface for namespace support |
83 | 83 |
// instead of org.xml.sax.DocumentHandler |
84 |
XMLDocumentHandler xmlDocHandler = new XMLDocumentHandlerImpl(conn);
|
|
84 |
XMLDocumentHandler xmlDocHandler = new DBSAXHandler(conn);
|
|
85 | 85 |
|
86 | 86 |
// For all the other interface use the default provided by |
87 | 87 |
// Handler base |
... | ... | |
149 | 149 |
} |
150 | 150 |
|
151 | 151 |
} |
152 |
|
|
153 |
/**************************************************************** |
|
154 |
* Implementation of XMLDocumentHandler interface. * |
|
155 |
* Other interfaces are implemented in the class HandlerBase. * |
|
156 |
****************************************************************/ |
|
157 |
|
|
158 |
class XMLDocumentHandlerImpl extends DefaultXMLDocumentHandler |
|
159 |
{ |
|
160 |
|
|
161 |
private boolean debug = false; |
|
162 |
private boolean stackCreated = false; |
|
163 |
private Stack elementStack; |
|
164 |
private Connection conn = null; |
|
165 |
|
|
166 |
public XMLDocumentHandlerImpl(Connection conn) |
|
167 |
{ |
|
168 |
System.out.println("\nINITIALIZING HANDLER....\n"); |
|
169 |
|
|
170 |
this.conn = conn; |
|
171 |
|
|
172 |
// Create the stack for keeping track of element context |
|
173 |
// if it doesn't already exist |
|
174 |
if (!stackCreated) { |
|
175 |
elementStack = new Stack(); |
|
176 |
stackCreated = true; |
|
177 |
} |
|
178 |
|
|
179 |
} |
|
180 |
|
|
181 |
public void startElement(NSName name, SAXAttrList atts) throws SAXException |
|
182 |
{ |
|
183 |
|
|
184 |
// Use the methods getQualifiedName(), getLocalName(), getNamespace() |
|
185 |
// and getExpandedName() in NSName interface to get Namespace |
|
186 |
// information. |
|
187 |
|
|
188 |
String qName; |
|
189 |
String localName; |
|
190 |
String nsName; |
|
191 |
String expName; |
|
192 |
DBSAXElement currentElement; |
|
193 |
|
|
194 |
qName = name.getQualifiedName(); |
|
195 |
localName = name.getLocalName(); |
|
196 |
nsName = name.getNamespace(); |
|
197 |
expName = name.getExpandedName(); |
|
198 |
|
|
199 |
// Create the current element representation |
|
200 |
currentElement = new DBSAXElement(conn, 1, localName, false, 0); |
|
201 |
System.out.println("Element created:" + currentElement.getTagName()); |
|
202 |
|
|
203 |
// Add all of the attributes |
|
204 |
for (int i=0; i<atts.getLength(); i++) |
|
205 |
{ |
|
206 |
|
|
207 |
// Use the methods getQualifiedName(), getLocalName(), getNamespace() |
|
208 |
// and getExpandedName() in SAXAttrList interface to get Namespace |
|
209 |
// information. |
|
210 |
|
|
211 |
qName = atts.getQualifiedName(i); |
|
212 |
localName = atts.getLocalName(i); |
|
213 |
nsName = atts.getNamespace(i); |
|
214 |
expName = atts.getExpandedName(i); |
|
215 |
|
|
216 |
// You can get the type and value of the attributes either |
|
217 |
// by index or by the Qualified Name. |
|
218 |
|
|
219 |
String type = atts.getType(qName); |
|
220 |
String value = atts.getValue(qName); |
|
221 |
|
|
222 |
currentElement.setAttribute(localName, value); |
|
223 |
|
|
224 |
System.out.println(" Added ATTRIBUTE Name:" + localName); |
|
225 |
System.out.println(" Type:" + type); |
|
226 |
System.out.println(" Value:" + value); |
|
227 |
System.out.println(); |
|
228 |
|
|
229 |
} |
|
230 |
|
|
231 |
// Add the element to the stack, so that any text data can be |
|
232 |
// added as it is encountered |
|
233 |
elementStack.push(currentElement); |
|
234 |
|
|
235 |
} |
|
236 |
|
|
237 |
public void characters(char[] cbuf, int start, int len) |
|
238 |
{ |
|
239 |
DBSAXElement currentElement = (DBSAXElement)elementStack.peek(); |
|
240 |
currentElement.appendContent(cbuf,start,len); |
|
241 |
System.out.println(" Characters: " + new String(cbuf,start,len)); |
|
242 |
System.out.println(" Appended: " + currentElement.getContent()); |
|
243 |
} |
|
244 |
|
|
245 |
public void ignorableWhitespace(char[] cbuf, int start, int len) |
|
246 |
{ |
|
247 |
System.out.println("IgnorableWhiteSpace"); |
|
248 |
} |
|
249 |
|
|
250 |
public void endElement(NSName name) throws SAXException |
|
251 |
{ |
|
252 |
// Use the methods getQualifiedName(), getLocalName(), getNamespace() |
|
253 |
// and getExpandedName() in NSName interface to get Namespace |
|
254 |
// information. |
|
255 |
|
|
256 |
String expName = name.getExpandedName(); |
|
257 |
|
|
258 |
// Remove the element from the stack |
|
259 |
DBSAXElement currentElement = (DBSAXElement)elementStack.pop(); |
|
260 |
|
|
261 |
System.out.println("Final content: " + currentElement.getContent()); |
|
262 |
System.out.println("Closing element: " + expName); |
|
263 |
} |
|
264 |
|
|
265 |
/** Debug routine */ |
|
266 |
private void db(int flag) { |
|
267 |
if (debug) { |
|
268 |
System.err.println("DEBUG POSITION " + flag); |
|
269 |
} |
|
270 |
} |
|
271 |
} |
Makefile | ||
---|---|---|
8 | 8 |
orasax: |
9 | 9 |
javac -classpath "$(CPATH)" \ |
10 | 10 |
DBSAXWriter.java \ |
11 |
DBSAXElement.java |
|
11 |
DBSAXElement.java \ |
|
12 |
DBSAXHandler.java |
|
12 | 13 |
|
13 | 14 |
sundom: |
14 | 15 |
javac -classpath "$(CPATH)" \ |
DBSAXWriter.java | ||
---|---|---|
81 | 81 |
|
82 | 82 |
// Use the XMLDocumentHandler interface for namespace support |
83 | 83 |
// instead of org.xml.sax.DocumentHandler |
84 |
XMLDocumentHandler xmlDocHandler = new XMLDocumentHandlerImpl(conn);
|
|
84 |
XMLDocumentHandler xmlDocHandler = new DBSAXHandler(conn);
|
|
85 | 85 |
|
86 | 86 |
// For all the other interface use the default provided by |
87 | 87 |
// Handler base |
... | ... | |
149 | 149 |
} |
150 | 150 |
|
151 | 151 |
} |
152 |
|
|
153 |
/**************************************************************** |
|
154 |
* Implementation of XMLDocumentHandler interface. * |
|
155 |
* Other interfaces are implemented in the class HandlerBase. * |
|
156 |
****************************************************************/ |
|
157 |
|
|
158 |
class XMLDocumentHandlerImpl extends DefaultXMLDocumentHandler |
|
159 |
{ |
|
160 |
|
|
161 |
private boolean debug = false; |
|
162 |
private boolean stackCreated = false; |
|
163 |
private Stack elementStack; |
|
164 |
private Connection conn = null; |
|
165 |
|
|
166 |
public XMLDocumentHandlerImpl(Connection conn) |
|
167 |
{ |
|
168 |
System.out.println("\nINITIALIZING HANDLER....\n"); |
|
169 |
|
|
170 |
this.conn = conn; |
|
171 |
|
|
172 |
// Create the stack for keeping track of element context |
|
173 |
// if it doesn't already exist |
|
174 |
if (!stackCreated) { |
|
175 |
elementStack = new Stack(); |
|
176 |
stackCreated = true; |
|
177 |
} |
|
178 |
|
|
179 |
} |
|
180 |
|
|
181 |
public void startElement(NSName name, SAXAttrList atts) throws SAXException |
|
182 |
{ |
|
183 |
|
|
184 |
// Use the methods getQualifiedName(), getLocalName(), getNamespace() |
|
185 |
// and getExpandedName() in NSName interface to get Namespace |
|
186 |
// information. |
|
187 |
|
|
188 |
String qName; |
|
189 |
String localName; |
|
190 |
String nsName; |
|
191 |
String expName; |
|
192 |
DBSAXElement currentElement; |
|
193 |
|
|
194 |
qName = name.getQualifiedName(); |
|
195 |
localName = name.getLocalName(); |
|
196 |
nsName = name.getNamespace(); |
|
197 |
expName = name.getExpandedName(); |
|
198 |
|
|
199 |
// Create the current element representation |
|
200 |
currentElement = new DBSAXElement(conn, 1, localName, false, 0); |
|
201 |
System.out.println("Element created:" + currentElement.getTagName()); |
|
202 |
|
|
203 |
// Add all of the attributes |
|
204 |
for (int i=0; i<atts.getLength(); i++) |
|
205 |
{ |
|
206 |
|
|
207 |
// Use the methods getQualifiedName(), getLocalName(), getNamespace() |
|
208 |
// and getExpandedName() in SAXAttrList interface to get Namespace |
|
209 |
// information. |
|
210 |
|
|
211 |
qName = atts.getQualifiedName(i); |
|
212 |
localName = atts.getLocalName(i); |
|
213 |
nsName = atts.getNamespace(i); |
|
214 |
expName = atts.getExpandedName(i); |
|
215 |
|
|
216 |
// You can get the type and value of the attributes either |
|
217 |
// by index or by the Qualified Name. |
|
218 |
|
|
219 |
String type = atts.getType(qName); |
|
220 |
String value = atts.getValue(qName); |
|
221 |
|
|
222 |
currentElement.setAttribute(localName, value); |
|
223 |
|
|
224 |
System.out.println(" Added ATTRIBUTE Name:" + localName); |
|
225 |
System.out.println(" Type:" + type); |
|
226 |
System.out.println(" Value:" + value); |
|
227 |
System.out.println(); |
|
228 |
|
|
229 |
} |
|
230 |
|
|
231 |
// Add the element to the stack, so that any text data can be |
|
232 |
// added as it is encountered |
|
233 |
elementStack.push(currentElement); |
|
234 |
|
|
235 |
} |
|
236 |
|
|
237 |
public void characters(char[] cbuf, int start, int len) |
|
238 |
{ |
|
239 |
DBSAXElement currentElement = (DBSAXElement)elementStack.peek(); |
|
240 |
currentElement.appendContent(cbuf,start,len); |
|
241 |
System.out.println(" Characters: " + new String(cbuf,start,len)); |
|
242 |
System.out.println(" Appended: " + currentElement.getContent()); |
|
243 |
} |
|
244 |
|
|
245 |
public void ignorableWhitespace(char[] cbuf, int start, int len) |
|
246 |
{ |
|
247 |
System.out.println("IgnorableWhiteSpace"); |
|
248 |
} |
|
249 |
|
|
250 |
public void endElement(NSName name) throws SAXException |
|
251 |
{ |
|
252 |
// Use the methods getQualifiedName(), getLocalName(), getNamespace() |
|
253 |
// and getExpandedName() in NSName interface to get Namespace |
|
254 |
// information. |
|
255 |
|
|
256 |
String expName = name.getExpandedName(); |
|
257 |
|
|
258 |
// Remove the element from the stack |
|
259 |
DBSAXElement currentElement = (DBSAXElement)elementStack.pop(); |
|
260 |
|
|
261 |
System.out.println("Final content: " + currentElement.getContent()); |
|
262 |
System.out.println("Closing element: " + expName); |
|
263 |
} |
|
264 |
|
|
265 |
/** Debug routine */ |
|
266 |
private void db(int flag) { |
|
267 |
if (debug) { |
|
268 |
System.err.println("DEBUG POSITION " + flag); |
|
269 |
} |
|
270 |
} |
|
271 |
} |
Also available in: Unified diff
Split XML SAX handler class into seperate file DBSAXHandler.java