Revision 159
Added by Matt Jones over 24 years ago
src/edu/ucsb/nceas/metacat/QuerySpecification.java | ||
---|---|---|
14 | 14 |
import java.io.*; |
15 | 15 |
import java.util.Stack; |
16 | 16 |
import java.util.Vector; |
17 |
import java.util.Enumeration; |
|
17 | 18 |
|
18 | 19 |
import org.xml.sax.AttributeList; |
19 | 20 |
import org.xml.sax.InputSource; |
... | ... | |
44 | 45 |
|
45 | 46 |
private Stack elementStack; |
46 | 47 |
private Stack queryStack; |
48 |
private String currentValue; |
|
49 |
private String currentPathexpr; |
|
47 | 50 |
|
48 | 51 |
/** |
49 | 52 |
* construct an instance of the QuerySpecification class |
... | ... | |
90 | 93 |
try { |
91 | 94 |
FileReader xml = new FileReader(new File(xmlfile)); |
92 | 95 |
QuerySpecification qspec = new QuerySpecification(xml); |
96 |
System.out.println(qspec); |
|
93 | 97 |
} catch (IOException e) { |
94 | 98 |
System.err.println(e.getMessage()); |
95 | 99 |
} |
... | ... | |
124 | 128 |
throws SAXException { |
125 | 129 |
|
126 | 130 |
BasicNode currentNode = new BasicNode(name); |
131 |
// add attributes to BasicNode here |
|
132 |
if (atts != null) { |
|
133 |
int len = atts.getLength(); |
|
134 |
for (int i = 0; i < len; i++) { |
|
135 |
currentNode.setAttribute(atts.getName(i), atts.getValue(i)); |
|
136 |
} |
|
137 |
} |
|
138 |
|
|
127 | 139 |
elementStack.push(currentNode); |
128 |
if currentNode.getTagName().equals("querygroup") {
|
|
140 |
if (currentNode.getTagName().equals("querygroup")) {
|
|
129 | 141 |
QueryGroup currentGroup = new QueryGroup( |
130 | 142 |
currentNode.getAttribute("booleantype")); |
131 |
queryStack.push(currentGroup); |
|
132 | 143 |
if (query == null) { |
133 | 144 |
query = currentGroup; |
145 |
} else { |
|
146 |
QueryGroup parentGroup = (QueryGroup)queryStack.peek(); |
|
147 |
parentGroup.addChild(currentGroup); |
|
134 | 148 |
} |
149 |
queryStack.push(currentGroup); |
|
135 | 150 |
} |
136 |
System.out.println(currentNode.getTagName() + " (start)"); |
|
137 | 151 |
} |
138 | 152 |
|
139 | 153 |
public void endElement (String name) throws SAXException { |
140 | 154 |
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 +"]"); |
|
155 |
if (leaving.getTagName().equals("queryterm")) { |
|
156 |
boolean isCaseSensitive = (new Boolean( |
|
157 |
leaving.getAttribute("casesensitive"))).booleanValue(); |
|
158 |
QueryTerm currentTerm = null; |
|
159 |
if (currentPathexpr == null) { |
|
160 |
currentTerm = new QueryTerm(isCaseSensitive, |
|
161 |
leaving.getAttribute("searchmode"),currentValue); |
|
162 |
} else { |
|
163 |
currentTerm = new QueryTerm(isCaseSensitive, |
|
164 |
leaving.getAttribute("searchmode"),currentValue, |
|
165 |
currentPathexpr); |
|
166 |
} |
|
167 |
QueryGroup currentGroup = (QueryGroup)queryStack.peek(); |
|
168 |
currentGroup.addChild(currentTerm); |
|
169 |
currentValue = null; |
|
170 |
currentPathexpr = null; |
|
171 |
} else if (leaving.getTagName().equals("querygroup")) { |
|
172 |
QueryGroup leavingGroup = (QueryGroup)queryStack.pop(); |
|
145 | 173 |
} |
146 |
System.out.println(name +"/" + leaving.getTagName() + " (end)"); |
|
147 | 174 |
} |
148 | 175 |
|
149 | 176 |
public void characters(char ch[], int start, int length) { |
... | ... | |
155 | 182 |
meta_file_id = inputString; |
156 | 183 |
} else if (currentTag.equals("querytitle")) { |
157 | 184 |
querytitle = inputString; |
185 |
} else if (currentTag.equals("value")) { |
|
186 |
currentValue = inputString; |
|
187 |
} else if (currentTag.equals("pathexpr")) { |
|
188 |
currentPathexpr = inputString; |
|
158 | 189 |
} |
159 | 190 |
} |
160 | 191 |
|
192 |
public String toString() { |
|
193 |
return "meta_file_id=" + meta_file_id + "\n" + |
|
194 |
"querytitle=" + querytitle + "\n" + query; |
|
195 |
} |
|
196 |
|
|
161 | 197 |
/** a utility class that represents a group of terms in a query */ |
162 | 198 |
private class QueryGroup { |
163 | 199 |
private String booleantype = null; // indicates how query terms are combined |
... | ... | |
179 | 215 |
public Enumeration getChildren() { |
180 | 216 |
return children.elements(); |
181 | 217 |
} |
218 |
|
|
219 |
public String toString() { |
|
220 |
StringBuffer self = new StringBuffer(); |
|
221 |
|
|
222 |
self.append(" (Query group booleantype=" + booleantype + "\n"); |
|
223 |
Enumeration en= getChildren(); |
|
224 |
while (en.hasMoreElements()) { |
|
225 |
Object qobject = en.nextElement(); |
|
226 |
self.append(qobject); |
|
227 |
} |
|
228 |
self.append(" )\n"); |
|
229 |
return self.toString(); |
|
230 |
} |
|
182 | 231 |
} |
183 | 232 |
|
184 | 233 |
/** a utility class that represents a single term in a query */ |
... | ... | |
216 | 265 |
public String getPathExpression() { |
217 | 266 |
return pathexpr; |
218 | 267 |
} |
268 |
|
|
269 |
public String toString() { |
|
270 |
StringBuffer self = new StringBuffer(); |
|
271 |
|
|
272 |
self.append(" Query Term iscasesensitive=" + casesensitive + "\n"); |
|
273 |
self.append(" searchmode=" + searchmode + "\n"); |
|
274 |
self.append(" value=" + value + "\n"); |
|
275 |
if (pathexpr != null) { |
|
276 |
self.append(" pathexpr=" + pathexpr + "\n"); |
|
277 |
} |
|
278 |
|
|
279 |
return self.toString(); |
|
280 |
} |
|
219 | 281 |
} |
220 | 282 |
} |
Also available in: Unified diff
continued work on structured query -- now it reads the queryspec xml docuemnt and can digest its contents