Revision 388
Added by Matt Jones over 24 years ago
src/edu/ucsb/nceas/metacat/ElementNode.java | ||
---|---|---|
16 | 16 |
|
17 | 17 |
import java.sql.*; |
18 | 18 |
import java.io.IOException; |
19 |
import java.util.Enumeration; |
|
20 |
import java.util.Iterator; |
|
21 |
import java.util.TreeSet; |
|
19 | 22 |
import java.util.Vector; |
20 |
import java.util.Enumeration; |
|
21 | 23 |
|
22 | 24 |
/** |
23 | 25 |
* A Class that represents an XML element and its contents, |
... | ... | |
25 | 27 |
*/ |
26 | 28 |
public class ElementNode extends BasicNode { |
27 | 29 |
|
28 |
private Connection conn; |
|
30 |
/** |
|
31 |
* Construct a new ElementNode instance, and recursively create its children |
|
32 |
* |
|
33 |
* @param nodeRecordList the nodedata to use to initialize, which is a |
|
34 |
* TreeSet of NodeRecord objects |
|
35 |
* @param nodeid the identifier for the node to be created |
|
36 |
*/ |
|
37 |
public ElementNode (TreeSet nodeRecordList, long nodeid) { |
|
29 | 38 |
|
30 |
/** |
|
31 |
* Construct a new ElementNode instance |
|
32 |
* |
|
33 |
* @param conn the database connection to use to initialize |
|
34 |
*/ |
|
35 |
public ElementNode (Connection conn) { |
|
36 |
this.conn = conn; |
|
37 |
} |
|
39 |
MetaCatUtil util = new MetaCatUtil(); |
|
38 | 40 |
|
39 |
/** |
|
40 |
* Construct a new ElementNode instance |
|
41 |
* |
|
42 |
* @param conn the database connection to use to initialize |
|
43 |
* @param nodeid the element_id for the node to be created |
|
44 |
*/ |
|
45 |
public ElementNode (Connection conn, long nodeid) { |
|
46 |
this(conn); |
|
41 |
// Step through all of the node records we were given |
|
42 |
Iterator it = nodeRecordList.iterator(); |
|
43 |
while (it.hasNext()) { |
|
44 |
NodeRecord currentNode = (NodeRecord)it.next(); |
|
45 |
if (currentNode.nodeid == nodeid) { |
|
46 |
util.debugMessage("Got Node ID: " + currentNode.nodeid + |
|
47 |
" (" + currentNode.parentnodeid + |
|
48 |
", " + currentNode.nodeindex + |
|
49 |
", " + currentNode.nodetype + ")"); |
|
50 |
// Process the current node |
|
51 |
setNodeType(currentNode.nodetype); |
|
52 |
setNodeID(currentNode.nodeid); |
|
53 |
setParentID(currentNode.parentnodeid); |
|
54 |
setTagName(currentNode.nodename); |
|
55 |
} else { |
|
56 |
// Process the children nodes |
|
57 |
if (currentNode.parentnodeid == getNodeID()) { |
|
58 |
util.debugMessage(" Processing child: " + currentNode.nodeid + |
|
59 |
" (" + currentNode.parentnodeid + |
|
60 |
", " + currentNode.nodeindex + |
|
61 |
", " + currentNode.nodetype + ")"); |
|
47 | 62 |
|
48 |
//Lookup data for self |
|
49 |
setElementInfo(nodeid); |
|
50 |
|
|
51 |
//Create child nodes (elements or attributes) |
|
52 |
setChildrenNodes(nodeid); |
|
53 |
} |
|
63 |
if ((currentNode.nodetype).equals("ELEMENT")) { |
|
64 |
util.debugMessage("Creating child node: " + currentNode.nodeid); |
|
65 |
ElementNode child = new ElementNode(nodeRecordList, |
|
66 |
currentNode.nodeid); |
|
67 |
addChildNode(child); |
|
68 |
} else if (currentNode.nodetype.equals("ATTRIBUTE")) { |
|
69 |
setAttribute(currentNode.nodename,currentNode.nodedata); |
|
70 |
} else if (currentNode.nodetype.equals("TEXT")) { |
|
71 |
TextNode child = new TextNode(currentNode.nodeid, |
|
72 |
currentNode.parentnodeid, |
|
73 |
currentNode.nodedata); |
|
74 |
addChildNode(child); |
|
75 |
} else if (currentNode.nodetype.equals("COMMENT")) { |
|
76 |
CommentNode child = new CommentNode(currentNode.nodeid, |
|
77 |
currentNode.parentnodeid, |
|
78 |
currentNode.nodedata); |
|
79 |
addChildNode(child); |
|
80 |
} else if (currentNode.nodetype.equals("PI")) { |
|
81 |
PINode child = new PINode(currentNode.nodeid, |
|
82 |
currentNode.parentnodeid, |
|
83 |
currentNode.nodename, |
|
84 |
currentNode.nodedata); |
|
85 |
addChildNode(child); |
|
86 |
} |
|
54 | 87 |
|
55 |
/** |
|
56 |
* Construct a new ElementNode instance |
|
57 |
* |
|
58 |
* @param conn the database connection to use to initialize |
|
59 |
* @param nodeid the element_id for the node to be created |
|
60 |
* @param parentnodeid the id of the parent node |
|
61 |
* @param nodename the name of the element |
|
62 |
*/ |
|
63 |
public ElementNode (Connection conn, long nodeid, long parentnodeid, |
|
64 |
String nodename, String nodetype) { |
|
65 |
this(conn); |
|
66 |
setNodeID(nodeid); |
|
67 |
setParentID(parentnodeid); |
|
68 |
setTagName(nodename); |
|
69 |
setNodeType(nodetype); |
|
70 |
|
|
71 |
|
|
72 |
// Create child nodes (elements or attributes) |
|
73 |
setChildrenNodes(nodeid); |
|
74 |
} |
|
75 |
|
|
76 |
/** Look up the info needed to construct this element from the DB */ |
|
77 |
private void setElementInfo(long nodeid) { |
|
78 |
long element_id=0; |
|
79 |
long parentnodeid=0; |
|
80 |
String nodetype=null; |
|
81 |
String nodename=null; |
|
82 |
String nodedata=null; |
|
83 |
|
|
84 |
PreparedStatement pstmt; |
|
85 |
try { |
|
86 |
pstmt = |
|
87 |
conn.prepareStatement("SELECT nodeid,parentnodeid,nodetype,nodename,"+ |
|
88 |
"replace(" + |
|
89 |
"replace(" + |
|
90 |
"replace(nodedata,'&','&') " + |
|
91 |
",'<','<') " + |
|
92 |
",'>','>') " + |
|
93 |
"FROM xml_nodes WHERE nodeid = ?"); |
|
94 |
// Bind the values to the query |
|
95 |
pstmt.setLong(1, nodeid); |
|
96 |
|
|
97 |
pstmt.execute(); |
|
98 |
try { |
|
99 |
ResultSet rs = pstmt.getResultSet(); |
|
100 |
try { |
|
101 |
boolean tableHasRows = rs.next(); |
|
102 |
if (tableHasRows) { |
|
103 |
try { |
|
104 |
element_id = rs.getLong(1); |
|
105 |
parentnodeid = rs.getLong(2); |
|
106 |
nodetype = rs.getString(3); |
|
107 |
nodename = rs.getString(4); |
|
108 |
nodedata = rs.getString(5); |
|
109 |
} catch (SQLException e) { |
|
110 |
System.out.println("Error with getInt: " + e.getMessage()); |
|
111 |
} |
|
112 |
} else { |
|
113 |
System.err.println("Error: no rows for nodeid: " + nodeid); |
|
114 |
} |
|
115 |
} catch (SQLException e) { |
|
116 |
System.out.println("Error with next: " + e.getMessage()); |
|
117 |
} |
|
118 |
} catch (SQLException e) { |
|
119 |
System.out.println("Error with getrset: " + e.getMessage()); |
|
88 |
} else { |
|
89 |
util.debugMessage(" Discarding child: " + currentNode.nodeid + |
|
90 |
" (" + currentNode.parentnodeid + |
|
91 |
", " + currentNode.nodeindex + |
|
92 |
", " + currentNode.nodetype + ")"); |
|
120 | 93 |
} |
121 |
pstmt.close(); |
|
122 |
} catch (SQLException e) { |
|
123 |
System.out.println("Error getting id: " + e.getMessage()); |
|
124 | 94 |
} |
125 |
|
|
126 |
// Record our node type |
|
127 |
setNodeType(nodetype); |
|
128 |
|
|
129 |
try { |
|
130 |
if (nodetype.equals("ELEMENT") || nodetype.equals("DOCUMENT") ) { |
|
131 |
setNodeID(element_id); |
|
132 |
setParentID(parentnodeid); |
|
133 |
setTagName(nodename); |
|
134 |
} |
|
135 |
} catch (NullPointerException npe) { |
|
136 |
System.err.println("Error with nodetype: " + |
|
137 |
nodetype + npe.getMessage()); |
|
138 |
System.err.println("Nodeid: " + element_id); |
|
139 |
} |
|
140 | 95 |
} |
96 |
} |
|
141 | 97 |
|
142 |
/** Look up each child node from the DB and and create it */ |
|
143 |
private void setChildrenNodes(long nodeid) { |
|
144 |
long element_id=0; |
|
145 |
long parentnodeid=0; |
|
146 |
String nodetype=null; |
|
147 |
String nodename=null; |
|
148 |
String nodedata=null; |
|
98 |
/** |
|
99 |
* String representation for display purposes (recursively descends through |
|
100 |
* children to create an XML subtree) |
|
101 |
*/ |
|
102 |
public String toString () { |
|
149 | 103 |
|
150 |
PreparedStatement pstmt; |
|
151 |
try { |
|
152 |
pstmt = |
|
153 |
conn.prepareStatement("SELECT nodeid,parentnodeid,nodetype, " + |
|
154 |
"nodename, " + |
|
155 |
"replace(" + |
|
156 |
"replace(" + |
|
157 |
"replace(nodedata,'&','&')" + |
|
158 |
",'<','<') " + |
|
159 |
",'>','>') " + |
|
160 |
"FROM xml_nodes WHERE parentnodeid = ? " + |
|
161 |
"ORDER BY nodeindex"); |
|
104 |
StringBuffer value = new StringBuffer(); |
|
105 |
String nodetype = getNodeType(); |
|
162 | 106 |
|
163 |
// Bind the values to the query |
|
164 |
pstmt.setLong(1, nodeid); |
|
107 |
if (nodetype.equals("ELEMENT")) { |
|
108 |
value.append('<'); |
|
109 |
value.append(getTagName()); |
|
110 |
value.append(getAttributes().toString()); |
|
111 |
value.append('>'); |
|
112 |
} |
|
165 | 113 |
|
166 |
pstmt.execute(); |
|
167 |
try { |
|
168 |
ResultSet rs = pstmt.getResultSet(); |
|
169 |
try { |
|
170 |
boolean tableHasRows = rs.next(); |
|
171 |
while (tableHasRows) { |
|
172 |
try { |
|
173 |
element_id = rs.getLong(1); |
|
174 |
parentnodeid = rs.getLong(2); |
|
175 |
nodetype = rs.getString(3); |
|
176 |
nodename = rs.getString(4); |
|
177 |
nodedata = rs.getString(5); |
|
114 |
// Process children recursively here |
|
115 |
BasicNode child = null; |
|
116 |
Enumeration e = getChildren(); |
|
117 |
while (e.hasMoreElements()) { |
|
118 |
child = (BasicNode)e.nextElement(); |
|
119 |
value.append(child); |
|
120 |
} |
|
178 | 121 |
|
179 |
if ( (nodetype.equals("ELEMENT")) || |
|
180 |
(nodetype.equals("DOCUMENT")) |
|
181 |
) { |
|
182 |
ElementNode child = new ElementNode(conn, |
|
183 |
element_id,parentnodeid,nodename, nodetype); |
|
184 |
addChildNode(child); |
|
185 |
} else if (nodetype.equals("ATTRIBUTE")) { |
|
186 |
setAttribute(nodename,nodedata); |
|
187 |
} else if (nodetype.equals("TEXT")) { |
|
188 |
TextNode child = new TextNode(element_id,parentnodeid, |
|
189 |
nodedata); |
|
190 |
addChildNode(child); |
|
191 |
} else if (nodetype.equals("COMMENT")) { |
|
192 |
CommentNode child = new CommentNode(element_id,parentnodeid, |
|
193 |
nodedata); |
|
194 |
addChildNode(child); |
|
195 |
} else if (nodetype.equals("PI")) { |
|
196 |
PINode child = new PINode(element_id,parentnodeid,nodename, |
|
197 |
nodedata); |
|
198 |
addChildNode(child); |
|
199 |
} |
|
200 |
|
|
201 |
} catch (SQLException e) { |
|
202 |
System.out.println("Error with getInt: " + e.getMessage()); |
|
203 |
} |
|
204 |
|
|
205 |
// Advance to the next record in the cursor |
|
206 |
tableHasRows = rs.next(); |
|
207 |
} |
|
208 |
} catch (SQLException e) { |
|
209 |
System.out.println("Error with next: " + e.getMessage()); |
|
210 |
} |
|
211 |
} catch (SQLException e) { |
|
212 |
System.out.println("Error with getrset: " + e.getMessage()); |
|
213 |
} |
|
214 |
pstmt.close(); |
|
215 |
} catch (SQLException e) { |
|
216 |
System.out.println("Error getting id: " + e.getMessage()); |
|
217 |
} |
|
218 |
|
|
122 |
if (nodetype.equals("ELEMENT")) { |
|
123 |
value.append("</"); |
|
124 |
value.append(getTagName()); |
|
125 |
value.append('>'); |
|
219 | 126 |
} |
220 | 127 |
|
221 |
/** |
|
222 |
* String representation for display purposes (recursively descends through |
|
223 |
* children to create an XML subtree) |
|
224 |
*/ |
|
225 |
public String toString () { |
|
226 |
|
|
227 |
StringBuffer value = new StringBuffer(); |
|
228 |
String nodetype = getNodeType(); |
|
229 |
|
|
230 |
if (nodetype.equals("ELEMENT")) { |
|
231 |
value.append('<'); |
|
232 |
value.append(getTagName()); |
|
233 |
value.append(getAttributes().toString()); |
|
234 |
value.append('>'); |
|
235 |
} |
|
236 |
|
|
237 |
// Process children recursively here |
|
238 |
BasicNode child = null; |
|
239 |
Enumeration e = getChildren(); |
|
240 |
while (e.hasMoreElements()) { |
|
241 |
child = (BasicNode)e.nextElement(); |
|
242 |
value.append(child); |
|
243 |
} |
|
244 |
|
|
245 |
if (nodetype.equals("ELEMENT")) { |
|
246 |
value.append("</"); |
|
247 |
value.append(getTagName()); |
|
248 |
value.append('>'); |
|
249 |
} |
|
250 |
|
|
251 |
return value.toString(); |
|
252 |
} |
|
128 |
return value.toString(); |
|
129 |
} |
|
253 | 130 |
} |
254 |
|
|
255 |
/** |
|
256 |
* '$Log$ |
|
257 |
* 'Revision 1.21 2000/06/28 02:36:26 jones |
|
258 |
* 'Added feature to now ouput COMMENTs and PIs when the document is |
|
259 |
* 'read from the database with DBReader. |
|
260 |
* ' |
|
261 |
* 'Revision 1.20 2000/06/26 10:35:05 jones |
|
262 |
* 'Merged in substantial changes to DBWriter and associated classes and to |
|
263 |
* 'the MetaCatServlet in order to accomodate the new UPDATE and DELETE |
|
264 |
* 'functions. The command line tools and the parameters for the |
|
265 |
* 'servlet have changed substantially. |
|
266 |
* ' |
|
267 |
* 'Revision 1.19.2.2 2000/06/25 23:38:16 jones |
|
268 |
* 'Added RCSfile keyword |
|
269 |
* ' |
|
270 |
* 'Revision 1.19.2.1 2000/06/25 23:34:18 jones |
|
271 |
* 'Changed documentation formatting, added log entries at bottom of source files |
|
272 |
* '' |
|
273 |
*/ |
src/edu/ucsb/nceas/metacat/DBReader.java | ||
---|---|---|
14 | 14 |
|
15 | 15 |
package edu.ucsb.nceas.metacat; |
16 | 16 |
|
17 |
import java.io.*; |
|
18 | 17 |
import java.sql.*; |
18 |
import java.io.PrintWriter; |
|
19 |
import java.util.TreeSet; |
|
19 | 20 |
|
20 | 21 |
/** |
21 | 22 |
* A Class that creates an XML text document |
... | ... | |
75 | 76 |
} |
76 | 77 |
|
77 | 78 |
/** |
78 |
* Get the root node id for an XML document given a document id
|
|
79 |
* Create an XML document from the database for the document with ID docid
|
|
79 | 80 |
* |
80 |
* @param docid the document node contains the root of the document |
|
81 |
* @returns long the nodeid of the root node for this document |
|
81 |
* @param docid the document identifier that we want retrieved |
|
82 | 82 |
*/ |
83 |
private long getRootNode(String docid) |
|
84 |
throws McdbDocNotFoundException |
|
85 |
{ |
|
86 |
// Now look up the root node id |
|
87 |
long rootnodeid = 0; |
|
88 |
|
|
89 |
try { |
|
90 |
PreparedStatement pstmt = |
|
91 |
conn.prepareStatement("SELECT rootnodeid " + |
|
92 |
"FROM xml_documents " + |
|
93 |
"WHERE docid = ?"); |
|
94 |
// Bind the values to the query |
|
95 |
pstmt.setString(1, docid); |
|
96 |
|
|
97 |
pstmt.execute(); |
|
98 |
ResultSet rs = pstmt.getResultSet(); |
|
99 |
boolean tableHasRows = rs.next(); |
|
100 |
if (tableHasRows) { |
|
101 |
rootnodeid = rs.getLong(1); |
|
102 |
|
|
103 |
} |
|
104 |
pstmt.close(); |
|
105 |
} catch (SQLException e) { |
|
106 |
throw new McdbDocNotFoundException( |
|
107 |
"Root node not found for: " + docid, e); |
|
108 |
} |
|
109 |
|
|
110 |
return rootnodeid; |
|
111 |
} |
|
112 |
|
|
113 |
/** |
|
114 |
* Create an XML document from the database starting with the element |
|
115 |
* having element_id nodeid |
|
116 |
* |
|
117 |
* @param docid the document that we want retrieved |
|
118 |
*/ |
|
119 | 83 |
public String readXMLDocument(String docid) |
120 | 84 |
throws McdbException |
121 | 85 |
{ |
122 | 86 |
try { |
123 | 87 |
StringBuffer doc = new StringBuffer(); |
88 |
|
|
89 |
// Look up the document information |
|
124 | 90 |
DoctypeInfo dti = getDoctypeInfo(docid); |
125 |
|
|
126 | 91 |
String docname = dti.getDocname(); |
127 | 92 |
String doctype = dti.getDoctype(); |
128 | 93 |
String sysid = dti.getSystemID(); |
94 |
long rootnodeid = dti.getRootNodeID(); |
|
129 | 95 |
|
130 |
ElementNode element = new ElementNode(conn, getRootNode(docid)); |
|
96 |
// Download all of the document nodes using a single SQL query |
|
97 |
TreeSet nodeRecordList = getNodeRecordList(docid); |
|
98 |
|
|
99 |
// Create the elements from the downloaded nodes |
|
100 |
//ElementNode element = new ElementNode(conn, rootnodeid); |
|
101 |
ElementNode element = new ElementNode(nodeRecordList, rootnodeid); |
|
102 |
|
|
103 |
// Append the resulting document to the StringBuffer |
|
131 | 104 |
doc.append("<?xml version=\"1.0\"?>\n"); |
132 | 105 |
|
133 | 106 |
if (docname != null) { |
... | ... | |
155 | 128 |
* @param docid the id of the document to look up |
156 | 129 |
*/ |
157 | 130 |
public DoctypeInfo getDoctypeInfo(String docid) |
158 |
throws McdbException |
|
131 |
throws McdbException
|
|
159 | 132 |
{ |
160 | 133 |
PreparedStatement pstmt; |
161 | 134 |
String doctype = null; |
162 | 135 |
String docname = null; |
163 | 136 |
String sysid = null; |
137 |
long rootnodeid = 0; |
|
164 | 138 |
DoctypeInfo dti = null; |
165 | 139 |
|
166 | 140 |
try { |
167 | 141 |
pstmt = |
168 |
conn.prepareStatement("SELECT docname,doctype " + |
|
142 |
conn.prepareStatement("SELECT docname,doctype,rootnodeid " +
|
|
169 | 143 |
"FROM xml_documents " + |
170 | 144 |
"WHERE docid = ?"); |
171 | 145 |
// Bind the values to the query |
... | ... | |
175 | 149 |
ResultSet rs = pstmt.getResultSet(); |
176 | 150 |
boolean tableHasRows = rs.next(); |
177 | 151 |
if (tableHasRows) { |
178 |
docname = rs.getString(1); |
|
179 |
doctype = rs.getString(2); |
|
152 |
docname = rs.getString(1); |
|
153 |
doctype = rs.getString(2); |
|
154 |
rootnodeid = rs.getLong(3); |
|
180 | 155 |
} |
181 | 156 |
pstmt.close(); |
182 | 157 |
|
... | ... | |
201 | 176 |
} |
202 | 177 |
|
203 | 178 |
if (docname != null) { |
204 |
dti = new DoctypeInfo(docname, doctype, sysid); |
|
179 |
dti = new DoctypeInfo(docname, doctype, sysid, rootnodeid);
|
|
205 | 180 |
return dti; |
206 | 181 |
} else { |
207 | 182 |
throw new McdbDocNotFoundException("Document not found: " + docid); |
... | ... | |
209 | 184 |
} |
210 | 185 |
|
211 | 186 |
/** |
187 |
* Look up the node data from the database |
|
188 |
* |
|
189 |
* @param docid the id of the document to look up |
|
190 |
*/ |
|
191 |
public TreeSet getNodeRecordList(String docid) |
|
192 |
throws McdbException |
|
193 |
{ |
|
194 |
PreparedStatement pstmt; |
|
195 |
TreeSet nodeRecordList = new TreeSet(new NodeComparator()); |
|
196 |
long nodeid = 0; |
|
197 |
long parentnodeid = 0; |
|
198 |
long nodeindex = 0; |
|
199 |
String nodetype = null; |
|
200 |
String nodename = null; |
|
201 |
String nodedata = null; |
|
202 |
|
|
203 |
try { |
|
204 |
pstmt = |
|
205 |
conn.prepareStatement("SELECT nodeid,parentnodeid,nodeindex, " + |
|
206 |
"nodetype,nodename,"+ |
|
207 |
"replace(" + |
|
208 |
"replace(" + |
|
209 |
"replace(nodedata,'&','&') " + |
|
210 |
",'<','<') " + |
|
211 |
",'>','>') " + |
|
212 |
"FROM xml_nodes WHERE docid = ?"); |
|
213 |
|
|
214 |
// Bind the values to the query |
|
215 |
pstmt.setString(1, docid); |
|
216 |
|
|
217 |
pstmt.execute(); |
|
218 |
ResultSet rs = pstmt.getResultSet(); |
|
219 |
boolean tableHasRows = rs.next(); |
|
220 |
while (tableHasRows) { |
|
221 |
nodeid = rs.getLong(1); |
|
222 |
parentnodeid = rs.getLong(2); |
|
223 |
nodeindex = rs.getLong(3); |
|
224 |
nodetype = rs.getString(4); |
|
225 |
nodename = rs.getString(5); |
|
226 |
nodedata = rs.getString(6); |
|
227 |
|
|
228 |
// add the data to the node record list hashtable |
|
229 |
NodeRecord currentRecord = new NodeRecord(nodeid, parentnodeid, |
|
230 |
nodeindex, nodetype, nodename, nodedata); |
|
231 |
nodeRecordList.add(currentRecord); |
|
232 |
|
|
233 |
// Advance to the next node |
|
234 |
tableHasRows = rs.next(); |
|
235 |
} |
|
236 |
pstmt.close(); |
|
237 |
|
|
238 |
} catch (SQLException e) { |
|
239 |
throw new McdbException("Error accessing database connection.", e); |
|
240 |
} |
|
241 |
|
|
242 |
if (nodeRecordList != null) { |
|
243 |
return nodeRecordList; |
|
244 |
} else { |
|
245 |
throw new McdbException("Error getting node data: " + docid); |
|
246 |
} |
|
247 |
} |
|
248 |
|
|
249 |
/** |
|
212 | 250 |
* A utility class that encapsulates document type information |
213 | 251 |
*/ |
214 | 252 |
public class DoctypeInfo { |
215 | 253 |
private String docname = null; |
216 | 254 |
private String doctype = null; |
217 | 255 |
private String system_id = null; |
256 |
private long rootnodeid; |
|
218 | 257 |
|
219 | 258 |
/** |
220 | 259 |
* Constructor |
221 | 260 |
*/ |
222 |
public DoctypeInfo(String docname, String doctype, String system_id) { |
|
261 |
public DoctypeInfo(String docname, String doctype, |
|
262 |
String system_id, long rootnodeid) { |
|
223 | 263 |
this.docname = docname; |
224 | 264 |
this.doctype = doctype; |
225 | 265 |
this.system_id = system_id; |
266 |
this.rootnodeid = rootnodeid; |
|
226 | 267 |
} |
227 | 268 |
|
228 | 269 |
/** |
... | ... | |
245 | 286 |
public String getSystemID() { |
246 | 287 |
return system_id; |
247 | 288 |
} |
289 |
|
|
290 |
/** |
|
291 |
* get the root node identifier |
|
292 |
*/ |
|
293 |
public long getRootNodeID() { |
|
294 |
return rootnodeid; |
|
295 |
} |
|
248 | 296 |
} |
249 |
|
|
250 | 297 |
} |
251 |
|
|
252 |
/** |
|
253 |
* '$Log$ |
|
254 |
* 'Revision 1.19 2000/08/11 22:20:04 jones |
|
255 |
* 'Changed exception handling mechanisms for DBReader |
|
256 |
* ' |
|
257 |
* 'Revision 1.18 2000/08/10 22:39:04 jones |
|
258 |
* 'changed getRootNode method from public to private |
|
259 |
* ' |
|
260 |
* 'Revision 1.17 2000/06/26 10:35:05 jones |
|
261 |
* 'Merged in substantial changes to DBWriter and associated classes and to |
|
262 |
* 'the MetaCatServlet in order to accomodate the new UPDATE and DELETE |
|
263 |
* 'functions. The command line tools and the parameters for the |
|
264 |
* 'servlet have changed substantially. |
|
265 |
* ' |
|
266 |
* 'Revision 1.16.2.2 2000/06/25 23:38:16 jones |
|
267 |
* 'Added RCSfile keyword |
|
268 |
* ' |
|
269 |
* 'Revision 1.16.2.1 2000/06/25 23:34:17 jones |
|
270 |
* 'Changed documentation formatting, added log entries at bottom of source files |
|
271 |
* '' |
|
272 |
*/ |
src/edu/ucsb/nceas/metacat/NodeComparator.java | ||
---|---|---|
1 |
/** |
|
2 |
* '$RCSfile$' |
|
3 |
* Purpose: A Class that sorts two NodeRecords |
|
4 |
* Copyright: 2000 Regents of the University of California and the |
|
5 |
* National Center for Ecological Analysis and Synthesis |
|
6 |
* Authors: Matt Jones |
|
7 |
* Release: @release@ |
|
8 |
* |
|
9 |
* '$Author$' |
|
10 |
* '$Date$' |
|
11 |
* '$Revision$' |
|
12 |
*/ |
|
13 |
|
|
14 |
package edu.ucsb.nceas.metacat; |
|
15 |
|
|
16 |
import java.util.Comparator; |
|
17 |
|
|
18 |
/** |
|
19 |
* A utility class that sorts two node records |
|
20 |
*/ |
|
21 |
public class NodeComparator implements Comparator { |
|
22 |
|
|
23 |
static int LESS = -1; |
|
24 |
static int EQUALS = 0; |
|
25 |
static int GREATER = 1; |
|
26 |
|
|
27 |
/** |
|
28 |
* Constructor |
|
29 |
*/ |
|
30 |
public NodeComparator() { |
|
31 |
} |
|
32 |
|
|
33 |
/** |
|
34 |
* compare two objects to determine proper sort order |
|
35 |
*/ |
|
36 |
public int compare(Object o1, Object o2) { |
|
37 |
return compare((NodeRecord)o1, (NodeRecord)o2); |
|
38 |
} |
|
39 |
|
|
40 |
/** |
|
41 |
* compare two NodeRecord objects to determine proper sort order. The |
|
42 |
* node records are equal if their nodeid fields are equal. One is |
|
43 |
* less than another if its parentnodeid is less, or if its parentnodeid |
|
44 |
* is equal and its nodeindex is less. One is greater than another if |
|
45 |
* its parentnodeid is greater, or if its parentnodeid is equal and |
|
46 |
* its nodeindex is greater. |
|
47 |
*/ |
|
48 |
private int compare(NodeRecord o1, NodeRecord o2) { |
|
49 |
if (o1.nodeid == o2.nodeid) { |
|
50 |
return EQUALS; |
|
51 |
} else if (o1.parentnodeid < o2.parentnodeid) { |
|
52 |
return LESS; |
|
53 |
} else if (o1.parentnodeid > o2.parentnodeid) { |
|
54 |
return GREATER; |
|
55 |
} else if (o1.parentnodeid == o2.parentnodeid) { |
|
56 |
if (o1.nodeindex < o2.nodeindex) { |
|
57 |
return LESS; |
|
58 |
} else if (o1.nodeindex > o2.nodeindex) { |
|
59 |
return GREATER; |
|
60 |
} else { |
|
61 |
// this should never happen because (parentnodeid,nodeindex) is unique |
|
62 |
return EQUALS; |
|
63 |
} |
|
64 |
} else { |
|
65 |
// this should never happen because parentnodeid is always <,>, or = |
|
66 |
return EQUALS; |
|
67 |
} |
|
68 |
} |
|
69 |
} |
|
0 | 70 |
src/edu/ucsb/nceas/metacat/NodeRecord.java | ||
---|---|---|
1 |
/** |
|
2 |
* '$RCSfile$' |
|
3 |
* Purpose: A Class that represents all node info from the database |
|
4 |
* Copyright: 2000 Regents of the University of California and the |
|
5 |
* National Center for Ecological Analysis and Synthesis |
|
6 |
* Authors: Matt Jones |
|
7 |
* Release: @release@ |
|
8 |
* |
|
9 |
* '$Author$' |
|
10 |
* '$Date$' |
|
11 |
* '$Revision$' |
|
12 |
*/ |
|
13 |
|
|
14 |
package edu.ucsb.nceas.metacat; |
|
15 |
|
|
16 |
/** |
|
17 |
* A utility class that encapsulates a node and its data |
|
18 |
*/ |
|
19 |
public class NodeRecord { |
|
20 |
public long nodeid; |
|
21 |
public long parentnodeid; |
|
22 |
public long nodeindex; |
|
23 |
public String nodename = null; |
|
24 |
public String nodetype = null; |
|
25 |
public String nodedata = null; |
|
26 |
|
|
27 |
/** |
|
28 |
* Constructor |
|
29 |
*/ |
|
30 |
public NodeRecord(long nodeid, long parentnodeid, long nodeindex, |
|
31 |
String nodetype, String nodename, String nodedata) { |
|
32 |
this.nodeid = nodeid; |
|
33 |
this.parentnodeid = parentnodeid; |
|
34 |
this.nodeindex = nodeindex; |
|
35 |
this.nodename = nodename; |
|
36 |
this.nodetype = nodetype; |
|
37 |
this.nodedata = nodedata; |
|
38 |
} |
|
39 |
} |
|
0 | 40 |
Also available in: Unified diff
Modified the DBReader and ElementNode classes to more efficiently read
documents from the database. In the old implementation, a db connection
was opened for each and every node in a document, recursively fromthe root
node. In the new implementation, all of the data from xml_nodes is
retrieved from the database with a single query and stored in a
sorted list of NodeRecord, and the node tree is build from this data structure
recursively. This should speed up the document retieval substantially,
especially as the document repository grows.