1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Purpose: A Class that represents an XML Text node 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
|
* Release: @release@
|
9
|
*
|
10
|
* '$Author: sgarg $'
|
11
|
* '$Date: 2004-08-19 18:09:25 -0700 (Thu, 19 Aug 2004) $'
|
12
|
* '$Revision: 2245 $'
|
13
|
*
|
14
|
* This program is free software; you can redistribute it and/or modify
|
15
|
* it under the terms of the GNU General Public License as published by
|
16
|
* the Free Software Foundation; either version 2 of the License, or
|
17
|
* (at your option) any later version.
|
18
|
*
|
19
|
* This program is distributed in the hope that it will be useful,
|
20
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
21
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
22
|
* GNU General Public License for more details.
|
23
|
*
|
24
|
* You should have received a copy of the GNU General Public License
|
25
|
* along with this program; if not, write to the Free Software
|
26
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
27
|
*/
|
28
|
|
29
|
package edu.ucsb.nceas.metacat;
|
30
|
|
31
|
import java.util.Comparator;
|
32
|
import java.util.Stack;
|
33
|
import java.sql.PreparedStatement;
|
34
|
import java.sql.SQLException;
|
35
|
import java.sql.ResultSet;
|
36
|
|
37
|
import org.xml.sax.SAXException;
|
38
|
|
39
|
/**
|
40
|
* A Class that represents an XML Subtree
|
41
|
*/
|
42
|
public class SubTree implements Comparator
|
43
|
{
|
44
|
protected String docId = null;
|
45
|
protected String subTreeId = null;
|
46
|
protected String startElementName = null;
|
47
|
protected long startNodeId = -1;
|
48
|
protected long endNodeId = -1;
|
49
|
private Stack subTreeNodeStack = null;
|
50
|
|
51
|
/**
|
52
|
* Defualt constructor
|
53
|
*/
|
54
|
public SubTree()
|
55
|
{
|
56
|
|
57
|
}
|
58
|
|
59
|
/**
|
60
|
* Constructor of subtree
|
61
|
*/
|
62
|
public SubTree(String myDocId, String mySubTreeId,
|
63
|
long myStartNodeId, long myEndNodeId)
|
64
|
{
|
65
|
this.docId = myDocId;
|
66
|
MetaCatUtil.debugMessage("Docid of Subtree: " + docId, 30);
|
67
|
this.subTreeId = mySubTreeId;
|
68
|
MetaCatUtil.debugMessage("id of Subtree: " + subTreeId, 30);
|
69
|
this.startNodeId = myStartNodeId;
|
70
|
MetaCatUtil.debugMessage("start node id of Subtree: " + startNodeId, 30);
|
71
|
this.endNodeId = myEndNodeId;
|
72
|
MetaCatUtil.debugMessage("end node id of subtree: " + endNodeId, 30);
|
73
|
|
74
|
}
|
75
|
|
76
|
/**
|
77
|
* Get subtree node stack
|
78
|
*/
|
79
|
public Stack getSubTreeNodeStack() throws SAXException
|
80
|
{
|
81
|
try
|
82
|
{
|
83
|
subTreeNodeStack = getSubTreeNodeList();
|
84
|
}
|
85
|
catch (McdbException e)
|
86
|
{
|
87
|
throw new SAXException(e.getMessage());
|
88
|
}
|
89
|
return this.subTreeNodeStack;
|
90
|
}
|
91
|
|
92
|
/**
|
93
|
* Set subtree node stack
|
94
|
*/
|
95
|
public void setSubTreeNodeStack(Stack myStack)
|
96
|
{
|
97
|
this.subTreeNodeStack = myStack;
|
98
|
}
|
99
|
|
100
|
/** Set the a docId */
|
101
|
public void setDocId(String myId)
|
102
|
{
|
103
|
MetaCatUtil.debugMessage("set doc id: "+myId, 35);
|
104
|
this.docId = myId;
|
105
|
}
|
106
|
|
107
|
/** Get the docId */
|
108
|
public String getDocId()
|
109
|
{
|
110
|
return this.docId;
|
111
|
}
|
112
|
|
113
|
|
114
|
/** Set the a subtreeId */
|
115
|
public void setSubTreeId(String myId)
|
116
|
{
|
117
|
MetaCatUtil.debugMessage("set sub tree id: "+myId, 35);
|
118
|
this.subTreeId = myId;
|
119
|
}
|
120
|
|
121
|
/** Get the subTreeId */
|
122
|
public String getSubTreeId()
|
123
|
{
|
124
|
return this.subTreeId;
|
125
|
}
|
126
|
|
127
|
/**
|
128
|
* Set a startElementName
|
129
|
*/
|
130
|
public void setStartElementName(String elementName)
|
131
|
{
|
132
|
MetaCatUtil.debugMessage("set start elementname: "+elementName, 35);
|
133
|
this.startElementName = elementName;
|
134
|
}
|
135
|
|
136
|
/**
|
137
|
* Get startElementName
|
138
|
*/
|
139
|
public String getStartElementName()
|
140
|
{
|
141
|
return this.startElementName;
|
142
|
}
|
143
|
|
144
|
/** Set a start node id */
|
145
|
public void setStartNodeId(long nodeId)
|
146
|
{
|
147
|
MetaCatUtil.debugMessage("set start node id: "+nodeId, 35);
|
148
|
this.startNodeId = nodeId;
|
149
|
}
|
150
|
|
151
|
/** Get start node id */
|
152
|
public long getStartNodeId()
|
153
|
{
|
154
|
return this.startNodeId;
|
155
|
}
|
156
|
|
157
|
/** Set a end node id */
|
158
|
public void setEndNodeId(long nodeId)
|
159
|
{
|
160
|
MetaCatUtil.debugMessage("set end node id: "+nodeId, 35);
|
161
|
this.endNodeId = nodeId;
|
162
|
}
|
163
|
|
164
|
/** Get end node id */
|
165
|
public long getEndNodeId()
|
166
|
{
|
167
|
return this.endNodeId;
|
168
|
}
|
169
|
|
170
|
/* Put a subtree node into a stack, on top is the start point of subtree*/
|
171
|
private Stack getSubTreeNodeList() throws McdbException
|
172
|
{
|
173
|
Stack nodeRecordList = new Stack();
|
174
|
// make sure it works
|
175
|
if ( docId == null || startNodeId == -1 || endNodeId == -1)
|
176
|
{
|
177
|
return nodeRecordList;
|
178
|
}
|
179
|
PreparedStatement pstmt = null;
|
180
|
DBConnection dbconn = null;
|
181
|
int serialNumber = -1;
|
182
|
|
183
|
long nodeid = 0;
|
184
|
long parentnodeid = 0;
|
185
|
long nodeindex = 0;
|
186
|
String nodetype = null;
|
187
|
String nodename = null;
|
188
|
String nodeprefix = null;
|
189
|
String nodedata = null;
|
190
|
String sql = "SELECT nodeid, parentnodeid, nodeindex, " +
|
191
|
"nodetype, nodename, nodeprefix, nodedata " +
|
192
|
"FROM xml_nodes WHERE docid = ? AND nodeid >= ? AND " +
|
193
|
"nodeid <= ? ORDER BY nodeid DESC";
|
194
|
try
|
195
|
{
|
196
|
dbconn=DBConnectionPool.
|
197
|
getDBConnection("SubTree.getSubTreeNodeList");
|
198
|
serialNumber=dbconn.getCheckOutSerialNumber();
|
199
|
pstmt = dbconn.prepareStatement(sql);
|
200
|
|
201
|
// Bind the values to the query
|
202
|
pstmt.setString(1, docId);
|
203
|
pstmt.setLong(2, startNodeId);
|
204
|
pstmt.setLong(3, endNodeId);
|
205
|
|
206
|
pstmt.execute();
|
207
|
ResultSet rs = pstmt.getResultSet();
|
208
|
boolean tableHasRows = rs.next();
|
209
|
|
210
|
while (tableHasRows)
|
211
|
{
|
212
|
nodeid = rs.getLong(1);
|
213
|
parentnodeid = rs.getLong(2);
|
214
|
nodeindex = rs.getLong(3);
|
215
|
nodetype = rs.getString(4);
|
216
|
nodename = rs.getString(5);
|
217
|
nodeprefix = rs.getString(6);
|
218
|
nodedata = rs.getString(7);
|
219
|
nodedata = MetaCatUtil.normalize(nodedata);
|
220
|
// add the data to the node record list hashtable
|
221
|
NodeRecord currentRecord = new NodeRecord(nodeid,parentnodeid,nodeindex,
|
222
|
nodetype, nodename, nodeprefix, nodedata);
|
223
|
nodeRecordList.push(currentRecord);
|
224
|
|
225
|
// Advance to the next node
|
226
|
tableHasRows = rs.next();
|
227
|
}//while
|
228
|
pstmt.close();
|
229
|
|
230
|
} //try
|
231
|
catch (SQLException e)
|
232
|
{
|
233
|
throw new McdbException("Error in SubTree.getSubTreeNodeList 1 " +
|
234
|
e.getMessage());
|
235
|
}//catch
|
236
|
finally
|
237
|
{
|
238
|
try
|
239
|
{
|
240
|
pstmt.close();
|
241
|
}
|
242
|
catch (SQLException ee)
|
243
|
{
|
244
|
MetaCatUtil.debugMessage("error in SubTree.getSubTreeNodeList 2: "
|
245
|
+ee.getMessage(), 30);
|
246
|
}
|
247
|
finally
|
248
|
{
|
249
|
DBConnectionPool.returnDBConnection(dbconn, serialNumber);
|
250
|
}
|
251
|
}//finally
|
252
|
|
253
|
return nodeRecordList;
|
254
|
|
255
|
}//getSubtreeNodeList
|
256
|
|
257
|
/** methods from Comparator interface */
|
258
|
public int compare(Object o1, Object o2)
|
259
|
{
|
260
|
SubTree tree1 = (SubTree) o1;
|
261
|
SubTree tree2 = (SubTree) o2;
|
262
|
if (tree1.getStartNodeId() > tree2.getStartNodeId())
|
263
|
{
|
264
|
return 1;
|
265
|
}
|
266
|
else if (tree1.getStartNodeId() < tree2.getStartNodeId())
|
267
|
{
|
268
|
return -1;
|
269
|
}
|
270
|
else
|
271
|
{
|
272
|
return 0;
|
273
|
}
|
274
|
|
275
|
}//cpmpare
|
276
|
|
277
|
/** method from Comparator interface */
|
278
|
public boolean equals(Object obj)
|
279
|
{
|
280
|
SubTree tree = (SubTree)obj;
|
281
|
if (startNodeId == tree.getStartNodeId())
|
282
|
{
|
283
|
return true;
|
284
|
}
|
285
|
else
|
286
|
{
|
287
|
return false;
|
288
|
}
|
289
|
}
|
290
|
|
291
|
}
|