concept document in category ant

appears as: documents, document, documents, document
Ant in Action

This is an excerpt from Manning's book Ant in Action.

Listing 1.1. A typical scenario: compile, document, package, and deploy

A software build process is a means of going from your source—code and documents—to the product you actually deliver. If you have a software project, you have a build process, whether or not you know it. It may just be “hit the compile button on the IDE,” or it may be “drag and drop some files by hand.” Neither of these are very good because they aren’t automated and they’re often limited in scope.

[xmlvalidate] Validating cdata-dtd.xml...
BUILD FAILED
/home/ant/xml/build.xml:96: Could not validate document /home/ant/xml/
build/cdata-dtd.xml
  at org.apache.tools.ant.taskdefs.optional.XMLValidateTask.
    doValidate(XMLValidateTask.java:540)
  ...
Caused by: java.io.FileNotFoundException: /home/ant/xml/build/simple.dtd
(No such file or directory)
  at java.io.FileInputStream.open(Native Method)
  ...

This shows two things. First, <xmlvalidate> really is trying to validate the document against the DTD, and, second, if the DTD isn’t found, validation fails. We need to tell the parser where to find the DTD:

<target name="validate-echo-dtd" depends="cdata-dtd" >
  <xmlvalidate file="${cdata-dtd.xml}">
    <dtd publicId="${dtd-name}"
       location="schemas/simple.dtd"/>
  </xmlvalidate>
</target>

By declaring a mapping from the publicId of the schema to the location where a local copy of the schema lives, the task can now verify that the document is valid.

13.3.2. Validating documents with XML Schema

The W3C XML Schema (usually abbreviated to XSD) is the Perl of XML description languages. You can describe nearly anything in it, but it’s nearly impossible to read. You also get some interesting error messages. For example, an invalid facet error means that you have defined a pattern for a valid element or attribute (such as the regular expression of a telephone number), and the number in the document doesn’t match the pattern. The other interesting error is a complaint about ambiguous particles. This means that there are two ways to parse a document, and the parser doesn’t know what to do.

Listing 13.3. A <presetdef> to validate documents against the SOAP1.2 specifications
<presetdef name="validate-soap"> <schemavalidate> <schema namespace="http://www.w3.org/2003/05/soap-envelope" file="${soap12.xsd}"/> <schema namespace="http://schemas.xmlsoap.org/wsdl/" file="${soap.dir}/wsdl.xsd"/> <schema namespace="http://schemas.xmlsoap.org/wsdl/soap/" file="${soap.dir}/wsdlsoap.xsd"/> </schemavalidate> </presetdef>

Issues with XSD not withstanding, it’s a powerful language, being namespace-aware and capable of validating documents without any <!DOCTYPE> declaration. Validating deployment descriptors and other XML manifests can be a chore that Ant can automate with it. That leaves one remaining problem: those documents described by the RelaxNG schema language, which is exactly the problem we need to address for the diary web application.