You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by st...@apache.org on 2005/02/23 13:51:07 UTC

cvs commit: ant/docs/manual/OptionalTasks schemavalidate.html

stevel      2005/02/23 04:51:07

  Modified:    src/main/org/apache/tools/ant/taskdefs/optional
                        SchemaValidate.java
               docs/manual/OptionalTasks schemavalidate.html
  Log:
  Worked out how to validate XSD files themselves. Changes to task made, and the examples of the task now show how to do this.
  
  Revision  Changes    Path
  1.4       +12 -3     ant/src/main/org/apache/tools/ant/taskdefs/optional/SchemaValidate.java
  
  Index: SchemaValidate.java
  ===================================================================
  RCS file: /home/cvs/ant/src/main/org/apache/tools/ant/taskdefs/optional/SchemaValidate.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- SchemaValidate.java	22 Feb 2005 17:45:40 -0000	1.3
  +++ SchemaValidate.java	23 Feb 2005 12:51:07 -0000	1.4
  @@ -56,6 +56,8 @@
       /** full checking of a schema */
       private boolean fullChecking=true;
   
  +    private boolean disableDTD=false;
  +
       /**
        * default URL for nonamespace schemas
        */
  @@ -179,6 +181,14 @@
       }
   
       /**
  +     * flag to disable DTD support.
  +     * @param disableDTD
  +     */
  +    public void setDisableDTD(boolean disableDTD) {
  +        this.disableDTD = disableDTD;
  +    }
  +
  +    /**
        * init the parser : load the parser class, and set features if necessary It
        * is only after this that the reader is valid
        *
  @@ -186,7 +196,6 @@
        */
       protected void initValidator() {
           super.initValidator();
  -        XMLReader xmlReader = getXmlReader();
           //validate the parser type
           if(isSax1Parser()) {
               throw new BuildException(ERROR_SAX_1);
  @@ -204,8 +213,8 @@
           //enable schema checking
           setFeature(XmlConstants.FEATURE_XSD_FULL_VALIDATION,fullChecking);
   
  -        //turn off DTDs
  -        setFeatureIfSupported(XmlConstants.FEATURE_DISALLOW_DTD,true);
  +        //turn off DTDs if desired
  +        setFeatureIfSupported(XmlConstants.FEATURE_DISALLOW_DTD,disableDTD);
           //schema declarations go in next
           addSchemaLocations();
       }
  
  
  
  1.3       +62 -104   ant/docs/manual/OptionalTasks/schemavalidate.html
  
  Index: schemavalidate.html
  ===================================================================
  RCS file: /home/cvs/ant/docs/manual/OptionalTasks/schemavalidate.html,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- schemavalidate.html	22 Feb 2005 16:04:20 -0000	1.2
  +++ schemavalidate.html	23 Feb 2005 12:51:07 -0000	1.3
  @@ -15,8 +15,8 @@
   <li>The parser is created validating and namespace aware
   </li>
   <li>Validation is turned on.</li>
  -<li>and Schema validation is turned on.</li>
  -<li>Any default schema supplied is used as the no-namespace schema
  +<li>Schema validation is turned on.</li>
  +<li>Any no-namespace schema URL or file supplied is used as the no-namespace schema
   <li>All nested schema declarations are turned into the list of namespace-url
   bindings for schema lookup.
   </ol>
  @@ -50,35 +50,34 @@
       <td align="center" valign="top"><b>Required</b></td>
     </tr>
     <tr>
  -    <td valign="top">file</td>
  -    <td valign="top">the file(s) you want to check. (optionally can use an embedded fileset)</td>
  -    <td valign="top" align="center">No</td>
  +    <td valign="top">classname</td>
  +    <td valign="top">the parser to use.</td>
  +    <td align="center" valign="top">No</td>
     </tr>
     <tr>
  -    <td valign="top">defaultSchemaFile</td>
  -    <td valign="top">
  -      filename of a no-namespace XSD file to provide the 
  -      schema for no-namespace XML content.
  -    </td>
  -    <td valign="top" align="center">No</td>
  +    <td valign="top">classpathref</td>
  +    <td valign="top">where to find the parser class. 
  +    Optionally can use an embedded <tt>&lt;classpath&gt;</tt> element.</td>
  +    <td align="center" valign="top">No</td>
     </tr>
     <tr>
  -    <td valign="top">noNamespaceURL</td>
  +    <td valign="top">disableDTD</td>
       <td valign="top">
  -      URL of a no-namespace XSD file to provide the 
  -      schema for no-namespace XML content.
  +      Flag to disable DTD support. DTD support is needed to 
  +      validate XSD files themselves, amongst others.
       </td>
  -    <td valign="top" align="center">No</td>
  +    <td valign="top" align="center">No - default false</td>
     </tr>
     <tr>
  -    <td valign="top">noNamespaceFile</td>
  -    <td valign="top">
  -      filename of a no-namespace XSD file to provide the 
  -      schema for no-namespace XML content.
  -    </td>
  +    <td valign="top">failonerror</td>
  +    <td valign="top">fails on a error if set to true (defaults to true).</td>
  +    <td align="center" valign="top">No</td>
  +  </tr>
  +  <tr>
  +    <td valign="top">file</td>
  +    <td valign="top">the file(s) you want to check. (optionally can use an embedded fileset)</td>
       <td valign="top" align="center">No</td>
     </tr>
  -  
     <tr>
       <td valign="top">fullchecking</td>
       <td valign="top">
  @@ -86,6 +85,7 @@
       </td>
       <td valign="top" align="center">No - default true</td>
     </tr>
  +  
     <tr>
       <td valign="top">lenient</td>
       <td valign="top">
  @@ -94,20 +94,20 @@
       <td valign="top" align="center">No</td>
     </tr>
     <tr>
  -    <td valign="top">classname</td>
  -    <td valign="top">the parser to use.</td>
  -    <td align="center" valign="top">No</td>
  -  </tr>
  -  <tr>
  -    <td valign="top">classpathref</td>
  -    <td valign="top">where to find the parser class. 
  -    Optionally can use an embedded <tt>&lt;classpath&gt;</tt> element.</td>
  -    <td align="center" valign="top">No</td>
  +    <td valign="top">noNamespaceFile</td>
  +    <td valign="top">
  +      filename of a no-namespace XSD file to provide the 
  +      schema for no-namespace XML content.
  +    </td>
  +    <td valign="top" align="center">No</td>
     </tr>
     <tr>
  -    <td valign="top">failonerror</td>
  -    <td valign="top">fails on a error if set to true (defaults to true).</td>
  -    <td align="center" valign="top">No</td>
  +    <td valign="top">noNamespaceURL</td>
  +    <td valign="top">
  +      URL of a no-namespace XSD file to provide the 
  +      schema for no-namespace XML content.
  +    </td>
  +    <td valign="top" align="center">No</td>
     </tr>
     <tr>
       <td valign="top">warn</td>
  @@ -227,79 +227,37 @@
   
   <h3>Examples</h3>
   <pre>
  -&lt;xmlvalidate file="toto.xml"/&gt;
  -</pre>
  -Validate toto.xml
  -<pre>
  -&lt;xmlvalidate failonerror="no" lenient="yes" warn="yes"
  -             classname="org.apache.xerces.parsers.SAXParser"&gt;
  -             classpath="lib/xerces.jar"&gt;
  -  &lt;fileset dir="src" includes="style/*.xsl"/&gt;
  -&lt;/xmlvalidate&gt;
  -</pre>
  -Validate all .xsl files in src/style, but only warn if there is an error, rather than
  -halt the build.
  -<pre>
  -
  -&lt;xmlvalidate file="struts-config.xml" warn="false"&gt;
  -  &lt;dtd publicId="-//Apache Software Foundation//DTD Struts Configuration 1.0//EN"
  -       location="struts-config_1_0.dtd"/&gt;
  -&lt;/xmlvalidate&gt;
  -</pre>
  -
  -Validate a struts configuration, using a local copy of the DTD. 
  -<pre> 
  -&lt;xmlvalidate failonerror="no"&gt;
  -  &lt;fileset dir="${project.dir}" includes="**/*.xml"/&gt;
  -  &lt;xmlcatalog refid="mycatalog"/&gt;
  -&lt;/xmlvalidate&gt;
  -</pre>
  -
  -Scan all XML files in the project, using a predefined catalog to map URIs to local files.
  -<pre>
  -&lt;xmlvalidate failonerror="no"&gt;
  -  &lt;fileset dir="${project.dir}" includes="**/*.xml"/&gt;
  -  &lt;xmlcatalog&gt;
  -       &lt;dtd
  -         publicId=&quot;-//ArielPartners//DTD XML Article V1.0//EN&quot;
  -         location=&quot;com/arielpartners/knowledgebase/dtd/article.dtd&quot;/&gt;
  -  &lt;/xmlcatalog&gt;
  -&lt;/xmlvalidate&gt;
  -</pre>
  -Scan all XML files in the project, using the catalog defined inline.
  -
  +    &lt;schemavalidate
  +      noNamespaceFile="document.xsd"
  +      file="xml/endpiece.xml"&gt;
  +    &lt;/schemavalidate&gt;
  +</pre>
  +Validate a document against an XML schema. The document does not declare
  +any schema itself, which is why the <tt>noNamespaceFile</tt> is needed. 
  +<pre>
  +    &lt;presetdef name="validate-soap"&gt;
  +      &lt;schemavalidate &gt;
  +        &lt;schema namespace="http://schemas.xmlsoap.org/ws/2003/03/addressing"
  +          file="${soap.dir}/ws-addressing.xsd" /&gt;
  +        &lt;schema namespace="http://www.w3.org/2003/05/soap-envelope"
  +          file="${soap.dir}/soap12.xsd" /&gt;
  +        &lt;schema namespace="http://schemas.xmlsoap.org/wsdl/"
  +          file="${soap.dir}/wsdl.xsd" /&gt;
  +        &lt;schema namespace="http://www.w3.org/2001/XMLSchema"
  +          file="${soap.dir}/XMLSchema.xsd" /&gt;
  +        &lt;/schemavalidate&gt;
  +    &lt;/presetdef&gt;
  +</pre>
  +Declare a new preset task, <tt>&lt;validate-soap&gt;</tt>, that validates
  +XSD and WSDL documents against the relevant specifications. 
  +To validate XSD documents, you also need XMLSchema.dtd and datatypes.dtd in 
  +the same directory as XMLSchema.xsd, or pointed to via the catalog. All
  +these files can be fetched from <a href="http://www.w3.org/2001/XMLSchema">
  +the W3C</a>. 
   <pre>
  -&lt;xmlvalidate failonerror="yes" lenient="no" warn="yes"&gt;
  -  &lt;fileset dir="xml" includes="**/*.xml"/&gt;
  -  &lt;attribute name="http://xml.org/sax/features/validation" value="true"/&gt;
  -  &lt;attribute name="http://apache.org/xml/features/validation/schema"  value="true"/&gt;
  -  &lt;attribute name="http://xml.org/sax/features/namespaces" value="true"/&gt;
  -&lt;/xmlvalidate&gt;
  +    &lt;validate-soap file="xml/test.xsd"/&gt;
   </pre>
  -Validate all .xml files in xml directory with the parser configured to perform schema validation. Note: The parser must support the
  -<pre>http://apache.org/xml/features/validation/schema</pre> feature.
  -
  -<br>
  -<pre>
  -<!-- Converts path to URL format -->
  -&lt;pathconvert dirsep="/" property="xsd.file"&gt;
  -&lt;path&gt;
  -   &lt;pathelement location="xml/doc.xsd"/&gt;
  -&lt;/path&gt;
  -&lt;/pathconvert&gt;
  -
  -&lt;xmlvalidate file="xml/endpiece-noSchema.xml" lenient="false"
  -  failonerror="true" warn="true"&gt;
  -  &lt;attribute name="http://apache.org/xml/features/validation/schema"
  -  value="true"/&gt;
  -  &lt;attribute name="http://xml.org/sax/features/namespaces" value="true"/&gt;
  -  &lt;property
  -  name="http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation"
  -  value="${xsd.file}"/&gt;
  -&lt;/xmlvalidate&gt;
  -</pre>
  -<br>
  -Validate the file xml/endpiece-noSchema.xml against the schema xml/doc.xsd.
  +Use the preset task defined above to validate an XML Schema document.
   <br>
   <hr>
   <p align="center">Copyright &copy; 2001-2002,2004-2005 The Apache Software Foundation. All rights
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org