Lines 72-77
Link Here
|
72 |
import org.apache.tools.ant.types.XMLCatalog; |
72 |
import org.apache.tools.ant.types.XMLCatalog; |
73 |
import org.apache.tools.ant.util.FileUtils; |
73 |
import org.apache.tools.ant.util.FileUtils; |
74 |
import org.apache.tools.ant.util.JAXPUtils; |
74 |
import org.apache.tools.ant.util.JAXPUtils; |
|
|
75 |
|
75 |
import org.xml.sax.EntityResolver; |
76 |
import org.xml.sax.EntityResolver; |
76 |
import org.xml.sax.ErrorHandler; |
77 |
import org.xml.sax.ErrorHandler; |
77 |
import org.xml.sax.InputSource; |
78 |
import org.xml.sax.InputSource; |
Lines 108-114
Link Here
|
108 |
protected boolean failOnError = true; |
109 |
protected boolean failOnError = true; |
109 |
protected boolean warn = true; |
110 |
protected boolean warn = true; |
110 |
protected boolean lenient = false; |
111 |
protected boolean lenient = false; |
111 |
protected String readerClassName = null; |
112 |
protected String readerClassName = null; |
112 |
/** file to be validated */ |
113 |
/** file to be validated */ |
113 |
protected File file = null; |
114 |
protected File file = null; |
Lines 116-135
Link Here
|
116 |
protected Vector filesets = new Vector(); |
117 |
protected Vector filesets = new Vector(); |
117 |
protected Path classpath; |
118 |
protected Path classpath; |
118 |
|
|
|
119 |
/** |
119 |
/** |
120 |
* the parser is viewed as a SAX2 XMLReader. If a SAX1 parser is specified, |
120 |
* the parser is viewed as a SAX2 XMLReader. If a SAX1 parser is specified, |
121 |
* it's wrapped in an adapter that make it behave as a XMLReader. |
121 |
* it's wrapped in an adapter that make it behave as a XMLReader. |
122 |
* a more 'standard' way of doing this would be to use the JAXP1.1 SAXParser |
122 |
* a more 'standard' way of doing this would be to use the JAXP1.1 SAXParser |
123 |
* interface. |
123 |
* interface. |
124 |
*/ |
124 |
*/ |
125 |
protected XMLReader xmlReader = null; // XMLReader used to validation process |
125 |
protected XMLReader xmlReader = null; |
126 |
protected ValidatorErrorHandler errorHandler |
126 |
// XMLReader used to validation process |
127 |
= new ValidatorErrorHandler(); // to report sax parsing errors |
127 |
protected ValidatorErrorHandler errorHandler = new ValidatorErrorHandler(); |
|
|
128 |
// to report sax parsing errors |
128 |
/** The vector to store all attributes (features) to be set on the parser. **/ |
129 |
/** The vector to store all attributes (features) to be set on the parser. **/ |
129 |
private Vector attributeList = new Vector(); |
130 |
private Vector attributeList = new Vector(); |
130 |
|
131 |
|
|
|
132 |
/** |
133 |
* List of properties. |
134 |
*/ |
135 |
private final Vector propertyList = new Vector(); |
131 |
private XMLCatalog xmlCatalog = new XMLCatalog(); |
136 |
private XMLCatalog xmlCatalog = new XMLCatalog(); |
Lines 182-188
Link Here
|
182 |
readerClassName = className; |
187 |
readerClassName = className; |
183 |
} |
188 |
} |
184 |
|
|
|
185 |
/** |
189 |
/** |
186 |
* Specify the classpath to be searched to load the parser (optional) |
190 |
* Specify the classpath to be searched to load the parser (optional) |
187 |
*/ |
191 |
*/ |
Lines 246-251
Link Here
|
246 |
return feature; |
250 |
return feature; |
247 |
} |
251 |
} |
|
|
252 |
/** |
253 |
* Creates a property. |
254 |
* |
255 |
* @return a property. |
256 |
*/ |
257 |
public Property createProperty() { |
258 |
final Property prop = new Property(); |
259 |
propertyList.addElement(prop); |
260 |
return prop; |
261 |
} |
262 |
|
263 |
// public |
264 |
|
248 |
public void init() throws BuildException { |
265 |
public void init() throws BuildException { |
249 |
super.init(); |
266 |
super.init(); |
250 |
xmlCatalog.setProject(getProject()); |
267 |
xmlCatalog.setProject(getProject()); |
Lines 270-283
Link Here
|
270 |
int fileProcessed = 0; |
287 |
int fileProcessed = 0; |
271 |
if (file == null && (filesets.size() == 0)) { |
288 |
if (file == null && (filesets.size() == 0)) { |
272 |
throw new BuildException("Specify at least one source - " |
289 |
throw new BuildException( |
273 |
+ "a file or a fileset."); |
290 |
"Specify at least one source - " + "a file or a fileset."); |
274 |
} |
291 |
} |
275 |
initValidator(); |
292 |
initValidator(); |
276 |
if (file != null) { |
293 |
if (file != null) { |
277 |
if (file.exists() && file.canRead() && file.isFile()) { |
294 |
if (file.exists() && file.canRead() && file.isFile()) { |
278 |
doValidate(file); |
295 |
doValidate(file); |
279 |
fileProcessed++; |
296 |
fileProcessed++; |
280 |
} else { |
297 |
} else { |
Lines 292-302
Link Here
|
292 |
for (int i = 0; i < filesets.size(); i++) { |
309 |
for (int i = 0; i < filesets.size(); i++) { |
293 |
FileSet fs = (FileSet) filesets.elementAt(i); |
310 |
FileSet fs = (FileSet)filesets.elementAt(i); |
294 |
DirectoryScanner ds = fs.getDirectoryScanner(getProject()); |
311 |
DirectoryScanner ds = fs.getDirectoryScanner(getProject()); |
295 |
String[] files = ds.getIncludedFiles(); |
312 |
String[] files = ds.getIncludedFiles(); |
296 |
for (int j = 0; j < files.length; j++) { |
313 |
for (int j = 0; j < files.length; j++) { |
297 |
File srcFile = new File(fs.getDir(getProject()), files[j]); |
314 |
File srcFile = new File(fs.getDir(getProject()), files[j]); |
298 |
doValidate(srcFile); |
315 |
doValidate(srcFile); |
299 |
fileProcessed++; |
316 |
fileProcessed++; |
Lines 324-331
Link Here
|
324 |
try { |
341 |
try { |
325 |
// load the parser class |
342 |
// load the parser class |
326 |
if (classpath != null) { |
343 |
if (classpath != null) { |
327 |
AntClassLoader loader |
344 |
AntClassLoader loader = |
328 |
= getProject().createClassLoader(classpath); |
345 |
getProject().createClassLoader(classpath); |
329 |
readerClass = Class.forName(readerClassName, true, loader); |
346 |
readerClass = Class.forName(readerClassName, true, loader); |
330 |
} else { |
347 |
} else { |
331 |
readerClass = Class.forName(readerClassName); |
348 |
readerClass = Class.forName(readerClassName); |
Lines 343-362
Link Here
|
343 |
// then check it implements XMLReader |
360 |
// then check it implements XMLReader |
344 |
if (reader instanceof XMLReader) { |
361 |
if (reader instanceof XMLReader) { |
345 |
xmlReader = (XMLReader) reader; |
362 |
xmlReader = (XMLReader)reader; |
346 |
log("Using SAX2 reader " + reader.getClass().getName(), |
363 |
log( |
|
|
364 |
"Using SAX2 reader " + reader.getClass().getName(), |
347 |
Project.MSG_VERBOSE); |
365 |
Project.MSG_VERBOSE); |
348 |
} else { |
366 |
} else { |
349 |
// see if it is a SAX1 Parser |
367 |
// see if it is a SAX1 Parser |
350 |
if (reader instanceof Parser) { |
368 |
if (reader instanceof Parser) { |
351 |
xmlReader = new ParserAdapter((Parser) reader); |
369 |
xmlReader = new ParserAdapter((Parser)reader); |
352 |
log("Using SAX1 parser " + reader.getClass().getName(), |
370 |
log( |
|
|
371 |
"Using SAX1 parser " + reader.getClass().getName(), |
353 |
Project.MSG_VERBOSE); |
372 |
Project.MSG_VERBOSE); |
354 |
} else { |
373 |
} else { |
355 |
throw new BuildException(INIT_FAILED_MSG |
374 |
throw new BuildException( |
356 |
+ reader.getClass().getName() |
375 |
INIT_FAILED_MSG |
357 |
+ " implements nor SAX1 Parser nor SAX2 XMLReader."); |
376 |
+ reader.getClass().getName() |
|
|
377 |
+ " implements nor SAX1 Parser nor SAX2 XMLReader."); |
358 |
} |
378 |
} |
359 |
} |
379 |
} |
Lines 370-379
Link Here
|
370 |
} |
390 |
} |
371 |
// set the feature from the attribute list |
391 |
// set the feature from the attribute list |
372 |
for (int i = 0; i < attributeList.size(); i++) { |
392 |
for (int i = 0; i < attributeList.size(); i++) { |
373 |
Attribute feature = (Attribute) attributeList.elementAt(i); |
393 |
Attribute feature = (Attribute)attributeList.elementAt(i); |
374 |
setFeature(feature.getName(), feature.getValue()); |
394 |
setFeature(feature.getName(), feature.getValue()); |
375 |
} |
395 |
} |
|
|
396 |
|
397 |
// Sets properties |
398 |
for (int i = 0; i < propertyList.size(); i++) { |
399 |
final Property prop = (Property)propertyList.elementAt(i); |
400 |
setProperty(prop.getName(), prop.getValue()); |
401 |
} |
376 |
} |
402 |
} |
377 |
} |
403 |
} |
Lines 382-388
Link Here
|
382 |
* @param feature the name of the feature to set |
408 |
* @param feature the name of the feature to set |
383 |
* @param value the value of the feature |
409 |
* @param value the value of the feature |
384 |
* @param warn whether to war if the parser does not support the feature |
410 |
* @param warn whether to war if the parser does not support the feature |
385 |
|
411 |
|
386 |
*/ |
412 |
*/ |
387 |
private void setFeature(String feature, boolean value) |
413 |
private void setFeature(String feature, boolean value) |
388 |
throws BuildException { |
414 |
throws BuildException { |
Lines 390-402
Link Here
|
390 |
try { |
416 |
try { |
391 |
xmlReader.setFeature(feature, value); |
417 |
xmlReader.setFeature(feature, value); |
392 |
} catch (SAXNotRecognizedException e) { |
418 |
} catch (SAXNotRecognizedException e) { |
393 |
throw new BuildException("Parser " + xmlReader.getClass().getName() |
419 |
throw new BuildException( |
394 |
+ " doesn't recognize feature " |
420 |
"Parser " |
395 |
+ feature, e, getLocation()); |
421 |
+ xmlReader.getClass().getName() |
396 |
} catch (SAXNotSupportedException e) { |
422 |
+ " doesn't recognize feature " |
397 |
throw new BuildException("Parser " + xmlReader.getClass().getName() |
423 |
+ feature, |
398 |
+ " doesn't support feature " |
424 |
e, |
399 |
+ feature, e, getLocation()); |
425 |
getLocation()); |
|
|
426 |
} catch (SAXNotSupportedException e) { |
427 |
throw new BuildException( |
428 |
"Parser " |
429 |
+ xmlReader.getClass().getName() |
430 |
+ " doesn't support feature " |
431 |
+ feature, |
432 |
e, |
433 |
getLocation()); |
434 |
} |
435 |
} |
436 |
|
437 |
/** |
438 |
* Sets a property. |
439 |
* |
440 |
* @param name a property name |
441 |
* @param value a property value. |
442 |
* @throws BuildException if an error occurs. |
443 |
*/ |
444 |
private void setProperty(String name, String value) throws BuildException { |
445 |
// Validates property |
446 |
if(name == null || value == null) { |
447 |
throw new BuildException("Property name and value must be specified."); |
448 |
} |
449 |
|
450 |
try { |
451 |
xmlReader.setProperty(name, value); |
452 |
} catch (SAXNotRecognizedException e) { |
453 |
throw new BuildException( |
454 |
"Parser " |
455 |
+ xmlReader.getClass().getName() |
456 |
+ " doesn't recognize property " |
457 |
+ name, |
458 |
e, |
459 |
getLocation()); |
460 |
} catch (SAXNotSupportedException e) { |
461 |
throw new BuildException( |
462 |
"Parser " |
463 |
+ xmlReader.getClass().getName() |
464 |
+ " doesn't support property " |
465 |
+ name, |
466 |
e, |
467 |
getLocation()); |
400 |
} |
468 |
} |
401 |
} |
469 |
} |
Lines 413-430
Link Here
|
413 |
xmlReader.parse(is); |
481 |
xmlReader.parse(is); |
414 |
} catch (SAXException ex) { |
482 |
} catch (SAXException ex) { |
415 |
if (failOnError) { |
483 |
if (failOnError) { |
416 |
throw new BuildException("Could not validate document " |
484 |
throw new BuildException( |
417 |
+ afile); |
485 |
"Could not validate document " + afile); |
|
|
486 |
} else { |
487 |
log("Could not validate document " + afile + ": " + ex.toString()); |
418 |
} |
488 |
} |
419 |
} catch (IOException ex) { |
489 |
} catch (IOException ex) { |
420 |
throw new BuildException("Could not validate document " + afile, |
490 |
throw new BuildException( |
|
|
491 |
"Could not validate document " + afile, |
421 |
ex); |
492 |
ex); |
422 |
} |
493 |
} |
423 |
if (errorHandler.getFailure()) { |
494 |
if (errorHandler.getFailure()) { |
424 |
if (failOnError) { |
495 |
if (failOnError) { |
425 |
throw new BuildException(afile |
496 |
throw new BuildException( |
426 |
+ " is not a valid XML document."); |
497 |
afile + " is not a valid XML document."); |
427 |
} else { |
498 |
} else { |
428 |
log(afile + " is not a valid XML document", Project.MSG_ERR); |
499 |
log(afile + " is not a valid XML document", Project.MSG_ERR); |
429 |
} |
500 |
} |
Lines 485-493
Link Here
|
485 |
int line = e.getLineNumber(); |
556 |
int line = e.getLineNumber(); |
486 |
int col = e.getColumnNumber(); |
557 |
int col = e.getColumnNumber(); |
487 |
return new URL(sysID).getFile() |
558 |
return new URL(sysID).getFile() |
488 |
+ (line == -1 ? "" : (":" + line |
559 |
+ (line == -1 |
489 |
+ (col == -1 ? "" : (":" + col)))) |
560 |
? "" |
490 |
+ ": " + e.getMessage(); |
561 |
: (":" + line + (col == -1 ? "" : (":" + col)))) |
|
|
562 |
+ ": " |
563 |
+ e.getMessage(); |
491 |
} catch (MalformedURLException mfue) { |
564 |
} catch (MalformedURLException mfue) { |
492 |
// ignore and just return exception message |
565 |
// ignore and just return exception message |
493 |
} |
566 |
} |
Lines 544-547
Link Here
|
544 |
return attributeValue; |
617 |
return attributeValue; |
545 |
} |
618 |
} |
546 |
} |
619 |
} |
|
|
620 |
|
621 |
/** |
622 |
* A Parser property |
623 |
*/ |
624 |
public final class Property { |
625 |
|
626 |
private String name; |
627 |
private String value; |
628 |
|
629 |
public String getName() { |
630 |
return name; |
631 |
} |
632 |
|
633 |
public void setName(String name) { |
634 |
this.name = name; |
635 |
} |
636 |
|
637 |
public String getValue() { |
638 |
return value; |
639 |
} |
640 |
|
641 |
public void setValue(String value) { |
642 |
this.value = value; |
643 |
} |
644 |
|
645 |
} // Property |
646 |
|
547 |
} |
647 |
} |