Revision 72
Added by bojilova almost 24 years ago
DBSAXHandler.java | ||
---|---|---|
9 | 9 |
* Version: '$Id$' |
10 | 10 |
*/ |
11 | 11 |
|
12 |
package edu.ucsb.nceas.metacat; |
|
12 |
//package edu.ucsb.nceas.metacat;
|
|
13 | 13 |
|
14 | 14 |
import org.xml.sax.*; |
15 | 15 |
|
... | ... | |
23 | 23 |
import oracle.xml.parser.v2.SAXAttrList; |
24 | 24 |
|
25 | 25 |
import oracle.xml.parser.v2.SAXParser; |
26 |
import oracle.xml.parser.v2.DTD; |
|
26 | 27 |
|
27 | 28 |
/** |
28 | 29 |
* A database aware Class implementing callback bethods for the SAX parser to |
... | ... | |
31 | 32 |
public class DBSAXHandler extends DefaultXMLDocumentHandler |
32 | 33 |
{ |
33 | 34 |
|
35 |
static int elementNo = 0; |
|
36 |
private String docname; |
|
37 |
private String doctype; |
|
38 |
private String systemid; |
|
34 | 39 |
private boolean debug = false; |
35 | 40 |
private boolean stackCreated = false; |
36 | 41 |
private Stack elementStack; |
... | ... | |
53 | 58 |
|
54 | 59 |
} |
55 | 60 |
|
61 |
/** 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 |
|
|
56 | 96 |
/** SAX Handler that is called at the start of each XML element */ |
57 | 97 |
public void startElement(NSName name, SAXAttrList atts) throws SAXException |
58 | 98 |
{ |
... | ... | |
73 | 113 |
nsName = name.getNamespace(); |
74 | 114 |
expName = name.getExpandedName(); |
75 | 115 |
|
116 |
elementNo++; |
|
117 |
if ((elementNo == 1) && (doctype == null)) |
|
118 |
throw new SAXException("No DOCTYPE declaration or PUBLIC ID provided"); |
|
76 | 119 |
// Get a reference to the parent element for the id |
77 | 120 |
long parent_id; |
121 |
int nodeIndex; |
|
78 | 122 |
try { |
79 | 123 |
parentElement = (DBSAXElement)elementStack.peek(); |
80 | 124 |
parent_id = parentElement.getElementID(); |
125 |
nodeIndex = parentElement.incChildNum(); |
|
81 | 126 |
} catch (EmptyStackException e) { |
82 | 127 |
parent_id = 0; |
128 |
nodeIndex = 0; |
|
83 | 129 |
} |
84 | 130 |
|
85 | 131 |
// Create the current element representation |
86 |
currentElement = new DBSAXElement(conn, localName, parent_id); |
|
132 |
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 |
} |
|
87 | 139 |
|
88 | 140 |
// Add all of the attributes |
89 | 141 |
for (int i=0; i<atts.getLength(); i++) |
... | ... | |
128 | 180 |
{ |
129 | 181 |
} |
130 | 182 |
|
183 |
/** 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 |
|
|
131 | 194 |
/** SAX Handler that is called at the end of each XML element */ |
132 | 195 |
public void endElement(NSName name) throws SAXException |
133 | 196 |
{ |
Also available in: Unified diff
Included new features about writing XML documents into db
This includes writing data into db XML Catalog, document data into xml_documents,
comments into xml_nodes, added nodeindex for the order of elements by given parent element.