FileDocCategorySizeDatePackage
JAXPUtils.javaAPI DocApache Ant 1.707889Wed Dec 13 06:16:22 GMT 2006org.apache.tools.ant.util

JAXPUtils

public class JAXPUtils extends Object
Collection of helper methods that retrieve a ParserFactory or Parsers and Readers.

This class will create only a single factory instance.

since
Ant 1.5

Fields Summary
private static final FileUtils
FILE_UTILS
Helper for systemId.
private static SAXParserFactory
parserFactory
Parser factory to use to create parsers.
private static SAXParserFactory
nsParserFactory
Parser Factory to create Namespace aware parsers.
private static DocumentBuilderFactory
builderFactory
Parser factory to use to create document builders.
Constructors Summary
Methods Summary
private static org.apache.tools.ant.BuildExceptionconvertToBuildException(org.xml.sax.SAXException e)
Translate a SAXException into a BuildException

since
Ant 1.5

        Exception nested = e.getException();
        if (nested != null) {
            return new BuildException(nested);
        } else {
            return new BuildException(e);
        }
    
public static javax.xml.parsers.DocumentBuildergetDocumentBuilder()
Returns a newly created DocumentBuilder.

return
a DocumentBuilder.
throws
BuildException on error.
since
Ant 1.6

        try {
            return getDocumentBuilderFactory().newDocumentBuilder();
        } catch (ParserConfigurationException e) {
            throw new BuildException(e);
        }
    
private static synchronized javax.xml.parsers.DocumentBuilderFactorygetDocumentBuilderFactory()
Obtains the default builder factory if not already.

since
Ant 1.6

        if (builderFactory == null) {
            try {
                builderFactory = DocumentBuilderFactory.newInstance();
            } catch (FactoryConfigurationError e) {
                throw new BuildException("Document builder factory has not "
                                         + "been configured correctly: "
                                         + e.getMessage(), e);
            }
        }
        return builderFactory;
    
public static synchronized javax.xml.parsers.SAXParserFactorygetNSParserFactory()
Returns the parser factory to use to create namespace aware parsers.

return
a SAXParserFactory to use which supports manufacture of namespace aware parsers.
throws
BuildException on error.
since
Ant 1.6


        if (nsParserFactory == null) {
            nsParserFactory = newParserFactory();
            nsParserFactory.setNamespaceAware(true);
        }
        return nsParserFactory;
    
public static org.xml.sax.XMLReadergetNamespaceXMLReader()
Returns a newly created SAX 2 XMLReader, which is namespace aware

return
a SAX 2 XMLReader.
throws
BuildException on error.
see
#getParserFactory
since
Ant 1.6

        try {
            return newSAXParser(getNSParserFactory()).getXMLReader();
        } catch (SAXException e) {
            throw convertToBuildException(e);
        }
    
public static org.xml.sax.ParsergetParser()
Returns a newly created SAX 1 Parser, using the default parser factory.

return
a SAX 1 Parser.
throws
BuildException on error.
see
#getParserFactory
since
Ant 1.5

        try {
            return newSAXParser(getParserFactory()).getParser();
        } catch (SAXException e) {
            throw convertToBuildException(e);
        }
    
public static synchronized javax.xml.parsers.SAXParserFactorygetParserFactory()
Returns the parser factory to use. Only one parser factory is ever created by this method and is then cached for future use.

return
a SAXParserFactory to use.
throws
BuildException on error.
since
Ant 1.5


                                            
        
          

        if (parserFactory == null) {
            parserFactory = newParserFactory();
        }
        return parserFactory;
    
public static java.lang.StringgetSystemId(java.io.File file)
This is a best attempt to provide a URL.toExternalForm() from a file URL. Some parsers like Crimson choke on uri that are made of backslashed paths (ie windows) as it is does not conform URI specifications.

param
file the file to create the system id from.
return
the systemid corresponding to the given file.
since
Ant 1.5.2

        return FILE_UTILS.toURI(file.getAbsolutePath());
    
public static org.xml.sax.XMLReadergetXMLReader()
Returns a newly created SAX 2 XMLReader, using the default parser factory.

return
a SAX 2 XMLReader.
throws
BuildException on error.
see
#getParserFactory
since
Ant 1.5

        try {
            return newSAXParser(getParserFactory()).getXMLReader();
        } catch (SAXException e) {
            throw convertToBuildException(e);
        }
    
public static javax.xml.parsers.SAXParserFactorynewParserFactory()
Returns a new parser factory instance.

return
the parser factory.
throws
BuildException on error.
since
Ant 1.5


        try {
            return SAXParserFactory.newInstance();
        } catch (FactoryConfigurationError e) {
            throw new BuildException("XML parser factory has not been "
                                     + "configured correctly: "
                                     + e.getMessage(), e);
        }
    
private static javax.xml.parsers.SAXParsernewSAXParser(javax.xml.parsers.SAXParserFactory factory)

return
a new SAXParser instance as helper for getParser and getXMLReader.
since
Ant 1.5

        try {
            return factory.newSAXParser();
        } catch (ParserConfigurationException e) {
            throw new BuildException("Cannot create parser for the given "
                                     + "configuration: " + e.getMessage(), e);
        } catch (SAXException e) {
            throw convertToBuildException(e);
        }