Index: src/main/org/apache/tools/ant/taskdefs/optional/XMLValidateTask.java =================================================================== RCS file: /home/cvspublic/ant/src/main/org/apache/tools/ant/taskdefs/optional/XMLValidateTask.java,v retrieving revision 1.36 diff -u -r1.36 XMLValidateTask.java --- src/main/org/apache/tools/ant/taskdefs/optional/XMLValidateTask.java 17 Sep 2003 20:11:44 -0000 1.36 +++ src/main/org/apache/tools/ant/taskdefs/optional/XMLValidateTask.java 24 Sep 2003 22:05:20 -0000 @@ -72,6 +72,7 @@ import org.apache.tools.ant.types.XMLCatalog; import org.apache.tools.ant.util.FileUtils; import org.apache.tools.ant.util.JAXPUtils; + import org.xml.sax.EntityResolver; import org.xml.sax.ErrorHandler; import org.xml.sax.InputSource; @@ -108,7 +109,7 @@ protected boolean failOnError = true; protected boolean warn = true; protected boolean lenient = false; - protected String readerClassName = null; + protected String readerClassName = null; /** file to be validated */ protected File file = null; @@ -116,20 +117,24 @@ protected Vector filesets = new Vector(); 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 XMLReader xmlReader = null; + // XMLReader used to validation process + protected ValidatorErrorHandler errorHandler = new ValidatorErrorHandler(); + // to report sax parsing errors /** The vector to store all attributes (features) to be set on the parser. **/ private Vector attributeList = new Vector(); - + + /** + * List of properties. + */ + private final Vector propertyList = new Vector(); private XMLCatalog xmlCatalog = new XMLCatalog(); @@ -182,7 +187,6 @@ readerClassName = className; } - /** * Specify the classpath to be searched to load the parser (optional) */ @@ -246,6 +250,19 @@ return feature; } + /** + * Creates a property. + * + * @return a property. + */ + public Property createProperty() { + final Property prop = new Property(); + propertyList.addElement(prop); + return prop; + } + + // public + public void init() throws BuildException { super.init(); xmlCatalog.setProject(getProject()); @@ -270,14 +287,14 @@ int fileProcessed = 0; if (file == null && (filesets.size() == 0)) { - throw new BuildException("Specify at least one source - " - + "a file or a fileset."); + throw new BuildException( + "Specify at least one source - " + "a file or a fileset."); } initValidator(); if (file != null) { - if (file.exists() && file.canRead() && file.isFile()) { + if (file.exists() && file.canRead() && file.isFile()) { doValidate(file); fileProcessed++; } else { @@ -292,11 +309,11 @@ for (int i = 0; i < filesets.size(); i++) { - FileSet fs = (FileSet) filesets.elementAt(i); + FileSet fs = (FileSet)filesets.elementAt(i); DirectoryScanner ds = fs.getDirectoryScanner(getProject()); String[] files = ds.getIncludedFiles(); - for (int j = 0; j < files.length; j++) { + for (int j = 0; j < files.length; j++) { File srcFile = new File(fs.getDir(getProject()), files[j]); doValidate(srcFile); fileProcessed++; @@ -324,8 +341,8 @@ try { // load the parser class if (classpath != null) { - AntClassLoader loader - = getProject().createClassLoader(classpath); + AntClassLoader loader = + getProject().createClassLoader(classpath); readerClass = Class.forName(readerClassName, true, loader); } else { readerClass = Class.forName(readerClassName); @@ -343,20 +360,23 @@ // then check it implements XMLReader if (reader instanceof XMLReader) { - xmlReader = (XMLReader) reader; - log("Using SAX2 reader " + reader.getClass().getName(), + xmlReader = (XMLReader)reader; + log( + "Using SAX2 reader " + reader.getClass().getName(), Project.MSG_VERBOSE); } else { // see if it is a SAX1 Parser if (reader instanceof Parser) { - xmlReader = new ParserAdapter((Parser) reader); - log("Using SAX1 parser " + reader.getClass().getName(), + xmlReader = new ParserAdapter((Parser)reader); + log( + "Using SAX1 parser " + reader.getClass().getName(), Project.MSG_VERBOSE); - } else { - throw new BuildException(INIT_FAILED_MSG - + reader.getClass().getName() - + " implements nor SAX1 Parser nor SAX2 XMLReader."); + } else { + throw new BuildException( + INIT_FAILED_MSG + + reader.getClass().getName() + + " implements nor SAX1 Parser nor SAX2 XMLReader."); } } @@ -370,10 +390,16 @@ } // set the feature from the attribute list for (int i = 0; i < attributeList.size(); i++) { - Attribute feature = (Attribute) attributeList.elementAt(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()); + } } } @@ -382,7 +408,7 @@ * @param feature the name of the feature to set * @param value the value of the feature * @param warn whether to war if the parser does not support the feature - + */ private void setFeature(String feature, boolean value) throws BuildException { @@ -390,13 +416,55 @@ try { xmlReader.setFeature(feature, value); } catch (SAXNotRecognizedException e) { - throw new BuildException("Parser " + xmlReader.getClass().getName() - + " doesn't recognize feature " - + feature, e, getLocation()); - } catch (SAXNotSupportedException e) { - throw new BuildException("Parser " + xmlReader.getClass().getName() - + " doesn't support feature " - + feature, e, getLocation()); + throw new BuildException( + "Parser " + + xmlReader.getClass().getName() + + " doesn't recognize feature " + + feature, + e, + getLocation()); + } catch (SAXNotSupportedException e) { + throw new BuildException( + "Parser " + + xmlReader.getClass().getName() + + " doesn't support feature " + + feature, + e, + getLocation()); + } + } + + /** + * Sets a property. + * + * @param name a property name + * @param value a property value. + * @throws BuildException if an error occurs. + */ + private 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."); + } + + try { + xmlReader.setProperty(name, value); + } catch (SAXNotRecognizedException e) { + throw new BuildException( + "Parser " + + xmlReader.getClass().getName() + + " doesn't recognize property " + + name, + e, + getLocation()); + } catch (SAXNotSupportedException e) { + throw new BuildException( + "Parser " + + xmlReader.getClass().getName() + + " doesn't support property " + + name, + e, + getLocation()); } } @@ -413,18 +481,21 @@ xmlReader.parse(is); } catch (SAXException ex) { if (failOnError) { - throw new BuildException("Could not validate document " - + afile); + throw new BuildException( + "Could not validate document " + afile); + } else { + log("Could not validate document " + afile + ": " + ex.toString()); } } catch (IOException ex) { - throw new BuildException("Could not validate document " + afile, + throw new BuildException( + "Could not validate document " + afile, ex); } if (errorHandler.getFailure()) { if (failOnError) { - throw new BuildException(afile - + " is not a valid XML document."); + throw new BuildException( + afile + " is not a valid XML document."); } else { log(afile + " is not a valid XML document", Project.MSG_ERR); } @@ -485,9 +556,11 @@ int line = e.getLineNumber(); int col = e.getColumnNumber(); return new URL(sysID).getFile() - + (line == -1 ? "" : (":" + line - + (col == -1 ? "" : (":" + col)))) - + ": " + e.getMessage(); + + (line == -1 + ? "" + : (":" + line + (col == -1 ? "" : (":" + col)))) + + ": " + + e.getMessage(); } catch (MalformedURLException mfue) { // ignore and just return exception message } @@ -544,4 +617,31 @@ return attributeValue; } } + + /** + * A Parser property + */ + public final class Property { + + private String name; + private String value; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + + } // Property + }