1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Purpose: A Class that implements org.xml.sax.EntityResolver interface
|
4
|
* for resolving external entities
|
5
|
* Copyright: 2000 Regents of the University of California and the
|
6
|
* National Center for Ecological Analysis and Synthesis
|
7
|
* Authors: Jivka Bojilova, Matt Jones
|
8
|
* Release: @release@
|
9
|
*
|
10
|
* '$Author: bojilova $'
|
11
|
* '$Date: 2001-02-05 16:17:32 -0800 (Mon, 05 Feb 2001) $'
|
12
|
* '$Revision: 694 $'
|
13
|
*
|
14
|
* This program is free software; you can redistribute it and/or modify
|
15
|
* it under the terms of the GNU General Public License as published by
|
16
|
* the Free Software Foundation; either version 2 of the License, or
|
17
|
* (at your option) any later version.
|
18
|
*
|
19
|
* This program is distributed in the hope that it will be useful,
|
20
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
21
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
22
|
* GNU General Public License for more details.
|
23
|
*
|
24
|
* You should have received a copy of the GNU General Public License
|
25
|
* along with this program; if not, write to the Free Software
|
26
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
27
|
*/
|
28
|
|
29
|
package edu.ucsb.nceas.metacat;
|
30
|
|
31
|
import org.xml.sax.*;
|
32
|
import org.xml.sax.helpers.DefaultHandler;
|
33
|
|
34
|
import java.sql.*;
|
35
|
import java.io.File;
|
36
|
import java.io.Reader;
|
37
|
import java.io.BufferedReader;
|
38
|
import java.io.BufferedInputStream;
|
39
|
import java.io.FileWriter;
|
40
|
import java.io.BufferedWriter;
|
41
|
import java.io.InputStream;
|
42
|
import java.io.IOException;
|
43
|
import java.net.URL;
|
44
|
import java.net.URLConnection;
|
45
|
import java.net.MalformedURLException;
|
46
|
|
47
|
/**
|
48
|
* A database aware Class implementing EntityResolver interface for the SAX
|
49
|
* parser to call when processing the XML stream and intercepting any
|
50
|
* external entities (including the external DTD subset and external
|
51
|
* parameter entities, if any) before including them.
|
52
|
*/
|
53
|
public class DBEntityResolver implements EntityResolver
|
54
|
{
|
55
|
private Connection conn = null;
|
56
|
private DefaultHandler handler = null;
|
57
|
private String docname = null;
|
58
|
private String doctype = null;
|
59
|
private String systemid = null;
|
60
|
private Reader dtdtext = null;
|
61
|
|
62
|
/**
|
63
|
* Construct an instance of the DBEntityResolver class
|
64
|
*
|
65
|
* @param conn the JDBC connection to which information is written
|
66
|
*/
|
67
|
public DBEntityResolver(Connection conn)
|
68
|
{
|
69
|
this.conn = conn;
|
70
|
}
|
71
|
/**
|
72
|
* Construct an instance of the DBEntityResolver class
|
73
|
*
|
74
|
* @param conn the JDBC connection to which information is written
|
75
|
* @param handler the SAX handler to determine parsing context
|
76
|
* @param dtd Reader of new dtd to be uploaded on server's file system
|
77
|
*/
|
78
|
public DBEntityResolver(Connection conn, DefaultHandler handler, Reader dtd)
|
79
|
{
|
80
|
this.conn = conn;
|
81
|
this.handler = handler;
|
82
|
this.dtdtext = dtd;
|
83
|
}
|
84
|
|
85
|
/**
|
86
|
* The Parser call this method before opening any external entity
|
87
|
* except the top-level document entity (including the external DTD subset,
|
88
|
* external entities referenced within the DTD, and external entities
|
89
|
* referenced within the document element)
|
90
|
*/
|
91
|
public InputSource resolveEntity (String publicId, String systemId)
|
92
|
throws SAXException
|
93
|
{
|
94
|
String dbSystemID;
|
95
|
String doctype = null;
|
96
|
|
97
|
// Won't have a handler under all cases
|
98
|
if ( handler != null ) {
|
99
|
if ( handler instanceof DBSAXHandler ) {
|
100
|
DBSAXHandler dhandler = null;
|
101
|
dhandler = (DBSAXHandler)handler;
|
102
|
if ( dhandler.processingDTD() ) {
|
103
|
// public ID is doctype
|
104
|
if (publicId != null) {
|
105
|
doctype = publicId;
|
106
|
// assume public ID (doctype) is docname
|
107
|
} else if (systemId != null) {
|
108
|
doctype = dhandler.getDocname();
|
109
|
}
|
110
|
}
|
111
|
} else if ( handler instanceof AccessControlList ) {
|
112
|
AccessControlList ahandler = null;
|
113
|
ahandler = (AccessControlList)handler;
|
114
|
//if ( ahandler.processingDTD() ) {
|
115
|
// public ID is doctype
|
116
|
if (publicId != null) {
|
117
|
doctype = publicId;
|
118
|
// assume public ID (doctype) is docname
|
119
|
} else if (systemId != null) {
|
120
|
doctype = ahandler.getDocname();
|
121
|
}
|
122
|
//}
|
123
|
}
|
124
|
}
|
125
|
|
126
|
// get System ID for doctype
|
127
|
if (doctype != null) {
|
128
|
// look at db XML Catalog for System ID
|
129
|
dbSystemID = getDTDSystemID(doctype);
|
130
|
boolean doctypeIsInDB = true;
|
131
|
// no System ID found in db XML Catalog
|
132
|
if (dbSystemID == null) {
|
133
|
doctypeIsInDB = false;
|
134
|
// use the provided System ID
|
135
|
if (systemId != null) {
|
136
|
dbSystemID = systemId;
|
137
|
}
|
138
|
}
|
139
|
|
140
|
// there are dtd text provided; try to upload on Metacat
|
141
|
if ( dtdtext != null ) {
|
142
|
dbSystemID = uploadDTD(dbSystemID);
|
143
|
}
|
144
|
|
145
|
// open URLConnection to check first
|
146
|
InputStream istream = checkURLConnection(dbSystemID);
|
147
|
|
148
|
// need to register System ID in db XML Catalog if not yet
|
149
|
if ( !doctypeIsInDB ) {
|
150
|
// new DTD from outside URL location; try to upload on Metacat
|
151
|
if ( dtdtext == null ) {
|
152
|
dbSystemID = uploadDTDFromURL(istream, dbSystemID);
|
153
|
}
|
154
|
registerDTD(doctype, dbSystemID);
|
155
|
}
|
156
|
|
157
|
// return a byte-input stream for use
|
158
|
InputSource is = new InputSource(dbSystemID);
|
159
|
|
160
|
// close and open URLConnection again
|
161
|
try {
|
162
|
istream.close();
|
163
|
} catch (IOException e) {
|
164
|
throw new SAXException
|
165
|
("DBEntityResolver.resolveEntity(): " + e.getMessage());
|
166
|
}
|
167
|
istream = checkURLConnection(dbSystemID);
|
168
|
is.setByteStream(istream);
|
169
|
return is;
|
170
|
|
171
|
} else {
|
172
|
// use provided systemId for the other cases
|
173
|
InputStream istream = checkURLConnection(systemId);
|
174
|
return null;
|
175
|
|
176
|
}
|
177
|
|
178
|
}
|
179
|
|
180
|
/**
|
181
|
* Look at db XML Catalog to get System ID (if any) for @doctype.
|
182
|
* Return null if there are no System ID found for @doctype
|
183
|
*/
|
184
|
private String getDTDSystemID( String doctype )
|
185
|
throws SAXException
|
186
|
{
|
187
|
String systemid = null;
|
188
|
Statement stmt;
|
189
|
try {
|
190
|
stmt = conn.createStatement();
|
191
|
stmt.execute("SELECT system_id FROM xml_catalog " +
|
192
|
"WHERE entry_type = 'DTD' AND public_id = '" +
|
193
|
doctype + "'");
|
194
|
ResultSet rs = stmt.getResultSet();
|
195
|
boolean tableHasRows = rs.next();
|
196
|
if (tableHasRows) {
|
197
|
systemid = rs.getString(1);
|
198
|
}
|
199
|
stmt.close();
|
200
|
} catch (SQLException e) {
|
201
|
throw new SAXException
|
202
|
("DBEntityResolver.getDTDSystemID(): " + e.getMessage());
|
203
|
}
|
204
|
|
205
|
// return the selected System ID
|
206
|
return systemid;
|
207
|
}
|
208
|
|
209
|
/**
|
210
|
* Register new DTD identified by @systemId in Metacat XML Catalog
|
211
|
* . make a reference with @systemId for @doctype in Metacat DB
|
212
|
*/
|
213
|
private void registerDTD ( String doctype, String systemId )
|
214
|
throws SAXException
|
215
|
{
|
216
|
// make a reference in db catalog table with @systemId for @doctype
|
217
|
try {
|
218
|
PreparedStatement pstmt;
|
219
|
pstmt = conn.prepareStatement(
|
220
|
"INSERT INTO xml_catalog " +
|
221
|
"(catalog_id, entry_type, public_id, system_id) " +
|
222
|
"VALUES (null, 'DTD', ?, ?)");
|
223
|
// Bind the values to the query
|
224
|
pstmt.setString(1, doctype);
|
225
|
pstmt.setString(2, systemId);
|
226
|
// Do the insertion
|
227
|
pstmt.execute();
|
228
|
pstmt.close();
|
229
|
} catch (SQLException e) {
|
230
|
throw new SAXException
|
231
|
("DBEntityResolver.registerDTD(): " + e.getMessage());
|
232
|
}
|
233
|
}
|
234
|
|
235
|
/**
|
236
|
* Upload new DTD text identified by @systemId to Metacat file system
|
237
|
*/
|
238
|
private String uploadDTD ( String systemId )
|
239
|
throws SAXException
|
240
|
{
|
241
|
MetaCatUtil util = new MetaCatUtil();
|
242
|
String dtdPath = util.getOption("dtdPath");
|
243
|
String dtdURL = util.getOption("dtdURL");
|
244
|
|
245
|
// get filename from systemId
|
246
|
String filename = systemId;
|
247
|
int slash = Math.max(filename.lastIndexOf('/'), filename.lastIndexOf('\\'));
|
248
|
if ( slash > -1 ) {
|
249
|
filename = filename.substring(slash + 1);
|
250
|
}
|
251
|
|
252
|
// writing dtd text on Metacat file system as filename
|
253
|
try {
|
254
|
// create a buffering character-input stream
|
255
|
// that uses a default-sized input buffer
|
256
|
BufferedReader in = new BufferedReader(dtdtext);
|
257
|
|
258
|
// open file writer to write the input into it
|
259
|
//String dtdPath = "/opt/tomcat/webapps/bojilova/dtd/";
|
260
|
File f = new File(dtdPath, filename);
|
261
|
synchronized (f) {
|
262
|
try {
|
263
|
if ( f.exists() ) {
|
264
|
throw new IOException("File already exist: " + f.getCanonicalFile());
|
265
|
//if ( f.exists() && !f.canWrite() ) {
|
266
|
// throw new IOException("Not writable: " + f.getCanonicalFile());
|
267
|
}
|
268
|
} catch (SecurityException se) {
|
269
|
// if a security manager exists,
|
270
|
// its checkRead method is called for f.exist()
|
271
|
// or checkWrite method is called for f.canWrite()
|
272
|
throw se;
|
273
|
}
|
274
|
// create a buffered character-output stream
|
275
|
// that uses a default-sized output buffer
|
276
|
FileWriter fw = new FileWriter(f);
|
277
|
BufferedWriter out = new BufferedWriter(fw);
|
278
|
|
279
|
// read the input and write into the file writer
|
280
|
String inputLine;
|
281
|
while ( (inputLine = in.readLine()) != null ) {
|
282
|
out.write(inputLine, 0, inputLine.length());
|
283
|
out.newLine(); //instead of out.write('\r\n');
|
284
|
}
|
285
|
|
286
|
// the input and the output streams must be closed
|
287
|
in.close();
|
288
|
out.flush();
|
289
|
out.close();
|
290
|
fw.close();
|
291
|
} // end of synchronized
|
292
|
} catch (MalformedURLException e) {
|
293
|
throw new SAXException
|
294
|
("DBEntityResolver.uploadDTD(): " + e.getMessage());
|
295
|
} catch (IOException e) {
|
296
|
throw new SAXException
|
297
|
("DBEntityResolver.uploadDTD(): " + e.getMessage());
|
298
|
} catch (SecurityException e) {
|
299
|
throw new SAXException
|
300
|
("DBEntityResolver.uploadDTD(): " + e.getMessage());
|
301
|
}
|
302
|
|
303
|
//String dtdURL = "http://dev.nceas.ucsb.edu/bojilova/dtd/";
|
304
|
return dtdURL + filename;
|
305
|
}
|
306
|
|
307
|
|
308
|
/**
|
309
|
* Upload new DTD located at outside URL to Metacat file system
|
310
|
*/
|
311
|
private String uploadDTDFromURL(InputStream istream, String systemId)
|
312
|
throws SAXException
|
313
|
{
|
314
|
MetaCatUtil util = new MetaCatUtil();
|
315
|
String dtdPath = util.getOption("dtdPath");
|
316
|
String dtdURL = util.getOption("dtdURL");
|
317
|
|
318
|
// get filename from systemId
|
319
|
String filename = systemId;
|
320
|
int slash = Math.max(filename.lastIndexOf('/'), filename.lastIndexOf('\\'));
|
321
|
if ( slash > -1 ) {
|
322
|
filename = filename.substring(slash + 1);
|
323
|
}
|
324
|
|
325
|
// writing dtd text on Metacat file system as filename
|
326
|
try {
|
327
|
// create a buffering character-input stream
|
328
|
// that uses a default-sized input buffer
|
329
|
BufferedInputStream in = new BufferedInputStream(istream);
|
330
|
|
331
|
// open file writer to write the input into it
|
332
|
//String dtdPath = "/opt/tomcat/webapps/bojilova/dtd/";
|
333
|
File f = new File(dtdPath, filename);
|
334
|
synchronized (f) {
|
335
|
try {
|
336
|
if ( f.exists() ) {
|
337
|
throw new IOException("File already exist: " + f.getCanonicalFile());
|
338
|
//if ( f.exists() && !f.canWrite() ) {
|
339
|
// throw new IOException("Not writable: " + f.getCanonicalFile());
|
340
|
}
|
341
|
} catch (SecurityException se) {
|
342
|
// if a security manager exists,
|
343
|
// its checkRead method is called for f.exist()
|
344
|
// or checkWrite method is called for f.canWrite()
|
345
|
throw se;
|
346
|
}
|
347
|
// create a buffered character-output stream
|
348
|
// that uses a default-sized output buffer
|
349
|
FileWriter fw = new FileWriter(f);
|
350
|
BufferedWriter out = new BufferedWriter(fw);
|
351
|
|
352
|
// read the input and write into the file writer
|
353
|
int inputByte;
|
354
|
while ( (inputByte = in.read()) != -1 ) {
|
355
|
out.write(inputByte);
|
356
|
//out.newLine(); //instead of out.write('\r\n');
|
357
|
}
|
358
|
|
359
|
// the input and the output streams must be closed
|
360
|
in.close();
|
361
|
out.flush();
|
362
|
out.close();
|
363
|
fw.close();
|
364
|
} // end of synchronized
|
365
|
} catch (MalformedURLException e) {
|
366
|
throw new SAXException
|
367
|
("DBEntityResolver.uploadDTDFromURL(): " + e.getMessage());
|
368
|
} catch (IOException e) {
|
369
|
throw new SAXException
|
370
|
("DBEntityResolver.uploadDTDFromURL(): " + e.getMessage());
|
371
|
} catch (SecurityException e) {
|
372
|
throw new SAXException
|
373
|
("DBEntityResolver.uploadDTDFromURL(): " + e.getMessage());
|
374
|
}
|
375
|
|
376
|
//String dtdURL = "http://dev.nceas.ucsb.edu/bojilova/dtd/";
|
377
|
return dtdURL + filename;
|
378
|
}
|
379
|
|
380
|
/**
|
381
|
* Check URL Connection for @systemId, and return an InputStream
|
382
|
* that can be used to read from the systemId URL. The parser ends
|
383
|
* up using this via the InputSource to read the DTD.
|
384
|
*
|
385
|
* @param systemId a URI (in practice URL) to be checked and opened
|
386
|
*/
|
387
|
private InputStream checkURLConnection (String systemId)
|
388
|
throws SAXException
|
389
|
{
|
390
|
try {
|
391
|
|
392
|
return (new URL(systemId).openStream());
|
393
|
|
394
|
} catch (MalformedURLException e) {
|
395
|
throw new SAXException
|
396
|
("DBEntityResolver.checkURLConnection(): " + e.getMessage());
|
397
|
} catch (IOException e) {
|
398
|
throw new SAXException
|
399
|
("DBEntityResolver.checkURLConnection(): " + e.getMessage());
|
400
|
}
|
401
|
}
|
402
|
}
|