From 2479f91a7aa7392e162e3ecadf95c5bc48c25c81 Mon Sep 17 00:00:00 2001 From: Steve Loughran Date: Mon, 9 Sep 2002 02:50:35 +0000 Subject: [PATCH] Feature support from Nick Pellow, nick.pellow@mindmatics.de. git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@273307 13f79535-47bb-0310-9956-ffa450edef68 --- docs/manual/OptionalTasks/xmlvalidate.html | 56 ++++++++++-- .../taskdefs/optional/XMLValidateTask.java | 87 ++++++++++++++++--- 2 files changed, 122 insertions(+), 21 deletions(-) diff --git a/docs/manual/OptionalTasks/xmlvalidate.html b/docs/manual/OptionalTasks/xmlvalidate.html index 3655ea7be..5cd4b693e 100644 --- a/docs/manual/OptionalTasks/xmlvalidate.html +++ b/docs/manual/OptionalTasks/xmlvalidate.html @@ -9,14 +9,17 @@

XMLValidate

Description

-

This task checks xml files are valid (or only well formed). The +

This task checks XML files are valid (or only well formed). The task uses the SAX2 parser implementation provided by JAXP by default -(probably the one that is used by Ant itself), but one can specify any -SAX1/2 parser if needed.

+(usually the one that is used by Ant itself), but one can specify any +SAX1/2 parser if needed. Ant ships with Xerces, which is also what is built in +to Java 1.4: XML parsers built into the runtime override Ant's choice.

-

This task supports the use of nested xmlcatalog elements and/or nested -<dtd> elements which are used to resolve DTDs and entities.

+

This task supports the use of nested +

  • <xmlcatalog> elements +
  • <dtd> elements which are used to resolve DTDs and other entities. +
  • <feature> elements which are used to set features. These can be any number of http://xml.org/sax/features/ +

    Parameters

    @@ -62,7 +65,7 @@ href="../CoreTypes/xmlcatalog.html">xmlcatalog elements and/or nested

    Nested Elements

    dtd

    -<dtd> is used to specify different locations for DTD resolution. +<dtd> is used to specify different locations for DTD and entity resolution.
    @@ -84,10 +87,17 @@ href="../CoreTypes/xmlcatalog.html">xmlcatalog elements and/or nested

    xmlcatalog

    The xmlcatalog element is used to perform Entity resolution.

    +

    feature

    +

    The feature element is used to set SAX Parser features. +A feature essentialy changes the mode of the parser. +There can be an arbitrary amount of features set as defined here: + http://xml.org/sax/features/ + +

    Examples

    -
    +
     <xmlvalidate file="toto.xml"/>
     
     <xmlvalidate failonerror="no" lenient="yes" warn="yes"
    @@ -95,16 +105,28 @@ element is used to perform Entity resolution.

    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"/>
    @@ -114,7 +136,23 @@ element is used to perform Entity resolution.

    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"/>
    +  <feature name="http://xml.org/sax/features/validation" value="true"/>
    +  <feature name="http://apache.org/xml/features/validation/schema"  value="true"/>
    +
    +</xmlvalidate>
    +
    + +Validate the XML files using XML Schema validation. +

    Copyright © 2001-2002 Apache Software Foundation. All rights diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/XMLValidateTask.java b/src/main/org/apache/tools/ant/taskdefs/optional/XMLValidateTask.java index 8aa3039bb..b0963a15f 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/XMLValidateTask.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/XMLValidateTask.java @@ -58,9 +58,10 @@ import java.io.FileReader; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; -import java.util.Enumeration; -import java.util.Hashtable; import java.util.Vector; +import java.util.List; +import java.util.LinkedList; + import org.apache.tools.ant.AntClassLoader; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.DirectoryScanner; @@ -89,6 +90,8 @@ import org.xml.sax.helpers.ParserAdapter; * (probably the one that is used by Ant itself), but one can specify any * SAX1/2 parser if needed * @author Raphael Pierquin raphael.pierquin@agisphere.com + * @author Nick Pellow + * Added support for setting features. */ public class XMLValidateTask extends Task { @@ -106,8 +109,7 @@ public class XMLValidateTask extends Task { protected Vector filesets = new Vector(); // sets of file to be validated protected Path classpath; - - /** + /** * the parser is viewed as a SAX2 XMLReader. If a SAX1 parser is specified, * it's wrapped in an adapter that make it behave as a XMLReader. * a more 'standard' way of doing this would be to use the JAXP1.1 SAXParser @@ -116,7 +118,8 @@ public class XMLValidateTask extends Task { protected XMLReader xmlReader = null; // XMLReader used to validation process protected ValidatorErrorHandler errorHandler = new ValidatorErrorHandler(); // to report sax parsing errors - protected Hashtable features = new Hashtable(); + + private List featureList = new LinkedList(); private XMLCatalog xmlCatalog = new XMLCatalog(); @@ -222,6 +225,17 @@ public class XMLValidateTask extends Task { filesets.addElement(set); } + + /** + * add a feature nested element + * @since ant1.6 + */ + public Feature createFeature() { + final Feature feature = new Feature(); + featureList.add(feature); + return feature; + } + public void init() throws BuildException { super.init(); xmlCatalog.setProject(getProject()); @@ -347,18 +361,18 @@ public class XMLValidateTask extends Task { + " doesn't provide validation"); } } - // set other features - Enumeration enum = features.keys(); - while (enum.hasMoreElements()) { - String featureId = (String) enum.nextElement(); - setFeature(featureId, ((Boolean) features.get(featureId)).booleanValue(), true); + // set the feature from the feature list + for (int i = 0; i < featureList.size(); i++) { + Feature feature = (Feature) featureList.get(i); + setFeature(feature.getFeatureName(), + feature.getFeatureValue(), + true); } } } /** - * set a feature on the parser. - * @todo find a way to set any feature from build.xml + * Set a feature on the parser. */ private boolean setFeature(String feature, boolean value, boolean warn) { @@ -482,4 +496,53 @@ public class XMLValidateTask extends Task { return e.getMessage(); } } + + /** + * The class to create to set a feature of the parser. + * @since ant1.6 + * @author Nick Pellow + */ + public class Feature { + /** The name of the feature to set. + * + * Valid features include. + */ + private String featureName = null; + + /** + * The value of the feature. + **/ + private boolean featureValue; + + /** + * Set the feature name. + * @param name the name to set + */ + public void setName(String name) { + featureName = name; + } + /** + * Set the feature value to true or false. + * @param value + */ + public void setValue(boolean value) { + featureValue = value; + } + + /** + * Gets the feature name. + * @return the feature name + */ + public String getFeatureName() { + return featureName; + } + + /** + * Gets the feature value. + * @return the featuree value + */ + public boolean getFeatureValue() { + return featureValue; + } + } }

    Attribute