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