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
 * Institution: National Center for Ecological Analysis and Synthesis
6
 *   Copyright: 2000
7
 *     Authors: Matt Jones
8
 *
9
 *     Version: '$Id: DBSAXHandler.java 31 2000-04-12 03:04:55Z jones $'
10
 */
11

    
12
import org.xml.sax.*;
13

    
14
import java.sql.*;
15
import java.util.Stack;
16
import java.util.EmptyStackException;
17

    
18
// Extensions to the SAX Interfaces for Namespace support.
19
import oracle.xml.parser.v2.DefaultXMLDocumentHandler;
20
import oracle.xml.parser.v2.NSName;
21
import oracle.xml.parser.v2.SAXAttrList;
22

    
23
import oracle.xml.parser.v2.SAXParser;
24

    
25
/** 
26
 * A database aware Class implementing callback bethods for the SAX parser to
27
 * call when processing the XML stream and generating events
28
 */
29
public class DBSAXHandler extends DefaultXMLDocumentHandler
30
{
31

    
32
   private boolean 	debug 	= false;
33
   private boolean 	stackCreated = false;
34
   private Stack 	elementStack;
35
   private Connection	conn = null;
36

    
37
   /** Construct an instance of the handler class 
38
    *
39
    * @param conn the JDBC connection to which information is written
40
    */
41
   public DBSAXHandler(Connection conn)
42
   {
43
      this.conn = conn;
44

    
45
      // Create the stack for keeping track of element context
46
      // if it doesn't already exist
47
      if (!stackCreated) {
48
        elementStack = new Stack();
49
        stackCreated = true;
50
      }
51

    
52
   }
53
 
54
   /** SAX Handler that is called at the start of each XML element */
55
   public void startElement(NSName name, SAXAttrList atts) throws SAXException 
56
   {
57

    
58
      // Use the methods getQualifiedName(), getLocalName(), getNamespace()
59
      // and getExpandedName() in NSName interface to get Namespace
60
      // information.
61

    
62
      String qName;
63
      String localName;
64
      String nsName;
65
      String expName;
66
      DBSAXElement parentElement;
67
      DBSAXElement currentElement;
68

    
69
      qName = name.getQualifiedName();
70
      localName = name.getLocalName();
71
      nsName = name.getNamespace();
72
      expName = name.getExpandedName();
73
      
74
      // Get a reference to the parent element for the id
75
      long parent_id;
76
      try {
77
        parentElement = (DBSAXElement)elementStack.peek();
78
        parent_id = parentElement.getElementID();
79
      } catch (EmptyStackException e) {
80
        parent_id = 0;
81
      }
82

    
83
      // Create the current element representation
84
      currentElement = new DBSAXElement(conn, localName, parent_id);
85

    
86
      // Add all of the attributes
87
      for (int i=0; i<atts.getLength(); i++)
88
      {
89

    
90
         // Use the methods getQualifiedName(), getLocalName(), getNamespace()
91
         // and getExpandedName() in SAXAttrList interface to get Namespace
92
         // information.
93

    
94
         qName = atts.getQualifiedName(i);
95
         localName = atts.getLocalName(i);
96
         nsName = atts.getNamespace(i);
97
         expName = atts.getExpandedName(i);
98

    
99
         // You can get the type and value of the attributes either
100
         // by index or by the Qualified Name.
101

    
102
         String type = atts.getType(qName);
103
         String value = atts.getValue(qName);
104

    
105
         currentElement.setAttribute(localName, value);
106
      }      
107

    
108
      // Add the element to the stack, so that any text data can be 
109
      // added as it is encountered
110
      elementStack.push(currentElement);
111

    
112
   }
113

    
114
   /** SAX Handler that is called for each XML text node */
115
   public void characters(char[] cbuf, int start, int len)
116
   {
117
      DBSAXElement currentElement = (DBSAXElement)elementStack.peek();
118
      currentElement.appendContent(cbuf,start,len);
119
   }
120

    
121
   /** 
122
    * SAX Handler that is called for each XML text node that is Ignorable
123
    * white space
124
    */
125
   public void ignorableWhitespace(char[] cbuf, int start, int len)
126
   {
127
   }
128

    
129
   /** SAX Handler that is called at the end of each XML element */
130
   public void endElement(NSName name) throws SAXException 
131
   {
132
      // Use the methods getQualifiedName(), getLocalName(), getNamespace()
133
      // and getExpandedName() in NSName interface to get Namespace
134
      // information.
135
      String expName = name.getExpandedName();
136

    
137
      // Get the element from the stack
138
      DBSAXElement currentElement = (DBSAXElement)elementStack.pop();
139
      
140
      // Write the content of the element to the database
141
      currentElement.writeContentToDB();
142
   }
143

    
144
   /** Debug routine */
145
   private void db(int flag) {
146
     if (debug) {
147
       System.err.println("DEBUG POSITION " + flag);
148
     }
149
   }   
150
}
(6-6/13)