1
|
package edu.ucsb.nceas.metacat;
|
2
|
|
3
|
|
4
|
import java.net.URL;
|
5
|
import java.net.MalformedURLException;
|
6
|
import java.util.*;
|
7
|
import java.io.*;
|
8
|
import java.lang.reflect.*;
|
9
|
|
10
|
import org.w3c.dom.*;
|
11
|
import org.xml.sax.*;
|
12
|
import org.xml.sax.Parser;
|
13
|
|
14
|
import com.arbortext.catalog.*;
|
15
|
|
16
|
/**
|
17
|
* Name: GenericXMLValidate.java
|
18
|
* Purpose: A Class that validates XML documents
|
19
|
* This class is designed to be 'parser independent
|
20
|
* i.e. it uses only org.xml.sax classes
|
21
|
* It is tied to SAX 1.0 methods
|
22
|
* Copyright: 2000 Regents of the University of California and the
|
23
|
* National Center for Ecological Analysis and Synthesis
|
24
|
* April 28, 2000
|
25
|
*
|
26
|
* @author Dan Higgins
|
27
|
* @version 1.0
|
28
|
*/
|
29
|
public class GenericXMLValidate{
|
30
|
|
31
|
static int WARNING = 0;
|
32
|
static int ERROR=1;
|
33
|
static int FATAL_ERROR=2;
|
34
|
|
35
|
|
36
|
Parser parser;
|
37
|
ErrorStorer ef;
|
38
|
String xml_doc; // document to be parsed
|
39
|
|
40
|
public GenericXMLValidate( Parser parse) {
|
41
|
parser = parse;
|
42
|
CatalogEntityResolver cer = new CatalogEntityResolver();
|
43
|
try {
|
44
|
Catalog myCatalog = new Catalog();
|
45
|
myCatalog.loadSystemCatalogs();
|
46
|
cer.setCatalog(myCatalog);
|
47
|
}
|
48
|
catch (Exception e) {System.out.println("Problem creating Catalog!");}
|
49
|
parser.setEntityResolver(cer);
|
50
|
/* Class defaultParserClass = Class.forName(DEFAULT_PARSER_NAME);
|
51
|
Object par = defaultParserClass.newInstance();
|
52
|
Class[] parameterTypes = new Class[] {boolean.class};
|
53
|
Method set_validation;
|
54
|
Object[] arguments = new Object[] {new Boolean(true)};
|
55
|
try {
|
56
|
set_validation = defaultParserClass.getMethod("setValidationMode", parameterTypes);
|
57
|
set_validation.invoke(par, arguments);
|
58
|
} catch (NoSuchMethodException e) {
|
59
|
System.out.println(e);
|
60
|
} catch (IllegalAccessException e) {
|
61
|
System.out.println(e);
|
62
|
} catch (InvocationTargetException e) {
|
63
|
System.out.println(e);
|
64
|
}
|
65
|
|
66
|
|
67
|
parser = (Parser)par;
|
68
|
*/
|
69
|
}
|
70
|
|
71
|
public boolean validate(String doc) {
|
72
|
xml_doc = doc;
|
73
|
ef = new ErrorStorer();
|
74
|
ef.resetErrors();
|
75
|
parser.setErrorHandler(ef);
|
76
|
try {
|
77
|
parser.parse((createURL(xml_doc)).toString());
|
78
|
}
|
79
|
catch (IOException e) {
|
80
|
System.out.println("IOException:Could not parse :"+xml_doc);
|
81
|
ParseError eip = null;
|
82
|
eip = new ParseError("",0,0,"","IOException:Could not parse :"+xml_doc);
|
83
|
if (ef.errorNodes == null) ef.errorNodes = new Vector();
|
84
|
ef.errorNodes.addElement(eip);
|
85
|
|
86
|
} catch (Exception e) {} // {System.out.println("Exception parsing:Could not parse :"+xml_doc);}
|
87
|
|
88
|
if (ef != null && ef.getErrorNodes()!=null && ef.getErrorNodes().size() > 0 ) {
|
89
|
return false;
|
90
|
|
91
|
}
|
92
|
else {return true;}
|
93
|
|
94
|
}
|
95
|
|
96
|
public boolean validateString(String xmldoc) {
|
97
|
// string is actual XML here, NOT URL or file name
|
98
|
ef = new ErrorStorer();
|
99
|
ef.resetErrors();
|
100
|
parser.setErrorHandler(ef);
|
101
|
|
102
|
InputSource is = new InputSource(new StringReader(xmldoc));
|
103
|
try {
|
104
|
parser.parse(is);
|
105
|
}
|
106
|
catch (Exception e) {System.out.println("Error in parsing!");}
|
107
|
|
108
|
|
109
|
if (ef != null && ef.getErrorNodes()!=null && ef.getErrorNodes().size() > 0 ) {
|
110
|
return false;
|
111
|
|
112
|
}
|
113
|
else {return true;}
|
114
|
|
115
|
}
|
116
|
|
117
|
|
118
|
public String returnErrors() {
|
119
|
String errorstring = "";
|
120
|
if (ef != null && ef.getErrorNodes()!=null && ef.getErrorNodes().size() > 0 ) {
|
121
|
Vector errors = ef.getErrorNodes();
|
122
|
for (Enumeration e = errors.elements() ; e.hasMoreElements() ;) {
|
123
|
errorstring = errorstring +((ParseError)(e.nextElement())).getMsg()+"\n";
|
124
|
errorstring = errorstring + "----\n";
|
125
|
}
|
126
|
}
|
127
|
else {errorstring = "No errors were detected!!!";}
|
128
|
return errorstring;
|
129
|
}
|
130
|
|
131
|
//
|
132
|
// Create a URL object from either a URL string or a plain file name.
|
133
|
//
|
134
|
private URL createURL(String name) throws Exception {
|
135
|
try {
|
136
|
URL u = new URL(name);
|
137
|
return u;
|
138
|
} catch (MalformedURLException ex) {
|
139
|
}
|
140
|
URL u = new URL("file:" + new File(name).getAbsolutePath());
|
141
|
return u;
|
142
|
}
|
143
|
|
144
|
/**
|
145
|
* ErrorStorer has been revised here to simply create a Vector of
|
146
|
* ParseError objects
|
147
|
*
|
148
|
*/
|
149
|
class ErrorStorer
|
150
|
implements ErrorHandler
|
151
|
|
152
|
{
|
153
|
|
154
|
//
|
155
|
// Data
|
156
|
//
|
157
|
Vector errorNodes = null;
|
158
|
|
159
|
/**
|
160
|
* Constructor
|
161
|
*/
|
162
|
public ErrorStorer() {
|
163
|
}
|
164
|
|
165
|
/**
|
166
|
* The client is is allowed to get a reference to the Hashtable,
|
167
|
* and so could corrupt it, or add to it...
|
168
|
*/
|
169
|
public Vector getErrorNodes() {
|
170
|
return errorNodes;
|
171
|
}
|
172
|
|
173
|
/**
|
174
|
* The ParseError object for the node key is returned.
|
175
|
* If the node doesn't have errors, null is returned.
|
176
|
*/
|
177
|
public Object getError() {
|
178
|
if (errorNodes == null)
|
179
|
return null;
|
180
|
return errorNodes;
|
181
|
}
|
182
|
|
183
|
/**
|
184
|
* Reset the error storage.
|
185
|
*/
|
186
|
public void resetErrors() {
|
187
|
if (errorNodes != null)
|
188
|
errorNodes.removeAllElements();
|
189
|
}
|
190
|
|
191
|
/***/
|
192
|
public void warning(SAXParseException ex) {
|
193
|
handleError(ex, WARNING);
|
194
|
}
|
195
|
|
196
|
public void error(SAXParseException ex) {
|
197
|
handleError(ex, ERROR);
|
198
|
}
|
199
|
|
200
|
public void fatalError(SAXParseException ex) throws SAXException {
|
201
|
handleError(ex, FATAL_ERROR);
|
202
|
}
|
203
|
|
204
|
private void handleError(SAXParseException ex, int type) {
|
205
|
// System.out.println("!!! handleError: "+ex.getMessage());
|
206
|
|
207
|
StringBuffer errorString = new StringBuffer();
|
208
|
errorString.append("at line number, ");
|
209
|
errorString.append(ex.getLineNumber());
|
210
|
errorString.append(": ");
|
211
|
errorString.append(ex.getMessage());
|
212
|
|
213
|
|
214
|
if (errorNodes == null) errorNodes = new Vector();
|
215
|
ParseError eip = null;
|
216
|
eip = new ParseError(ex.getSystemId(),ex.getLineNumber(),ex.getColumnNumber(),"",errorString.toString());
|
217
|
|
218
|
|
219
|
// put it in the Hashtable.
|
220
|
errorNodes.addElement(eip);
|
221
|
}
|
222
|
|
223
|
}
|
224
|
|
225
|
/**
|
226
|
* The ParseError class wraps up all the error info from
|
227
|
* the ErrorStorer's error method.
|
228
|
*
|
229
|
* @see ErrorStorer
|
230
|
*/
|
231
|
class ParseError extends Object {
|
232
|
|
233
|
//
|
234
|
// Data
|
235
|
//
|
236
|
|
237
|
String fileName;
|
238
|
int lineNo;
|
239
|
int charOffset;
|
240
|
Object key;
|
241
|
String msg;
|
242
|
|
243
|
/**
|
244
|
* Constructor
|
245
|
*/
|
246
|
public ParseError(String fileName, int lineNo, int charOffset,
|
247
|
Object key,
|
248
|
String msg)
|
249
|
{
|
250
|
this. fileName=fileName;
|
251
|
this. lineNo=lineNo;
|
252
|
this. charOffset=charOffset;
|
253
|
this. key=key;
|
254
|
this. msg=msg;
|
255
|
}
|
256
|
|
257
|
//
|
258
|
// Getters...
|
259
|
//
|
260
|
public String getFileName() { return fileName; }
|
261
|
public int getLineNo() { return lineNo; }
|
262
|
public int getCharOffset() { return charOffset;}
|
263
|
public Object getKey() { return key; }
|
264
|
public String getMsg() { return msg; }
|
265
|
public void setMsg(String s) { msg = s; }
|
266
|
}
|
267
|
|
268
|
|
269
|
}
|