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 18 2000-04-11 16:40:51Z 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
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
      System.out.println("Element created:" + currentElement.getTagName());
77

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

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

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

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

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

    
97
         currentElement.setAttribute(localName, value);
98

    
99
         System.out.println(" Added ATTRIBUTE Name:" + localName);
100
         System.out.println("                 Type:" + type);
101
         System.out.println("                Value:" + value);
102
         System.out.println();
103
      }      
104

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

    
109
   }
110

    
111
   public void characters(char[] cbuf, int start, int len)
112
   {
113
      DBSAXElement currentElement = (DBSAXElement)elementStack.peek();
114
      currentElement.appendContent(cbuf,start,len);
115
      System.out.println("   Characters: " + new String(cbuf,start,len));
116
      System.out.println("     Appended: " + currentElement.getContent());
117
   }
118

    
119
   public void ignorableWhitespace(char[] cbuf, int start, int len)
120
   {
121
      System.out.println("IgnorableWhiteSpace");
122
   }
123

    
124
   public void endElement(NSName name) throws SAXException 
125
   {
126
      // Use the methods getQualifiedName(), getLocalName(), getNamespace()
127
      // and getExpandedName() in NSName interface to get Namespace
128
      // information.
129
      String expName = name.getExpandedName();
130

    
131
      // Get the element from the stack
132
      DBSAXElement currentElement = (DBSAXElement)elementStack.pop();
133
      
134
      // Write the content of the element to the database
135
      currentElement.writeContentToDB();
136

    
137
      System.out.println("Final content: " + currentElement.getContent());
138
      System.out.println("Closing element: " + expName);
139
   }
140

    
141
   /** Debug routine */
142
   private void db(int flag) {
143
     if (debug) {
144
       System.err.println("DEBUG POSITION " + flag);
145
     }
146
   }   
147
}
(5-5/11)