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 51 2000-04-17 23:06: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

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

    
34
   private boolean 	debug 	= false;
35
   private boolean 	stackCreated = false;
36
   private Stack 	elementStack;
37
   private Connection	conn = null;
38

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

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

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

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

    
64
      String qName;
65
      String localName;
66
      String nsName;
67
      String expName;
68
      DBSAXElement parentElement;
69
      DBSAXElement currentElement;
70

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

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

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

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

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

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

    
104
         String type = atts.getType(qName);
105
         String value = atts.getValue(qName);
106

    
107
         currentElement.setAttribute(localName, value);
108
      }      
109

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

    
114
   }
115

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

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

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

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

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