1
|
/**
|
2
|
* Name: ReaderElement.java
|
3
|
* Purpose: A Class that represents an XML element and its contents,
|
4
|
* and can build itself from a database connection
|
5
|
* Institution: National Center for Ecological Analysis and Synthesis
|
6
|
* Copyright: 2000
|
7
|
* Authors: Matt Jones
|
8
|
*
|
9
|
* Version: '$Id: ReaderElement.java 24 2000-04-11 22:19:11Z jones $'
|
10
|
*/
|
11
|
|
12
|
//package project;
|
13
|
|
14
|
import java.sql.*;
|
15
|
import java.io.IOException;
|
16
|
import java.util.Vector;
|
17
|
import java.util.Enumeration;
|
18
|
|
19
|
public class ReaderElement extends BasicElement {
|
20
|
|
21
|
private Connection conn;
|
22
|
private Vector children;
|
23
|
|
24
|
public ReaderElement (Connection conn) {
|
25
|
this.conn = conn;
|
26
|
this.children = new Vector();
|
27
|
}
|
28
|
|
29
|
public ReaderElement (Connection conn, long nodeid) {
|
30
|
this(conn);
|
31
|
|
32
|
//Lookup data for self
|
33
|
setElementInfo(nodeid);
|
34
|
|
35
|
//Create child nodes (elements or attributes)
|
36
|
setChildrenNodes(nodeid);
|
37
|
}
|
38
|
|
39
|
public ReaderElement (Connection conn, long nodeid, long parentnodeid,
|
40
|
String nodename, String nodedata) {
|
41
|
this(conn);
|
42
|
setElementID(nodeid);
|
43
|
setParentID(parentnodeid);
|
44
|
setTagName(nodename);
|
45
|
appendContent(nodedata);
|
46
|
|
47
|
// Create child nodes (elements or attributes)
|
48
|
setChildrenNodes(nodeid);
|
49
|
}
|
50
|
|
51
|
/** Look up the info needed to construct this element from the DB */
|
52
|
private void setElementInfo(long nodeid) {
|
53
|
long element_id=0;
|
54
|
long parentnodeid=0;
|
55
|
String nodetype=null;
|
56
|
String nodename=null;
|
57
|
String nodedata=null;
|
58
|
|
59
|
PreparedStatement pstmt;
|
60
|
try {
|
61
|
pstmt =
|
62
|
conn.prepareStatement("SELECT nodeid,parentnodeid,nodetype, " +
|
63
|
"nodename,nodedata FROM xml_nodes WHERE nodeid = ?");
|
64
|
// Bind the values to the query
|
65
|
pstmt.setLong(1, nodeid);
|
66
|
|
67
|
pstmt.execute();
|
68
|
try {
|
69
|
ResultSet rs = pstmt.getResultSet();
|
70
|
try {
|
71
|
boolean tableHasRows = rs.next();
|
72
|
if (tableHasRows) {
|
73
|
try {
|
74
|
element_id = rs.getLong(1);
|
75
|
parentnodeid = rs.getLong(2);
|
76
|
nodetype = rs.getString(3);
|
77
|
nodename = rs.getString(4);
|
78
|
nodedata = rs.getString(5);
|
79
|
} catch (SQLException e) {
|
80
|
System.out.println("Error with getInt: " + e.getMessage());
|
81
|
}
|
82
|
}
|
83
|
} catch (SQLException e) {
|
84
|
System.out.println("Error with next: " + e.getMessage());
|
85
|
}
|
86
|
} catch (SQLException e) {
|
87
|
System.out.println("Error with getrset: " + e.getMessage());
|
88
|
}
|
89
|
pstmt.close();
|
90
|
} catch (SQLException e) {
|
91
|
System.out.println("Error getting id: " + e.getMessage());
|
92
|
}
|
93
|
|
94
|
if (nodetype.equals("ELEMENT")) {
|
95
|
setElementID(element_id);
|
96
|
setParentID(parentnodeid);
|
97
|
setTagName(nodename);
|
98
|
appendContent(nodedata);
|
99
|
}
|
100
|
}
|
101
|
|
102
|
/** Look up each child node from the DB and and create it */
|
103
|
private void setChildrenNodes(long nodeid) {
|
104
|
long element_id=0;
|
105
|
long parentnodeid=0;
|
106
|
String nodetype=null;
|
107
|
String nodename=null;
|
108
|
String nodedata=null;
|
109
|
|
110
|
PreparedStatement pstmt;
|
111
|
try {
|
112
|
pstmt =
|
113
|
conn.prepareStatement("SELECT nodeid,parentnodeid,nodetype, " +
|
114
|
"nodename,nodedata FROM xml_nodes WHERE parentnodeid = ?");
|
115
|
// Bind the values to the query
|
116
|
pstmt.setLong(1, nodeid);
|
117
|
|
118
|
pstmt.execute();
|
119
|
try {
|
120
|
ResultSet rs = pstmt.getResultSet();
|
121
|
try {
|
122
|
boolean tableHasRows = rs.next();
|
123
|
while (tableHasRows) {
|
124
|
try {
|
125
|
element_id = rs.getLong(1);
|
126
|
parentnodeid = rs.getLong(2);
|
127
|
nodetype = rs.getString(3);
|
128
|
nodename = rs.getString(4);
|
129
|
nodedata = rs.getString(5);
|
130
|
|
131
|
if (nodetype.equals("ELEMENT")) {
|
132
|
ReaderElement child = new ReaderElement(conn,
|
133
|
element_id,parentnodeid,nodename,nodedata);
|
134
|
children.add(child);
|
135
|
} else if (nodetype.equals("ATTRIBUTE")) {
|
136
|
setAttribute(nodename,nodedata);
|
137
|
}
|
138
|
|
139
|
} catch (SQLException e) {
|
140
|
System.out.println("Error with getInt: " + e.getMessage());
|
141
|
}
|
142
|
|
143
|
// Advance to the next record in the cursor
|
144
|
tableHasRows = rs.next();
|
145
|
}
|
146
|
} catch (SQLException e) {
|
147
|
System.out.println("Error with next: " + e.getMessage());
|
148
|
}
|
149
|
} catch (SQLException e) {
|
150
|
System.out.println("Error with getrset: " + e.getMessage());
|
151
|
}
|
152
|
pstmt.close();
|
153
|
} catch (SQLException e) {
|
154
|
System.out.println("Error getting id: " + e.getMessage());
|
155
|
}
|
156
|
|
157
|
}
|
158
|
|
159
|
/** String representation for display purposes */
|
160
|
public String toString ()
|
161
|
{
|
162
|
StringBuffer value = new StringBuffer();
|
163
|
value.append('<');
|
164
|
value.append(getTagName());
|
165
|
value.append(getAttributes().toString());
|
166
|
value.append('>');
|
167
|
|
168
|
// Process children recursively here
|
169
|
// Or do it in ReaderElement using a stack so we don;t have the
|
170
|
// whole thing in memory at once?
|
171
|
ReaderElement child = null;
|
172
|
Enumeration e = children.elements();
|
173
|
while (e.hasMoreElements()) {
|
174
|
child = (ReaderElement)e.nextElement();
|
175
|
value.append(child);
|
176
|
}
|
177
|
|
178
|
String cont = getContent();
|
179
|
if (!cont.equals("null")) {
|
180
|
value.append(cont);
|
181
|
}
|
182
|
|
183
|
value.append("</");
|
184
|
value.append(getTagName());
|
185
|
value.append('>');
|
186
|
return value.toString();
|
187
|
}
|
188
|
|
189
|
}
|