Browse Source

Patch XMLValidate to create a new parser every time, plus test case that verifies it works.

Note that there is an extra change, the return code of doVerify() is now boolean and not void. I have plans for an option to halt the build only if a schema is valid (I'm using ant/gump) to validate XSD files, and want to test that a schema correctly rejects invalid files. This is just a step on the way, while I was in the method.


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@278246 13f79535-47bb-0310-9956-ffa450edef68
master
Steve Loughran 20 years ago
parent
commit
091610197b
3 changed files with 20 additions and 2 deletions
  1. +9
    -0
      src/etc/testcases/taskdefs/optional/schemavalidate.xml
  2. +8
    -2
      src/main/org/apache/tools/ant/taskdefs/optional/XMLValidateTask.java
  3. +3
    -0
      src/testcases/org/apache/tools/ant/taskdefs/optional/SchemaValidateTest.java

+ 9
- 0
src/etc/testcases/taskdefs/optional/schemavalidate.xml View File

@@ -69,6 +69,15 @@
</schemavalidate>
</target>
<target name="testFileset">
<schemavalidate noNamespaceFile="${doc.xsd}"
>
<schema namespace="${namespace}" file="${doc-in-ns.xsd}" />
<fileset dir="xml"
includes="endpiece.xml, endpiece-ns-no-location.xml, endpiece-no-schema.xml" />
</schemavalidate>
</target>
<target name="default" depends="testNoNamespace,testNSMapping" />
</project>

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

@@ -285,7 +285,7 @@ public class XMLValidateTask extends Task {
"Specify at least one source - " + "a file or a fileset.");
}

initValidator();

if (file != null) {
if (file.exists() && file.canRead() && file.isFile()) {
@@ -518,7 +518,10 @@ public class XMLValidateTask extends Task {
/**
* parse the file
*/
protected void doValidate(File afile) {
protected boolean doValidate(File afile) {
//for every file, we have a new instance of the validator
initValidator();

try {
log("Validating " + afile.getName() + "... ", Project.MSG_VERBOSE);
errorHandler.init(afile);
@@ -526,6 +529,7 @@ public class XMLValidateTask extends Task {
String uri = FILE_UTILS.toURI(afile.getAbsolutePath());
is.setSystemId(uri);
xmlReader.parse(is);
return true;
} catch (SAXException ex) {
log("Caught when validating: " + ex.toString(), Project.MSG_DEBUG);
if (failOnError) {
@@ -548,6 +552,8 @@ public class XMLValidateTask extends Task {
log(afile + " is not a valid XML document", Project.MSG_ERR);
}
}
//if we got here. it was as a result of a caught and logged exception.
return false;
}

/**


+ 3
- 0
src/testcases/org/apache/tools/ant/taskdefs/optional/SchemaValidateTest.java View File

@@ -92,4 +92,7 @@ public class SchemaValidateTest extends BuildFileTest {
executeTarget("testEqualsSchemasOK");
}

public void testFileset() throws Exception {
executeTarget("testFileset");
}
}

Loading…
Cancel
Save