1 |
21
|
jones
|
/**
|
2 |
|
|
* Name: DBReader.java
|
3 |
|
|
* Purpose: A Class that creates an XML text document
|
4 |
|
|
* from a query to a relational DB containing a DOM representation
|
5 |
35
|
jones
|
* Copyright: 2000 Regents of the University of California and the
|
6 |
|
|
* National Center for Ecological Analysis and Synthesis
|
7 |
21
|
jones
|
* Authors: Matt Jones
|
8 |
|
|
*
|
9 |
|
|
* Version: '$Id$'
|
10 |
|
|
*/
|
11 |
|
|
|
12 |
51
|
jones
|
package edu.ucsb.nceas.metacat;
|
13 |
|
|
|
14 |
21
|
jones
|
import java.io.*;
|
15 |
|
|
import java.net.URL;
|
16 |
|
|
import java.net.MalformedURLException;
|
17 |
|
|
import java.sql.*;
|
18 |
|
|
import java.util.Stack;
|
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 |
|
|
static String defaultDB = "jdbc:oracle:thin:@localhost:1521:test";
|
27 |
|
|
private Connection conn = null;
|
28 |
|
|
|
29 |
31
|
jones
|
/**
|
30 |
|
|
* main routine used for testing.
|
31 |
|
|
*
|
32 |
|
|
* Usage: java DBReader <nodeid> <user> <password> [dbstring]
|
33 |
|
|
*
|
34 |
|
|
* @param nodeid the id number of the root of the subtree to display
|
35 |
|
|
* @param user the username to use for the database connection
|
36 |
|
|
* @param password the password to use for the database connection
|
37 |
|
|
* @param dbstring the connection info to use for the database connection
|
38 |
|
|
*/
|
39 |
21
|
jones
|
static public void main(String[] args) {
|
40 |
|
|
|
41 |
|
|
if (args.length < 3)
|
42 |
|
|
{
|
43 |
|
|
System.err.println("Wrong number of arguments!!!");
|
44 |
|
|
System.err.println("USAGE: java DBReader " +
|
45 |
|
|
"<nodeid> <user> <password> [dbstring]");
|
46 |
|
|
return;
|
47 |
|
|
} else {
|
48 |
|
|
try {
|
49 |
|
|
|
50 |
23
|
jones
|
String nodeidstr = args[0];
|
51 |
|
|
long nodeid = (new Long(nodeidstr).longValue());
|
52 |
21
|
jones
|
String user = args[1];
|
53 |
|
|
String password = args[2];
|
54 |
|
|
String dbstring = null;
|
55 |
|
|
|
56 |
|
|
if (args.length <= 3) {
|
57 |
|
|
dbstring = defaultDB;
|
58 |
|
|
} else {
|
59 |
|
|
dbstring = args[3];
|
60 |
|
|
}
|
61 |
|
|
|
62 |
50
|
jones
|
// Open a connection to the database
|
63 |
|
|
Connection dbconn = MetaCatUtil.openDBConnection(
|
64 |
|
|
"oracle.jdbc.driver.OracleDriver",
|
65 |
|
|
dbstring, user, password);
|
66 |
|
|
|
67 |
|
|
DBReader rd = new DBReader( dbconn );
|
68 |
31
|
jones
|
String xml = rd.readXMLDocument(nodeid);
|
69 |
21
|
jones
|
System.out.println(xml);
|
70 |
|
|
|
71 |
|
|
} catch (Exception e) {
|
72 |
|
|
System.err.println("EXCEPTION HANDLING REQUIRED");
|
73 |
|
|
System.err.println(e.getMessage());
|
74 |
|
|
e.printStackTrace(System.err);
|
75 |
|
|
}
|
76 |
|
|
}
|
77 |
|
|
}
|
78 |
|
|
|
79 |
31
|
jones
|
/**
|
80 |
|
|
* construct a DBReader instance.
|
81 |
|
|
*
|
82 |
|
|
* Generally, one calls readXMLDocument() after constructing the instance
|
83 |
|
|
*
|
84 |
50
|
jones
|
* @param conn the database connection from which to read the document
|
85 |
31
|
jones
|
*/
|
86 |
50
|
jones
|
public DBReader( Connection conn )
|
87 |
21
|
jones
|
throws IOException,
|
88 |
|
|
SQLException,
|
89 |
|
|
ClassNotFoundException
|
90 |
|
|
{
|
91 |
50
|
jones
|
this.conn = conn;
|
92 |
21
|
jones
|
}
|
93 |
|
|
|
94 |
31
|
jones
|
/**
|
95 |
86
|
jones
|
* Get the root node id for an XML document given a document id
|
96 |
|
|
*
|
97 |
|
|
* @param docid the document node contains the root of the document
|
98 |
|
|
* @returns long the nodeid of the root node for this document
|
99 |
|
|
*/
|
100 |
|
|
public long getRootNode(long docid) {
|
101 |
|
|
// Now look up the root node id
|
102 |
|
|
long rootnodeid = 0;
|
103 |
|
|
|
104 |
|
|
try {
|
105 |
|
|
PreparedStatement pstmt =
|
106 |
|
|
conn.prepareStatement("SELECT rootnodeid " +
|
107 |
|
|
"FROM xml_documents " +
|
108 |
|
|
"WHERE docid = ?");
|
109 |
|
|
// Bind the values to the query
|
110 |
|
|
pstmt.setLong(1, docid);
|
111 |
|
|
|
112 |
|
|
pstmt.execute();
|
113 |
|
|
try {
|
114 |
|
|
ResultSet rs = pstmt.getResultSet();
|
115 |
|
|
try {
|
116 |
|
|
boolean tableHasRows = rs.next();
|
117 |
|
|
if (tableHasRows) {
|
118 |
|
|
try {
|
119 |
|
|
rootnodeid = rs.getLong(1);
|
120 |
|
|
|
121 |
|
|
} catch (SQLException e) {
|
122 |
|
|
System.out.println("Error with getLong: " + e.getMessage());
|
123 |
|
|
}
|
124 |
|
|
}
|
125 |
|
|
} catch (SQLException e) {
|
126 |
|
|
System.out.println("Error with next: " + e.getMessage());
|
127 |
|
|
}
|
128 |
|
|
} catch (SQLException e) {
|
129 |
|
|
System.out.println("Error with getrset: " + e.getMessage());
|
130 |
|
|
}
|
131 |
|
|
pstmt.close();
|
132 |
|
|
} catch (SQLException e) {
|
133 |
|
|
System.out.println("Error getting id: " + e.getMessage());
|
134 |
|
|
}
|
135 |
|
|
|
136 |
|
|
return rootnodeid;
|
137 |
|
|
}
|
138 |
|
|
|
139 |
|
|
/**
|
140 |
31
|
jones
|
* Create an XML document from the database starting with the element
|
141 |
|
|
* having element_id nodeid
|
142 |
|
|
*
|
143 |
86
|
jones
|
* @param docid the document that we want retrieved
|
144 |
31
|
jones
|
*/
|
145 |
86
|
jones
|
public String readXMLDocument(long docid) {
|
146 |
21
|
jones
|
StringBuffer doc = new StringBuffer();
|
147 |
103
|
jones
|
DoctypeInfo dti = getDoctypeInfo(docid);
|
148 |
|
|
String docname = dti.getDocname();
|
149 |
|
|
String doctype = dti.getDoctype();
|
150 |
|
|
String sysid = dti.getSystemID();
|
151 |
21
|
jones
|
|
152 |
86
|
jones
|
ReaderElement element = new ReaderElement(conn, getRootNode(docid));
|
153 |
24
|
jones
|
doc.append("<?xml version=\"1.0\"?>\n");
|
154 |
103
|
jones
|
|
155 |
|
|
if (docname != null) {
|
156 |
|
|
if ((doctype != null) && (sysid != null)) {
|
157 |
|
|
doc.append("<!DOCTYPE " + docname + " PUBLIC \"" + doctype +
|
158 |
|
|
"\" \"" + sysid + "\">\n");
|
159 |
|
|
} else {
|
160 |
|
|
doc.append("<!DOCTYPE " + docname + ">\n");
|
161 |
|
|
}
|
162 |
|
|
}
|
163 |
24
|
jones
|
doc.append(element.toString());
|
164 |
21
|
jones
|
|
165 |
|
|
return (doc.toString());
|
166 |
|
|
}
|
167 |
103
|
jones
|
|
168 |
|
|
/**
|
169 |
|
|
* Look up the document type information from the database
|
170 |
|
|
*
|
171 |
|
|
* @param docid the id of the document to look up
|
172 |
|
|
*/
|
173 |
|
|
public DoctypeInfo getDoctypeInfo(long docid) {
|
174 |
|
|
PreparedStatement pstmt;
|
175 |
|
|
String doctype = null;
|
176 |
|
|
String docname = null;
|
177 |
|
|
String sysid = null;
|
178 |
|
|
DoctypeInfo dti = null;
|
179 |
|
|
|
180 |
|
|
try {
|
181 |
|
|
pstmt =
|
182 |
|
|
conn.prepareStatement("SELECT docname,doctype " +
|
183 |
|
|
"FROM xml_documents " +
|
184 |
|
|
"WHERE docid = ?");
|
185 |
|
|
// Bind the values to the query
|
186 |
|
|
pstmt.setLong(1, new Long(docid).longValue());
|
187 |
|
|
|
188 |
|
|
pstmt.execute();
|
189 |
|
|
ResultSet rs = pstmt.getResultSet();
|
190 |
|
|
boolean tableHasRows = rs.next();
|
191 |
|
|
if (tableHasRows) {
|
192 |
|
|
docname = rs.getString(1);
|
193 |
|
|
doctype = rs.getString(2);
|
194 |
|
|
}
|
195 |
|
|
pstmt.close();
|
196 |
|
|
|
197 |
|
|
pstmt =
|
198 |
|
|
conn.prepareStatement("SELECT system_id " +
|
199 |
|
|
"FROM xml_catalog " +
|
200 |
|
|
"WHERE public_id = ?");
|
201 |
|
|
// Bind the values to the query
|
202 |
|
|
pstmt.setString(1, doctype);
|
203 |
|
|
|
204 |
|
|
pstmt.execute();
|
205 |
|
|
rs = pstmt.getResultSet();
|
206 |
|
|
tableHasRows = rs.next();
|
207 |
|
|
if (tableHasRows) {
|
208 |
|
|
sysid = rs.getString(1);
|
209 |
|
|
}
|
210 |
|
|
pstmt.close();
|
211 |
|
|
} catch (SQLException e) {
|
212 |
|
|
System.out.println("Error getting id: " + e.getMessage());
|
213 |
|
|
}
|
214 |
|
|
|
215 |
|
|
dti = new DoctypeInfo(docname, doctype, sysid);
|
216 |
|
|
return dti;
|
217 |
|
|
}
|
218 |
|
|
|
219 |
|
|
/**
|
220 |
|
|
* A utility class that encapsulates document type information
|
221 |
|
|
*/
|
222 |
|
|
public class DoctypeInfo {
|
223 |
|
|
private String docname = null;
|
224 |
|
|
private String doctype = null;
|
225 |
|
|
private String system_id = null;
|
226 |
|
|
|
227 |
|
|
/**
|
228 |
|
|
* Constructor
|
229 |
|
|
*/
|
230 |
|
|
public DoctypeInfo(String docname, String doctype, String system_id) {
|
231 |
|
|
this.docname = docname;
|
232 |
|
|
this.doctype = doctype;
|
233 |
|
|
this.system_id = system_id;
|
234 |
|
|
}
|
235 |
|
|
|
236 |
|
|
/**
|
237 |
|
|
* get the document name
|
238 |
|
|
*/
|
239 |
|
|
public String getDocname() {
|
240 |
|
|
return docname;
|
241 |
|
|
}
|
242 |
|
|
|
243 |
|
|
/**
|
244 |
|
|
* get the document type
|
245 |
|
|
*/
|
246 |
|
|
public String getDoctype() {
|
247 |
|
|
return doctype;
|
248 |
|
|
}
|
249 |
|
|
|
250 |
|
|
/**
|
251 |
|
|
* get the system identifier
|
252 |
|
|
*/
|
253 |
|
|
public String getSystemID() {
|
254 |
|
|
return system_id;
|
255 |
|
|
}
|
256 |
|
|
}
|
257 |
|
|
|
258 |
21
|
jones
|
}
|