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