1 |
22
|
jones
|
/**
|
2 |
130
|
jones
|
* Name: ElementNode.java
|
3 |
22
|
jones
|
* Purpose: A Class that represents an XML element and its contents,
|
4 |
|
|
* and can build itself from a database connection
|
5 |
35
|
jones
|
* Copyright: 2000 Regents of the University of California and the
|
6 |
|
|
* National Center for Ecological Analysis and Synthesis
|
7 |
22
|
jones
|
* Authors: Matt Jones
|
8 |
|
|
*
|
9 |
|
|
* Version: '$Id$'
|
10 |
|
|
*/
|
11 |
|
|
|
12 |
51
|
jones
|
package edu.ucsb.nceas.metacat;
|
13 |
22
|
jones
|
|
14 |
|
|
import java.sql.*;
|
15 |
|
|
import java.io.IOException;
|
16 |
23
|
jones
|
import java.util.Vector;
|
17 |
22
|
jones
|
import java.util.Enumeration;
|
18 |
|
|
|
19 |
31
|
jones
|
/**
|
20 |
|
|
* A Class that represents an XML element and its contents,
|
21 |
|
|
* and can build itself from a database connection
|
22 |
|
|
*/
|
23 |
130
|
jones
|
public class ElementNode extends BasicNode {
|
24 |
22
|
jones
|
|
25 |
23
|
jones
|
private Connection conn;
|
26 |
22
|
jones
|
|
27 |
31
|
jones
|
/**
|
28 |
130
|
jones
|
* Construct a new ElementNode instance
|
29 |
31
|
jones
|
*
|
30 |
|
|
* @param conn the database connection to use to initialize
|
31 |
|
|
*/
|
32 |
130
|
jones
|
public ElementNode (Connection conn) {
|
33 |
24
|
jones
|
this.conn = conn;
|
34 |
|
|
}
|
35 |
|
|
|
36 |
31
|
jones
|
/**
|
37 |
130
|
jones
|
* Construct a new ElementNode instance
|
38 |
31
|
jones
|
*
|
39 |
|
|
* @param conn the database connection to use to initialize
|
40 |
|
|
* @param nodeid the element_id for the node to be created
|
41 |
|
|
*/
|
42 |
130
|
jones
|
public ElementNode (Connection conn, long nodeid) {
|
43 |
24
|
jones
|
this(conn);
|
44 |
22
|
jones
|
|
45 |
|
|
//Lookup data for self
|
46 |
24
|
jones
|
setElementInfo(nodeid);
|
47 |
22
|
jones
|
|
48 |
24
|
jones
|
//Create child nodes (elements or attributes)
|
49 |
|
|
setChildrenNodes(nodeid);
|
50 |
|
|
}
|
51 |
22
|
jones
|
|
52 |
31
|
jones
|
/**
|
53 |
130
|
jones
|
* Construct a new ElementNode instance
|
54 |
31
|
jones
|
*
|
55 |
|
|
* @param conn the database connection to use to initialize
|
56 |
|
|
* @param nodeid the element_id for the node to be created
|
57 |
|
|
* @param parentnodeid the id of the parent node
|
58 |
|
|
* @param nodename the name of the element
|
59 |
|
|
*/
|
60 |
130
|
jones
|
public ElementNode (Connection conn, long nodeid, long parentnodeid,
|
61 |
127
|
jones
|
String nodename, String nodetype) {
|
62 |
24
|
jones
|
this(conn);
|
63 |
134
|
jones
|
setNodeID(nodeid);
|
64 |
24
|
jones
|
setParentID(parentnodeid);
|
65 |
|
|
setTagName(nodename);
|
66 |
127
|
jones
|
setNodeType(nodetype);
|
67 |
24
|
jones
|
|
68 |
132
|
jones
|
|
69 |
24
|
jones
|
// Create child nodes (elements or attributes)
|
70 |
|
|
setChildrenNodes(nodeid);
|
71 |
22
|
jones
|
}
|
72 |
23
|
jones
|
|
73 |
24
|
jones
|
/** Look up the info needed to construct this element from the DB */
|
74 |
|
|
private void setElementInfo(long nodeid) {
|
75 |
23
|
jones
|
long element_id=0;
|
76 |
|
|
long parentnodeid=0;
|
77 |
|
|
String nodetype=null;
|
78 |
|
|
String nodename=null;
|
79 |
|
|
String nodedata=null;
|
80 |
|
|
|
81 |
|
|
PreparedStatement pstmt;
|
82 |
|
|
try {
|
83 |
|
|
pstmt =
|
84 |
96
|
jones
|
conn.prepareStatement("SELECT nodeid,parentnodeid,nodetype,nodename,"+
|
85 |
|
|
"replace(" +
|
86 |
|
|
"replace(" +
|
87 |
|
|
"replace(nodedata,'&','&') " +
|
88 |
|
|
",'<','<') " +
|
89 |
|
|
",'>','>') " +
|
90 |
|
|
"FROM xml_nodes WHERE nodeid = ?");
|
91 |
23
|
jones
|
// Bind the values to the query
|
92 |
|
|
pstmt.setLong(1, nodeid);
|
93 |
|
|
|
94 |
|
|
pstmt.execute();
|
95 |
|
|
try {
|
96 |
|
|
ResultSet rs = pstmt.getResultSet();
|
97 |
|
|
try {
|
98 |
|
|
boolean tableHasRows = rs.next();
|
99 |
|
|
if (tableHasRows) {
|
100 |
|
|
try {
|
101 |
|
|
element_id = rs.getLong(1);
|
102 |
|
|
parentnodeid = rs.getLong(2);
|
103 |
|
|
nodetype = rs.getString(3);
|
104 |
|
|
nodename = rs.getString(4);
|
105 |
|
|
nodedata = rs.getString(5);
|
106 |
|
|
} catch (SQLException e) {
|
107 |
|
|
System.out.println("Error with getInt: " + e.getMessage());
|
108 |
|
|
}
|
109 |
132
|
jones
|
} else {
|
110 |
|
|
System.err.println("Error: no rows for nodeid: " + nodeid);
|
111 |
23
|
jones
|
}
|
112 |
|
|
} catch (SQLException e) {
|
113 |
|
|
System.out.println("Error with next: " + e.getMessage());
|
114 |
|
|
}
|
115 |
|
|
} catch (SQLException e) {
|
116 |
|
|
System.out.println("Error with getrset: " + e.getMessage());
|
117 |
|
|
}
|
118 |
|
|
pstmt.close();
|
119 |
|
|
} catch (SQLException e) {
|
120 |
|
|
System.out.println("Error getting id: " + e.getMessage());
|
121 |
|
|
}
|
122 |
|
|
|
123 |
122
|
jones
|
// Record our node type
|
124 |
|
|
setNodeType(nodetype);
|
125 |
|
|
|
126 |
132
|
jones
|
try {
|
127 |
122
|
jones
|
if (nodetype.equals("ELEMENT") || nodetype.equals("DOCUMENT") ) {
|
128 |
134
|
jones
|
setNodeID(element_id);
|
129 |
23
|
jones
|
setParentID(parentnodeid);
|
130 |
|
|
setTagName(nodename);
|
131 |
|
|
}
|
132 |
132
|
jones
|
} catch (NullPointerException npe) {
|
133 |
|
|
System.err.println("Error with nodetype: " +
|
134 |
|
|
nodetype + npe.getMessage());
|
135 |
|
|
System.err.println("Nodeid: " + element_id);
|
136 |
|
|
}
|
137 |
23
|
jones
|
}
|
138 |
24
|
jones
|
|
139 |
|
|
/** Look up each child node from the DB and and create it */
|
140 |
|
|
private void setChildrenNodes(long nodeid) {
|
141 |
|
|
long element_id=0;
|
142 |
|
|
long parentnodeid=0;
|
143 |
|
|
String nodetype=null;
|
144 |
|
|
String nodename=null;
|
145 |
|
|
String nodedata=null;
|
146 |
|
|
|
147 |
|
|
PreparedStatement pstmt;
|
148 |
|
|
try {
|
149 |
|
|
pstmt =
|
150 |
|
|
conn.prepareStatement("SELECT nodeid,parentnodeid,nodetype, " +
|
151 |
96
|
jones
|
"nodename, " +
|
152 |
|
|
"replace(" +
|
153 |
|
|
"replace(" +
|
154 |
|
|
"replace(nodedata,'&','&')" +
|
155 |
|
|
",'<','<') " +
|
156 |
|
|
",'>','>') " +
|
157 |
136
|
jones
|
"FROM xml_nodes WHERE parentnodeid = ? " +
|
158 |
|
|
"ORDER BY nodeindex");
|
159 |
122
|
jones
|
|
160 |
24
|
jones
|
// Bind the values to the query
|
161 |
|
|
pstmt.setLong(1, nodeid);
|
162 |
|
|
|
163 |
|
|
pstmt.execute();
|
164 |
|
|
try {
|
165 |
|
|
ResultSet rs = pstmt.getResultSet();
|
166 |
|
|
try {
|
167 |
|
|
boolean tableHasRows = rs.next();
|
168 |
|
|
while (tableHasRows) {
|
169 |
|
|
try {
|
170 |
|
|
element_id = rs.getLong(1);
|
171 |
|
|
parentnodeid = rs.getLong(2);
|
172 |
|
|
nodetype = rs.getString(3);
|
173 |
|
|
nodename = rs.getString(4);
|
174 |
|
|
nodedata = rs.getString(5);
|
175 |
|
|
|
176 |
122
|
jones
|
if ( (nodetype.equals("ELEMENT")) ||
|
177 |
128
|
jones
|
(nodetype.equals("DOCUMENT"))
|
178 |
122
|
jones
|
) {
|
179 |
130
|
jones
|
ElementNode child = new ElementNode(conn,
|
180 |
127
|
jones
|
element_id,parentnodeid,nodename, nodetype);
|
181 |
|
|
addChildNode(child);
|
182 |
24
|
jones
|
} else if (nodetype.equals("ATTRIBUTE")) {
|
183 |
|
|
setAttribute(nodename,nodedata);
|
184 |
128
|
jones
|
} else if (nodetype.equals("TEXT")) {
|
185 |
157
|
jones
|
TextNode child = new TextNode(element_id,parentnodeid,
|
186 |
|
|
nodedata,nodetype);
|
187 |
128
|
jones
|
addChildNode(child);
|
188 |
126
|
jones
|
}
|
189 |
24
|
jones
|
|
190 |
|
|
} catch (SQLException e) {
|
191 |
|
|
System.out.println("Error with getInt: " + e.getMessage());
|
192 |
|
|
}
|
193 |
|
|
|
194 |
|
|
// Advance to the next record in the cursor
|
195 |
|
|
tableHasRows = rs.next();
|
196 |
|
|
}
|
197 |
|
|
} catch (SQLException e) {
|
198 |
|
|
System.out.println("Error with next: " + e.getMessage());
|
199 |
|
|
}
|
200 |
|
|
} catch (SQLException e) {
|
201 |
|
|
System.out.println("Error with getrset: " + e.getMessage());
|
202 |
|
|
}
|
203 |
|
|
pstmt.close();
|
204 |
|
|
} catch (SQLException e) {
|
205 |
|
|
System.out.println("Error getting id: " + e.getMessage());
|
206 |
|
|
}
|
207 |
|
|
|
208 |
|
|
}
|
209 |
|
|
|
210 |
31
|
jones
|
/**
|
211 |
|
|
* String representation for display purposes (recursively descends through
|
212 |
|
|
* children to create an XML subtree)
|
213 |
|
|
*/
|
214 |
128
|
jones
|
public String toString () {
|
215 |
|
|
|
216 |
24
|
jones
|
StringBuffer value = new StringBuffer();
|
217 |
128
|
jones
|
String nodetype = getNodeType();
|
218 |
24
|
jones
|
|
219 |
122
|
jones
|
if (nodetype.equals("ELEMENT")) {
|
220 |
|
|
value.append('<');
|
221 |
|
|
value.append(getTagName());
|
222 |
|
|
value.append(getAttributes().toString());
|
223 |
|
|
value.append('>');
|
224 |
|
|
}
|
225 |
|
|
|
226 |
24
|
jones
|
// Process children recursively here
|
227 |
129
|
jones
|
BasicNode child = null;
|
228 |
127
|
jones
|
Enumeration e = getChildren();
|
229 |
24
|
jones
|
while (e.hasMoreElements()) {
|
230 |
129
|
jones
|
child = (BasicNode)e.nextElement();
|
231 |
128
|
jones
|
value.append(child);
|
232 |
24
|
jones
|
}
|
233 |
|
|
|
234 |
122
|
jones
|
if (nodetype.equals("ELEMENT")) {
|
235 |
|
|
value.append("</");
|
236 |
|
|
value.append(getTagName());
|
237 |
|
|
value.append('>');
|
238 |
|
|
}
|
239 |
|
|
|
240 |
24
|
jones
|
return value.toString();
|
241 |
|
|
}
|
242 |
22
|
jones
|
}
|