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 |
349
|
jones
|
* Release: @release@
|
8 |
15
|
jones
|
*
|
9 |
203
|
jones
|
* '$Author$'
|
10 |
|
|
* '$Date$'
|
11 |
|
|
* '$Revision$'
|
12 |
669
|
jones
|
*
|
13 |
|
|
* This program is free software; you can redistribute it and/or modify
|
14 |
|
|
* it under the terms of the GNU General Public License as published by
|
15 |
|
|
* the Free Software Foundation; either version 2 of the License, or
|
16 |
|
|
* (at your option) any later version.
|
17 |
|
|
*
|
18 |
|
|
* This program is distributed in the hope that it will be useful,
|
19 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
20 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
21 |
|
|
* GNU General Public License for more details.
|
22 |
|
|
*
|
23 |
|
|
* You should have received a copy of the GNU General Public License
|
24 |
|
|
* along with this program; if not, write to the Free Software
|
25 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
26 |
15
|
jones
|
*/
|
27 |
|
|
|
28 |
75
|
jones
|
package edu.ucsb.nceas.metacat;
|
29 |
15
|
jones
|
|
30 |
|
|
import java.sql.*;
|
31 |
|
|
import java.io.IOException;
|
32 |
|
|
import java.util.Hashtable;
|
33 |
|
|
import java.util.Enumeration;
|
34 |
763
|
bojilova
|
//import oracle.jdbc.driver.*;
|
35 |
461
|
bojilova
|
import org.xml.sax.SAXException;
|
36 |
15
|
jones
|
|
37 |
777
|
bojilova
|
import edu.ucsb.nceas.dbadapter.AbstractDatabase;
|
38 |
747
|
bojilova
|
|
39 |
31
|
jones
|
/**
|
40 |
137
|
jones
|
* A Class that represents an XML node and its contents and
|
41 |
31
|
jones
|
* can write its own representation to a database connection
|
42 |
|
|
*/
|
43 |
137
|
jones
|
public class DBSAXNode extends BasicNode {
|
44 |
15
|
jones
|
|
45 |
1217
|
tao
|
private DBConnection connection;
|
46 |
176
|
jones
|
private DBSAXNode parentNode;
|
47 |
777
|
bojilova
|
private static final AbstractDatabase dbAdapter = MetaCatUtil.dbAdapter;
|
48 |
15
|
jones
|
|
49 |
133
|
jones
|
/**
|
50 |
136
|
jones
|
* Construct a new node instance for DOCUMENT nodes
|
51 |
133
|
jones
|
*
|
52 |
|
|
* @param conn the JDBC Connection to which all information is written
|
53 |
|
|
*/
|
54 |
1217
|
tao
|
public DBSAXNode (DBConnection conn, String docid) throws SAXException {
|
55 |
747
|
bojilova
|
|
56 |
408
|
jones
|
super();
|
57 |
1217
|
tao
|
this.connection = conn;
|
58 |
176
|
jones
|
this.parentNode = null;
|
59 |
457
|
bojilova
|
writeChildNodeToDB("DOCUMENT", null, null, docid);
|
60 |
758
|
bojilova
|
updateRootNodeID(getNodeID());
|
61 |
136
|
jones
|
}
|
62 |
|
|
|
63 |
|
|
/**
|
64 |
|
|
* Construct a new node instance for ELEMENT nodes
|
65 |
|
|
*
|
66 |
|
|
* @param conn the JDBC Connection to which all information is written
|
67 |
|
|
* @param tagname the name of the node
|
68 |
|
|
* @param parentNode the parent node for this node being created
|
69 |
|
|
*/
|
70 |
1217
|
tao
|
public DBSAXNode (DBConnection conn, String qName, String lName,
|
71 |
826
|
bojilova
|
DBSAXNode parentNode, long rootnodeid,
|
72 |
|
|
String docid, String doctype)
|
73 |
|
|
throws SAXException {
|
74 |
21
|
jones
|
|
75 |
826
|
bojilova
|
super(lName);
|
76 |
1217
|
tao
|
this.connection = conn;
|
77 |
747
|
bojilova
|
this.parentNode = parentNode;
|
78 |
136
|
jones
|
setParentID(parentNode.getNodeID());
|
79 |
457
|
bojilova
|
setRootNodeID(rootnodeid);
|
80 |
|
|
setDocID(docid);
|
81 |
136
|
jones
|
setNodeIndex(parentNode.incChildNum());
|
82 |
826
|
bojilova
|
writeChildNodeToDB("ELEMENT", qName, null, docid);
|
83 |
471
|
bojilova
|
//No writing XML Index from here. New Thread used instead.
|
84 |
|
|
//updateNodeIndex(docid, doctype);
|
85 |
133
|
jones
|
}
|
86 |
15
|
jones
|
|
87 |
133
|
jones
|
/** creates SQL code and inserts new node into DB connection */
|
88 |
1416
|
tao
|
public long writeChildNodeToDB(String nodetype, String nodename,
|
89 |
461
|
bojilova
|
String data, String docid)
|
90 |
1416
|
tao
|
throws SAXException
|
91 |
|
|
{
|
92 |
|
|
long nid = -1;
|
93 |
|
|
try
|
94 |
|
|
{
|
95 |
|
|
|
96 |
133
|
jones
|
PreparedStatement pstmt;
|
97 |
|
|
if (nodetype == "DOCUMENT") {
|
98 |
1217
|
tao
|
pstmt = connection.prepareStatement(
|
99 |
133
|
jones
|
"INSERT INTO xml_nodes " +
|
100 |
826
|
bojilova
|
"(nodetype, nodename, nodeprefix, docid) " +
|
101 |
|
|
"VALUES (?, ?, ?, ?)");
|
102 |
1217
|
tao
|
|
103 |
|
|
// Increase DBConnection usage count
|
104 |
|
|
connection.increaseUsageCount(1);
|
105 |
1495
|
tao
|
MetaCatUtil.debugMessage("INSERTING DOCNAME: " + nodename, 35);
|
106 |
133
|
jones
|
} else {
|
107 |
1217
|
tao
|
pstmt = connection.prepareStatement(
|
108 |
133
|
jones
|
"INSERT INTO xml_nodes " +
|
109 |
826
|
bojilova
|
"(nodetype, nodename, nodeprefix, docid, " +
|
110 |
457
|
bojilova
|
"rootnodeid, parentnodeid, nodedata, nodeindex) " +
|
111 |
826
|
bojilova
|
"VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
|
112 |
1217
|
tao
|
// Increase DBConnection usage count
|
113 |
|
|
connection.increaseUsageCount(1);
|
114 |
133
|
jones
|
}
|
115 |
19
|
jones
|
|
116 |
133
|
jones
|
// Bind the values to the query
|
117 |
758
|
bojilova
|
pstmt.setString(1, nodetype);
|
118 |
826
|
bojilova
|
int idx;
|
119 |
|
|
if ( nodename != null && (idx = nodename.indexOf(":")) != -1 ) {
|
120 |
|
|
pstmt.setString(2, nodename.substring(idx+1));
|
121 |
|
|
pstmt.setString(3, nodename.substring(0,idx));
|
122 |
|
|
} else {
|
123 |
|
|
pstmt.setString(2, nodename);
|
124 |
|
|
pstmt.setString(3, null);
|
125 |
|
|
}
|
126 |
|
|
pstmt.setString(4, docid);
|
127 |
408
|
jones
|
if (nodetype == "DOCUMENT") {
|
128 |
758
|
bojilova
|
// moved it to separate method updateRootNodeID
|
129 |
|
|
//pstmt.setLong(4, nid);
|
130 |
408
|
jones
|
} else {
|
131 |
133
|
jones
|
if (nodetype == "ELEMENT") {
|
132 |
826
|
bojilova
|
pstmt.setLong(5, getRootNodeID());
|
133 |
|
|
pstmt.setLong(6, getParentID());
|
134 |
|
|
pstmt.setString(7, data);
|
135 |
|
|
pstmt.setInt(8, getNodeIndex());
|
136 |
133
|
jones
|
} else {
|
137 |
826
|
bojilova
|
pstmt.setLong(5, getRootNodeID());
|
138 |
|
|
pstmt.setLong(6, getNodeID());
|
139 |
|
|
pstmt.setString(7, data);
|
140 |
|
|
pstmt.setInt(8, incChildNum());
|
141 |
72
|
bojilova
|
}
|
142 |
133
|
jones
|
}
|
143 |
|
|
// Do the insertion
|
144 |
|
|
pstmt.execute();
|
145 |
|
|
pstmt.close();
|
146 |
758
|
bojilova
|
|
147 |
|
|
// get the generated unique id afterward
|
148 |
1416
|
tao
|
nid = dbAdapter.getUniqueID(connection.getConnections(), "xml_nodes");
|
149 |
1217
|
tao
|
//should incease connection usage!!!!!!
|
150 |
|
|
|
151 |
174
|
bojilova
|
|
152 |
408
|
jones
|
if (nodetype.equals("DOCUMENT")) {
|
153 |
|
|
// Record the root node id that was generated from the database
|
154 |
|
|
setRootNodeID(nid);
|
155 |
|
|
}
|
156 |
|
|
|
157 |
177
|
jones
|
if (nodetype.equals("DOCUMENT") || nodetype.equals("ELEMENT")) {
|
158 |
176
|
jones
|
|
159 |
177
|
jones
|
// Record the node id that was generated from the database
|
160 |
|
|
setNodeID(nid);
|
161 |
176
|
jones
|
|
162 |
177
|
jones
|
// Record the node type that was passed to the method
|
163 |
|
|
setNodeType(nodetype);
|
164 |
|
|
|
165 |
|
|
}
|
166 |
|
|
|
167 |
133
|
jones
|
} catch (SQLException e) {
|
168 |
675
|
berkley
|
System.out.println("Error in DBSaxNode.writeChildNodeToDB");
|
169 |
133
|
jones
|
System.err.println("Error inserting node: (" + nodetype + ", " +
|
170 |
|
|
nodename + ", " +
|
171 |
136
|
jones
|
data + ")" );
|
172 |
133
|
jones
|
System.err.println(e.getMessage());
|
173 |
915
|
berkley
|
e.printStackTrace(System.err);
|
174 |
461
|
bojilova
|
throw new SAXException(e.getMessage());
|
175 |
110
|
bojilova
|
}
|
176 |
1416
|
tao
|
return nid;
|
177 |
133
|
jones
|
}
|
178 |
110
|
bojilova
|
|
179 |
133
|
jones
|
/**
|
180 |
758
|
bojilova
|
* update rootnodeid=nodeid for 'DOCUMENT' type of nodes only
|
181 |
|
|
*/
|
182 |
|
|
public void updateRootNodeID(long nodeid) throws SAXException {
|
183 |
|
|
try {
|
184 |
|
|
PreparedStatement pstmt;
|
185 |
1217
|
tao
|
pstmt = connection.prepareStatement(
|
186 |
758
|
bojilova
|
"UPDATE xml_nodes set rootnodeid = ? " +
|
187 |
|
|
"WHERE nodeid = ?");
|
188 |
1217
|
tao
|
// Increase DBConnection usage count
|
189 |
|
|
connection.increaseUsageCount(1);
|
190 |
758
|
bojilova
|
|
191 |
|
|
// Bind the values to the query
|
192 |
|
|
pstmt.setLong(1, nodeid);
|
193 |
|
|
pstmt.setLong(2, nodeid);
|
194 |
|
|
// Do the update
|
195 |
|
|
pstmt.execute();
|
196 |
|
|
pstmt.close();
|
197 |
|
|
} catch (SQLException e) {
|
198 |
|
|
System.out.println("Error in DBSaxNode.updateRootNodeID: " +
|
199 |
|
|
e.getMessage());
|
200 |
|
|
throw new SAXException(e.getMessage());
|
201 |
|
|
}
|
202 |
|
|
}
|
203 |
|
|
|
204 |
|
|
/**
|
205 |
133
|
jones
|
* creates SQL code to put nodename for the document node
|
206 |
|
|
* into DB connection
|
207 |
|
|
*/
|
208 |
461
|
bojilova
|
public void writeNodename(String nodename) throws SAXException {
|
209 |
133
|
jones
|
try {
|
210 |
|
|
PreparedStatement pstmt;
|
211 |
1217
|
tao
|
pstmt = connection.prepareStatement(
|
212 |
133
|
jones
|
"UPDATE xml_nodes set nodename = ? " +
|
213 |
|
|
"WHERE nodeid = ?");
|
214 |
1217
|
tao
|
// Increase DBConnection usage count
|
215 |
|
|
connection.increaseUsageCount(1);
|
216 |
16
|
jones
|
|
217 |
133
|
jones
|
// Bind the values to the query
|
218 |
|
|
pstmt.setString(1, nodename);
|
219 |
134
|
jones
|
pstmt.setLong(2, getNodeID());
|
220 |
133
|
jones
|
// Do the insertion
|
221 |
|
|
pstmt.execute();
|
222 |
|
|
pstmt.close();
|
223 |
|
|
} catch (SQLException e) {
|
224 |
675
|
berkley
|
System.out.println("Error in DBSaxNode.writeNodeName: " +
|
225 |
|
|
e.getMessage());
|
226 |
461
|
bojilova
|
throw new SAXException(e.getMessage());
|
227 |
133
|
jones
|
}
|
228 |
|
|
}
|
229 |
16
|
jones
|
|
230 |
174
|
bojilova
|
/** get next node id from DB connection */
|
231 |
461
|
bojilova
|
private long generateNodeID() throws SAXException {
|
232 |
174
|
bojilova
|
long nid=0;
|
233 |
133
|
jones
|
Statement stmt;
|
234 |
1217
|
tao
|
DBConnection dbConn = null;
|
235 |
|
|
int serialNumber = -1;
|
236 |
133
|
jones
|
try {
|
237 |
1217
|
tao
|
// Get DBConnection
|
238 |
|
|
dbConn=DBConnectionPool.getDBConnection("DBSAXNode.generateNodeID");
|
239 |
|
|
serialNumber=dbConn.getCheckOutSerialNumber();
|
240 |
|
|
stmt = dbConn.createStatement();
|
241 |
174
|
bojilova
|
stmt.execute("SELECT xml_nodes_id_seq.nextval FROM dual");
|
242 |
133
|
jones
|
ResultSet rs = stmt.getResultSet();
|
243 |
|
|
boolean tableHasRows = rs.next();
|
244 |
|
|
if (tableHasRows) {
|
245 |
174
|
bojilova
|
nid = rs.getLong(1);
|
246 |
19
|
jones
|
}
|
247 |
133
|
jones
|
stmt.close();
|
248 |
|
|
} catch (SQLException e) {
|
249 |
675
|
berkley
|
System.out.println("Error in DBSaxNode.generateNodeID: " +
|
250 |
|
|
e.getMessage());
|
251 |
461
|
bojilova
|
throw new SAXException(e.getMessage());
|
252 |
15
|
jones
|
}
|
253 |
1217
|
tao
|
finally
|
254 |
|
|
{
|
255 |
|
|
// Return DBconnection
|
256 |
|
|
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
257 |
|
|
}//finally
|
258 |
15
|
jones
|
|
259 |
174
|
bojilova
|
return nid;
|
260 |
133
|
jones
|
}
|
261 |
18
|
jones
|
|
262 |
137
|
jones
|
/** Add a new attribute to this node, or set its value */
|
263 |
1416
|
tao
|
public long setAttribute(String attName, String attValue, String docid)
|
264 |
|
|
throws SAXException
|
265 |
|
|
{
|
266 |
|
|
long nodeId = -1;
|
267 |
|
|
if (attName != null)
|
268 |
|
|
{
|
269 |
133
|
jones
|
// Enter the attribute in the hash table
|
270 |
|
|
super.setAttribute(attName, attValue);
|
271 |
18
|
jones
|
|
272 |
133
|
jones
|
// And enter the attribute in the database
|
273 |
1416
|
tao
|
nodeId = writeChildNodeToDB("ATTRIBUTE", attName, attValue, docid);
|
274 |
|
|
}
|
275 |
|
|
else
|
276 |
|
|
{
|
277 |
133
|
jones
|
System.err.println("Attribute name must not be null!");
|
278 |
461
|
bojilova
|
throw new SAXException("Attribute name must not be null!");
|
279 |
18
|
jones
|
}
|
280 |
1416
|
tao
|
return nodeId;
|
281 |
133
|
jones
|
}
|
282 |
176
|
jones
|
|
283 |
821
|
bojilova
|
/** Add a namespace to this node */
|
284 |
1416
|
tao
|
public long setNamespace(String prefix, String uri, String docid)
|
285 |
|
|
throws SAXException
|
286 |
|
|
{
|
287 |
|
|
long nodeId = -1;
|
288 |
|
|
if (prefix != null)
|
289 |
|
|
{
|
290 |
821
|
bojilova
|
// Enter the namespace in a hash table
|
291 |
|
|
super.setNamespace(prefix, uri);
|
292 |
|
|
// And enter the namespace in the database
|
293 |
1416
|
tao
|
nodeId = writeChildNodeToDB("NAMESPACE", prefix, uri, docid);
|
294 |
|
|
}
|
295 |
|
|
else
|
296 |
|
|
{
|
297 |
821
|
bojilova
|
System.err.println("Namespace prefix must not be null!");
|
298 |
|
|
throw new SAXException("Namespace prefix must not be null!");
|
299 |
|
|
}
|
300 |
1416
|
tao
|
return nodeId;
|
301 |
821
|
bojilova
|
}
|
302 |
|
|
|
303 |
1217
|
tao
|
|
304 |
176
|
jones
|
|
305 |
471
|
bojilova
|
/**
|
306 |
|
|
* USED FROM SEPARATE THREAD RUNNED from DBSAXHandler on endDocument()
|
307 |
|
|
* Update the node index (xml_index) for this node by generating
|
308 |
|
|
* test strings that represent all of the relative and absolute
|
309 |
|
|
* paths through the XML tree from document root to this node
|
310 |
|
|
*/
|
311 |
1217
|
tao
|
public void updateNodeIndex(DBConnection conn, String docid, String doctype)
|
312 |
471
|
bojilova
|
throws SAXException
|
313 |
|
|
{
|
314 |
|
|
Hashtable pathlist = new Hashtable();
|
315 |
|
|
boolean atStartingNode = true;
|
316 |
|
|
boolean atRootDocumentNode = false;
|
317 |
|
|
DBSAXNode nodePointer = this;
|
318 |
|
|
StringBuffer currentPath = new StringBuffer();
|
319 |
|
|
int counter = 0;
|
320 |
|
|
|
321 |
|
|
// Create a Hashtable of all of the paths to reach this node
|
322 |
|
|
// including absolute paths and relative paths
|
323 |
|
|
while (!atRootDocumentNode) {
|
324 |
|
|
if (atStartingNode) {
|
325 |
|
|
currentPath.insert(0, nodePointer.getTagName());
|
326 |
|
|
pathlist.put(currentPath.toString(), new Long(getNodeID()));
|
327 |
|
|
counter++;
|
328 |
|
|
atStartingNode = false;
|
329 |
|
|
} else {
|
330 |
|
|
currentPath.insert(0, "/");
|
331 |
|
|
currentPath.insert(0, nodePointer.getTagName());
|
332 |
|
|
pathlist.put(currentPath.toString(), new Long(getNodeID()));
|
333 |
|
|
counter++;
|
334 |
|
|
}
|
335 |
|
|
|
336 |
|
|
// advance to the next parent node
|
337 |
|
|
nodePointer = nodePointer.getParentNode();
|
338 |
|
|
|
339 |
|
|
// If we're at the DOCUMENT node (root of DOM tree), add
|
340 |
|
|
// the root "/" to make the absolute path
|
341 |
|
|
if (nodePointer.getNodeType().equals("DOCUMENT")) {
|
342 |
|
|
currentPath.insert(0, "/");
|
343 |
|
|
pathlist.put(currentPath.toString(), new Long(getNodeID()));
|
344 |
|
|
counter++;
|
345 |
|
|
atRootDocumentNode = true;
|
346 |
|
|
}
|
347 |
|
|
}
|
348 |
|
|
|
349 |
|
|
try {
|
350 |
|
|
// Create an insert statement to reuse for all of the path insertions
|
351 |
|
|
PreparedStatement pstmt = conn.prepareStatement(
|
352 |
|
|
"INSERT INTO xml_index (nodeid, path, docid, doctype, " +
|
353 |
|
|
"parentnodeid) " +
|
354 |
|
|
"VALUES (?, ?, ?, ?, ?)");
|
355 |
1217
|
tao
|
// Increase usage count
|
356 |
|
|
conn.increaseUsageCount(1);
|
357 |
471
|
bojilova
|
|
358 |
|
|
pstmt.setString(3, docid);
|
359 |
|
|
pstmt.setString(4, doctype);
|
360 |
|
|
pstmt.setLong(5, getParentID());
|
361 |
|
|
|
362 |
|
|
// Step through the hashtable and insert each of the path values
|
363 |
176
|
jones
|
Enumeration en = pathlist.keys();
|
364 |
|
|
while (en.hasMoreElements()) {
|
365 |
|
|
String path = (String)en.nextElement();
|
366 |
|
|
Long nodeid = (Long)pathlist.get(path);
|
367 |
|
|
pstmt.setLong(1, nodeid.longValue());
|
368 |
|
|
pstmt.setString(2, path);
|
369 |
899
|
berkley
|
|
370 |
457
|
bojilova
|
pstmt.executeUpdate();
|
371 |
176
|
jones
|
}
|
372 |
|
|
// Close the database statement
|
373 |
|
|
pstmt.close();
|
374 |
|
|
} catch (SQLException sqe) {
|
375 |
675
|
berkley
|
System.err.println("SQL Exception while inserting path to index in " +
|
376 |
899
|
berkley
|
"DBSAXNode.updateNodeIndex for document " + docid);
|
377 |
176
|
jones
|
System.err.println(sqe.getMessage());
|
378 |
461
|
bojilova
|
throw new SAXException(sqe.getMessage());
|
379 |
176
|
jones
|
}
|
380 |
|
|
}
|
381 |
|
|
|
382 |
|
|
/** get the parent of this node */
|
383 |
|
|
public DBSAXNode getParentNode() {
|
384 |
|
|
return parentNode;
|
385 |
|
|
}
|
386 |
15
|
jones
|
}
|