Revision 155
Added by Matt Jones over 24 years ago
src/edu/ucsb/nceas/metacat/QuerySpecification.java | ||
---|---|---|
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$' |
|
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 |
} |
|
0 | 119 |
src/edu/ucsb/nceas/metacat/DBQuery.java | ||
---|---|---|
1 |
/** |
|
2 |
* Name: DBQuery.java |
|
3 |
* Purpose: A Class that searches a relational DB for elements and |
|
4 |
* attributes that have free text matches to the query string. |
|
5 |
* It returns a result set consisting of the root nodeid for |
|
6 |
* each document that satisfies the query |
|
7 |
* Copyright: 2000 Regents of the University of California and the |
|
8 |
* National Center for Ecological Analysis and Synthesis |
|
9 |
* Authors: Matt Jones |
|
10 |
* |
|
11 |
* Version: '$Id$' |
|
12 |
*/ |
|
13 |
|
|
14 |
package edu.ucsb.nceas.metacat; |
|
15 |
|
|
16 |
import java.io.*; |
|
17 |
import java.net.URL; |
|
18 |
import java.net.MalformedURLException; |
|
19 |
import java.sql.*; |
|
20 |
import java.util.Stack; |
|
21 |
import java.util.Hashtable; |
|
22 |
import java.util.Enumeration; |
|
23 |
|
|
24 |
/** |
|
25 |
* A Class that searches a relational DB for elements and attributes that |
|
26 |
* have structured text matches to the query string. It returns a result set |
|
27 |
* consisting of the root nodeid for each document that satisfies the query |
|
28 |
*/ |
|
29 |
public class DBQuery { |
|
30 |
|
|
31 |
static String defaultDB = "jdbc:oracle:thin:@localhost:1521:test"; |
|
32 |
private Connection conn = null; |
|
33 |
|
|
34 |
/** |
|
35 |
* the main routine used to test the DBQuery utility. |
|
36 |
* |
|
37 |
* Usage: java DBQuery <query> <user> <password> [dbstring] |
|
38 |
* |
|
39 |
* @param query the text to search for in the element and attribute content |
|
40 |
* @param user the username to use for the database connection |
|
41 |
* @param password the password to use for the database connection |
|
42 |
* @param dbstring the connection info to use for the database connection |
|
43 |
*/ |
|
44 |
static public void main(String[] args) { |
|
45 |
|
|
46 |
if (args.length < 4) |
|
47 |
{ |
|
48 |
System.err.println("Wrong number of arguments!!!"); |
|
49 |
System.err.println("USAGE: java DBQuery " + |
|
50 |
"<query> <doctype> <user> <password> [dbstring]"); |
|
51 |
return; |
|
52 |
} else { |
|
53 |
try { |
|
54 |
|
|
55 |
String query = args[0]; |
|
56 |
String doctype = args[1]; |
|
57 |
String user = args[2]; |
|
58 |
String password = args[3]; |
|
59 |
String dbstring = null; |
|
60 |
|
|
61 |
if (args.length <= 4) { |
|
62 |
dbstring = defaultDB; |
|
63 |
} else { |
|
64 |
dbstring = args[4]; |
|
65 |
} |
|
66 |
|
|
67 |
// Open a connection to the database |
|
68 |
Connection dbconn = MetaCatUtil.openDBConnection( |
|
69 |
"oracle.jdbc.driver.OracleDriver", |
|
70 |
dbstring, user, password); |
|
71 |
// Execute the simple query |
|
72 |
DBQuery rd = new DBQuery(dbconn); |
|
73 |
Hashtable nodelist = null; |
|
74 |
nodelist = rd.findDocuments(query); |
|
75 |
|
|
76 |
// Print the reulting root nodes |
|
77 |
StringBuffer result = new StringBuffer(); |
|
78 |
String document = null; |
|
79 |
Long docid = null; |
|
80 |
result.append("<?xml version=\"1.0\"?>\n"); |
|
81 |
result.append("<resultset>\n"); |
|
82 |
result.append(" <query>" + query + "</query>\n"); |
|
83 |
Enumeration doclist = nodelist.keys(); |
|
84 |
while (doclist.hasMoreElements()) { |
|
85 |
docid = (Long)doclist.nextElement(); |
|
86 |
document = (String)nodelist.get(docid); |
|
87 |
result.append(" <document>\n " + document + |
|
88 |
"\n </document>\n"); |
|
89 |
} |
|
90 |
result.append("</resultset>\n"); |
|
91 |
|
|
92 |
System.out.println(result); |
|
93 |
|
|
94 |
} catch (Exception e) { |
|
95 |
System.err.println("EXCEPTION HANDLING REQUIRED"); |
|
96 |
System.err.println(e.getMessage()); |
|
97 |
e.printStackTrace(System.err); |
|
98 |
} |
|
99 |
} |
|
100 |
} |
|
101 |
|
|
102 |
/** |
|
103 |
* construct an instance of the DBQuery class |
|
104 |
* |
|
105 |
* <p>Generally, one would call the findDocuments() routine after creating |
|
106 |
* an instance to specify the search query</p> |
|
107 |
* |
|
108 |
* @param conn the JDBC connection that we use for the query |
|
109 |
*/ |
|
110 |
public DBQuery( Connection conn ) |
|
111 |
throws IOException, |
|
112 |
SQLException, |
|
113 |
ClassNotFoundException |
|
114 |
{ |
|
115 |
this.conn = conn; |
|
116 |
} |
|
117 |
|
|
118 |
/** |
|
119 |
* routine to search the elements and attributes looking to match query |
|
120 |
* |
|
121 |
* @param queryspec the xml serialization of the query |
|
122 |
* @param requestedDoctype the type of documents to return from the query |
|
123 |
*/ |
|
124 |
public Hashtable findDocuments(String queryspec) { |
|
125 |
Hashtable docListResult = new Hashtable(); |
|
126 |
|
|
127 |
|
|
128 |
//QuerySpecification qspec = new QuerySpecification(queryspec); |
|
129 |
|
|
130 |
|
|
131 |
/* |
|
132 |
PreparedStatement pstmt; |
|
133 |
|
|
134 |
// Now look up the document id |
|
135 |
long docid = 0; |
|
136 |
String docname = null; |
|
137 |
String doctype = null; |
|
138 |
String doctitle = null; |
|
139 |
StringBuffer document = null; |
|
140 |
|
|
141 |
try { |
|
142 |
if (requestedDoctype == null || |
|
143 |
requestedDoctype.equals("any") || |
|
144 |
requestedDoctype.equals("ANY")) { |
|
145 |
pstmt = conn.prepareStatement( |
|
146 |
"SELECT docid,docname,doctype,doctitle " + |
|
147 |
"FROM xml_documents " + |
|
148 |
"WHERE docid IN " + |
|
149 |
"(SELECT docid " + |
|
150 |
"FROM xml_nodes WHERE nodedata LIKE ? )"); |
|
151 |
|
|
152 |
// Bind the values to the query |
|
153 |
pstmt.setString(1, query); |
|
154 |
} else { |
|
155 |
pstmt = conn.prepareStatement( |
|
156 |
"SELECT docid,docname,doctype,doctitle " + |
|
157 |
"FROM xml_documents " + |
|
158 |
"WHERE docid IN " + |
|
159 |
"(SELECT docid " + |
|
160 |
"FROM xml_nodes WHERE nodedata LIKE ? ) " + |
|
161 |
"AND doctype = ?"); |
|
162 |
|
|
163 |
// Bind the values to the query |
|
164 |
pstmt.setString(1, query); |
|
165 |
pstmt.setString(2, requestedDoctype); |
|
166 |
} |
|
167 |
|
|
168 |
pstmt.execute(); |
|
169 |
ResultSet rs = pstmt.getResultSet(); |
|
170 |
boolean tableHasRows = rs.next(); |
|
171 |
while (tableHasRows) { |
|
172 |
docid = rs.getLong(1); |
|
173 |
docname = rs.getString(2); |
|
174 |
doctype = rs.getString(3); |
|
175 |
doctitle = rs.getString(4); |
|
176 |
|
|
177 |
document = new StringBuffer(); |
|
178 |
document.append("<docid>").append(docid).append("</docid>"); |
|
179 |
if (docname != null) { |
|
180 |
document.append("<docname>" + docname + "</docname>"); |
|
181 |
} |
|
182 |
if (doctype != null) { |
|
183 |
document.append("<doctype>" + doctype + "</doctype>"); |
|
184 |
} |
|
185 |
if (doctitle != null) { |
|
186 |
document.append("<doctitle>" + doctitle + "</doctitle>"); |
|
187 |
} |
|
188 |
|
|
189 |
// Store the document id and the root node id |
|
190 |
docListResult.put(new Long(docid),(String)document.toString()); |
|
191 |
|
|
192 |
// Advance to the next record in the cursor |
|
193 |
tableHasRows = rs.next(); |
|
194 |
} |
|
195 |
pstmt.close(); |
|
196 |
} catch (SQLException e) { |
|
197 |
System.out.println("Error getting id: " + e.getMessage()); |
|
198 |
} |
|
199 |
*/ |
|
200 |
return docListResult; |
|
201 |
} |
|
202 |
} |
|
0 | 203 |
Also available in: Unified diff
added incomplete classes for structured query