1
|
/**
|
2
|
* '$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
|
*
|
9
|
* '$Author: jones $'
|
10
|
* '$Date: 2000-06-26 03:35:05 -0700 (Mon, 26 Jun 2000) $'
|
11
|
* '$Revision: 203 $'
|
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
|
|
22
|
/**
|
23
|
* A Class that creates an XML text document
|
24
|
* from a query to a relational DB containing a DOM representation
|
25
|
*/
|
26
|
public class DBReader {
|
27
|
|
28
|
private Connection conn = null;
|
29
|
|
30
|
/**
|
31
|
* main routine used for testing.
|
32
|
* <p>
|
33
|
* Usage: java DBReader <docid>
|
34
|
*
|
35
|
* @param nodeid the id number of the root of the subtree to display
|
36
|
*/
|
37
|
static public void main(String[] args) {
|
38
|
|
39
|
if (args.length < 1)
|
40
|
{
|
41
|
System.err.println("Wrong number of arguments!!!");
|
42
|
System.err.println("USAGE: java DBReader <docid>");
|
43
|
return;
|
44
|
} else {
|
45
|
try {
|
46
|
|
47
|
String docid = args[0];
|
48
|
|
49
|
// Open a connection to the database
|
50
|
MetaCatUtil util = new MetaCatUtil();
|
51
|
Connection dbconn = util.openDBConnection();
|
52
|
|
53
|
DBReader rd = new DBReader( dbconn );
|
54
|
String xml = rd.readXMLDocument(docid);
|
55
|
System.out.println(xml);
|
56
|
|
57
|
} 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
|
/**
|
66
|
* construct a DBReader instance.
|
67
|
*
|
68
|
* Generally, one calls readXMLDocument() after constructing the instance
|
69
|
*
|
70
|
* @param conn the database connection from which to read the document
|
71
|
*/
|
72
|
public DBReader( Connection conn )
|
73
|
throws IOException,
|
74
|
SQLException,
|
75
|
ClassNotFoundException
|
76
|
{
|
77
|
this.conn = conn;
|
78
|
}
|
79
|
|
80
|
/**
|
81
|
* Get the root node id for an XML document given a document id
|
82
|
*
|
83
|
* @param docid the document node contains the root of the document
|
84
|
* @returns long the nodeid of the root node for this document
|
85
|
*/
|
86
|
public long getRootNode(String docid) {
|
87
|
// Now look up the root node id
|
88
|
long rootnodeid = 0;
|
89
|
|
90
|
try {
|
91
|
PreparedStatement pstmt =
|
92
|
conn.prepareStatement("SELECT rootnodeid " +
|
93
|
"FROM xml_documents " +
|
94
|
"WHERE docid = ?");
|
95
|
// Bind the values to the query
|
96
|
pstmt.setString(1, docid);
|
97
|
|
98
|
pstmt.execute();
|
99
|
try {
|
100
|
ResultSet rs = pstmt.getResultSet();
|
101
|
try {
|
102
|
boolean tableHasRows = rs.next();
|
103
|
if (tableHasRows) {
|
104
|
try {
|
105
|
rootnodeid = rs.getLong(1);
|
106
|
|
107
|
} catch (SQLException e) {
|
108
|
System.out.println("Error with getLong: " + e.getMessage());
|
109
|
}
|
110
|
}
|
111
|
} catch (SQLException e) {
|
112
|
System.out.println("Error with next: " + e.getMessage());
|
113
|
}
|
114
|
} catch (SQLException e) {
|
115
|
System.out.println("Error with getrset: " + e.getMessage());
|
116
|
}
|
117
|
pstmt.close();
|
118
|
} catch (SQLException e) {
|
119
|
System.out.println("Error getting id: " + e.getMessage());
|
120
|
}
|
121
|
|
122
|
return rootnodeid;
|
123
|
}
|
124
|
|
125
|
/**
|
126
|
* Create an XML document from the database starting with the element
|
127
|
* having element_id nodeid
|
128
|
*
|
129
|
* @param docid the document that we want retrieved
|
130
|
*/
|
131
|
public String readXMLDocument(String docid) {
|
132
|
StringBuffer doc = new StringBuffer();
|
133
|
DoctypeInfo dti = getDoctypeInfo(docid);
|
134
|
|
135
|
if (dti != null) {
|
136
|
String docname = dti.getDocname();
|
137
|
String doctype = dti.getDoctype();
|
138
|
String sysid = dti.getSystemID();
|
139
|
|
140
|
ElementNode element = new ElementNode(conn, getRootNode(docid));
|
141
|
doc.append("<?xml version=\"1.0\"?>\n");
|
142
|
|
143
|
if (docname != null) {
|
144
|
if ((doctype != null) && (sysid != null)) {
|
145
|
doc.append("<!DOCTYPE " + docname + " PUBLIC \"" + doctype +
|
146
|
"\" \"" + sysid + "\">\n");
|
147
|
} else {
|
148
|
doc.append("<!DOCTYPE " + docname + ">\n");
|
149
|
}
|
150
|
}
|
151
|
doc.append(element.toString());
|
152
|
} else {
|
153
|
doc.append("<error>Document " + docid + " not found.</error>");
|
154
|
}
|
155
|
|
156
|
return (doc.toString());
|
157
|
}
|
158
|
|
159
|
/**
|
160
|
* Look up the document type information from the database
|
161
|
*
|
162
|
* @param docid the id of the document to look up
|
163
|
*/
|
164
|
public DoctypeInfo getDoctypeInfo(String docid) {
|
165
|
PreparedStatement pstmt;
|
166
|
String doctype = null;
|
167
|
String docname = null;
|
168
|
String sysid = null;
|
169
|
DoctypeInfo dti = null;
|
170
|
|
171
|
try {
|
172
|
pstmt =
|
173
|
conn.prepareStatement("SELECT docname,doctype " +
|
174
|
"FROM xml_documents " +
|
175
|
"WHERE docid = ?");
|
176
|
// Bind the values to the query
|
177
|
pstmt.setString(1, docid);
|
178
|
|
179
|
pstmt.execute();
|
180
|
ResultSet rs = pstmt.getResultSet();
|
181
|
boolean tableHasRows = rs.next();
|
182
|
if (tableHasRows) {
|
183
|
docname = rs.getString(1);
|
184
|
doctype = rs.getString(2);
|
185
|
}
|
186
|
pstmt.close();
|
187
|
|
188
|
if (doctype != null) {
|
189
|
pstmt =
|
190
|
conn.prepareStatement("SELECT system_id " +
|
191
|
"FROM xml_catalog " +
|
192
|
"WHERE public_id = ?");
|
193
|
// Bind the values to the query
|
194
|
pstmt.setString(1, doctype);
|
195
|
|
196
|
pstmt.execute();
|
197
|
rs = pstmt.getResultSet();
|
198
|
tableHasRows = rs.next();
|
199
|
if (tableHasRows) {
|
200
|
sysid = rs.getString(1);
|
201
|
}
|
202
|
pstmt.close();
|
203
|
}
|
204
|
} catch (SQLException e) {
|
205
|
System.out.println("Error getting id: " + e.getMessage());
|
206
|
}
|
207
|
|
208
|
if (docname != null) {
|
209
|
dti = new DoctypeInfo(docname, doctype, sysid);
|
210
|
} else {
|
211
|
dti = null;
|
212
|
}
|
213
|
|
214
|
return dti;
|
215
|
}
|
216
|
|
217
|
/**
|
218
|
* A utility class that encapsulates document type information
|
219
|
*/
|
220
|
public class DoctypeInfo {
|
221
|
private String docname = null;
|
222
|
private String doctype = null;
|
223
|
private String system_id = null;
|
224
|
|
225
|
/**
|
226
|
* Constructor
|
227
|
*/
|
228
|
public DoctypeInfo(String docname, String doctype, String system_id) {
|
229
|
this.docname = docname;
|
230
|
this.doctype = doctype;
|
231
|
this.system_id = system_id;
|
232
|
}
|
233
|
|
234
|
/**
|
235
|
* get the document name
|
236
|
*/
|
237
|
public String getDocname() {
|
238
|
return docname;
|
239
|
}
|
240
|
|
241
|
/**
|
242
|
* get the document type
|
243
|
*/
|
244
|
public String getDoctype() {
|
245
|
return doctype;
|
246
|
}
|
247
|
|
248
|
/**
|
249
|
* get the system identifier
|
250
|
*/
|
251
|
public String getSystemID() {
|
252
|
return system_id;
|
253
|
}
|
254
|
}
|
255
|
|
256
|
}
|
257
|
|
258
|
/**
|
259
|
* '$Log$
|
260
|
* 'Revision 1.16.2.2 2000/06/25 23:38:16 jones
|
261
|
* 'Added RCSfile keyword
|
262
|
* '
|
263
|
* 'Revision 1.16.2.1 2000/06/25 23:34:17 jones
|
264
|
* 'Changed documentation formatting, added log entries at bottom of source files
|
265
|
* ''
|
266
|
*/
|