Project

General

Profile

1
/**
2
 *        Name: DBSAXHandler.java
3
 *     Purpose: A Class that handles the SAX XML events as they
4
 *              are generated from XML documents
5
 *   Copyright: 2000 Regents of the University of California and the
6
 *              National Center for Ecological Analysis and Synthesis
7
 *     Authors: Matt Jones
8
 *
9
 *     Version: '$Id: DBSAXHandler.java 76 2000-05-05 02:45:07Z jones $'
10
 */
11

    
12
package edu.ucsb.nceas.metacat;
13

    
14
import org.xml.sax.*;
15

    
16
import java.sql.*;
17
import java.util.Stack;
18
import java.util.EmptyStackException;
19

    
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
import oracle.xml.parser.v2.DTD;
27

    
28
/** 
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
public class DBSAXHandler extends DefaultXMLDocumentHandler
33
{
34

    
35
   static int elementNo = 0;
36
   private String docname;
37
   private String doctype;
38
   private String systemid;
39
   private boolean 	debug 	= false;
40
   private boolean 	stackCreated = false;
41
   private Stack 	elementStack;
42
   private Connection	conn = null;
43

    
44
   /** Construct an instance of the handler class 
45
    *
46
    * @param conn the JDBC connection to which information is written
47
    */
48
   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
   /** 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
    if (doctype == null) {
80
      doctype = docname;
81
    }
82
    systemid = dtd.getSystemId();
83
    System.out.println("DOCNAME: " + docname);
84
    System.out.println("DOCTYPE: " + doctype);
85
    System.out.println("  SYSID: " + systemid);
86
   }
87

    
88
   /** SAX Handler that receives notification of end of DTD 
89
     * All events in DTDHandler about all unparsed entities and the event in EntityResolver for the DTD file declaration
90
     * appear between setDoctype and endDoctype.
91
     * The rest of parsable external entities inside DTD file appear later in the elements from where they are referred to. */
92
   public void endDoctype() throws SAXException
93
   {
94
    System.out.println("end of DOCTYPE");
95
    if (doctype == null)
96
        doctype = DBEntityResolver.doctype;
97
   }
98

    
99
   /** SAX Handler that is called at the start of each XML element */
100
   public void startElement(NSName name, SAXAttrList atts) throws SAXException 
101
   {
102

    
103
      // Use the methods getQualifiedName(), getLocalName(), getNamespace()
104
      // and getExpandedName() in NSName interface to get Namespace
105
      // information.
106

    
107
      String qName;
108
      String localName;
109
      String nsName;
110
      String expName;
111
      DBSAXElement parentElement;
112
      DBSAXElement currentElement;
113

    
114
      qName = name.getQualifiedName();
115
      localName = name.getLocalName();
116
      nsName = name.getNamespace();
117
      expName = name.getExpandedName();
118
      
119
      elementNo++;
120
      if ((elementNo == 1) && (doctype == null))
121
        throw new SAXException("No DOCTYPE declaration or PUBLIC ID provided");
122
      // Get a reference to the parent element for the id
123
      long parent_id;
124
      int nodeIndex;
125
      try {
126
        parentElement = (DBSAXElement)elementStack.peek();
127
        parent_id = parentElement.getElementID();
128
        nodeIndex = parentElement.incChildNum();
129
      } catch (EmptyStackException e) {
130
        parent_id = 0;
131
        nodeIndex = 0;
132
      }
133

    
134
      // Create the current element representation
135
      currentElement = new DBSAXElement(conn, localName, parent_id, nodeIndex);
136
      // go to create document definition in the db
137
      // call once after insertion of the first element
138
      if (parent_id == 0) {
139
        long rootnodeid = currentElement.getElementID();
140
        new DBSAXDocument(conn, rootnodeid, docname, doctype);
141
      }
142

    
143
      // Add all of the attributes
144
      for (int i=0; i<atts.getLength(); i++)
145
      {
146

    
147
         // Use the methods getQualifiedName(), getLocalName(), getNamespace()
148
         // and getExpandedName() in SAXAttrList interface to get Namespace
149
         // information.
150

    
151
         qName = atts.getQualifiedName(i);
152
         localName = atts.getLocalName(i);
153
         nsName = atts.getNamespace(i);
154
         expName = atts.getExpandedName(i);
155

    
156
         // You can get the type and value of the attributes either
157
         // by index or by the Qualified Name.
158

    
159
         String type = atts.getType(qName);
160
         String value = atts.getValue(qName);
161

    
162
         currentElement.setAttribute(localName, value);
163
      }      
164

    
165
      // Add the element to the stack, so that any text data can be 
166
      // added as it is encountered
167
      elementStack.push(currentElement);
168

    
169
   }
170

    
171
   /** SAX Handler that is called for each XML text node */
172
   public void characters(char[] cbuf, int start, int len)
173
   {
174
      DBSAXElement currentElement = (DBSAXElement)elementStack.peek();
175
      currentElement.appendContent(cbuf,start,len);
176
   }
177

    
178
   /** 
179
    * SAX Handler that is called for each XML text node that is Ignorable
180
    * white space
181
    */
182
   public void ignorableWhitespace(char[] cbuf, int start, int len)
183
   {
184
   }
185

    
186
   /** SAX Handler called once for each comment found: 
187
     * node that comment may occur before or after the root element.
188
     * For now works only for comments after the root element. */
189
   public void comment(String data) throws SAXException
190
   {
191
    if (elementNo > 0) {
192
      DBSAXElement currentElement = (DBSAXElement)elementStack.peek();
193
      currentElement.writeCommentToDB(data);
194
    }
195
   }
196

    
197
   /** SAX Handler that is called at the end of each XML element */
198
   public void endElement(NSName name) throws SAXException 
199
   {
200
      // Use the methods getQualifiedName(), getLocalName(), getNamespace()
201
      // and getExpandedName() in NSName interface to get Namespace
202
      // information.
203
      String expName = name.getExpandedName();
204

    
205
      // Get the element from the stack
206
      DBSAXElement currentElement = (DBSAXElement)elementStack.pop();
207
      
208
      // Write the content of the element to the database
209
      currentElement.writeContentToDB();
210
   }
211

    
212
   /** Debug routine */
213
   private void db(int flag) {
214
     if (debug) {
215
       System.err.println("DEBUG POSITION " + flag);
216
     }
217
   }   
218
}
(9-9/26)