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: 2005-10-10 11:06:55 -0700 (Mon, 10 Oct 2005) $'
|
12
|
* '$Revision: 2663 $'
|
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.apache.log4j.Logger;
|
38
|
import org.xml.sax.SAXException;
|
39
|
|
40
|
/**
|
41
|
* A Class that represents an XML Subtree
|
42
|
*/
|
43
|
public class SubTree implements Comparator
|
44
|
{
|
45
|
protected String docId = null;
|
46
|
protected String subTreeId = null;
|
47
|
protected String startElementName = null;
|
48
|
protected long startNodeId = -1;
|
49
|
protected long endNodeId = -1;
|
50
|
private Stack subTreeNodeStack = null;
|
51
|
|
52
|
private static Logger logMetacat = Logger.getLogger(SubTree.class);
|
53
|
|
54
|
/**
|
55
|
* Defualt constructor
|
56
|
*/
|
57
|
public SubTree()
|
58
|
{
|
59
|
|
60
|
}
|
61
|
|
62
|
/**
|
63
|
* Constructor of subtree
|
64
|
*/
|
65
|
public SubTree(String myDocId, String mySubTreeId,
|
66
|
long myStartNodeId, long myEndNodeId)
|
67
|
{
|
68
|
this.docId = myDocId;
|
69
|
logMetacat.info("Docid of Subtree: " + docId);
|
70
|
this.subTreeId = mySubTreeId;
|
71
|
logMetacat.info("id of Subtree: " + subTreeId);
|
72
|
this.startNodeId = myStartNodeId;
|
73
|
logMetacat.info("start node id of Subtree: " + startNodeId);
|
74
|
this.endNodeId = myEndNodeId;
|
75
|
logMetacat.info("end node id of subtree: " + endNodeId);
|
76
|
|
77
|
}
|
78
|
|
79
|
/**
|
80
|
* Get subtree node stack
|
81
|
*/
|
82
|
public Stack getSubTreeNodeStack() throws SAXException
|
83
|
{
|
84
|
try
|
85
|
{
|
86
|
subTreeNodeStack = getSubTreeNodeList();
|
87
|
}
|
88
|
catch (McdbException e)
|
89
|
{
|
90
|
throw new SAXException(e.getMessage());
|
91
|
}
|
92
|
return this.subTreeNodeStack;
|
93
|
}
|
94
|
|
95
|
/**
|
96
|
* Set subtree node stack
|
97
|
*/
|
98
|
public void setSubTreeNodeStack(Stack myStack)
|
99
|
{
|
100
|
this.subTreeNodeStack = myStack;
|
101
|
}
|
102
|
|
103
|
/** Set the a docId */
|
104
|
public void setDocId(String myId)
|
105
|
{
|
106
|
logMetacat.info("set doc id: "+myId);
|
107
|
this.docId = myId;
|
108
|
}
|
109
|
|
110
|
/** Get the docId */
|
111
|
public String getDocId()
|
112
|
{
|
113
|
return this.docId;
|
114
|
}
|
115
|
|
116
|
|
117
|
/** Set the a subtreeId */
|
118
|
public void setSubTreeId(String myId)
|
119
|
{
|
120
|
logMetacat.info("set sub tree id: "+myId);
|
121
|
this.subTreeId = myId;
|
122
|
}
|
123
|
|
124
|
/** Get the subTreeId */
|
125
|
public String getSubTreeId()
|
126
|
{
|
127
|
return this.subTreeId;
|
128
|
}
|
129
|
|
130
|
/**
|
131
|
* Set a startElementName
|
132
|
*/
|
133
|
public void setStartElementName(String elementName)
|
134
|
{
|
135
|
logMetacat.info("set start elementname: "+elementName);
|
136
|
this.startElementName = elementName;
|
137
|
}
|
138
|
|
139
|
/**
|
140
|
* Get startElementName
|
141
|
*/
|
142
|
public String getStartElementName()
|
143
|
{
|
144
|
return this.startElementName;
|
145
|
}
|
146
|
|
147
|
/** Set a start node id */
|
148
|
public void setStartNodeId(long nodeId)
|
149
|
{
|
150
|
logMetacat.info("set start node id: "+nodeId);
|
151
|
this.startNodeId = nodeId;
|
152
|
}
|
153
|
|
154
|
/** Get start node id */
|
155
|
public long getStartNodeId()
|
156
|
{
|
157
|
return this.startNodeId;
|
158
|
}
|
159
|
|
160
|
/** Set a end node id */
|
161
|
public void setEndNodeId(long nodeId)
|
162
|
{
|
163
|
logMetacat.info("set end node id: "+nodeId);
|
164
|
this.endNodeId = nodeId;
|
165
|
}
|
166
|
|
167
|
/** Get end node id */
|
168
|
public long getEndNodeId()
|
169
|
{
|
170
|
return this.endNodeId;
|
171
|
}
|
172
|
|
173
|
/* Put a subtree node into a stack, on top is the start point of subtree*/
|
174
|
private Stack getSubTreeNodeList() throws McdbException
|
175
|
{
|
176
|
Stack nodeRecordList = new Stack();
|
177
|
// make sure it works
|
178
|
if ( docId == null || startNodeId == -1 || endNodeId == -1)
|
179
|
{
|
180
|
return nodeRecordList;
|
181
|
}
|
182
|
PreparedStatement pstmt = null;
|
183
|
DBConnection dbconn = null;
|
184
|
int serialNumber = -1;
|
185
|
|
186
|
long nodeid = 0;
|
187
|
long parentnodeid = 0;
|
188
|
long nodeindex = 0;
|
189
|
String nodetype = null;
|
190
|
String nodename = null;
|
191
|
String nodeprefix = null;
|
192
|
String nodedata = null;
|
193
|
String sql = "SELECT nodeid, parentnodeid, nodeindex, " +
|
194
|
"nodetype, nodename, nodeprefix, nodedata " +
|
195
|
"FROM xml_nodes WHERE docid = ? AND nodeid >= ? AND " +
|
196
|
"nodeid <= ? ORDER BY nodeid DESC";
|
197
|
try
|
198
|
{
|
199
|
dbconn=DBConnectionPool.
|
200
|
getDBConnection("SubTree.getSubTreeNodeList");
|
201
|
serialNumber=dbconn.getCheckOutSerialNumber();
|
202
|
pstmt = dbconn.prepareStatement(sql);
|
203
|
|
204
|
// Bind the values to the query
|
205
|
pstmt.setString(1, docId);
|
206
|
pstmt.setLong(2, startNodeId);
|
207
|
pstmt.setLong(3, endNodeId);
|
208
|
|
209
|
pstmt.execute();
|
210
|
ResultSet rs = pstmt.getResultSet();
|
211
|
boolean tableHasRows = rs.next();
|
212
|
|
213
|
while (tableHasRows)
|
214
|
{
|
215
|
nodeid = rs.getLong(1);
|
216
|
parentnodeid = rs.getLong(2);
|
217
|
nodeindex = rs.getLong(3);
|
218
|
nodetype = rs.getString(4);
|
219
|
nodename = rs.getString(5);
|
220
|
nodeprefix = rs.getString(6);
|
221
|
nodedata = rs.getString(7);
|
222
|
nodedata = MetaCatUtil.normalize(nodedata);
|
223
|
// add the data to the node record list hashtable
|
224
|
NodeRecord currentRecord = new NodeRecord(nodeid,parentnodeid,nodeindex,
|
225
|
nodetype, nodename, nodeprefix, nodedata);
|
226
|
nodeRecordList.push(currentRecord);
|
227
|
|
228
|
// Advance to the next node
|
229
|
tableHasRows = rs.next();
|
230
|
}//while
|
231
|
pstmt.close();
|
232
|
|
233
|
} //try
|
234
|
catch (SQLException e)
|
235
|
{
|
236
|
throw new McdbException("Error in SubTree.getSubTreeNodeList 1 " +
|
237
|
e.getMessage());
|
238
|
}//catch
|
239
|
finally
|
240
|
{
|
241
|
try
|
242
|
{
|
243
|
pstmt.close();
|
244
|
}
|
245
|
catch (SQLException ee)
|
246
|
{
|
247
|
logMetacat.error("error in SubTree.getSubTreeNodeList 2: "
|
248
|
+ee.getMessage());
|
249
|
}
|
250
|
finally
|
251
|
{
|
252
|
DBConnectionPool.returnDBConnection(dbconn, serialNumber);
|
253
|
}
|
254
|
}//finally
|
255
|
|
256
|
return nodeRecordList;
|
257
|
|
258
|
}//getSubtreeNodeList
|
259
|
|
260
|
/** methods from Comparator interface */
|
261
|
public int compare(Object o1, Object o2)
|
262
|
{
|
263
|
SubTree tree1 = (SubTree) o1;
|
264
|
SubTree tree2 = (SubTree) o2;
|
265
|
if (tree1.getStartNodeId() > tree2.getStartNodeId())
|
266
|
{
|
267
|
return 1;
|
268
|
}
|
269
|
else if (tree1.getStartNodeId() < tree2.getStartNodeId())
|
270
|
{
|
271
|
return -1;
|
272
|
}
|
273
|
else
|
274
|
{
|
275
|
return 0;
|
276
|
}
|
277
|
|
278
|
}//cpmpare
|
279
|
|
280
|
/** method from Comparator interface */
|
281
|
public boolean equals(Object obj)
|
282
|
{
|
283
|
SubTree tree = (SubTree)obj;
|
284
|
if (startNodeId == tree.getStartNodeId())
|
285
|
{
|
286
|
return true;
|
287
|
}
|
288
|
else
|
289
|
{
|
290
|
return false;
|
291
|
}
|
292
|
}
|
293
|
|
294
|
}
|