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