Revision 158
Added by Matt Jones over 24 years ago
src/edu/ucsb/nceas/metacat/QuerySpecification.java | ||
---|---|---|
12 | 12 |
package edu.ucsb.nceas.metacat; |
13 | 13 |
|
14 | 14 |
import java.io.*; |
15 |
import java.net.URL; |
|
16 |
import java.net.MalformedURLException; |
|
17 | 15 |
import java.util.Stack; |
18 |
import java.util.Hashtable; |
|
19 |
import java.util.Enumeration; |
|
20 |
import org.xml.sax.*; |
|
21 |
import oracle.xml.parser.v2.SAXParser; |
|
16 |
import java.util.Vector; |
|
22 | 17 |
|
18 |
import org.xml.sax.AttributeList; |
|
19 |
import org.xml.sax.InputSource; |
|
20 |
import org.xml.sax.HandlerBase; |
|
21 |
import org.xml.sax.Parser; |
|
22 |
import org.xml.sax.SAXException; |
|
23 |
import org.xml.sax.SAXParseException; |
|
24 |
import org.xml.sax.helpers.ParserFactory; |
|
25 |
//import oracle.xml.parser.v2.SAXParser; |
|
23 | 26 |
|
27 |
|
|
28 |
|
|
24 | 29 |
/** |
25 | 30 |
* A Class that represents a structuredd query,and can be constructed from an |
26 | 31 |
* XML serialization |
27 | 32 |
*/ |
28 | 33 |
public class QuerySpecification extends HandlerBase { |
29 | 34 |
|
35 |
/** Default parser name. */ |
|
36 |
private static final String |
|
37 |
DEFAULT_PARSER = "org.apache.xerces.parsers.SAXParser"; |
|
38 |
//DEFAULT_PARSER = "oracle.xml.parser.v2.SAXParser"; |
|
39 |
|
|
40 |
// Query data structures |
|
41 |
private String meta_file_id; |
|
42 |
private String querytitle; |
|
43 |
private QueryGroup query = null; |
|
44 |
|
|
45 |
private Stack elementStack; |
|
46 |
private Stack queryStack; |
|
47 |
|
|
30 | 48 |
/** |
31 | 49 |
* construct an instance of the QuerySpecification class |
32 | 50 |
* |
... | ... | |
34 | 52 |
*/ |
35 | 53 |
public QuerySpecification( Reader queryspec ) throws IOException { |
36 | 54 |
super(); |
37 |
/* |
|
38 |
int bytesread = 0; |
|
39 |
while (bytesread != -1) { |
|
40 |
char[] cbuf = new char[1000]; |
|
41 |
bytesread = queryspec.read(cbuf); |
|
42 |
System.out.print(cbuf); |
|
43 |
} |
|
44 |
*/ |
|
45 |
SAXParser parser = initializeParser(); |
|
55 |
|
|
56 |
// Initialize the stack |
|
57 |
elementStack = new Stack(); |
|
58 |
queryStack = new Stack(); |
|
59 |
|
|
60 |
// Initialize the parser and read the queryspec |
|
61 |
Parser parser = initializeParser(); |
|
46 | 62 |
try { |
47 | 63 |
parser.parse(new InputSource(queryspec)); |
48 | 64 |
} catch (SAXException e) { |
... | ... | |
81 | 97 |
} |
82 | 98 |
} |
83 | 99 |
|
84 |
private SAXParser initializeParser() {
|
|
85 |
SAXParser parser = null;
|
|
100 |
private Parser initializeParser() { |
|
101 |
Parser parser = null; |
|
86 | 102 |
// |
87 | 103 |
// Set up the SAX document handlers for parsing |
88 | 104 |
// |
89 | 105 |
try { |
90 |
// For all the other interface use the default provided by |
|
91 |
// Handler base |
|
92 |
HandlerBase defHandler = new HandlerBase(); |
|
93 | 106 |
|
94 | 107 |
// Get an instance of the parser |
95 |
parser = new SAXParser();
|
|
108 |
parser = ParserFactory.makeParser(DEFAULT_PARSER);
|
|
96 | 109 |
|
97 |
// Set Handlers in the parser |
|
98 |
// Set the DocumentHandler to XMLDocumentHandler |
|
110 |
// Set the DocumentHandler to this instance |
|
99 | 111 |
parser.setDocumentHandler(this); |
100 | 112 |
|
101 | 113 |
// Set the other Handler to the defHandler |
102 |
parser.setErrorHandler(defHandler);
|
|
114 |
parser.setErrorHandler(this);
|
|
103 | 115 |
|
104 | 116 |
} catch (Exception e) { |
105 | 117 |
System.err.println(e.toString()); |
... | ... | |
110 | 122 |
|
111 | 123 |
public void startElement (String name, AttributeList atts) |
112 | 124 |
throws SAXException { |
113 |
System.out.println(name); |
|
125 |
|
|
126 |
BasicNode currentNode = new BasicNode(name); |
|
127 |
elementStack.push(currentNode); |
|
128 |
if currentNode.getTagName().equals("querygroup") { |
|
129 |
QueryGroup currentGroup = new QueryGroup( |
|
130 |
currentNode.getAttribute("booleantype")); |
|
131 |
queryStack.push(currentGroup); |
|
132 |
if (query == null) { |
|
133 |
query = currentGroup; |
|
134 |
} |
|
135 |
} |
|
136 |
System.out.println(currentNode.getTagName() + " (start)"); |
|
114 | 137 |
} |
115 | 138 |
|
116 | 139 |
public void endElement (String name) throws SAXException { |
140 |
BasicNode leaving = (BasicNode)elementStack.pop(); |
|
141 |
if (leaving.getTagName().equals("meta_file_id")) { |
|
142 |
System.out.println("[" + meta_file_id +"]"); |
|
143 |
} else if (leaving.getTagName().equals("querytitle")) { |
|
144 |
System.out.println("[" + querytitle +"]"); |
|
145 |
} |
|
146 |
System.out.println(name +"/" + leaving.getTagName() + " (end)"); |
|
117 | 147 |
} |
148 |
|
|
149 |
public void characters(char ch[], int start, int length) { |
|
150 |
|
|
151 |
String inputString = new String(ch, start, length); |
|
152 |
BasicNode currentNode = (BasicNode)elementStack.peek(); |
|
153 |
String currentTag = currentNode.getTagName(); |
|
154 |
if (currentTag.equals("meta_file_id")) { |
|
155 |
meta_file_id = inputString; |
|
156 |
} else if (currentTag.equals("querytitle")) { |
|
157 |
querytitle = inputString; |
|
158 |
} |
|
159 |
} |
|
160 |
|
|
161 |
/** a utility class that represents a group of terms in a query */ |
|
162 |
private class QueryGroup { |
|
163 |
private String booleantype = null; // indicates how query terms are combined |
|
164 |
private Vector children = null; // the list of query terms and groups |
|
165 |
|
|
166 |
public QueryGroup(String booleantype) { |
|
167 |
this.booleantype = booleantype; |
|
168 |
children = new Vector(); |
|
169 |
} |
|
170 |
|
|
171 |
public void addChild(QueryGroup qgroup) { |
|
172 |
children.add((Object)qgroup); |
|
173 |
} |
|
174 |
|
|
175 |
public void addChild(QueryTerm qterm) { |
|
176 |
children.add((Object)qterm); |
|
177 |
} |
|
178 |
|
|
179 |
public Enumeration getChildren() { |
|
180 |
return children.elements(); |
|
181 |
} |
|
182 |
} |
|
183 |
|
|
184 |
/** a utility class that represents a single term in a query */ |
|
185 |
private class QueryTerm { |
|
186 |
private boolean casesensitive = false; |
|
187 |
private String searchmode = null; |
|
188 |
private String value = null; |
|
189 |
private String pathexpr = null; |
|
190 |
|
|
191 |
public QueryTerm(boolean casesensitive, String searchmode, |
|
192 |
String value) { |
|
193 |
this.casesensitive = casesensitive; |
|
194 |
this.searchmode = searchmode; |
|
195 |
this.value = value; |
|
196 |
} |
|
197 |
|
|
198 |
public QueryTerm(boolean casesensitive, String searchmode, |
|
199 |
String value, String pathexpr) { |
|
200 |
this(casesensitive, searchmode, value); |
|
201 |
this.pathexpr = pathexpr; |
|
202 |
} |
|
203 |
|
|
204 |
public boolean isCaseSensitive() { |
|
205 |
return casesensitive; |
|
206 |
} |
|
207 |
|
|
208 |
public String getSearchMode() { |
|
209 |
return searchmode; |
|
210 |
} |
|
211 |
|
|
212 |
public String getValue() { |
|
213 |
return value; |
|
214 |
} |
|
215 |
|
|
216 |
public String getPathExpression() { |
|
217 |
return pathexpr; |
|
218 |
} |
|
219 |
} |
|
118 | 220 |
} |
Also available in: Unified diff
continued work on developing structured query functionality using the QuerySpecification class -- incomplete still