Project

General

Profile

« Previous | Next » 

Revision 176

Added by Matt Jones about 24 years ago

added code to build an index of the paths through the DOM tree to each node in the tree as the node is being inserted

View differences:

DBSAXNode.java
21 21
 */
22 22
public class DBSAXNode extends BasicNode {
23 23

  
24
  private Connection		conn;
24
  private Connection	conn;
25
  private DBSAXNode	parentNode;
25 26

  
26 27
  /** 
27 28
   * Construct a new node instance for DOCUMENT nodes
......
32 33
  public DBSAXNode (Connection conn, String tagname) {
33 34
    super(tagname);
34 35
    this.conn = conn;
36
    this.parentNode = null;
35 37
    writeChildNodeToDB("DOCUMENT", tagname, null);
36
    //setNodeID(getAssignedNodeID());
37 38
    setRootNodeID(getNodeID());
38 39
  }
39 40

  
......
44 45
   * @param tagname the name of the node
45 46
   * @param parentNode the parent node for this node being created
46 47
   */
47
  public DBSAXNode (Connection conn, String tagname, 
48
                    DBSAXNode parentNode, DBSAXNode rootNode, DBSAXDocument currentDocument) {
48
  public DBSAXNode (Connection conn, String tagname, DBSAXNode parentNode, 
49
                    DBSAXNode rootNode, DBSAXDocument currentDocument) {
49 50

  
50 51
    super(tagname);
51 52
    setParentID(parentNode.getNodeID());
......
53 54
    setDocID(currentDocument.getDocID());
54 55
    setNodeIndex(parentNode.incChildNum());
55 56
    this.conn = conn;
57
    this.parentNode = parentNode;
56 58
    writeChildNodeToDB("ELEMENT", getTagName(), null);
57
    //setNodeID(getAssignedNodeID());
59
    updateNodeIndex();
58 60
  }
59 61
    
60 62
  /** creates SQL code and inserts new node into DB connection */
......
76 78
      }
77 79

  
78 80
      // Bind the values to the query
79
      long nid = genNodeID();
81
      long nid = generateNodeID();
80 82
      pstmt.setLong(1, nid);
81 83
      pstmt.setString(2, nodetype);
82 84
      pstmt.setString(3, nodename);
......
99 101
      pstmt.execute();
100 102
      pstmt.close();
101 103
      
102
      if (nodetype == "DOCUMENT" || nodetype == "ELEMENT")
103
        setNodeID(nid);
104
      
104
      // Record the node id that was generated fromthe database
105
      setNodeID(nid);
106

  
107
      // Record the node type that was passed in via the constructor
108
      setNodeType(nodetype);
109

  
105 110
    } catch (SQLException e) {
106 111
      System.err.println("Error inserting node: (" + nodetype + ", " +
107 112
                                                     nodename + ", " + 
......
154 159
      }
155 160
  }
156 161
  /** 
157
   * creates SQL code to put doc ID for the document node and for comment/PI nodes under document node
158
   * into DB connection 
162
   * creates SQL code to put doc ID for the document node and for 
163
   * comment/PI nodes under document node into DB connection 
159 164
   */
160 165
  public void writeDocID(String doc_id) {
161 166
      try {
......
187 192
  }
188 193

  
189 194
  /** get next node id from DB connection */
190
  private long genNodeID() {
195
  private long generateNodeID() {
191 196
      long nid=0;
192 197
      Statement stmt;
193 198
      try {
......
218 223
      System.err.println("Attribute name must not be null!");
219 224
    }
220 225
  }
226

  
227
  /** 
228
   * Update the node index (xml_index) for this node by generating
229
   * test strings that represent all of the relative and absolute
230
   * paths through the XML tree from document root to this node
231
   */
232
  private void updateNodeIndex() {
233
    Hashtable pathlist = new Hashtable();
234
    boolean atStartingNode = true;
235
    boolean atRootDocumentNode = false;
236
    DBSAXNode nodePointer = this;
237
    StringBuffer currentPath = new StringBuffer();
238

  
239
    // Create a Hashtable of all of the paths to reach this node
240
    // including absolute paths and relative paths
241
    while (!atRootDocumentNode) {
242
      if (atStartingNode) {
243
        currentPath.insert(0, nodePointer.getTagName());
244
        pathlist.put(currentPath.toString(), new Long(getNodeID()));
245
        atStartingNode = false;
246
      } else {
247
        currentPath.insert(0, "/");
248
        currentPath.insert(0, nodePointer.getTagName());
249
        pathlist.put(currentPath.toString(), new Long(getNodeID()));
250
      }
251

  
252
      // advance to the next parent node
253
      nodePointer = nodePointer.getParentNode();
254

  
255
      // If we're at the DOCUMENT node (root of DOM tree), add
256
      // the root "/" to make the absolute path
257
      if (nodePointer.getNodeType().equals("DOCUMENT")) {
258
        currentPath.insert(0, "/");
259
        pathlist.put(currentPath.toString(), new Long(getNodeID()));
260
        atRootDocumentNode = true;
261
      } 
262
    }
263

  
264
    try {
265
      // Create an insert statement to reuse for all of the path insertions
266
      PreparedStatement pstmt = conn.prepareStatement(
267
              "INSERT INTO xml_index (nodeid, path) VALUES (?, ?)");
268
  
269
      // Step through the hashtable and insert each of the path values
270
      Enumeration en = pathlist.keys();
271
      while (en.hasMoreElements()) {
272
        String path = (String)en.nextElement();
273
        Long nodeid = (Long)pathlist.get(path);
274
        pstmt.setLong(1, nodeid.longValue());
275
        pstmt.setString(2, path);
276
        pstmt.execute();
277
  
278
        //System.out.println(nodeid + " ==> " + path);
279
      }
280

  
281
      // Close the database statement
282
      pstmt.close();
283
    } catch (SQLException sqe) {
284
      System.err.println("SQL Exception while inserting path to index.");
285
      System.err.println(sqe.getMessage());
286
    }
287
  }
288
 
289
  /** get the parent of this node */
290
  public DBSAXNode getParentNode() {
291
    return parentNode;
292
  }
221 293
}

Also available in: Unified diff