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 30 2000-04-12 02:15:07Z 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
public class DBSAXHandler extends DefaultXMLDocumentHandler
26
{
27

    
28
   private boolean 	debug 	= false;
29
   private boolean 	stackCreated = false;
30
   private Stack 	elementStack;
31
   private Connection	conn = null;
32

    
33
   public DBSAXHandler(Connection conn)
34
   {
35
      this.conn = conn;
36

    
37
      // Create the stack for keeping track of element context
38
      // if it doesn't already exist
39
      if (!stackCreated) {
40
        elementStack = new Stack();
41
        stackCreated = true;
42
      }
43

    
44
   }
45
 
46
   public void startElement(NSName name, SAXAttrList atts) throws SAXException 
47
   {
48

    
49
      // Use the methods getQualifiedName(), getLocalName(), getNamespace()
50
      // and getExpandedName() in NSName interface to get Namespace
51
      // information.
52

    
53
      String qName;
54
      String localName;
55
      String nsName;
56
      String expName;
57
      DBSAXElement parentElement;
58
      DBSAXElement currentElement;
59

    
60
      qName = name.getQualifiedName();
61
      localName = name.getLocalName();
62
      nsName = name.getNamespace();
63
      expName = name.getExpandedName();
64
      
65
      // Get a reference to the parent element for the id
66
      long parent_id;
67
      try {
68
        parentElement = (DBSAXElement)elementStack.peek();
69
        parent_id = parentElement.getElementID();
70
      } catch (EmptyStackException e) {
71
        parent_id = 0;
72
      }
73

    
74
      // Create the current element representation
75
      currentElement = new DBSAXElement(conn, localName, parent_id);
76

    
77
      // Add all of the attributes
78
      for (int i=0; i<atts.getLength(); i++)
79
      {
80

    
81
         // Use the methods getQualifiedName(), getLocalName(), getNamespace()
82
         // and getExpandedName() in SAXAttrList interface to get Namespace
83
         // information.
84

    
85
         qName = atts.getQualifiedName(i);
86
         localName = atts.getLocalName(i);
87
         nsName = atts.getNamespace(i);
88
         expName = atts.getExpandedName(i);
89

    
90
         // You can get the type and value of the attributes either
91
         // by index or by the Qualified Name.
92

    
93
         String type = atts.getType(qName);
94
         String value = atts.getValue(qName);
95

    
96
         currentElement.setAttribute(localName, value);
97
      }      
98

    
99
      // Add the element to the stack, so that any text data can be 
100
      // added as it is encountered
101
      elementStack.push(currentElement);
102

    
103
   }
104

    
105
   public void characters(char[] cbuf, int start, int len)
106
   {
107
      DBSAXElement currentElement = (DBSAXElement)elementStack.peek();
108
      currentElement.appendContent(cbuf,start,len);
109
   }
110

    
111
   public void ignorableWhitespace(char[] cbuf, int start, int len)
112
   {
113
   }
114

    
115
   public void endElement(NSName name) throws SAXException 
116
   {
117
      // Use the methods getQualifiedName(), getLocalName(), getNamespace()
118
      // and getExpandedName() in NSName interface to get Namespace
119
      // information.
120
      String expName = name.getExpandedName();
121

    
122
      // Get the element from the stack
123
      DBSAXElement currentElement = (DBSAXElement)elementStack.pop();
124
      
125
      // Write the content of the element to the database
126
      currentElement.writeContentToDB();
127
   }
128

    
129
   /** Debug routine */
130
   private void db(int flag) {
131
     if (debug) {
132
       System.err.println("DEBUG POSITION " + flag);
133
     }
134
   }   
135
}
(6-6/13)