Project

General

Profile

« Previous | Next » 

Revision 137

Added by Matt Jones about 24 years ago

Renamed DBSAXElement to DBSAXNode

View differences:

src/edu/ucsb/nceas/metacat/DBSAXElement.java
1
/**
2
 *        Name: DBSAXElement.java
3
 *     Purpose: A Class that represents an XML element and its contents
4
 *   Copyright: 2000 Regents of the University of California and the
5
 *              National Center for Ecological Analysis and Synthesis
6
 *     Authors: Matt Jones
7
 *
8
 *     Version: '$Id$'
9
 */
10

  
11
package edu.ucsb.nceas.metacat;
12

  
13
import java.sql.*;
14
import java.io.IOException;
15
import java.util.Hashtable;
16
import java.util.Enumeration;
17

  
18
/** 
19
 * A Class that represents an XML element and its contents and
20
 * can write its own representation to a database connection
21
 */
22
public class DBSAXElement extends BasicNode {
23

  
24
  private Connection		conn;
25

  
26
  /** 
27
   * Construct a new node instance for DOCUMENT nodes
28
   *
29
   * @param conn the JDBC Connection to which all information is written
30
   * @param tagname the name of the node
31
   */
32
  public DBSAXElement (Connection conn, String tagname) {
33
    super(tagname);
34
    this.conn = conn;
35
    writeChildNodeToDB("DOCUMENT", tagname, null);
36
    setNodeID(getAssignedNodeID());
37
  }
38

  
39
  /** 
40
   * Construct a new node instance for ELEMENT nodes
41
   *
42
   * @param conn the JDBC Connection to which all information is written
43
   * @param tagname the name of the node
44
   * @param parentNode the parent node for this node being created
45
   */
46
  public DBSAXElement (Connection conn, String tagname, 
47
                       DBSAXElement parentNode) {
48

  
49
    super(tagname);
50
    setParentID(parentNode.getNodeID());
51
    setNodeIndex(parentNode.incChildNum());
52
    this.conn = conn;
53
    writeChildNodeToDB("ELEMENT", getTagName(), null);
54
    setNodeID(getAssignedNodeID());
55
  }
56
    
57
  /** creates SQL code and inserts new node into DB connection */
58
  public void writeChildNodeToDB(String nodetype, String nodename,
59
                                 String data) {
60
    try {
61
      PreparedStatement pstmt;
62
      if (nodetype == "DOCUMENT") {
63
        pstmt = conn.prepareStatement(
64
            "INSERT INTO xml_nodes " +
65
            "(nodeid, nodetype, nodename) " +
66
            "VALUES (null, ?, ?)");
67
      } else {
68
        pstmt = conn.prepareStatement(
69
            "INSERT INTO xml_nodes " +
70
            "(nodeid, nodetype, nodename, " +
71
            "parentnodeid, nodedata, nodeindex) " +
72
            "VALUES (null, ?, ?, ?, ?, ?)");
73
      }
74

  
75
      // Bind the values to the query
76
      pstmt.setString(1, nodetype);
77
      pstmt.setString(2, nodename);
78
      if (nodetype != "DOCUMENT") {
79
        if (nodetype == "ELEMENT") {
80
          pstmt.setLong(3, getParentID());
81
          pstmt.setString(4, data);
82
          pstmt.setInt(5, getNodeIndex());
83
        } else {
84
          pstmt.setLong(3, getNodeID());
85
          pstmt.setString(4, data);
86
          pstmt.setInt(5, incChildNum());
87
        }
88
      }
89
      // Do the insertion
90
      pstmt.execute();
91
      pstmt.close();
92
    } catch (SQLException e) {
93
      System.err.println("Error inserting node: (" + nodetype + ", " +
94
                                                     nodename + ", " + 
95
                                                     data + ")" );
96
      System.err.println(e.getMessage());
97
    }
98
  }
99

  
100
  /** 
101
   * creates SQL code to put nodename for the document node 
102
   * into DB connection 
103
   */
104
  public void writeNodename(String nodename) {
105
      try {
106
        PreparedStatement pstmt;
107
        pstmt = conn.prepareStatement(
108
              "UPDATE xml_nodes set nodename = ? " +
109
              "WHERE nodeid = ?");
110

  
111
        // Bind the values to the query
112
        pstmt.setString(1, nodename);
113
        pstmt.setLong(2, getNodeID());
114
        // Do the insertion
115
        pstmt.execute();
116
        pstmt.close();
117
      } catch (SQLException e) {
118
        System.out.println(e.getMessage());
119
      }
120
  }
121

  
122
  /** look up the assigned element id from DB connection */
123
  private long getAssignedNodeID() {
124
      long assigned_id=0;
125
      Statement stmt;
126
      try {
127
        stmt = conn.createStatement();
128
        stmt.execute("SELECT xml_nodes_id_seq.currval FROM dual");
129
        ResultSet rs = stmt.getResultSet();
130
        boolean tableHasRows = rs.next();
131
        if (tableHasRows) {
132
          assigned_id = rs.getLong(1);
133
        }
134
        stmt.close();
135
      } catch (SQLException e) {
136
        System.out.println("Error getting id: " + e.getMessage());
137
      }
138

  
139
      // assign the new ID number
140
      return assigned_id;
141
  }
142

  
143
  /** Add a new attribute to this element, or set its value */
144
  public void setAttribute(String attName, String attValue) {
145
    if (attName != null) {
146
      // Enter the attribute in the hash table
147
      super.setAttribute(attName, attValue);
148

  
149
      // And enter the attribute in the database
150
      writeChildNodeToDB("ATTRIBUTE", attName, attValue);
151
    } else {
152
      System.err.println("Attribute name must not be null!");
153
    }
154
  }
155
}
156 0

  
src/edu/ucsb/nceas/metacat/DBSAXHandler.java
37 37
   private String 	systemid;
38 38
   private boolean 	debug 	= false;
39 39
   private boolean 	stackCreated = false;
40
   private Stack 	elementStack;
40
   private Stack 	nodeStack;
41 41
   private Connection	conn = null;
42 42

  
43 43
   /** Construct an instance of the handler class 
......
47 47
   public DBSAXHandler(Connection conn) {
48 48
      this.conn = conn;
49 49

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

  
......
62 62
      System.out.println("start Document");
63 63
    }
64 64
    // Create the document node represantation as root
65
    DBSAXElement documentNode = new DBSAXElement(conn, docname);
66
    // Add the element to the stack, so that any text data can be 
65
    DBSAXNode documentNode = new DBSAXNode(conn, docname);
66
    // Add the node to the stack, so that any text data can be 
67 67
    // added as it is encountered
68
    elementStack.push(documentNode);
68
    nodeStack.push(documentNode);
69 69
   }
70 70

  
71 71
   /** SAX Handler that receives notification of end of the document */
......
114 114
      String localName;
115 115
      String nsName;
116 116
      String expName;
117
      DBSAXElement parentElement = null;
118
      DBSAXElement currentElement = null;
117
      DBSAXNode parentNode = null;
118
      DBSAXNode currentNode = null;
119 119

  
120 120
      qName = name.getQualifiedName();
121 121
      localName = name.getLocalName();
......
123 123
      expName = name.getExpandedName();
124 124
      
125 125
      elementNo++;
126
      // Get a reference to the parent element for the id
126
      // Get a reference to the parent node for the id
127 127
      try {
128
        parentElement = (DBSAXElement)elementStack.peek();
128
        parentNode = (DBSAXNode)nodeStack.peek();
129 129
      } catch (EmptyStackException e) {
130 130
      }
131 131

  
......
137 137
        } else if (doctype == null) {
138 138
            doctype = DBEntityResolver.doctype;
139 139
        }
140
        DBSAXElement documentNode = (DBSAXElement)elementStack.peek();
140
        DBSAXNode documentNode = (DBSAXNode)nodeStack.peek();
141 141
        documentNode.writeNodename(docname);
142 142
        new DBSAXDocument(conn, documentNode.getNodeID(), docname, doctype);
143 143
      }      
144 144

  
145
      // Create the current element representation
146
      currentElement = new DBSAXElement(conn, localName, parentElement);
145
      // Create the current node representation
146
      currentNode = new DBSAXNode(conn, localName, parentNode);
147 147

  
148 148
      // Add all of the attributes
149 149
      for (int i=0; i<atts.getLength(); i++)
......
164 164
         String type = atts.getType(qName);
165 165
         String value = atts.getValue(qName);
166 166

  
167
         currentElement.setAttribute(localName, value);
167
         currentNode.setAttribute(localName, value);
168 168
      }      
169 169

  
170
      // Add the element to the stack, so that any text data can be 
170
      // Add the node to the stack, so that any text data can be 
171 171
      // added as it is encountered
172
      elementStack.push(currentElement);
172
      nodeStack.push(currentNode);
173 173

  
174 174
   }
175 175

  
176 176
   /** SAX Handler that is called for each XML text node */
177 177
   public void characters(char[] cbuf, int start, int len) {
178
      DBSAXElement currentElement = (DBSAXElement)elementStack.peek();
178
      DBSAXNode currentNode = (DBSAXNode)nodeStack.peek();
179 179
      String data = new String(cbuf, start, len);
180 180

  
181
      // Write the content of the element to the database
182
      currentElement.writeChildNodeToDB("TEXT", null, data);
181
      // Write the content of the node to the database
182
      currentNode.writeChildNodeToDB("TEXT", null, data);
183 183
   }
184 184

  
185 185
   /** 
......
192 192
   /** 
193 193
    * SAX Handler called once for each comment found: 
194 194
    * node that comment may occur before or after the root element.
195
    * For now works only for comments after the root element. 
196 195
    */
197 196
   public void comment(String data) throws SAXException {
198
      DBSAXElement currentElement = (DBSAXElement)elementStack.peek();
199
      currentElement.writeChildNodeToDB("COMMENT", null, data);
197
      DBSAXNode currentNode = (DBSAXNode)nodeStack.peek();
198
      currentNode.writeChildNodeToDB("COMMENT", null, data);
200 199
   }
201 200

  
202 201
   /** 
......
205 204
    */
206 205
   public void processingInstruction(String target, String data) 
207 206
          throws SAXException {
208
      DBSAXElement currentElement = (DBSAXElement)elementStack.peek();
209
      currentElement.writeChildNodeToDB("PI", target, data);
207
      DBSAXNode currentNode = (DBSAXNode)nodeStack.peek();
208
      currentNode.writeChildNodeToDB("PI", target, data);
210 209
   }
211 210

  
212 211
   /** SAX Handler that is called at the end of each XML element */
......
216 215
      // information.
217 216
      String expName = name.getExpandedName();
218 217

  
219
      // Get the element from the stack
220
      DBSAXElement currentElement = (DBSAXElement)elementStack.pop();
218
      // Get the node from the stack
219
      DBSAXNode currentNode = (DBSAXNode)nodeStack.pop();
221 220
   }
222 221

  
223 222
   /** Debug routine */
src/edu/ucsb/nceas/metacat/DBSAXNode.java
1 1
/**
2
 *        Name: DBSAXElement.java
3
 *     Purpose: A Class that represents an XML element and its contents
2
 *        Name: DBSAXNode.java
3
 *     Purpose: A Class that represents an XML node and its contents
4 4
 *   Copyright: 2000 Regents of the University of California and the
5 5
 *              National Center for Ecological Analysis and Synthesis
6 6
 *     Authors: Matt Jones
......
16 16
import java.util.Enumeration;
17 17

  
18 18
/** 
19
 * A Class that represents an XML element and its contents and
19
 * A Class that represents an XML node and its contents and
20 20
 * can write its own representation to a database connection
21 21
 */
22
public class DBSAXElement extends BasicNode {
22
public class DBSAXNode extends BasicNode {
23 23

  
24 24
  private Connection		conn;
25 25

  
......
29 29
   * @param conn the JDBC Connection to which all information is written
30 30
   * @param tagname the name of the node
31 31
   */
32
  public DBSAXElement (Connection conn, String tagname) {
32
  public DBSAXNode (Connection conn, String tagname) {
33 33
    super(tagname);
34 34
    this.conn = conn;
35 35
    writeChildNodeToDB("DOCUMENT", tagname, null);
......
43 43
   * @param tagname the name of the node
44 44
   * @param parentNode the parent node for this node being created
45 45
   */
46
  public DBSAXElement (Connection conn, String tagname, 
47
                       DBSAXElement parentNode) {
46
  public DBSAXNode (Connection conn, String tagname, 
47
                       DBSAXNode parentNode) {
48 48

  
49 49
    super(tagname);
50 50
    setParentID(parentNode.getNodeID());
......
119 119
      }
120 120
  }
121 121

  
122
  /** look up the assigned element id from DB connection */
122
  /** look up the assigned node id from DB connection */
123 123
  private long getAssignedNodeID() {
124 124
      long assigned_id=0;
125 125
      Statement stmt;
......
140 140
      return assigned_id;
141 141
  }
142 142

  
143
  /** Add a new attribute to this element, or set its value */
143
  /** Add a new attribute to this node, or set its value */
144 144
  public void setAttribute(String attName, String attValue) {
145 145
    if (attName != null) {
146 146
      // Enter the attribute in the hash table

Also available in: Unified diff