1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Purpose: A Class that validates XML documents
|
4
|
* This class is designed to be 'parser independent
|
5
|
* i.e. it uses only org.xml.sax classes
|
6
|
* It is tied to SAX 2.0 methods
|
7
|
* Copyright: 2000 Regents of the University of California and the
|
8
|
* National Center for Ecological Analysis and Synthesis
|
9
|
* Authors: Dan Higgins, Matt Jones
|
10
|
*
|
11
|
* '$Author: daigle $'
|
12
|
* '$Date: 2008-07-06 21:25:34 -0700 (Sun, 06 Jul 2008) $'
|
13
|
* '$Revision: 4080 $'
|
14
|
*
|
15
|
* This program is free software; you can redistribute it and/or modify
|
16
|
* it under the terms of the GNU General Public License as published by
|
17
|
* the Free Software Foundation; either version 2 of the License, or
|
18
|
* (at your option) any later version.
|
19
|
*
|
20
|
* This program is distributed in the hope that it will be useful,
|
21
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
22
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
23
|
* GNU General Public License for more details.
|
24
|
*
|
25
|
* You should have received a copy of the GNU General Public License
|
26
|
* along with this program; if not, write to the Free Software
|
27
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
28
|
*/
|
29
|
package edu.ucsb.nceas.metacat;
|
30
|
|
31
|
|
32
|
import java.net.URL;
|
33
|
import java.net.MalformedURLException;
|
34
|
import java.util.*;
|
35
|
import java.io.*;
|
36
|
import java.lang.reflect.*;
|
37
|
import java.sql.*;
|
38
|
|
39
|
import org.w3c.dom.*;
|
40
|
import org.xml.sax.*;
|
41
|
import org.xml.sax.helpers.XMLReaderFactory;
|
42
|
|
43
|
import com.arbortext.catalog.*;
|
44
|
|
45
|
import edu.ucsb.nceas.metacat.service.PropertyService;
|
46
|
|
47
|
/**
|
48
|
* Name: DBValidate.java
|
49
|
* Purpose: A Class that validates XML documents
|
50
|
* This class is designed to be parser independent
|
51
|
* i.e. it uses only org.xml.sax classes
|
52
|
* It is tied to SAX 2.0 methods
|
53
|
* Copyright: 2000 Regents of the University of California and the
|
54
|
* National Center for Ecological Analysis and Synthesis
|
55
|
* April 28, 2000
|
56
|
* Authors: Dan Higgins, Matt Jones
|
57
|
*/
|
58
|
public class DBValidate {
|
59
|
|
60
|
static int WARNING = 0;
|
61
|
static int ERROR=1;
|
62
|
static int FATAL_ERROR=2;
|
63
|
|
64
|
XMLReader parser;
|
65
|
ErrorStorer ef;
|
66
|
String xml_doc; // document to be parsed
|
67
|
public boolean alreadyHandle = false;
|
68
|
|
69
|
/** Construct a new validation object */
|
70
|
public DBValidate() {
|
71
|
alreadyHandle = false;
|
72
|
try {
|
73
|
// Get an instance of the parser
|
74
|
String parserName = PropertyService.getProperty("saxparser");
|
75
|
parser = XMLReaderFactory.createXMLReader(parserName);
|
76
|
parser.setFeature("http://xml.org/sax/features/validation",true);
|
77
|
//parser.setValidationMode(true); // Oracle
|
78
|
} catch (Exception e) {
|
79
|
System.err.println("Could not create parser in DBValidate.DBValidate");
|
80
|
}
|
81
|
}
|
82
|
|
83
|
/** Construct a new validation object using an OASIS catalog file */
|
84
|
public DBValidate(String xmlcatalogfile) {
|
85
|
this();
|
86
|
|
87
|
CatalogEntityResolver cer = new CatalogEntityResolver();
|
88
|
try {
|
89
|
Catalog myCatalog = new Catalog();
|
90
|
myCatalog.loadSystemCatalogs();
|
91
|
myCatalog.parseCatalog(xmlcatalogfile);
|
92
|
cer.setCatalog(myCatalog);
|
93
|
} catch (Exception e) {
|
94
|
System.out.println("Problem creating Catalog in DBValidate.DBValidate");
|
95
|
}
|
96
|
|
97
|
parser.setEntityResolver(cer);
|
98
|
}
|
99
|
|
100
|
/** Construct a new validation object using a database entity resolver */
|
101
|
public DBValidate(DBConnection conn) {
|
102
|
this();
|
103
|
|
104
|
DBEntityResolver dbresolver = new DBEntityResolver(conn);
|
105
|
parser.setEntityResolver(dbresolver);
|
106
|
}
|
107
|
|
108
|
/**
|
109
|
* validate an xml document against its DTD
|
110
|
*
|
111
|
* @param doc the filename of the document to validate
|
112
|
*/
|
113
|
public boolean validate(String doc) {
|
114
|
xml_doc = doc;
|
115
|
ef = new ErrorStorer();
|
116
|
ef.resetErrors();
|
117
|
parser.setErrorHandler(ef);
|
118
|
try {
|
119
|
parser.parse((createURL(xml_doc)).toString());
|
120
|
} catch (IOException e) {
|
121
|
System.out.println("IOException:Could not parse :" + xml_doc +
|
122
|
" from DBValidate.validate");
|
123
|
ParseError eip = null;
|
124
|
eip = new ParseError("",0,0,
|
125
|
"IOException:Could not parse :"+xml_doc);
|
126
|
if (ef.errorNodes == null) ef.errorNodes = new Vector();
|
127
|
ef.errorNodes.addElement(eip);
|
128
|
|
129
|
} catch (Exception e) {}
|
130
|
|
131
|
|
132
|
|
133
|
if (ef != null && ef.getErrorNodes()!=null &&
|
134
|
ef.getErrorNodes().size() > 0 ) {
|
135
|
return false;
|
136
|
} else {
|
137
|
return true;
|
138
|
}
|
139
|
}
|
140
|
|
141
|
/**
|
142
|
* validate an xml document against its DTD
|
143
|
*
|
144
|
* @param xmldoc the String containing the xml document to validate
|
145
|
*/
|
146
|
public boolean validateString(String xmldoc) {
|
147
|
// string is actual XML here, NOT URL or file name
|
148
|
ef = new ErrorStorer();
|
149
|
ef.resetErrors();
|
150
|
parser.setErrorHandler(ef);
|
151
|
|
152
|
InputSource is = new InputSource(new StringReader(xmldoc));
|
153
|
try
|
154
|
{
|
155
|
|
156
|
parser.parse(is);
|
157
|
|
158
|
}
|
159
|
catch (SAXParseException e)
|
160
|
{
|
161
|
System.out.println("SAXParseException Error in DBValidate.validateString"
|
162
|
+e.getMessage());
|
163
|
ef.error(e);
|
164
|
}
|
165
|
catch (SAXException saxe)
|
166
|
{
|
167
|
System.out.println("SAXException error in validateString: "
|
168
|
+saxe.getMessage());
|
169
|
ef.otherError(saxe, null);
|
170
|
|
171
|
}
|
172
|
catch (IOException ioe)
|
173
|
{
|
174
|
System.out.println("IOExcption error in validateString "
|
175
|
+ioe.getMessage());
|
176
|
ef.otherError(ioe, null);
|
177
|
}
|
178
|
|
179
|
if (ef != null && ef.getErrorNodes()!=null &&
|
180
|
ef.getErrorNodes().size() > 0 ) {
|
181
|
return false;
|
182
|
} else {
|
183
|
return true;
|
184
|
}
|
185
|
}
|
186
|
|
187
|
/** provide a list of errors from the validation process */
|
188
|
public String returnErrors() {
|
189
|
StringBuffer errorstring = new StringBuffer();
|
190
|
errorstring.append("<?xml version=\"1.0\" ?>\n");
|
191
|
if (ef != null && ef.getErrorNodes()!=null &&
|
192
|
ef.getErrorNodes().size() > 0 ) {
|
193
|
Vector errors = ef.getErrorNodes();
|
194
|
errorstring.append("<validationerrors>\n");
|
195
|
for (Enumeration e = errors.elements() ; e.hasMoreElements() ;) {
|
196
|
errorstring.append(
|
197
|
((ParseError)(e.nextElement())).toXML());
|
198
|
}
|
199
|
errorstring.append("</validationerrors>\n");
|
200
|
} else {
|
201
|
errorstring.append("<valid />\n");
|
202
|
}
|
203
|
return errorstring.toString();
|
204
|
}
|
205
|
|
206
|
/** Create a URL object from either a URL string or a plain file name. */
|
207
|
private URL createURL(String name) throws Exception {
|
208
|
try {
|
209
|
URL u = new URL(name);
|
210
|
return u;
|
211
|
} catch (MalformedURLException ex) {
|
212
|
}
|
213
|
URL u = new URL("file:" + new File(name).getAbsolutePath());
|
214
|
return u;
|
215
|
}
|
216
|
|
217
|
/**
|
218
|
* main method for testing
|
219
|
* <p>
|
220
|
* Usage: java DBValidate <xmlfile or URL>
|
221
|
*/
|
222
|
public static void main(String[] args) {
|
223
|
|
224
|
if (args.length != 1) {
|
225
|
System.out.println("Usage: java DBValidate <xmlfile or URL>");
|
226
|
System.exit(0);
|
227
|
}
|
228
|
|
229
|
String doc = args[0];
|
230
|
|
231
|
DBConnection conn = null;
|
232
|
int serailNumber = -1;
|
233
|
try {
|
234
|
conn = DBConnectionPool.getDBConnection("DBValidate.main");
|
235
|
serailNumber = conn.getCheckOutSerialNumber();
|
236
|
|
237
|
DBValidate gxv = new DBValidate(conn);
|
238
|
if (gxv.validate(doc)) {
|
239
|
System.out.print(gxv.returnErrors());
|
240
|
} else {
|
241
|
System.out.print(gxv.returnErrors());
|
242
|
}
|
243
|
} catch (SQLException e) {
|
244
|
System.out.println("<error>Couldn't open database connection.</error>");
|
245
|
}
|
246
|
finally
|
247
|
{
|
248
|
DBConnectionPool.returnDBConnection(conn, serailNumber);
|
249
|
}//finally
|
250
|
}
|
251
|
|
252
|
|
253
|
/**
|
254
|
* ErrorStorer has been revised here to simply create a Vector of
|
255
|
* ParseError objects
|
256
|
*
|
257
|
*/
|
258
|
class ErrorStorer implements ErrorHandler {
|
259
|
|
260
|
//
|
261
|
// Data
|
262
|
//
|
263
|
Vector errorNodes = null;
|
264
|
|
265
|
/**
|
266
|
* Constructor
|
267
|
*/
|
268
|
public ErrorStorer() {
|
269
|
}
|
270
|
|
271
|
/**
|
272
|
* The client is is allowed to get a reference to the Hashtable,
|
273
|
* and so could corrupt it, or add to it...
|
274
|
*/
|
275
|
public Vector getErrorNodes() {
|
276
|
return errorNodes;
|
277
|
}
|
278
|
|
279
|
/**
|
280
|
* The ParseError object for the node key is returned.
|
281
|
* If the node doesn't have errors, null is returned.
|
282
|
*/
|
283
|
public Object getError() {
|
284
|
if (errorNodes == null)
|
285
|
return null;
|
286
|
return errorNodes;
|
287
|
}
|
288
|
|
289
|
/**
|
290
|
* Reset the error storage.
|
291
|
*/
|
292
|
public void resetErrors() {
|
293
|
if (errorNodes != null)
|
294
|
errorNodes.removeAllElements();
|
295
|
}
|
296
|
|
297
|
/***/
|
298
|
public void warning(SAXParseException ex) {
|
299
|
|
300
|
handleError(ex, WARNING);
|
301
|
|
302
|
}
|
303
|
|
304
|
public void error(SAXParseException ex) {
|
305
|
|
306
|
handleError(ex, ERROR);
|
307
|
|
308
|
}
|
309
|
|
310
|
public void fatalError(SAXParseException ex){
|
311
|
|
312
|
handleError(ex, FATAL_ERROR);
|
313
|
|
314
|
}
|
315
|
|
316
|
public void otherError(Exception ex, String fileName)
|
317
|
{
|
318
|
if (!alreadyHandle)
|
319
|
{
|
320
|
if (errorNodes == null)
|
321
|
{
|
322
|
errorNodes = new Vector();
|
323
|
}
|
324
|
|
325
|
ParseError error = new ParseError(fileName, ex.getMessage());
|
326
|
errorNodes.addElement(error);
|
327
|
|
328
|
}
|
329
|
}
|
330
|
|
331
|
private void handleError(SAXParseException ex, int type) {
|
332
|
|
333
|
if (errorNodes == null) {
|
334
|
errorNodes = new Vector();
|
335
|
}
|
336
|
|
337
|
ParseError eip = null;
|
338
|
|
339
|
eip = new ParseError(ex.getSystemId(), ex.getLineNumber(),
|
340
|
ex.getColumnNumber(), ex.getMessage());
|
341
|
|
342
|
// put it in the Hashtable.
|
343
|
errorNodes.addElement(eip);
|
344
|
alreadyHandle = true;
|
345
|
|
346
|
}
|
347
|
|
348
|
}
|
349
|
|
350
|
/**
|
351
|
* The ParseError class wraps up all the error info from
|
352
|
* the ErrorStorer's error method.
|
353
|
*
|
354
|
* @see ErrorStorer
|
355
|
*/
|
356
|
class ParseError extends Object {
|
357
|
|
358
|
//
|
359
|
// Data
|
360
|
//
|
361
|
|
362
|
String fileName;
|
363
|
int lineNo;
|
364
|
int charOffset;
|
365
|
String msg;
|
366
|
|
367
|
/**
|
368
|
* Constructor
|
369
|
*/
|
370
|
public ParseError(String fileName, int lineNo, int charOffset, String msg) {
|
371
|
this.fileName=fileName;
|
372
|
this.lineNo=lineNo;
|
373
|
this.charOffset=charOffset;
|
374
|
this.msg=msg;
|
375
|
}
|
376
|
public ParseError(String fileName, String msg) {
|
377
|
this.fileName=fileName;
|
378
|
this.msg=msg;
|
379
|
}
|
380
|
//
|
381
|
// Getters...
|
382
|
//
|
383
|
public String getFileName() { return fileName; }
|
384
|
public int getLineNo() { return lineNo; }
|
385
|
public int getCharOffset() { return charOffset;}
|
386
|
public String getMsg() { return msg; }
|
387
|
public void setMsg(String s) { msg = s; }
|
388
|
|
389
|
/** Return the error message as an xml fragment */
|
390
|
public String toXML() {
|
391
|
StringBuffer err = new StringBuffer();
|
392
|
err.append("<error>\n");
|
393
|
err.append("<filename>").append(getFileName()).append("</filename>\n");
|
394
|
err.append("<line>").append(getLineNo()).append("</line>\n");
|
395
|
err.append("<offset>").append(getCharOffset()).append("</offset>\n");
|
396
|
err.append("<message>").append(getMsg()).append("</message>\n");
|
397
|
err.append("</error>\n");
|
398
|
return err.toString();
|
399
|
}
|
400
|
}
|
401
|
}
|