1 |
21
|
jones
|
/**
|
2 |
203
|
jones
|
* '$RCSfile$'
|
3 |
|
|
* Purpose: A Class that creates an XML text document
|
4 |
|
|
* from a query to a relational DB containing a DOM representation
|
5 |
|
|
* Copyright: 2000 Regents of the University of California and the
|
6 |
|
|
* National Center for Ecological Analysis and Synthesis
|
7 |
|
|
* Authors: Matt Jones
|
8 |
349
|
jones
|
* Release: @release@
|
9 |
21
|
jones
|
*
|
10 |
203
|
jones
|
* '$Author$'
|
11 |
|
|
* '$Date$'
|
12 |
|
|
* '$Revision$'
|
13 |
21
|
jones
|
*/
|
14 |
|
|
|
15 |
51
|
jones
|
package edu.ucsb.nceas.metacat;
|
16 |
|
|
|
17 |
21
|
jones
|
import java.io.*;
|
18 |
|
|
import java.sql.*;
|
19 |
|
|
|
20 |
31
|
jones
|
/**
|
21 |
|
|
* A Class that creates an XML text document
|
22 |
|
|
* from a query to a relational DB containing a DOM representation
|
23 |
|
|
*/
|
24 |
21
|
jones
|
public class DBReader {
|
25 |
|
|
|
26 |
|
|
private Connection conn = null;
|
27 |
|
|
|
28 |
31
|
jones
|
/**
|
29 |
|
|
* main routine used for testing.
|
30 |
184
|
jones
|
* <p>
|
31 |
|
|
* Usage: java DBReader <docid>
|
32 |
31
|
jones
|
*
|
33 |
|
|
* @param nodeid the id number of the root of the subtree to display
|
34 |
|
|
*/
|
35 |
21
|
jones
|
static public void main(String[] args) {
|
36 |
|
|
|
37 |
184
|
jones
|
if (args.length < 1)
|
38 |
21
|
jones
|
{
|
39 |
|
|
System.err.println("Wrong number of arguments!!!");
|
40 |
184
|
jones
|
System.err.println("USAGE: java DBReader <docid>");
|
41 |
21
|
jones
|
return;
|
42 |
|
|
} else {
|
43 |
|
|
try {
|
44 |
|
|
|
45 |
184
|
jones
|
String docid = args[0];
|
46 |
21
|
jones
|
|
47 |
50
|
jones
|
// Open a connection to the database
|
48 |
184
|
jones
|
MetaCatUtil util = new MetaCatUtil();
|
49 |
|
|
Connection dbconn = util.openDBConnection();
|
50 |
50
|
jones
|
|
51 |
|
|
DBReader rd = new DBReader( dbconn );
|
52 |
184
|
jones
|
String xml = rd.readXMLDocument(docid);
|
53 |
21
|
jones
|
System.out.println(xml);
|
54 |
|
|
|
55 |
343
|
jones
|
} catch (McdbException me) {
|
56 |
|
|
me.toXml(new PrintWriter(System.err));
|
57 |
21
|
jones
|
} catch (Exception e) {
|
58 |
|
|
System.err.println("EXCEPTION HANDLING REQUIRED");
|
59 |
|
|
System.err.println(e.getMessage());
|
60 |
|
|
e.printStackTrace(System.err);
|
61 |
|
|
}
|
62 |
|
|
}
|
63 |
|
|
}
|
64 |
|
|
|
65 |
31
|
jones
|
/**
|
66 |
|
|
* construct a DBReader instance.
|
67 |
|
|
*
|
68 |
|
|
* Generally, one calls readXMLDocument() after constructing the instance
|
69 |
|
|
*
|
70 |
50
|
jones
|
* @param conn the database connection from which to read the document
|
71 |
31
|
jones
|
*/
|
72 |
50
|
jones
|
public DBReader( Connection conn )
|
73 |
21
|
jones
|
{
|
74 |
50
|
jones
|
this.conn = conn;
|
75 |
21
|
jones
|
}
|
76 |
|
|
|
77 |
31
|
jones
|
/**
|
78 |
86
|
jones
|
* Get the root node id for an XML document given a document id
|
79 |
|
|
*
|
80 |
|
|
* @param docid the document node contains the root of the document
|
81 |
|
|
* @returns long the nodeid of the root node for this document
|
82 |
|
|
*/
|
83 |
343
|
jones
|
private long getRootNode(String docid)
|
84 |
|
|
throws McdbDocNotFoundException
|
85 |
|
|
{
|
86 |
86
|
jones
|
// Now look up the root node id
|
87 |
|
|
long rootnodeid = 0;
|
88 |
|
|
|
89 |
|
|
try {
|
90 |
|
|
PreparedStatement pstmt =
|
91 |
|
|
conn.prepareStatement("SELECT rootnodeid " +
|
92 |
|
|
"FROM xml_documents " +
|
93 |
|
|
"WHERE docid = ?");
|
94 |
|
|
// Bind the values to the query
|
95 |
162
|
bojilova
|
pstmt.setString(1, docid);
|
96 |
86
|
jones
|
|
97 |
|
|
pstmt.execute();
|
98 |
|
|
ResultSet rs = pstmt.getResultSet();
|
99 |
|
|
boolean tableHasRows = rs.next();
|
100 |
|
|
if (tableHasRows) {
|
101 |
|
|
rootnodeid = rs.getLong(1);
|
102 |
|
|
|
103 |
|
|
}
|
104 |
|
|
pstmt.close();
|
105 |
|
|
} catch (SQLException e) {
|
106 |
343
|
jones
|
throw new McdbDocNotFoundException(
|
107 |
|
|
"Root node not found for: " + docid, e);
|
108 |
86
|
jones
|
}
|
109 |
|
|
|
110 |
|
|
return rootnodeid;
|
111 |
|
|
}
|
112 |
|
|
|
113 |
|
|
/**
|
114 |
31
|
jones
|
* Create an XML document from the database starting with the element
|
115 |
|
|
* having element_id nodeid
|
116 |
|
|
*
|
117 |
86
|
jones
|
* @param docid the document that we want retrieved
|
118 |
31
|
jones
|
*/
|
119 |
343
|
jones
|
public String readXMLDocument(String docid)
|
120 |
|
|
throws McdbException
|
121 |
|
|
{
|
122 |
|
|
try {
|
123 |
|
|
StringBuffer doc = new StringBuffer();
|
124 |
|
|
DoctypeInfo dti = getDoctypeInfo(docid);
|
125 |
21
|
jones
|
|
126 |
132
|
jones
|
String docname = dti.getDocname();
|
127 |
|
|
String doctype = dti.getDoctype();
|
128 |
|
|
String sysid = dti.getSystemID();
|
129 |
343
|
jones
|
|
130 |
132
|
jones
|
ElementNode element = new ElementNode(conn, getRootNode(docid));
|
131 |
|
|
doc.append("<?xml version=\"1.0\"?>\n");
|
132 |
343
|
jones
|
|
133 |
132
|
jones
|
if (docname != null) {
|
134 |
|
|
if ((doctype != null) && (sysid != null)) {
|
135 |
|
|
doc.append("<!DOCTYPE " + docname + " PUBLIC \"" + doctype +
|
136 |
|
|
"\" \"" + sysid + "\">\n");
|
137 |
|
|
} else {
|
138 |
|
|
doc.append("<!DOCTYPE " + docname + ">\n");
|
139 |
|
|
}
|
140 |
103
|
jones
|
}
|
141 |
132
|
jones
|
doc.append(element.toString());
|
142 |
343
|
jones
|
|
143 |
|
|
return (doc.toString());
|
144 |
|
|
|
145 |
|
|
} catch (McdbException ex) {
|
146 |
|
|
throw ex;
|
147 |
|
|
} catch (Throwable t) {
|
148 |
|
|
throw new McdbException("Error reading document " + docid + ".");
|
149 |
103
|
jones
|
}
|
150 |
21
|
jones
|
}
|
151 |
103
|
jones
|
|
152 |
|
|
/**
|
153 |
|
|
* Look up the document type information from the database
|
154 |
|
|
*
|
155 |
|
|
* @param docid the id of the document to look up
|
156 |
|
|
*/
|
157 |
343
|
jones
|
public DoctypeInfo getDoctypeInfo(String docid)
|
158 |
|
|
throws McdbException
|
159 |
|
|
{
|
160 |
103
|
jones
|
PreparedStatement pstmt;
|
161 |
|
|
String doctype = null;
|
162 |
|
|
String docname = null;
|
163 |
|
|
String sysid = null;
|
164 |
|
|
DoctypeInfo dti = null;
|
165 |
|
|
|
166 |
|
|
try {
|
167 |
|
|
pstmt =
|
168 |
|
|
conn.prepareStatement("SELECT docname,doctype " +
|
169 |
|
|
"FROM xml_documents " +
|
170 |
|
|
"WHERE docid = ?");
|
171 |
|
|
// Bind the values to the query
|
172 |
165
|
jones
|
pstmt.setString(1, docid);
|
173 |
103
|
jones
|
|
174 |
|
|
pstmt.execute();
|
175 |
|
|
ResultSet rs = pstmt.getResultSet();
|
176 |
|
|
boolean tableHasRows = rs.next();
|
177 |
|
|
if (tableHasRows) {
|
178 |
|
|
docname = rs.getString(1);
|
179 |
|
|
doctype = rs.getString(2);
|
180 |
|
|
}
|
181 |
|
|
pstmt.close();
|
182 |
|
|
|
183 |
132
|
jones
|
if (doctype != null) {
|
184 |
|
|
pstmt =
|
185 |
|
|
conn.prepareStatement("SELECT system_id " +
|
186 |
|
|
"FROM xml_catalog " +
|
187 |
|
|
"WHERE public_id = ?");
|
188 |
|
|
// Bind the values to the query
|
189 |
|
|
pstmt.setString(1, doctype);
|
190 |
|
|
|
191 |
|
|
pstmt.execute();
|
192 |
|
|
rs = pstmt.getResultSet();
|
193 |
|
|
tableHasRows = rs.next();
|
194 |
|
|
if (tableHasRows) {
|
195 |
|
|
sysid = rs.getString(1);
|
196 |
|
|
}
|
197 |
|
|
pstmt.close();
|
198 |
|
|
}
|
199 |
103
|
jones
|
} catch (SQLException e) {
|
200 |
343
|
jones
|
throw new McdbException("Error accessing database connection.", e);
|
201 |
103
|
jones
|
}
|
202 |
|
|
|
203 |
132
|
jones
|
if (docname != null) {
|
204 |
|
|
dti = new DoctypeInfo(docname, doctype, sysid);
|
205 |
343
|
jones
|
return dti;
|
206 |
132
|
jones
|
} else {
|
207 |
343
|
jones
|
throw new McdbDocNotFoundException("Document not found: " + docid);
|
208 |
132
|
jones
|
}
|
209 |
103
|
jones
|
}
|
210 |
|
|
|
211 |
|
|
/**
|
212 |
|
|
* A utility class that encapsulates document type information
|
213 |
|
|
*/
|
214 |
|
|
public class DoctypeInfo {
|
215 |
|
|
private String docname = null;
|
216 |
|
|
private String doctype = null;
|
217 |
|
|
private String system_id = null;
|
218 |
|
|
|
219 |
|
|
/**
|
220 |
|
|
* Constructor
|
221 |
|
|
*/
|
222 |
|
|
public DoctypeInfo(String docname, String doctype, String system_id) {
|
223 |
|
|
this.docname = docname;
|
224 |
|
|
this.doctype = doctype;
|
225 |
|
|
this.system_id = system_id;
|
226 |
|
|
}
|
227 |
|
|
|
228 |
|
|
/**
|
229 |
|
|
* get the document name
|
230 |
|
|
*/
|
231 |
|
|
public String getDocname() {
|
232 |
|
|
return docname;
|
233 |
|
|
}
|
234 |
|
|
|
235 |
|
|
/**
|
236 |
|
|
* get the document type
|
237 |
|
|
*/
|
238 |
|
|
public String getDoctype() {
|
239 |
|
|
return doctype;
|
240 |
|
|
}
|
241 |
|
|
|
242 |
|
|
/**
|
243 |
|
|
* get the system identifier
|
244 |
|
|
*/
|
245 |
|
|
public String getSystemID() {
|
246 |
|
|
return system_id;
|
247 |
|
|
}
|
248 |
|
|
}
|
249 |
|
|
|
250 |
21
|
jones
|
}
|
251 |
203
|
jones
|
|
252 |
|
|
/**
|
253 |
|
|
* '$Log$
|
254 |
349
|
jones
|
* 'Revision 1.19 2000/08/11 22:20:04 jones
|
255 |
|
|
* 'Changed exception handling mechanisms for DBReader
|
256 |
|
|
* '
|
257 |
343
|
jones
|
* 'Revision 1.18 2000/08/10 22:39:04 jones
|
258 |
|
|
* 'changed getRootNode method from public to private
|
259 |
|
|
* '
|
260 |
339
|
jones
|
* 'Revision 1.17 2000/06/26 10:35:05 jones
|
261 |
|
|
* 'Merged in substantial changes to DBWriter and associated classes and to
|
262 |
|
|
* 'the MetaCatServlet in order to accomodate the new UPDATE and DELETE
|
263 |
|
|
* 'functions. The command line tools and the parameters for the
|
264 |
|
|
* 'servlet have changed substantially.
|
265 |
|
|
* '
|
266 |
203
|
jones
|
* 'Revision 1.16.2.2 2000/06/25 23:38:16 jones
|
267 |
|
|
* 'Added RCSfile keyword
|
268 |
|
|
* '
|
269 |
|
|
* 'Revision 1.16.2.1 2000/06/25 23:34:17 jones
|
270 |
|
|
* 'Changed documentation formatting, added log entries at bottom of source files
|
271 |
|
|
* ''
|
272 |
|
|
*/
|