ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject cvs commit: jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional XMLValidateTask.java
Date Mon, 22 Apr 2002 08:56:13 GMT
bodewig     02/04/22 01:56:13

  Modified:    docs/manual/OptionalTasks xmlvalidate.html
               src/main/org/apache/tools/ant/taskdefs/optional
                        XMLValidateTask.java
  Log:
  Make <xmlvalidate> use JAXP when the user didn't provide a classname.
  
  This should make the tests work no matter which XML parser you used
  (failed for me now as I still have Crimson on my CLASSPATH).
  
  Revision  Changes    Path
  1.4       +6 -4      jakarta-ant/docs/manual/OptionalTasks/xmlvalidate.html
  
  Index: xmlvalidate.html
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/docs/manual/OptionalTasks/xmlvalidate.html,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- xmlvalidate.html	19 Jan 2002 21:19:42 -0000	1.3
  +++ xmlvalidate.html	22 Apr 2002 08:56:13 -0000	1.4
  @@ -8,9 +8,11 @@
   
   <h2><a name="xmlvalidate">XMLValidate</a></h2>
   <h3>Description</h3>
  -<p>
  -  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
  -</p>
  +
  +<p>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.</p>
   
   <h3>Parameters</h3>
   <table border="1" cellpadding="2" cellspacing="0">
  @@ -34,7 +36,7 @@
     </tr>
     <tr>
       <td valign="top">classname</td>
  -    <td valign="top">the parser to use. (default: crimson).</td>
  +    <td valign="top">the parser to use.</td>
       <td align="center" valign="top">No</td>
     </tr>
     <tr>
  
  
  
  1.18      +76 -40    jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/XMLValidateTask.java
  
  Index: XMLValidateTask.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/XMLValidateTask.java,v
  retrieving revision 1.17
  retrieving revision 1.18
  diff -u -r1.17 -r1.18
  --- XMLValidateTask.java	21 Apr 2002 13:46:07 -0000	1.17
  +++ XMLValidateTask.java	22 Apr 2002 08:56:13 -0000	1.18
  @@ -64,6 +64,10 @@
   import java.util.Vector;
   import java.util.Hashtable;
   import java.util.Enumeration;
  +import javax.xml.parsers.SAXParserFactory;
  +import javax.xml.parsers.SAXParser;
  +import javax.xml.parsers.ParserConfigurationException;
  +import javax.xml.parsers.FactoryConfigurationError;
   import org.apache.tools.ant.AntClassLoader;
   import org.apache.tools.ant.BuildException;
   import org.apache.tools.ant.DirectoryScanner;
  @@ -93,21 +97,20 @@
   public class XMLValidateTask extends Task {
   
       /**
  -     * The default implementation parser classname used by the task to process
  -     * validation.
  +     * Parser factory to use to create parsers.
  +     * @see #getParserFactory
        */
  -    // The Xerces implementation ships with Ant.
  -    public static String DEFAULT_XML_READER_CLASSNAME
  -        = "org.apache.xerces.parsers.SAXParser";
  +    private static SAXParserFactory parserFactory = null;
   
  -    protected static String INIT_FAILED_MSG = "Could not start xml validation: ";
  +    protected static String INIT_FAILED_MSG = 
  +        "Could not 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 String  readerClassName = null;
   
       protected File file = null; // file to be validated
       protected Vector filesets = new Vector(); // sets of file to be validated
  @@ -289,44 +292,62 @@
        */
       private void initValidator() {
   
  -        try {
  -            // load the parser class
  -            // with JAXP, we would use a SAXParser factory
  -            Class readerClass = null;
  -            //Class readerImpl = null;
  -            //Class parserImpl = null;
  -            if (classpath != null) {
  -                AntClassLoader loader = new AntClassLoader(project, classpath);
  -//                loader.addSystemPackageRoot("org.xml"); // needed to avoid conflict
  -                readerClass = loader.loadClass(readerClassName);
  -                AntClassLoader.initializeClass(readerClass);
  -            } else {
  -                readerClass = Class.forName(readerClassName);
  +        Object reader = null;
  +        if (readerClassName == null) {
  +            // use JAXP
  +            try {
  +                SAXParser saxParser = getParserFactory().newSAXParser();
  +                try {
  +                    reader = saxParser.getXMLReader();
  +                } catch (SAXException exc) {
  +                    reader = saxParser.getParser();
  +                }
  +            } catch (ParserConfigurationException e) {
  +                throw new BuildException(INIT_FAILED_MSG + e.getMessage(), 
  +                                         e, getLocation());
  +            } catch (SAXException e) {
  +                throw new BuildException(INIT_FAILED_MSG + e.getMessage(), 
  +                                         e, getLocation());
               }
  +        } else {
  +        
  +            Class readerClass = null;
  +            try {
  +                // load the parser class
  +                if (classpath != null) {
  +                    AntClassLoader loader = new AntClassLoader(project, classpath);
  +                    readerClass = loader.loadClass(readerClassName);
  +                    AntClassLoader.initializeClass(readerClass);
  +                } else {
  +                    readerClass = Class.forName(readerClassName);
  +                }
   
  -            // then check it implements XMLReader
  -            if (XMLReader.class.isAssignableFrom(readerClass)) {
  +                reader = readerClass.newInstance();
  +            } catch (ClassNotFoundException e) {
  +                throw new BuildException(INIT_FAILED_MSG + readerClassName, e);
  +            } catch (InstantiationException e) {
  +                throw new BuildException(INIT_FAILED_MSG + readerClassName, e);
  +            } catch (IllegalAccessException e) {
  +                throw new BuildException(INIT_FAILED_MSG + readerClassName, e);
  +            }
  +        }
   
  -                xmlReader = (XMLReader) readerClass.newInstance();
  -                log("Using SAX2 reader " + readerClassName, Project.MSG_VERBOSE);
  -            } else {
  +        // then check it implements XMLReader
  +        if (reader instanceof XMLReader) {
  +            xmlReader = (XMLReader) reader; 
  +            log("Using SAX2 reader " + reader.getClass().getName(), 
  +                Project.MSG_VERBOSE);
  +        } else {
   
  -                // see if it is a SAX1 Parser
  -                if (Parser.class.isAssignableFrom(readerClass)) {
  -                    Parser parser = (Parser) readerClass.newInstance();
  -                    xmlReader = new ParserAdapter(parser);
  -                    log("Using SAX1 parser " + readerClassName, Project.MSG_VERBOSE);
  -                }  else {
  -                    throw new BuildException(INIT_FAILED_MSG + readerClassName
  -                        + " implements nor SAX1 Parser nor SAX2 XMLReader.");
  -                }
  +            // see if it is a SAX1 Parser
  +            if (reader instanceof Parser) {
  +                xmlReader = new ParserAdapter((Parser) reader);
  +                log("Using SAX1 parser " + reader.getClass().getName(), 
  +                    Project.MSG_VERBOSE);
  +            }  else {
  +                throw new BuildException(INIT_FAILED_MSG + readerClassName
  +                                         + " implements nor SAX1 Parser nor SAX2 XMLReader.");
               }
  -        } catch (ClassNotFoundException e) {
  -            throw new BuildException(INIT_FAILED_MSG + readerClassName, e);
  -        } catch (InstantiationException e) {
  -            throw new BuildException(INIT_FAILED_MSG + readerClassName, e);
  -        } catch (IllegalAccessException e) {
  -            throw new BuildException(INIT_FAILED_MSG + readerClassName, e);
           }
   
           xmlReader.setEntityResolver(getEntityResolver());
  @@ -349,6 +370,21 @@
                   setFeature(featureId, ((Boolean) features.get(featureId)).booleanValue(),
true);
               }
           }
  +    }
  +
  +    /**
  +     * Returns the parser factory to use. Only one parser
  +     * factory is ever created by this method (multi-threading
  +     * issues aside) and is then cached for future use.
  +     *
  +     * @return a SAXParserFactory to use within this class
  +     */
  +    private static SAXParserFactory getParserFactory() {
  +        if (parserFactory == null) {
  +            parserFactory = SAXParserFactory.newInstance();
  +        }
  +
  +        return parserFactory;
       }
   
       /*
  
  
  

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


Mime
View raw message