Project

General

Profile

1 15 jones
/**
2 203 jones
 *  '$RCSfile$'
3
 *    Purpose: A Class that represents an XML node 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 15 jones
 *
8 203 jones
 *   '$Author$'
9
 *     '$Date$'
10
 * '$Revision$'
11 669 jones
 *
12
 * This program is free software; you can redistribute it and/or modify
13
 * it under the terms of the GNU General Public License as published by
14
 * the Free Software Foundation; either version 2 of the License, or
15
 * (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU General Public License
23
 * along with this program; if not, write to the Free Software
24
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25 15 jones
 */
26
27 75 jones
package edu.ucsb.nceas.metacat;
28 15 jones
29
import java.sql.*;
30 6012 leinfelder
import java.text.ParseException;
31
import java.text.SimpleDateFormat;
32
import java.util.Date;
33 15 jones
import java.util.Hashtable;
34
import java.util.Enumeration;
35 2663 sgarg
import org.apache.log4j.Logger;
36 461 bojilova
import org.xml.sax.SAXException;
37 15 jones
38 5015 daigle
import edu.ucsb.nceas.metacat.database.DBConnection;
39
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
40
import edu.ucsb.nceas.metacat.database.DatabaseService;
41 747 bojilova
42 2358 sgarg
/**
43 137 jones
 * A Class that represents an XML node and its contents and
44 31 jones
 * can write its own representation to a database connection
45
 */
46 137 jones
public class DBSAXNode extends BasicNode {
47 15 jones
48 1217 tao
  private DBConnection	connection;
49 176 jones
  private DBSAXNode	parentNode;
50 2663 sgarg
  private Logger logMetacat = Logger.getLogger(DBSAXNode.class);
51 15 jones
52 2358 sgarg
  /**
53 136 jones
   * Construct a new node instance for DOCUMENT nodes
54 133 jones
   *
55
   * @param conn the JDBC Connection to which all information is written
56
   */
57 1217 tao
  public DBSAXNode (DBConnection conn, String docid) throws SAXException {
58 747 bojilova
59 408 jones
    super();
60 1217 tao
    this.connection = conn;
61 176 jones
    this.parentNode = null;
62 457 bojilova
    writeChildNodeToDB("DOCUMENT", null, null, docid);
63 758 bojilova
    updateRootNodeID(getNodeID());
64 136 jones
  }
65
66 2358 sgarg
  /**
67 136 jones
   * Construct a new node instance for ELEMENT nodes
68
   *
69
   * @param conn the JDBC Connection to which all information is written
70
   * @param tagname the name of the node
71
   * @param parentNode the parent node for this node being created
72
   */
73 1217 tao
  public DBSAXNode (DBConnection conn, String qName, String lName,
74 2358 sgarg
                    DBSAXNode parentNode, long rootnodeid,
75
                    String docid, String doctype)
76 826 bojilova
                                               throws SAXException {
77 21 jones
78 826 bojilova
    super(lName);
79 1217 tao
    this.connection = conn;
80 747 bojilova
    this.parentNode = parentNode;
81 136 jones
    setParentID(parentNode.getNodeID());
82 457 bojilova
    setRootNodeID(rootnodeid);
83
    setDocID(docid);
84 136 jones
    setNodeIndex(parentNode.incChildNum());
85 826 bojilova
    writeChildNodeToDB("ELEMENT", qName, null, docid);
86 471 bojilova
    //No writing XML Index from here. New Thread used instead.
87
    //updateNodeIndex(docid, doctype);
88 133 jones
  }
89 2358 sgarg
90
  /**
91 1803 tao
   * Construct a new node instance for DTD nodes
92
   * This Node will write docname, publicId and systemId into db. Only
93
   * handle systemid  existed.(external dtd)
94
   *
95
   * @param conn the JDBC Connection to which all information is written
96
   * @param tagname the name of the node
97
   * @param parentNode the parent node for this node being created
98
   */
99 2358 sgarg
  public DBSAXNode (DBConnection conn, String docName, String publicId,
100
                    String systemId, DBSAXNode parentNode, long rootnodeid,
101
                    String docid) throws SAXException
102 1803 tao
  {
103
104
    super();
105
    this.connection = conn;
106
    this.parentNode = parentNode;
107
    setParentID(parentNode.getNodeID());
108
    setRootNodeID(rootnodeid);
109
    setDocID(docid);
110
    // insert dtd node only for external dtd definiation
111
    if (systemId != null)
112
    {
113
      //write docname to DB
114
      if (docName != null)
115
      {
116
        setNodeIndex(parentNode.incChildNum());
117
        writeDTDNodeToDB(DocumentImpl.DOCNAME, docName, docid);
118
      }
119
      //write publicId to DB
120
      if (publicId != null)
121
      {
122
        setNodeIndex(parentNode.incChildNum());
123
        writeDTDNodeToDB(DocumentImpl.PUBLICID, publicId, docid);
124
      }
125
      //write systemId to DB
126
      setNodeIndex(parentNode.incChildNum());
127
      writeDTDNodeToDB(DocumentImpl.SYSTEMID, systemId, docid);
128
    }
129
  }
130 5208 daigle
131 133 jones
  /** creates SQL code and inserts new node into DB connection */
132 1416 tao
  public long writeChildNodeToDB(String nodetype, String nodename,
133 2358 sgarg
                                 String data, String docid)
134 5208 daigle
                                 throws SAXException {
135
    String limitedData = null;
136
    int leftover = 0;
137
    if (data != null) {
138
    	leftover = data.length();
139
    }
140
    int offset = 0;
141
    boolean moredata = true;
142
    long endNodeId = -1;
143
144
    // This loop deals with the case where there are more characters
145
    // than can fit in a single database text field (limit is
146
    // MAXDATACHARS). If the text to be inserted exceeds MAXDATACHARS,
147
    // write a series of nodes that are MAXDATACHARS long, and then the
148
    // final node contains the remainder
149
    while (moredata) {
150 5311 daigle
        if (leftover > (DBSAXHandler.MAXDATACHARS)) {
151 5208 daigle
        	limitedData = data.substring(offset, DBSAXHandler.MAXDATACHARS);
152 5311 daigle
            leftover -= (DBSAXHandler.MAXDATACHARS - 1);
153
            offset += (DBSAXHandler.MAXDATACHARS - 1);
154 5208 daigle
        } else {
155 5311 daigle
        	if (data != null) {
156
        		limitedData = data.substring(offset, offset + leftover);
157
        	} else {
158
        		limitedData = null;
159
        	}
160
        	moredata = false;
161 5208 daigle
        }
162 5311 daigle
163
        endNodeId =  writeChildNodeToDBDataLimited(nodetype, nodename, limitedData, docid);
164 5208 daigle
    }
165
166
    return endNodeId;
167
  }
168
169
  /** creates SQL code and inserts new node into DB connection */
170
  public long writeChildNodeToDBDataLimited(String nodetype, String nodename,
171
                                 String data, String docid)
172 2358 sgarg
                                 throws SAXException
173 1416 tao
  {
174
    long nid = -1;
175 2358 sgarg
    try
176 1416 tao
    {
177 2358 sgarg
178 133 jones
      PreparedStatement pstmt;
179 6012 leinfelder
      Timestamp timestampData = null;
180
181 133 jones
      if (nodetype == "DOCUMENT") {
182 1217 tao
        pstmt = connection.prepareStatement(
183 133 jones
            "INSERT INTO xml_nodes " +
184 826 bojilova
            "(nodetype, nodename, nodeprefix, docid) " +
185
            "VALUES (?, ?, ?, ?)");
186 2358 sgarg
187 1217 tao
        // Increase DBConnection usage count
188
        connection.increaseUsageCount(1);
189 5208 daigle
        logMetacat.debug("DBSAXNode.writeChildNodeToDBDataLimited - inserting doc name: " + nodename);
190 133 jones
      } else {
191 2364 sgarg
          if(data != null && !data.trim().equals("")
192 3197 tao
             && !data.trim().equals("NaN") && !data.trim().equalsIgnoreCase("Infinity")){
193 6012 leinfelder
        	  try{
194
        		  // try some common ISO 8601 formats
195
        		  SimpleDateFormat sdf = null;
196
        		  if (data.length() == 10) {
197
        			  sdf = new SimpleDateFormat("yyyy-MM-dd");
198
        		  } else if (data.length() == 19) {
199
        			  sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss");
200
        		  } else {
201
        			  // throw this so we continue with parsing as numeric or string.
202
        			  throw new ParseException("String length will not match date/dateTime patterns", -1);
203
        		  }
204
        		  Date dateData = sdf.parse(data);
205
        		  timestampData = new Timestamp(dateData.getTime());
206 2358 sgarg
                  pstmt = connection.prepareStatement(
207
                      "INSERT INTO xml_nodes " +
208
                      "(nodetype, nodename, nodeprefix, docid, " +
209 6012 leinfelder
                      "rootnodeid, parentnodeid, nodedata, nodeindex, nodedatadate) " +
210
                      "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");
211
              } catch (ParseException pe) {
212
	        	  try{
213
	                  double numberData = Double.parseDouble(data);
214
	                  pstmt = connection.prepareStatement(
215
	                      "INSERT INTO xml_nodes " +
216
	                      "(nodetype, nodename, nodeprefix, docid, " +
217
	                      "rootnodeid, parentnodeid, nodedata, nodeindex, nodedatanumerical) " +
218
	                      "VALUES (?, ?, ?, ?, ?, ?, ?, ?, "+ numberData +")");
219
	              } catch (NumberFormatException nfe) {
220
	                  pstmt = connection.prepareStatement(
221
	                      "INSERT INTO xml_nodes " +
222
	                      "(nodetype, nodename, nodeprefix, docid, " +
223
	                      "rootnodeid, parentnodeid, nodedata, nodeindex) " +
224
	                      "VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
225
	              }
226 2358 sgarg
              }
227
          } else {
228
              pstmt = connection.prepareStatement(
229
                  "INSERT INTO xml_nodes " +
230
                  "(nodetype, nodename, nodeprefix, docid, " +
231
                  "rootnodeid, parentnodeid, nodedata, nodeindex) " +
232
                  "VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
233
          }
234 1217 tao
        // Increase DBConnection usage count
235
        connection.increaseUsageCount(1);
236 133 jones
      }
237 19 jones
238 133 jones
      // Bind the values to the query
239 758 bojilova
      pstmt.setString(1, nodetype);
240 826 bojilova
      int idx;
241
      if ( nodename != null && (idx = nodename.indexOf(":")) != -1 ) {
242
        pstmt.setString(2, nodename.substring(idx+1));
243
        pstmt.setString(3, nodename.substring(0,idx));
244
      } else {
245
        pstmt.setString(2, nodename);
246
        pstmt.setString(3, null);
247
      }
248
      pstmt.setString(4, docid);
249 5208 daigle
      if (nodetype != "DOCUMENT") {
250 133 jones
        if (nodetype == "ELEMENT") {
251 826 bojilova
          pstmt.setLong(5, getRootNodeID());
252
          pstmt.setLong(6, getParentID());
253
          pstmt.setString(7, data);
254
          pstmt.setInt(8, getNodeIndex());
255 133 jones
        } else {
256 826 bojilova
          pstmt.setLong(5, getRootNodeID());
257
          pstmt.setLong(6, getNodeID());
258
          pstmt.setString(7, data);
259
          pstmt.setInt(8, incChildNum());
260 72 bojilova
        }
261 133 jones
      }
262 6012 leinfelder
      if (timestampData != null) {
263
    	  pstmt.setTimestamp(9, timestampData);
264
      }
265 133 jones
      // Do the insertion
266 5208 daigle
      logMetacat.debug("DBSAXNode.writeChildNodeToDBDataLimited - SQL insert: " + pstmt.toString());
267 133 jones
      pstmt.execute();
268
      pstmt.close();
269 758 bojilova
270
      // get the generated unique id afterward
271 5319 jones
      nid = DatabaseService.getInstance().getDBAdapter().getUniqueID(connection.getConnections(), "xml_nodes");
272 5208 daigle
      //should increase connection usage!!!!!!
273 2358 sgarg
274
275 408 jones
      if (nodetype.equals("DOCUMENT")) {
276
        // Record the root node id that was generated from the database
277
        setRootNodeID(nid);
278
      }
279
280 177 jones
      if (nodetype.equals("DOCUMENT") || nodetype.equals("ELEMENT")) {
281 176 jones
282 177 jones
        // Record the node id that was generated from the database
283
        setNodeID(nid);
284 176 jones
285 177 jones
        // Record the node type that was passed to the method
286
        setNodeType(nodetype);
287
288
      }
289
290 5208 daigle
    } catch (SQLException sqle) {
291
      logMetacat.error("DBSAXNode.writeChildNodeToDBDataLimited - SQL error inserting node: " +
292
    		  "(" + nodetype + ", " + nodename + ", " + data + ") : " + sqle.getMessage());
293
      sqle.printStackTrace(System.err);
294
      throw new SAXException(sqle.getMessage());
295 110 bojilova
    }
296 1416 tao
    return nid;
297 133 jones
  }
298 110 bojilova
299 2358 sgarg
  /**
300 758 bojilova
   * update rootnodeid=nodeid for 'DOCUMENT' type of nodes only
301
   */
302
  public void updateRootNodeID(long nodeid) throws SAXException {
303
      try {
304
        PreparedStatement pstmt;
305 1217 tao
        pstmt = connection.prepareStatement(
306 758 bojilova
              "UPDATE xml_nodes set rootnodeid = ? " +
307
              "WHERE nodeid = ?");
308 1217 tao
        // Increase DBConnection usage count
309
        connection.increaseUsageCount(1);
310 758 bojilova
311
        // Bind the values to the query
312
        pstmt.setLong(1, nodeid);
313
        pstmt.setLong(2, nodeid);
314
        // Do the update
315
        pstmt.execute();
316
        pstmt.close();
317
      } catch (SQLException e) {
318 2358 sgarg
        System.out.println("Error in DBSaxNode.updateRootNodeID: " +
319 758 bojilova
                           e.getMessage());
320
        throw new SAXException(e.getMessage());
321
      }
322
  }
323
324 2358 sgarg
  /**
325
   * creates SQL code to put nodename for the document node
326
   * into DB connection
327 133 jones
   */
328 461 bojilova
  public void writeNodename(String nodename) throws SAXException {
329 133 jones
      try {
330
        PreparedStatement pstmt;
331 1217 tao
        pstmt = connection.prepareStatement(
332 133 jones
              "UPDATE xml_nodes set nodename = ? " +
333
              "WHERE nodeid = ?");
334 1217 tao
        // Increase DBConnection usage count
335
        connection.increaseUsageCount(1);
336 16 jones
337 133 jones
        // Bind the values to the query
338
        pstmt.setString(1, nodename);
339 134 jones
        pstmt.setLong(2, getNodeID());
340 133 jones
        // Do the insertion
341
        pstmt.execute();
342
        pstmt.close();
343
      } catch (SQLException e) {
344 2358 sgarg
        System.out.println("Error in DBSaxNode.writeNodeName: " +
345 675 berkley
                           e.getMessage());
346 461 bojilova
        throw new SAXException(e.getMessage());
347 133 jones
      }
348
  }
349 16 jones
350 1803 tao
 /** creates SQL code and inserts new node into DB connection */
351 2358 sgarg
  public long writeDTDNodeToDB(String nodename, String data, String docid)
352
                                 throws SAXException
353 1803 tao
  {
354
    long nid = -1;
355 2358 sgarg
    try
356 1803 tao
    {
357 2358 sgarg
358 1803 tao
      PreparedStatement pstmt;
359 6012 leinfelder
      Timestamp timestampData = null;
360 5208 daigle
      logMetacat.info("DBSAXNode.writeDTDNodeToDB - Insert dtd into db: "+nodename +" "+data);
361 2358 sgarg
      if(data != null && !data.trim().equals("")){
362 6012 leinfelder
    	  try{
363
    		  // try some common ISO 8601 formats
364
    		  SimpleDateFormat sdf = null;
365
    		  if (data.length() == 10) {
366
    			  sdf = new SimpleDateFormat("yyyy-MM-dd");
367
    		  } else if (data.length() == 19) {
368
    			  sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss");
369
    		  }
370
    		  Date dateData = sdf.parse(data);
371
    		  timestampData = new Timestamp(dateData.getTime());
372
              pstmt = connection.prepareStatement(
373
                  "INSERT INTO xml_nodes " +
374
                  "(nodetype, nodename, docid, " +
375
                  "rootnodeid, parentnodeid, nodedata, nodeindex, nodedatadate) " +
376
                  "VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
377
          } catch (ParseException pe) {
378
    	  	try{
379 2358 sgarg
                double numberData = Double.parseDouble(data);
380
                pstmt = connection.prepareStatement(
381
                    "INSERT INTO xml_nodes " +
382
                    "(nodetype, nodename, docid, " +
383
                    "rootnodeid, parentnodeid, nodedata, nodeindex, nodedatanumerical) " +
384
                    "VALUES (?, ?, ?, ?, ?, ?, ?, "+ numberData +")");
385
            } catch (NumberFormatException nfe) {
386
                pstmt = connection.prepareStatement(
387
                    "INSERT INTO xml_nodes " +
388
                    "(nodetype, nodename,  docid, " +
389
                    "rootnodeid, parentnodeid, nodedata, nodeindex) " +
390
                    "VALUES (?, ?, ?, ?, ?, ?, ?)");
391
            }
392 6012 leinfelder
          }
393 2358 sgarg
        } else {
394
            pstmt = connection.prepareStatement(
395
                  "INSERT INTO xml_nodes " +
396
                  "(nodetype, nodename, docid, " +
397
                  "rootnodeid, parentnodeid, nodedata, nodeindex) " +
398
                  "VALUES (?, ?, ?, ?, ?, ?, ?)");
399
        }
400
401 1803 tao
       // Increase DBConnection usage count
402
      connection.increaseUsageCount(1);
403 2358 sgarg
404 1803 tao
      // Bind the values to the query
405
      pstmt.setString(1, DocumentImpl.DTD);
406
      pstmt.setString(2, nodename);
407
      pstmt.setString(3, docid);
408
      pstmt.setLong(4, getRootNodeID());
409
      pstmt.setLong(5, getParentID());
410
      pstmt.setString(6, data);
411
      pstmt.setInt(7, getNodeIndex());
412 6012 leinfelder
      if (timestampData != null) {
413
    	  pstmt.setTimestamp(8, timestampData);
414
      }
415 2358 sgarg
416 1803 tao
      // Do the insertion
417
      pstmt.execute();
418
      pstmt.close();
419
420
      // get the generated unique id afterward
421 5319 jones
      nid = DatabaseService.getInstance().getDBAdapter().getUniqueID(connection.getConnections(), "xml_nodes");
422 1803 tao
      //should incease connection usage!!!!!!
423 2358 sgarg
424 1803 tao
    } catch (SQLException e) {
425
      System.out.println("Error in DBSaxNode.writeDTDNodeToDB");
426
      System.err.println("Error inserting node: (" + DocumentImpl.DTD + ", " +
427 2358 sgarg
                                                     nodename + ", " +
428 1803 tao
                                                     data + ")" );
429
      System.err.println(e.getMessage());
430
      e.printStackTrace(System.err);
431
      throw new SAXException(e.getMessage());
432
    }
433
    return nid;
434
  }
435 2358 sgarg
436
437 174 bojilova
  /** get next node id from DB connection */
438 461 bojilova
  private long generateNodeID() throws SAXException {
439 174 bojilova
      long nid=0;
440 133 jones
      Statement stmt;
441 1217 tao
      DBConnection dbConn = null;
442
      int serialNumber = -1;
443 133 jones
      try {
444 1217 tao
        // Get DBConnection
445
        dbConn=DBConnectionPool.getDBConnection("DBSAXNode.generateNodeID");
446
        serialNumber=dbConn.getCheckOutSerialNumber();
447
        stmt = dbConn.createStatement();
448 174 bojilova
        stmt.execute("SELECT xml_nodes_id_seq.nextval FROM dual");
449 133 jones
        ResultSet rs = stmt.getResultSet();
450
        boolean tableHasRows = rs.next();
451
        if (tableHasRows) {
452 174 bojilova
          nid = rs.getLong(1);
453 19 jones
        }
454 133 jones
        stmt.close();
455
      } catch (SQLException e) {
456 2358 sgarg
        System.out.println("Error in DBSaxNode.generateNodeID: " +
457 675 berkley
                            e.getMessage());
458 461 bojilova
        throw new SAXException(e.getMessage());
459 15 jones
      }
460 1217 tao
      finally
461
      {
462
        // Return DBconnection
463
        DBConnectionPool.returnDBConnection(dbConn, serialNumber);
464
      }//finally
465 15 jones
466 174 bojilova
      return nid;
467 133 jones
  }
468 18 jones
469 137 jones
  /** Add a new attribute to this node, or set its value */
470 1416 tao
  public long setAttribute(String attName, String attValue, String docid)
471 2358 sgarg
              throws SAXException
472 1416 tao
  {
473
    long nodeId = -1;
474 2358 sgarg
    if (attName != null)
475 1416 tao
    {
476 133 jones
      // Enter the attribute in the hash table
477
      super.setAttribute(attName, attValue);
478 18 jones
479 133 jones
      // And enter the attribute in the database
480 1416 tao
      nodeId = writeChildNodeToDB("ATTRIBUTE", attName, attValue, docid);
481 2358 sgarg
    }
482
    else
483 1416 tao
    {
484 133 jones
      System.err.println("Attribute name must not be null!");
485 461 bojilova
      throw new SAXException("Attribute name must not be null!");
486 18 jones
    }
487 1416 tao
    return nodeId;
488 133 jones
  }
489 176 jones
490 821 bojilova
  /** Add a namespace to this node */
491 1416 tao
  public long setNamespace(String prefix, String uri, String docid)
492 2358 sgarg
              throws SAXException
493 1416 tao
  {
494
    long nodeId = -1;
495 2358 sgarg
    if (prefix != null)
496 1416 tao
    {
497 821 bojilova
      // Enter the namespace in a hash table
498
      super.setNamespace(prefix, uri);
499
      // And enter the namespace in the database
500 1416 tao
      nodeId = writeChildNodeToDB("NAMESPACE", prefix, uri, docid);
501 2358 sgarg
    }
502
    else
503 1416 tao
    {
504 821 bojilova
      System.err.println("Namespace prefix must not be null!");
505
      throw new SAXException("Namespace prefix must not be null!");
506
    }
507 1416 tao
    return nodeId;
508 821 bojilova
  }
509
510 176 jones
511 2358 sgarg
512
  /**
513 471 bojilova
   * USED FROM SEPARATE THREAD RUNNED from DBSAXHandler on endDocument()
514
   * Update the node index (xml_index) for this node by generating
515
   * test strings that represent all of the relative and absolute
516
   * paths through the XML tree from document root to this node
517
   */
518 2358 sgarg
  public void updateNodeIndex(DBConnection conn, String docid, String doctype)
519 471 bojilova
               throws SAXException
520
  {
521
    Hashtable pathlist = new Hashtable();
522
    boolean atStartingNode = true;
523
    boolean atRootDocumentNode = false;
524
    DBSAXNode nodePointer = this;
525
    StringBuffer currentPath = new StringBuffer();
526
    int counter = 0;
527
528
    // Create a Hashtable of all of the paths to reach this node
529
    // including absolute paths and relative paths
530
    while (!atRootDocumentNode) {
531
      if (atStartingNode) {
532
        currentPath.insert(0, nodePointer.getTagName());
533
        pathlist.put(currentPath.toString(), new Long(getNodeID()));
534
        counter++;
535
        atStartingNode = false;
536
      } else {
537
        currentPath.insert(0, "/");
538
        currentPath.insert(0, nodePointer.getTagName());
539
        pathlist.put(currentPath.toString(), new Long(getNodeID()));
540
        counter++;
541
      }
542
543
      // advance to the next parent node
544
      nodePointer = nodePointer.getParentNode();
545
546
      // If we're at the DOCUMENT node (root of DOM tree), add
547
      // the root "/" to make the absolute path
548
      if (nodePointer.getNodeType().equals("DOCUMENT")) {
549
        currentPath.insert(0, "/");
550
        pathlist.put(currentPath.toString(), new Long(getNodeID()));
551
        counter++;
552
        atRootDocumentNode = true;
553 2358 sgarg
      }
554 471 bojilova
    }
555
556
    try {
557
      // Create an insert statement to reuse for all of the path insertions
558
      PreparedStatement pstmt = conn.prepareStatement(
559 2358 sgarg
              "INSERT INTO xml_index (nodeid, path, docid, doctype, " +
560
               "parentnodeid) " +
561 471 bojilova
              "VALUES (?, ?, ?, ?, ?)");
562 1217 tao
      // Increase usage count
563
      conn.increaseUsageCount(1);
564 2358 sgarg
565 471 bojilova
      pstmt.setString(3, docid);
566
      pstmt.setString(4, doctype);
567
      pstmt.setLong(5, getParentID());
568 2358 sgarg
569 471 bojilova
      // Step through the hashtable and insert each of the path values
570 176 jones
      Enumeration en = pathlist.keys();
571
      while (en.hasMoreElements()) {
572
        String path = (String)en.nextElement();
573
        Long nodeid = (Long)pathlist.get(path);
574
        pstmt.setLong(1, nodeid.longValue());
575
        pstmt.setString(2, path);
576 2358 sgarg
577 457 bojilova
        pstmt.executeUpdate();
578 176 jones
      }
579
      // Close the database statement
580
      pstmt.close();
581
    } catch (SQLException sqe) {
582 675 berkley
      System.err.println("SQL Exception while inserting path to index in " +
583 899 berkley
                         "DBSAXNode.updateNodeIndex for document " + docid);
584 176 jones
      System.err.println(sqe.getMessage());
585 461 bojilova
      throw new SAXException(sqe.getMessage());
586 176 jones
    }
587
  }
588 2358 sgarg
589 176 jones
  /** get the parent of this node */
590
  public DBSAXNode getParentNode() {
591
    return parentNode;
592
  }
593 15 jones
}