1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Copyright: 2007 Regents of the University of California and the
|
4
|
* National Center for Ecological Analysis and Synthesis
|
5
|
* Authors: Chad Berkley
|
6
|
*
|
7
|
* '$Author: berkley $'
|
8
|
* '$Date: 2007-04-03 13:10:52 -0700 (Tue, 03 Apr 2007) $'
|
9
|
* '$Revision: 3221 $'
|
10
|
*
|
11
|
* This program is free software; you can redistribute it and/or modify
|
12
|
* it under the terms of the GNU General Public License as published by
|
13
|
* the Free Software Foundation; either version 2 of the License, or
|
14
|
* (at your option) any later version.
|
15
|
*
|
16
|
* This program is distributed in the hope that it will be useful,
|
17
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
18
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
19
|
* GNU General Public License for more details.
|
20
|
*
|
21
|
* You should have received a copy of the GNU General Public License
|
22
|
* along with this program; if not, write to the Free Software
|
23
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
24
|
*/
|
25
|
|
26
|
package edu.ucsb.nceas.metacat;
|
27
|
|
28
|
import java.io.*;
|
29
|
import java.util.*;
|
30
|
|
31
|
import org.w3c.dom.*;
|
32
|
import org.apache.xpath.*;
|
33
|
|
34
|
import org.apache.log4j.Logger;
|
35
|
|
36
|
import org.apache.log4j.Logger;
|
37
|
import org.xml.sax.Attributes;
|
38
|
import org.xml.sax.SAXException;
|
39
|
import org.xml.sax.SAXParseException;
|
40
|
import org.xml.sax.ext.DeclHandler;
|
41
|
import org.xml.sax.ext.LexicalHandler;
|
42
|
import org.xml.sax.helpers.DefaultHandler;
|
43
|
import org.xml.sax.XMLReader;
|
44
|
import org.xml.sax.helpers.XMLReaderFactory;
|
45
|
import org.xml.sax.ContentHandler;
|
46
|
import org.xml.sax.ErrorHandler;
|
47
|
import org.xml.sax.InputSource;
|
48
|
|
49
|
/**
|
50
|
* this class implements a metacat resultset and can be serialized to xml
|
51
|
* for printing to the servlet output stream.
|
52
|
*/
|
53
|
public class MetacatResultSet extends DefaultHandler
|
54
|
{
|
55
|
public static final String VALIDATIONFEATURE = "http://xml.org/sax/features/validation";
|
56
|
public static final String SCHEMAVALIDATIONFEATURE = "http://apache.org/xml/features/validation/schema";
|
57
|
public static final String NAMESPACEFEATURE = "http://xml.org/sax/features/namespaces";
|
58
|
public static final String NAMESPACEPREFIXESFEATURE = "http://xml.org/sax/features/namespace-prefixes";
|
59
|
public static final String RESOLVEDTDFEATURE = "http://apache.org/sax/features/load-dtd-grammar";
|
60
|
public static final String LOADDTDFEATURE = "http://apache.org/sax/features/load-external-dtd";
|
61
|
public static final String DOCTYPEDECLFEATURE = "http://apache.org/sax/features/disallow-doctype-decl";
|
62
|
public static final String DECLARATIONHANDLERPROPERTY = "http://xml.org/sax/properties/declaration-handler";
|
63
|
public static final String LEXICALPROPERTY = "http://xml.org/sax/properties/lexical-handler";
|
64
|
|
65
|
private Logger log = Logger.getLogger(MetacatResultSet.class);
|
66
|
private Vector results = new Vector();
|
67
|
private Result currentResult = null;
|
68
|
private String currentElementName = "";
|
69
|
Attributes atts = null;
|
70
|
|
71
|
/**
|
72
|
* default constructor
|
73
|
*/
|
74
|
public MetacatResultSet()
|
75
|
{
|
76
|
|
77
|
}
|
78
|
|
79
|
/**
|
80
|
* constructor that can process a dom. WARNING: this is very slow.
|
81
|
*/
|
82
|
public MetacatResultSet(Document d)
|
83
|
throws Exception
|
84
|
{
|
85
|
log.warn("processing resultset...");
|
86
|
NodeList nl = XPathAPI.selectNodeList(d, "//document");
|
87
|
for(int i=0; i<nl.getLength(); i++)
|
88
|
{ //get each of the document nodes
|
89
|
Node docNode = nl.item(i);
|
90
|
Node docidNode = XPathAPI.selectSingleNode(docNode, "./docid");
|
91
|
log.warn("processing " + docidNode.getFirstChild().getNodeValue());
|
92
|
Node docnameNode = XPathAPI.selectSingleNode(docNode, "./docname");
|
93
|
Node doctypeNode = XPathAPI.selectSingleNode(docNode, "./doctype");
|
94
|
Node createdateNode = XPathAPI.selectSingleNode(docNode, "./createdate");
|
95
|
Node updatedateNode = XPathAPI.selectSingleNode(docNode, "./updatedate");
|
96
|
//process the returnfields
|
97
|
NodeList returnfieldNL = XPathAPI.selectNodeList(docNode, "./param");
|
98
|
Hashtable returnfieldHash = new Hashtable();
|
99
|
for(int j=0; j<returnfieldNL.getLength(); j++)
|
100
|
{
|
101
|
Node returnfieldNode = returnfieldNL.item(j);
|
102
|
Node nameNode = XPathAPI.selectSingleNode(returnfieldNode, "@name");
|
103
|
String value = returnfieldNode.getFirstChild().getNodeValue();
|
104
|
String name = nameNode.getNodeValue();
|
105
|
returnfieldHash.put(name, value);
|
106
|
}
|
107
|
|
108
|
Result r = new Result(docidNode.getFirstChild().getNodeValue(),
|
109
|
docnameNode.getFirstChild().getNodeValue(),
|
110
|
doctypeNode.getFirstChild().getNodeValue(),
|
111
|
createdateNode.getFirstChild().getNodeValue(),
|
112
|
updatedateNode.getFirstChild().getNodeValue(),
|
113
|
returnfieldHash);
|
114
|
addResult(r);
|
115
|
}
|
116
|
}
|
117
|
|
118
|
/**
|
119
|
* process a resultset using SAX
|
120
|
*/
|
121
|
public MetacatResultSet(String xmlDoc)
|
122
|
throws SAXException, IOException
|
123
|
{
|
124
|
XMLReader parser = null;
|
125
|
String parserName = MetaCatUtil.getOption("saxparser");
|
126
|
parser = XMLReaderFactory.createXMLReader(parserName);
|
127
|
parser.setContentHandler(this);
|
128
|
parser.setErrorHandler(this);
|
129
|
parser.parse(new InputSource(new StringReader(xmlDoc)));
|
130
|
}
|
131
|
|
132
|
/**
|
133
|
* process the beginning of an element
|
134
|
*/
|
135
|
public void startElement(String uri, String localName, String qName,
|
136
|
Attributes atts) throws SAXException
|
137
|
{
|
138
|
//get the attributes
|
139
|
this.atts = atts;
|
140
|
if(localName.equals("document"))
|
141
|
{
|
142
|
currentResult = new Result();
|
143
|
}
|
144
|
else if(localName.equals("docid") ||
|
145
|
localName.equals("docname") ||
|
146
|
localName.equals("doctype") ||
|
147
|
localName.equals("createdate") ||
|
148
|
localName.equals("updatedate") ||
|
149
|
localName.equals("param"))
|
150
|
{ //set the current element name
|
151
|
currentElementName = localName;
|
152
|
}
|
153
|
}
|
154
|
|
155
|
/**
|
156
|
* process the end of an element
|
157
|
*/
|
158
|
public void endElement(String uri, String localName, String qName)
|
159
|
throws SAXException
|
160
|
{
|
161
|
if(localName.equals("document"))
|
162
|
{ //if we're closing a document, save the result
|
163
|
addResult(new Result(currentResult));
|
164
|
currentResult = new Result();
|
165
|
currentElementName = "";
|
166
|
}
|
167
|
}
|
168
|
|
169
|
/**
|
170
|
* process character data
|
171
|
*/
|
172
|
public void characters(char[] cbuf, int start, int len)
|
173
|
throws SAXException
|
174
|
{ //get the character data for each node we care about
|
175
|
String s = new String(cbuf, start, len);
|
176
|
if(currentElementName.equals("docid"))
|
177
|
{
|
178
|
currentResult.docid = s;
|
179
|
}
|
180
|
else if(currentElementName.equals("docname"))
|
181
|
{
|
182
|
currentResult.docname = s;
|
183
|
}
|
184
|
else if(currentElementName.equals("doctype"))
|
185
|
{
|
186
|
currentResult.doctype = s;
|
187
|
}
|
188
|
else if(currentElementName.equals("createdate"))
|
189
|
{
|
190
|
currentResult.createDate = s;
|
191
|
}
|
192
|
else if(currentElementName.equals("updatedate"))
|
193
|
{
|
194
|
currentResult.updateDate = s;
|
195
|
}
|
196
|
else if(currentElementName.equals("param"))
|
197
|
{ //add the returnfields to the hashtable
|
198
|
for (int i = 0; i < atts.getLength(); i++)
|
199
|
{
|
200
|
String attributeName = atts.getQName(i);
|
201
|
String attributeValue = null;
|
202
|
if(attributeName.equals("name"))
|
203
|
{
|
204
|
attributeValue = atts.getValue(i);
|
205
|
}
|
206
|
currentResult.returnfields.put(attributeValue, s);
|
207
|
}
|
208
|
}
|
209
|
}
|
210
|
|
211
|
/**
|
212
|
* SAX Handler that receives notification of fatal parsing errors
|
213
|
*/
|
214
|
public void fatalError(SAXParseException exception) throws SAXException
|
215
|
{
|
216
|
log.fatal("FATALERROR: " + exception.getMessage());
|
217
|
throw (new SAXException("Fatal error processing/caching resultset.", exception));
|
218
|
}
|
219
|
|
220
|
/**
|
221
|
* SAX Handler that receives notification of recoverable parsing errors
|
222
|
*/
|
223
|
public void error(SAXParseException exception) throws SAXException
|
224
|
{
|
225
|
log.error("ERROR: " + exception.getMessage());
|
226
|
throw (new SAXException("Error in processing/caching resultset.", exception));
|
227
|
}
|
228
|
|
229
|
/**
|
230
|
* SAX Handler that receives notification of warnings
|
231
|
*/
|
232
|
public void warning(SAXParseException exception) throws SAXException
|
233
|
{
|
234
|
log.warn("WARNING while parsing/caching resultset: " + exception.getMessage());
|
235
|
throw (new SAXException("Warning.", exception));
|
236
|
}
|
237
|
|
238
|
/**
|
239
|
* add a new result to the resultSet
|
240
|
*/
|
241
|
public void addResult(Result r)
|
242
|
{
|
243
|
results.addElement(r);
|
244
|
}
|
245
|
|
246
|
/**
|
247
|
* returns a vector of the results
|
248
|
*/
|
249
|
public Vector getResults()
|
250
|
{
|
251
|
return results;
|
252
|
}
|
253
|
|
254
|
/**
|
255
|
* serialize a selection of the results. This will print the results from
|
256
|
* start to end-1. if end is 0, nothing will be printed.
|
257
|
*/
|
258
|
public String serializeToXML(int start, int end)
|
259
|
{
|
260
|
StringBuffer sb = new StringBuffer();
|
261
|
if(start > results.size() || end > results.size())
|
262
|
{ //make sure we don't go over the edge of the vector
|
263
|
start = results.size() - 10;
|
264
|
end = results.size();
|
265
|
}
|
266
|
|
267
|
for(int i=start; i<end; i++)
|
268
|
{
|
269
|
Result r = (Result)results.elementAt(i);
|
270
|
sb.append(r.toString());
|
271
|
sb.append("\n");
|
272
|
}
|
273
|
return sb.toString();
|
274
|
}
|
275
|
|
276
|
/**
|
277
|
* returns an xml representation of this object
|
278
|
*/
|
279
|
public String toString()
|
280
|
{
|
281
|
StringBuffer sb = new StringBuffer();
|
282
|
for(int i=0; i<results.size(); i++)
|
283
|
{
|
284
|
Result r = (Result)results.elementAt(i);
|
285
|
sb.append(r.toString());
|
286
|
sb.append("\n");
|
287
|
}
|
288
|
return sb.toString();
|
289
|
}
|
290
|
|
291
|
|
292
|
/**
|
293
|
* a class to store one result
|
294
|
*/
|
295
|
public class Result
|
296
|
{
|
297
|
protected String docid;
|
298
|
protected String docname;
|
299
|
protected String doctype;
|
300
|
protected String createDate;
|
301
|
protected String updateDate;
|
302
|
protected Hashtable returnfields;
|
303
|
|
304
|
/**
|
305
|
* copy constructor
|
306
|
*/
|
307
|
public Result(Result r)
|
308
|
{
|
309
|
if(r != null)
|
310
|
{
|
311
|
docid = r.docid;
|
312
|
docname = r.docname;
|
313
|
doctype = r.doctype;
|
314
|
createDate = r.createDate;
|
315
|
updateDate = r.updateDate;
|
316
|
returnfields = r.returnfields;
|
317
|
}
|
318
|
}
|
319
|
|
320
|
/**
|
321
|
* constructor
|
322
|
*/
|
323
|
public Result(String docid, String docname, String doctype,
|
324
|
String createDate, String updateDate, Hashtable returnfields)
|
325
|
{
|
326
|
this.docid = docid;
|
327
|
this.doctype = doctype;
|
328
|
this.createDate = createDate;
|
329
|
this.updateDate = updateDate;
|
330
|
this.returnfields = returnfields;
|
331
|
}
|
332
|
|
333
|
/**
|
334
|
* default constructor
|
335
|
*/
|
336
|
public Result()
|
337
|
{
|
338
|
returnfields = new Hashtable();
|
339
|
}
|
340
|
|
341
|
/**
|
342
|
* returns serialized version of this result
|
343
|
*/
|
344
|
public String toString()
|
345
|
{
|
346
|
StringBuffer sb = new StringBuffer();
|
347
|
sb.append("<document>\n");
|
348
|
sb.append(" <docid>" + docid + "</docid>\n");
|
349
|
sb.append(" <docname>" + docname + "</docname>\n");
|
350
|
sb.append(" <doctype>" + doctype + "</doctype>\n");
|
351
|
sb.append(" <createdate>" + createDate + "</createdate>\n");
|
352
|
sb.append(" <updatedate>" + updateDate + "</updatedate>\n");
|
353
|
|
354
|
Enumeration keys = returnfields.keys();
|
355
|
while(keys.hasMoreElements())
|
356
|
{
|
357
|
String key = (String)keys.nextElement();
|
358
|
String value = (String)returnfields.get(key);
|
359
|
sb.append(" <param name=\"" + key + "\">" + value + "</param>\n");
|
360
|
}
|
361
|
sb.append("</document>");
|
362
|
return sb.toString();
|
363
|
}
|
364
|
}
|
365
|
}
|