diff --git a/docs/manual/OptionalTasks/schemavalidate.html b/docs/manual/OptionalTasks/schemavalidate.html new file mode 100644 index 000000000..344ca0d0a --- /dev/null +++ b/docs/manual/OptionalTasks/schemavalidate.html @@ -0,0 +1,310 @@ + + +
+This task validates XML files described by an XML Schema. +The task extends the XmlValidate task with XSD-specific features.
+This task supports the use of nested +
+The task only supports SAX2 or later parsers: it is an error to specify a SAX1 +parser. + + +
Attribute | +Description | +Required | +
file | +the file(s) you want to check. (optionally can use an embedded fileset) | +No | +
defaultSchemaFile | ++ filename of a no-namespace XSD file to provide the + schema for no-namespace XML content. + | +No | +
noNamespaceURL | ++ URL of a no-namespace XSD file to provide the + schema for no-namespace XML content. + | +No | +
noNamespaceFile | ++ filename of a no-namespace XSD file to provide the + schema for no-namespace XML content. + | +No | +
fullchecking | ++ enable full schema checking. Slow but strict. + | +No - default true | +
lenient | ++ if true, only check the XML document is well formed + | +No | +
classname | +the parser to use. | +No | +
classpathref | +where to find the parser class. + Optionally can use an embedded <classpath> element. | +No | +
failonerror | +fails on a error if set to true (defaults to true). | +No | +
warn | +log parser warn events. | +No | +
+Identify the name and location of a schema that may be used in validating +the document(s). +
+Attribute | +Description | +Required | +
namespace | +URI of the schema namespace | +Yes | +
url | +URL of the schema | +One of url or file is required | +
file | +file of the schema | +One of url or file is required | +
+<dtd> is used to specify different locations for DTD resolution. +
+Attribute | +Description | +Required | +
publicId | +Public ID of the DTD to resolve | +Yes | +
location | +Location of the DTD to use, which can be a file, + a resource, or a URL | +Yes | +
The <xmlcatalog> +element is used to perform entity resolution.
+The <attribute> element is used to set parser features.
+Features usable with the xerces parser are defined here :
+ Setting features
+
+SAX features are defined here:
+ http://xml.org/sax/features/
+
Attribute | +Description | +Required | +
name | +The name of the feature | +Yes | +
value | +The boolean value of the feature | +Yes | +
The <property> element is used to set properties. +These properties are defined here for the xerces XML parser implementation : + XML Parser properties +Properties can be used to set the schema used to validate the XML file. +
+Attribute | +Description | +Required | +
name | +The name of the feature | +Yes | +
value | +The string value of the property | +Yes | +
+<xmlvalidate file="toto.xml"/> ++Validate toto.xml +
+<xmlvalidate failonerror="no" lenient="yes" warn="yes" + classname="org.apache.xerces.parsers.SAXParser"> + classpath="lib/xerces.jar"> + <fileset dir="src" includes="style/*.xsl"/> +</xmlvalidate> ++Validate all .xsl files in src/style, but only warn if there is an error, rather than +halt the build. +
+ +<xmlvalidate file="struts-config.xml" warn="false"> + <dtd publicId="-//Apache Software Foundation//DTD Struts Configuration 1.0//EN" + location="struts-config_1_0.dtd"/> +</xmlvalidate> ++ +Validate a struts configuration, using a local copy of the DTD. +
+<xmlvalidate failonerror="no"> + <fileset dir="${project.dir}" includes="**/*.xml"/> + <xmlcatalog refid="mycatalog"/> +</xmlvalidate> ++ +Scan all XML files in the project, using a predefined catalog to map URIs to local files. +
+<xmlvalidate failonerror="no"> + <fileset dir="${project.dir}" includes="**/*.xml"/> + <xmlcatalog> + <dtd + publicId="-//ArielPartners//DTD XML Article V1.0//EN" + location="com/arielpartners/knowledgebase/dtd/article.dtd"/> + </xmlcatalog> +</xmlvalidate> ++Scan all XML files in the project, using the catalog defined inline. + +
+<xmlvalidate failonerror="yes" lenient="no" warn="yes"> + <fileset dir="xml" includes="**/*.xml"/> + <attribute name="http://xml.org/sax/features/validation" value="true"/> + <attribute name="http://apache.org/xml/features/validation/schema" value="true"/> + <attribute name="http://xml.org/sax/features/namespaces" value="true"/> +</xmlvalidate> ++Validate all .xml files in xml directory with the parser configured to perform schema validation. Note: The parser must support the +
http://apache.org/xml/features/validation/schemafeature. + +
+ +<pathconvert dirsep="/" property="xsd.file"> +<path> + <pathelement location="xml/doc.xsd"/> +</path> +</pathconvert> + +<xmlvalidate file="xml/endpiece-noSchema.xml" lenient="false" + failonerror="true" warn="true"> + <attribute name="http://apache.org/xml/features/validation/schema" + value="true"/> + <attribute name="http://xml.org/sax/features/namespaces" value="true"/> + <property + name="http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation" + value="${xsd.file}"/> +</xmlvalidate> ++
Copyright © 2001-2002,2004 The Apache Software Foundation. All rights +Reserved.
+ + + + diff --git a/docs/manual/optionaltasklist.html b/docs/manual/optionaltasklist.html index bcf3339ee..9e4a3082c 100644 --- a/docs/manual/optionaltasklist.html +++ b/docs/manual/optionaltasklist.html @@ -56,11 +56,12 @@ ReplaceRegExpfailonerror
is true and an error happens
@@ -304,16 +318,56 @@ public class XMLValidateTask extends Task {
/**
* init the parser :
* load the parser class, and set features if necessary
+ * It is only after this that the reader is valid
+ * @throws BuildException if something went wrong
+ */
+ protected void initValidator() {
+
+ xmlReader=createXmlReader();
+
+ xmlReader.setEntityResolver(getEntityResolver());
+ xmlReader.setErrorHandler(errorHandler);
+
+ if (!isSax1Parser()) {
+ // turn validation on
+ if (!lenient) {
+ setFeature(XmlConstants.FEATURE_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());
+
+ }
+
+ // Sets properties
+ for (int i = 0; i < propertyList.size(); i++) {
+ final Property prop = (Property) propertyList.elementAt(i);
+ setProperty(prop.getName(), prop.getValue());
+ }
+ }
+ }
+
+ /**
+ * test that returns true if we are using a SAX1 parser.
+ * @return true when a SAX1 parser is in use
*/
- private void initValidator() {
+ protected boolean isSax1Parser() {
+ return (xmlReader instanceof ParserAdapter);
+ }
+ /**
+ * create the XML reader.
+ * This is one by instantiating anything specified by {@link #readerClassName},
+ * falling back to a default reader if not.
+ * If the returned reader is an instance of {@link ParserAdapter} then
+ * we have created and wrapped a SAX1 parser.
+ * @returns the new XMLReader.
+ */
+ protected XMLReader createXmlReader() {
Object reader = null;
if (readerClassName == null) {
- try {
- reader = JAXPUtils.getXMLReader();
- } catch (BuildException exc) {
- reader = JAXPUtils.getParser();
- }
+ reader = createDefaultReaderOrParser();
} else {
Class readerClass = null;
@@ -338,8 +392,9 @@ public class XMLValidateTask extends Task {
}
// then check it implements XMLReader
+ XMLReader newReader;
if (reader instanceof XMLReader) {
- xmlReader = (XMLReader) reader;
+ newReader = (XMLReader) reader;
log(
"Using SAX2 reader " + reader.getClass().getName(),
Project.MSG_VERBOSE);
@@ -347,7 +402,7 @@ public class XMLValidateTask extends Task {
// see if it is a SAX1 Parser
if (reader instanceof Parser) {
- xmlReader = new ParserAdapter((Parser) reader);
+ newReader = new ParserAdapter((Parser) reader);
log(
"Using SAX1 parser " + reader.getClass().getName(),
Project.MSG_VERBOSE);
@@ -358,36 +413,41 @@ public class XMLValidateTask extends Task {
+ " implements nor SAX1 Parser nor SAX2 XMLReader.");
}
}
+ return newReader;
+ }
- xmlReader.setEntityResolver(getEntityResolver());
- xmlReader.setErrorHandler(errorHandler);
-
- if (!(xmlReader instanceof ParserAdapter)) {
- // turn validation on
- if (!lenient) {
- 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());
-
- }
-
- // Sets properties
- for (int i = 0; i < propertyList.size(); i++) {
- final Property prop = (Property) propertyList.elementAt(i);
- setProperty(prop.getName(), prop.getValue());
- }
+ /**
+ *
+ * @return
+ */
+ private Object createDefaultReaderOrParser() {
+ Object reader;
+ try {
+ reader = createDefaultReader();
+ } catch (BuildException exc) {
+ reader = JAXPUtils.getParser();
}
+ return reader;
+ }
+
+ /**
+ * create a reader if the use of the class did not specify another one.
+ * If a BuildException is thrown, the caller may revert to an alternate
+ * reader.
+ * @return a new reader.
+ * @throws BuildException if something went wrong
+ */
+ protected XMLReader createDefaultReader() {
+ return JAXPUtils.getXMLReader();
}
/**
* Set a feature on the parser.
* @param feature the name of the feature to set
* @param value the value of the feature
+ * @throws BuildException if the feature was not supported
*/
- private void setFeature(String feature, boolean value)
+ protected void setFeature(String feature, boolean value)
throws BuildException {
log("Setting feature " + feature + "=" + value, Project.MSG_DEBUG);
try {
@@ -417,8 +477,9 @@ public class XMLValidateTask extends Task {
* @param name a property name
* @param value a property value.
* @throws BuildException if an error occurs.
+ * @throws BuildException if the property was not supported
*/
- private void setProperty(String name, String value) throws BuildException {
+ protected void setProperty(String name, String value) throws BuildException {
// Validates property
if (name == null || value == null) {
throw new BuildException("Property name and value must be specified.");
@@ -448,7 +509,7 @@ public class XMLValidateTask extends Task {
/**
* parse the file
*/
- private void doValidate(File afile) {
+ protected void doValidate(File afile) {
try {
log("Validating " + afile.getName() + "... ", Project.MSG_VERBOSE);
errorHandler.init(afile);
@@ -655,4 +716,6 @@ public class XMLValidateTask extends Task {
} // Property
+
+
}
diff --git a/src/main/org/apache/tools/ant/util/XmlConstants.java b/src/main/org/apache/tools/ant/util/XmlConstants.java
new file mode 100644
index 000000000..39f060a06
--- /dev/null
+++ b/src/main/org/apache/tools/ant/util/XmlConstants.java
@@ -0,0 +1,47 @@
+/*
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package org.apache.tools.ant.util;
+
+/**
+ * XML Parser constants, all kept in one place for ease of reuse
+ * @see Xerces features
+ * @see Xerces properties
+ * @see SAX.
+ */
+
+public class XmlConstants {
+ public static final String PROPERTY_SCHEMA_LOCATION =
+ "http://apache.org/xml/properties/schema/external-schemaLocation";
+ public static final String PROPERTY_NO_NAMESPACE_SCHEMA_LOCATION =
+ "http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation";
+ public static final String FEATURE_XSD_FULL_VALIDATION =
+ "http://apache.org/xml/features/validation/schema-full-checking";
+ public static final String FEATURE_XSD = "http://apache.org/xml/features/validation/schema";
+
+ public static final String FEATURE_VALIDATION = "http://xml.org/sax/features/validation";
+ public static final String FEATURE_NAMESPACES = "http://xml.org/sax/features/namespaces";
+ public static final String FEATURE_JAXP12_SCHEMA_LANGUAGE =
+ "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
+ public static final String FEATURE_JAXP12_SCHEMA_SOURCE =
+ "http://java.sun.com/xml/jaxp/properties/schemaSource";
+ public static final String URI_XSD =
+ "http://www.w3.org/2001/XMLSchema";
+ public static final String FEATURE_EXTERNAL_ENTITIES =
+ "http://xml.org/sax/features/external-general-entities";
+ public static final String FEATURE_DISALLOW_DTD =
+ "http://apache.org/xml/features/disallow-doctype-decl";
+}
diff --git a/src/testcases/org/apache/tools/ant/taskdefs/optional/SchemaValidateTest.java b/src/testcases/org/apache/tools/ant/taskdefs/optional/SchemaValidateTest.java
new file mode 100644
index 000000000..1fe811d9b
--- /dev/null
+++ b/src/testcases/org/apache/tools/ant/taskdefs/optional/SchemaValidateTest.java
@@ -0,0 +1,63 @@
+/*
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package org.apache.tools.ant.taskdefs.optional;
+
+import org.apache.tools.ant.BuildFileTest;
+
+/**
+ * Test schema validation
+ */
+
+public class SchemaValidateTest extends BuildFileTest {
+
+ /**
+ * where tasks run
+ */
+ private final static String TASKDEFS_DIR =
+ "src/etc/testcases/taskdefs/optional/";
+
+ /**
+ * Constructor
+ *
+ * @param name testname
+ */
+ public SchemaValidateTest(String name) {
+ super(name);
+ }
+
+ /**
+ * The JUnit setup method
+ */
+ public void setUp() {
+ configureProject(TASKDEFS_DIR + "schemavalidate.xml");
+ }
+
+ /**
+ * test with no namespace
+ */
+ public void testNoNamespace() throws Exception {
+ executeTarget("testNoNamespace");
+ }
+
+ /**
+ * add namespace awareness.
+ */
+ public void testNSMapping() throws Exception {
+ executeTarget("testNSMapping");
+ }
+
+}