Browse Source

Throw BuildException in <xmlvalidate> if a requested feature is not

supported.

Make <xmlvalidate>'s unit tests handle parsers without knowledge about
XML Schema gracefully, passes with Crimson and Xerces on my box now.


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@273384 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 22 years ago
parent
commit
e42bd46a51
3 changed files with 45 additions and 35 deletions
  1. +1
    -1
      WHATSNEW
  2. +12
    -28
      src/main/org/apache/tools/ant/taskdefs/optional/XMLValidateTask.java
  3. +32
    -6
      src/testcases/org/apache/tools/ant/taskdefs/optional/XmlValidateTest.java

+ 1
- 1
WHATSNEW View File

@@ -42,7 +42,7 @@ Fixed bugs:

* <property environment=... /> now works on OS/400.

* <filterset> nested into <filterset>s didn't work.
* <filterset>s nested into <filterset>s didn't work.

Other changes:
--------------


+ 12
- 28
src/main/org/apache/tools/ant/taskdefs/optional/XMLValidateTask.java View File

@@ -345,7 +345,8 @@ public class XMLValidateTask extends Task {
log("Using SAX1 parser " + reader.getClass().getName(),
Project.MSG_VERBOSE);
} else {
throw new BuildException(INIT_FAILED_MSG + readerClassName
throw new BuildException(INIT_FAILED_MSG
+ reader.getClass().getName()
+ " implements nor SAX1 Parser nor SAX2 XMLReader.");
}
}
@@ -356,19 +357,12 @@ public class XMLValidateTask extends Task {
if (!(xmlReader instanceof ParserAdapter)) {
// turn validation on
if (!lenient) {
boolean ok = setFeature("http://xml.org/sax/features/validation", true, true);
if (!ok) {
throw new BuildException(INIT_FAILED_MSG
+ readerClassName
+ " doesn't provide validation");
}
setFeature("http://xml.org/sax/features/validation", true);
}
// set the feature from the attribute list
for (int i = 0; i < attributeList.size(); i++) {
Attribute feature = (Attribute) attributeList.elementAt(i);
setFeature(feature.getName(),
feature.getValue(),
true);
setFeature(feature.getName(), feature.getValue());

}
}
@@ -381,30 +375,20 @@ public class XMLValidateTask extends Task {
* @param warn whether to war if the parser does not support the feature

*/
private boolean setFeature(String feature, boolean value, boolean warn) {
private void setFeature(String feature, boolean value)
throws BuildException {

boolean toReturn = false;
try {
xmlReader.setFeature(feature, value);
toReturn = true;
} catch (SAXNotRecognizedException e) {
if (warn) {
log("Could not set feature '"
+ feature
+ "' because the '" +
readerClassName + "' parser doesn't recognize it",
Project.MSG_WARN);
}
throw new BuildException("Parser " + xmlReader.getClass().getName()
+ " doesn't recognize feature "
+ feature, e, getLocation());
} catch (SAXNotSupportedException e) {
if (warn) {
log("Could not set feature '"
+ feature
+ "' because the '" +
readerClassName + "' parser doesn't support it",
Project.MSG_WARN);
}
throw new BuildException("Parser " + xmlReader.getClass().getName()
+ " doesn't support feature "
+ feature, e, getLocation());
}
return toReturn;
}

/**


+ 32
- 6
src/testcases/org/apache/tools/ant/taskdefs/optional/XmlValidateTest.java View File

@@ -56,6 +56,7 @@ package org.apache.tools.ant.taskdefs.optional;
import java.io.*;
import java.util.Properties;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.BuildFileTest;

/**
@@ -130,16 +131,41 @@ public class XmlValidateTest extends BuildFileTest {
/**
* Test xml schema validation
*/
public void testXmlSchemaGood() {
executeTarget("testSchemaGood");
public void testXmlSchemaGood() throws BuildException {
try {
executeTarget("testSchemaGood");
} catch (BuildException e) {
if (e.getMessage()
.endsWith(" doesn't recognize feature http://apache.org/xml/features/validation/schema") ||
e.getMessage()
.endsWith(" doesn't support feature http://apache.org/xml/features/validation/schema")) {
System.err.println(" skipped, parser doesn't support schema");
} else {
throw e;
}
}
}
/**
* Test xml schema validation
*/
public void testXmlSchemaBad() {
expectBuildExceptionContaining(
"testSchemaBad",
"Bad Schema Validation", "not a valid XML document");

try {
executeTarget("testSchemaBad");
fail("Should throw BuildException because 'Bad Schema Validation'");

expectBuildExceptionContaining("testSchemaBad",
"Bad Schema Validation",
"not a valid XML document");
} catch (BuildException e) {
if (e.getMessage()
.endsWith(" doesn't recognize feature http://apache.org/xml/features/validation/schema") ||
e.getMessage()
.endsWith(" doesn't support feature http://apache.org/xml/features/validation/schema")) {
System.err.println(" skipped, parser doesn't support schema");
} else {
assertTrue(e.getMessage()
.indexOf("not a valid XML document") > -1);
}
}
}
}

Loading…
Cancel
Save