1
|
/**
|
2
|
* Name: QuerySpecification.java
|
3
|
* Purpose: A Class that represents a structured query, and can be
|
4
|
* constructed from an XML serialization
|
5
|
* Copyright: 2000 Regents of the University of California and the
|
6
|
* National Center for Ecological Analysis and Synthesis
|
7
|
* Authors: Matt Jones
|
8
|
*
|
9
|
* Version: '$Id: QuerySpecification.java 155 2000-06-14 20:54:13Z jones $'
|
10
|
*/
|
11
|
|
12
|
package edu.ucsb.nceas.metacat;
|
13
|
|
14
|
import java.io.*;
|
15
|
import java.net.URL;
|
16
|
import java.net.MalformedURLException;
|
17
|
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;
|
22
|
|
23
|
|
24
|
/**
|
25
|
* A Class that represents a structuredd query,and can be constructed from an
|
26
|
* XML serialization
|
27
|
*/
|
28
|
public class QuerySpecification extends HandlerBase {
|
29
|
|
30
|
/**
|
31
|
* construct an instance of the QuerySpecification class
|
32
|
*
|
33
|
* @param queryspec the XML representation of the query as a Reader
|
34
|
*/
|
35
|
public QuerySpecification( Reader queryspec ) throws IOException {
|
36
|
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();
|
46
|
try {
|
47
|
parser.parse(new InputSource(queryspec));
|
48
|
} catch (SAXException e) {
|
49
|
System.out.println("error parsing data");
|
50
|
System.out.println(e.getMessage());
|
51
|
}
|
52
|
}
|
53
|
|
54
|
/**
|
55
|
* construct an instance of the QuerySpecification class
|
56
|
*
|
57
|
* @param queryspec the XML representation of the query as a String
|
58
|
*/
|
59
|
public QuerySpecification( String queryspec ) throws IOException {
|
60
|
|
61
|
this(new StringReader(queryspec));
|
62
|
}
|
63
|
|
64
|
/** Main routine for testing */
|
65
|
static public void main(String[] args) {
|
66
|
|
67
|
if (args.length < 1) {
|
68
|
System.err.println("Wrong number of arguments!!!");
|
69
|
System.err.println("USAGE: java QuerySpecification <xmlfile>");
|
70
|
return;
|
71
|
} else {
|
72
|
String xmlfile = args[0];
|
73
|
|
74
|
try {
|
75
|
FileReader xml = new FileReader(new File(xmlfile));
|
76
|
QuerySpecification qspec = new QuerySpecification(xml);
|
77
|
} catch (IOException e) {
|
78
|
System.err.println(e.getMessage());
|
79
|
}
|
80
|
|
81
|
}
|
82
|
}
|
83
|
|
84
|
private SAXParser initializeParser() {
|
85
|
SAXParser parser = null;
|
86
|
//
|
87
|
// Set up the SAX document handlers for parsing
|
88
|
//
|
89
|
try {
|
90
|
// For all the other interface use the default provided by
|
91
|
// Handler base
|
92
|
HandlerBase defHandler = new HandlerBase();
|
93
|
|
94
|
// Get an instance of the parser
|
95
|
parser = new SAXParser();
|
96
|
|
97
|
// Set Handlers in the parser
|
98
|
// Set the DocumentHandler to XMLDocumentHandler
|
99
|
parser.setDocumentHandler(this);
|
100
|
|
101
|
// Set the other Handler to the defHandler
|
102
|
parser.setErrorHandler(defHandler);
|
103
|
|
104
|
} catch (Exception e) {
|
105
|
System.err.println(e.toString());
|
106
|
}
|
107
|
|
108
|
return parser;
|
109
|
}
|
110
|
|
111
|
public void startElement (String name, AttributeList atts)
|
112
|
throws SAXException {
|
113
|
System.out.println(name);
|
114
|
}
|
115
|
|
116
|
public void endElement (String name) throws SAXException {
|
117
|
}
|
118
|
}
|