From c6d570f7ef261f738cf2959023ec60f30bf56c7d Mon Sep 17 00:00:00 2001 From: Peter Donald Date: Tue, 10 Apr 2001 04:47:00 +0000 Subject: [PATCH] Added in xmlvalidate task Submitted By: Raphael PIERQUIN git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@268931 13f79535-47bb-0310-9956-ffa450edef68 --- docs/manual/OptionalTasks/xmlvalidate.html | 73 +++ docs/manual/optionaltasklist.html | 1 + .../tools/ant/taskdefs/defaults.properties | 1 + .../taskdefs/optional/XMLValidateTask.java | 418 ++++++++++++++++++ 4 files changed, 493 insertions(+) create mode 100644 docs/manual/OptionalTasks/xmlvalidate.html create mode 100644 src/main/org/apache/tools/ant/taskdefs/optional/XMLValidateTask.java diff --git a/docs/manual/OptionalTasks/xmlvalidate.html b/docs/manual/OptionalTasks/xmlvalidate.html new file mode 100644 index 000000000..ef6d40b78 --- /dev/null +++ b/docs/manual/OptionalTasks/xmlvalidate.html @@ -0,0 +1,73 @@ + + + +XMLValidate Task + + + + +

XMLValidate

+

Description

+

+ This task checks xml files are valid (or only well formed). The task uses crimson SAX2 parser implementation by default, but one can specify any SAX1/2 parser if needed +

+ +

Parameters

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
AttributeDescriptionRequired
filethe file(s) you want to check. (optionally can use an embedded fileset)No
lenient + if true, only check the xml document is well formed + (ignored if the specified parser is as SAX1 parser) + No
classnamethe parser to use. (default: crimson).No
classpathrefwhere to find the parser class. Optionally can use an embedded classpath element.No
failonerrorfails on a error if set to true (defaults to true).No
warnlog parser warn events.No
+

Examples

+
+<xmlvalidate file="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>
+
+
+ +

Copyright © 2000,2001 Apache Software Foundation. All rights +Reserved.

+ + + + diff --git a/docs/manual/optionaltasklist.html b/docs/manual/optionaltasklist.html index 24945066e..d011fc637 100644 --- a/docs/manual/optionaltasklist.html +++ b/docs/manual/optionaltasklist.html @@ -43,6 +43,7 @@ VssGet
VssHistory
VssLabel
+XmlValidate
diff --git a/src/main/org/apache/tools/ant/taskdefs/defaults.properties b/src/main/org/apache/tools/ant/taskdefs/defaults.properties index 9570bffcd..5ba9cd0e5 100644 --- a/src/main/org/apache/tools/ant/taskdefs/defaults.properties +++ b/src/main/org/apache/tools/ant/taskdefs/defaults.properties @@ -96,6 +96,7 @@ vsslabel=org.apache.tools.ant.taskdefs.optional.vss.MSVSSLABEL vsshistory=org.apache.tools.ant.taskdefs.optional.vss.MSVSSHISTORY blgenclient=org.apache.tools.ant.taskdefs.optional.ejb.BorlandGenerateClient rpm=org.apache.tools.ant.taskdefs.optional.Rpm +xmlvalidate=org.apache.tools.ant.taskdefs.optional.XMLValidateTask # deprecated ant tasks (kept for back compatibility) javadoc2=org.apache.tools.ant.taskdefs.Javadoc diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/XMLValidateTask.java b/src/main/org/apache/tools/ant/taskdefs/optional/XMLValidateTask.java new file mode 100644 index 000000000..cd0ae8463 --- /dev/null +++ b/src/main/org/apache/tools/ant/taskdefs/optional/XMLValidateTask.java @@ -0,0 +1,418 @@ +/* + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2000-2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowlegement: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowlegement may appear in the software itself, + * if and wherever such third-party acknowlegements normally appear. + * + * 4. The names "The Jakarta Project", "Ant", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Group. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ +package org.apache.tools.ant.taskdefs.optional; + +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.Enumeration; +import java.util.Hashtable; +import java.util.Vector; +import org.apache.tools.ant.AntClassLoader; +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.DirectoryScanner; +import org.apache.tools.ant.Project; +import org.apache.tools.ant.Task; +import org.apache.tools.ant.types.FileSet; +import org.apache.tools.ant.types.Path; +import org.apache.tools.ant.types.Reference; +import org.xml.sax.ErrorHandler; +import org.xml.sax.InputSource; +import org.xml.sax.Parser; +import org.xml.sax.SAXException; +import org.xml.sax.SAXNotRecognizedException; +import org.xml.sax.SAXNotSupportedException; +import org.xml.sax.SAXParseException; +import org.xml.sax.XMLReader; +import org.xml.sax.helpers.ParserAdapter; + +/** + * The XMLValidateTask checks that an XML document is valid, + * with a SAX validating parser. + * @author Raphael Pierquin raphael.pierquin@agisphere.com + */ +public class XMLValidateTask extends Task { + + /** + * The default implementation parser classname used by the task to process + * validation. + */ + // The crimson implementation is shipped with ant. + public static String DEFAULT_XML_READER_CLASSNAME= "org.apache.crimson.parser.XMLReaderImpl"; + + protected static String INIT_FAILED_MSG = "Could'nt start xml validation: "; + + // ant task properties + // defaults + protected boolean failOnError = true; + protected boolean warn = true; + protected boolean lenient = false; + protected String readerClassName = DEFAULT_XML_READER_CLASSNAME; + + protected File file = null; // file to be validated + 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 + * interface. + */ + protected XMLReader xmlReader = null; // XMLReader used to validation process + protected ValidatorErrorHandler errorHandler + = new ValidatorErrorHandler(); // to report sax parsing errors + protected Hashtable features = new Hashtable(); + + + /** + * Specify how parser error are to be handled. + *

+ * If set to true (default), throw a buildException if the parser yields an error. + */ + public void setFailOnError(boolean fail) { + + failOnError = fail; + } + + /** + * Specify how parser error are to be handled. + *

+ * If set to true (default), log a warn message for each SAX warn event. + */ + public void setWarn(boolean bool) { + + warn = bool; + } + + /** + * Specify whether the parser should be validating. Default is true. + *

+ * If set to false, the validation will fail only if the parsed document is not well formed XML. + *

+ * this option is ignored if the specified class with {@link #setClassName(String)} is not a SAX2 + * XMLReader. + */ + public void setLenient(boolean bool) { + + lenient = bool; + } + + /** + * Specify the class name of the SAX parser to be used. (optional) + * @param className should be an implementation of SAX2 org.xml.sax.XMLReader + * or SAX2 org.xml.sax.Parser. + *

if className is an implementation of org.xml.sax.Parser, {@link #setLenient(boolean)}, + * will be ignored. + *

if not set, the default {@link #DEFAULT_XML_READER_CLASSNAME} will be used. + * @see org.xml.sax.XMLReader; + * @see org.xml.sax.Parser; + */ + public void setClassName(String className) { + + readerClassName = className; + } + + + /** + * Specify the classpath to be searched to load the parser (optional) + */ + public void setClasspath(Path classpath) { + + if (this.classpath == null) { + this.classpath = classpath; + } else { + this.classpath.append(classpath); + } + } + + /** + * @see #setClassPath + */ + public Path createClasspath() { + if (this.classpath == null) { + this.classpath = new Path(project); + } + return this.classpath.createPath(); + } + + /** + * @see #setClassPath + */ + public void setClasspathRef(Reference r) { + createClasspath().setRefid(r); + } + + /** + * specifify the file to be checked + */ + public void setFile(File file) { + this.file = file; + } + + /** + * specifify a set of file to be checked + */ + public void addFileset(FileSet set) { + filesets.addElement(set); + } + + public void execute() throws BuildException { + + int fileProcessed = 0; + if (file == null && (filesets.size()==0) ) { + throw new BuildException("Specify at least one source - a file or a fileset."); + } + + initValidator(); + + if (file != null) { + if (file.exists() && file.canRead() && file.isFile()) { + doValidate(file); + fileProcessed++; + } + else { + String errorMsg = "File " + file + " cannot be read"; + if (failOnError) + throw new BuildException(errorMsg); + else + log(errorMsg, Project.MSG_ERR); + } + } + + for (int i=0; i + *

  • log SAX parse exceptions, + *
  • remember if an error occured + * + */ + protected class ValidatorErrorHandler implements ErrorHandler { + + protected File currentFile = null; + protected String lastErrorMessage = null; + protected boolean failed = false; + + public void init(File file) { + currentFile = file; + failed = false; + } + + // did an error happen during last parsing ? + public boolean getFailure() { + + return failed; + } + + public void fatalError(SAXParseException exception) { + + failed = true; + doLog(exception,Project.MSG_ERR); + } + + public void error(SAXParseException exception) { + + failed = true; + doLog(exception,Project.MSG_ERR); + } + + public void warning(SAXParseException exception) { + // depending on implementation, XMLReader can yield hips of warning, + // only output then if user explicitely asked for it + if (warn) + doLog(exception,Project.MSG_WARN); + } + + private void doLog(SAXParseException e, int logLevel) { + + log(getMessage(e), logLevel); + } + + private String getMessage(SAXParseException e) { + + return currentFile + ":" + e.getLineNumber() + ": " + e.getMessage(); + } + } +}