1 |
5363
|
berkley
|
/**
|
2 |
|
|
*
|
3 |
|
|
*/
|
4 |
|
|
package edu.ucsb.nceas.metacat;
|
5 |
|
|
|
6 |
|
|
import java.io.*;
|
7 |
|
|
import java.util.*;
|
8 |
|
|
|
9 |
|
|
import org.xml.sax.helpers.DefaultHandler;
|
10 |
|
|
import org.xml.sax.Attributes;
|
11 |
|
|
import org.xml.sax.SAXException;
|
12 |
|
|
import javax.xml.parsers.SAXParserFactory;
|
13 |
|
|
import javax.xml.parsers.SAXParser;
|
14 |
|
|
import org.xml.sax.InputSource;
|
15 |
|
|
|
16 |
|
|
/**
|
17 |
|
|
* @author berkley
|
18 |
|
|
* A class to parse, then encapsulate a metacat result set
|
19 |
|
|
*/
|
20 |
|
|
public class MetacatResultSet extends DefaultHandler
|
21 |
|
|
{
|
22 |
|
|
private Vector<Document> documents;
|
23 |
|
|
private Document document;
|
24 |
|
|
private String currentElement;
|
25 |
|
|
private String paramName;
|
26 |
|
|
private boolean inDocument = false;
|
27 |
|
|
|
28 |
|
|
public MetacatResultSet(String s)
|
29 |
|
|
throws Exception
|
30 |
|
|
{
|
31 |
|
|
try
|
32 |
|
|
{
|
33 |
|
|
documents = new Vector();
|
34 |
|
|
SAXParserFactory factory = SAXParserFactory.newInstance();
|
35 |
|
|
factory.setValidating(false);
|
36 |
|
|
SAXParser parser = factory.newSAXParser();
|
37 |
|
|
parser.parse(new InputSource(new StringReader(s)), this);
|
38 |
|
|
}
|
39 |
|
|
catch(Exception e)
|
40 |
|
|
{
|
41 |
|
|
e.printStackTrace();
|
42 |
|
|
throw new Exception("Could not parse the resultset: " + e.getMessage());
|
43 |
|
|
}
|
44 |
|
|
}
|
45 |
|
|
|
46 |
|
|
public void characters(char[] ch, int start, int length) throws SAXException
|
47 |
|
|
{
|
48 |
|
|
String content = new String(ch, start, length);
|
49 |
|
|
//System.out.println("currentElement: " + currentElement);
|
50 |
|
|
//System.out.println("content: " + content);
|
51 |
|
|
|
52 |
|
|
if(currentElement.equals("document"))
|
53 |
|
|
{
|
54 |
|
|
|
55 |
|
|
}
|
56 |
|
|
else if(currentElement.equals("docid"))
|
57 |
|
|
{
|
58 |
|
|
document.docid = content;
|
59 |
|
|
}
|
60 |
|
|
else if(currentElement.equals("docname"))
|
61 |
|
|
{
|
62 |
|
|
document.docname = content;
|
63 |
|
|
}
|
64 |
|
|
else if(currentElement.equals("doctype"))
|
65 |
|
|
{
|
66 |
|
|
document.doctype = content;
|
67 |
|
|
}
|
68 |
|
|
else if(currentElement.equals("createdate"))
|
69 |
|
|
{
|
70 |
|
|
document.createdate = content;
|
71 |
|
|
}
|
72 |
|
|
else if(currentElement.equals("updatedate"))
|
73 |
|
|
{
|
74 |
|
|
document.updatedate = content;
|
75 |
|
|
}
|
76 |
|
|
else if(currentElement.equals("param"))
|
77 |
|
|
{
|
78 |
|
|
//System.out.println("setting param: " + paramName + " with value " + content);
|
79 |
|
|
document.setField(paramName, content);
|
80 |
|
|
}
|
81 |
|
|
}
|
82 |
|
|
|
83 |
|
|
public void startDocument()
|
84 |
|
|
{
|
85 |
|
|
|
86 |
|
|
}
|
87 |
|
|
|
88 |
|
|
public void endDocument()
|
89 |
|
|
{
|
90 |
|
|
|
91 |
|
|
}
|
92 |
|
|
|
93 |
|
|
public void startElement(String uri, String localName, String qName, Attributes attributes)
|
94 |
|
|
{
|
95 |
|
|
currentElement = qName;
|
96 |
|
|
//System.out.println("currentElement: " + currentElement);
|
97 |
|
|
//System.out.println("inDocument: " + inDocument);
|
98 |
|
|
if(qName.equals("document"))
|
99 |
|
|
{
|
100 |
|
|
document = new Document();
|
101 |
|
|
inDocument = true;
|
102 |
|
|
}
|
103 |
|
|
|
104 |
|
|
if(qName.equals("param"))
|
105 |
|
|
{
|
106 |
|
|
paramName = attributes.getValue("name");
|
107 |
|
|
}
|
108 |
|
|
}
|
109 |
|
|
|
110 |
|
|
public void endElement(String uri, String localName, String qName)
|
111 |
|
|
{
|
112 |
|
|
if(qName.equals("document"))
|
113 |
|
|
{
|
114 |
|
|
documents.add(document);
|
115 |
|
|
}
|
116 |
|
|
}
|
117 |
|
|
|
118 |
|
|
public List getDocuments()
|
119 |
|
|
{
|
120 |
|
|
return documents;
|
121 |
|
|
}
|
122 |
|
|
|
123 |
|
|
public class Document
|
124 |
|
|
{
|
125 |
|
|
private Hashtable<String, String> fields = new Hashtable();
|
126 |
|
|
public String docid;
|
127 |
|
|
public String docname;
|
128 |
|
|
public String doctype;
|
129 |
|
|
public String createdate;
|
130 |
|
|
public String updatedate;
|
131 |
|
|
|
132 |
|
|
public Document()
|
133 |
|
|
{
|
134 |
|
|
docid = null;
|
135 |
|
|
docname = null;
|
136 |
|
|
doctype = null;
|
137 |
|
|
createdate = null;
|
138 |
|
|
updatedate = null;
|
139 |
|
|
}
|
140 |
|
|
|
141 |
|
|
public String getField(String name)
|
142 |
|
|
{
|
143 |
|
|
return fields.get(name);
|
144 |
|
|
}
|
145 |
|
|
|
146 |
|
|
public void setField(String name, String value)
|
147 |
|
|
{
|
148 |
|
|
String val = fields.get(name);
|
149 |
|
|
if(val != null)
|
150 |
|
|
{
|
151 |
|
|
val += value;
|
152 |
|
|
}
|
153 |
|
|
else
|
154 |
|
|
{
|
155 |
|
|
val = value;
|
156 |
|
|
}
|
157 |
|
|
fields.put(name, val);
|
158 |
|
|
}
|
159 |
|
|
|
160 |
|
|
public String toString()
|
161 |
|
|
{
|
162 |
|
|
String s = new String();
|
163 |
|
|
s = "{docid=" + docid.trim() + ", " +
|
164 |
|
|
"docname=" + docname.trim() + ", " +
|
165 |
|
|
"doctype=" + doctype.trim() + ", " +
|
166 |
|
|
"createdate=" + createdate.trim() + ", " +
|
167 |
|
|
"updatedate=" + updatedate.trim();
|
168 |
|
|
Enumeration keys = fields.keys();
|
169 |
|
|
while(keys.hasMoreElements())
|
170 |
|
|
{
|
171 |
|
|
s += ", ";
|
172 |
|
|
String name = (String)keys.nextElement();
|
173 |
|
|
String value = fields.get(name);
|
174 |
|
|
s += name.trim() + "=" + value.trim();
|
175 |
|
|
}
|
176 |
|
|
s += "}";
|
177 |
|
|
return s;
|
178 |
|
|
}
|
179 |
|
|
}
|
180 |
|
|
|
181 |
|
|
|
182 |
|
|
}
|