Search in sources :

Example 1 with HandlerBase

use of org.xml.sax.HandlerBase in project ant by apache.

the class ProjectHelperImpl method parse.

/**
 * Parses the project file, configuring the project as it goes.
 *
 * @param project project instance to be configured.
 * @param source the source from which the project is read.
 * @exception BuildException if the configuration is invalid or cannot
 *                           be read.
 */
public void parse(Project project, Object source) throws BuildException {
    if (!(source instanceof File)) {
        throw new BuildException("Only File source supported by " + "default plugin");
    }
    File bFile = (File) source;
    InputStream inputStream = null;
    InputSource inputSource = null;
    this.project = project;
    this.buildFile = new File(bFile.getAbsolutePath());
    buildFileParent = new File(this.buildFile.getParent());
    try {
        try {
            parser = JAXPUtils.getParser();
        } catch (BuildException e) {
            parser = new XMLReaderAdapter(JAXPUtils.getXMLReader());
        }
        String uri = FILE_UTILS.toURI(bFile.getAbsolutePath());
        inputStream = Files.newInputStream(bFile.toPath());
        inputSource = new InputSource(inputStream);
        inputSource.setSystemId(uri);
        project.log("parsing buildfile " + bFile + " with URI = " + uri, Project.MSG_VERBOSE);
        HandlerBase hb = new RootHandler(this);
        parser.setDocumentHandler(hb);
        parser.setEntityResolver(hb);
        parser.setErrorHandler(hb);
        parser.setDTDHandler(hb);
        parser.parse(inputSource);
    } catch (SAXParseException exc) {
        Location location = new Location(exc.getSystemId(), exc.getLineNumber(), exc.getColumnNumber());
        Throwable t = exc.getException();
        if (t instanceof BuildException) {
            BuildException be = (BuildException) t;
            if (be.getLocation() == Location.UNKNOWN_LOCATION) {
                be.setLocation(location);
            }
            throw be;
        }
        throw new BuildException(exc.getMessage(), t, location);
    } catch (SAXException exc) {
        Throwable t = exc.getException();
        if (t instanceof BuildException) {
            throw (BuildException) t;
        }
        throw new BuildException(exc.getMessage(), t);
    } catch (FileNotFoundException exc) {
        throw new BuildException(exc);
    } catch (UnsupportedEncodingException exc) {
        throw new BuildException("Encoding of project file is invalid.", exc);
    } catch (IOException exc) {
        throw new BuildException("Error reading project file: " + exc.getMessage(), exc);
    } finally {
        FileUtils.close(inputStream);
    }
}
Also used : InputSource(org.xml.sax.InputSource) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) SAXParseException(org.xml.sax.SAXParseException) HandlerBase(org.xml.sax.HandlerBase) XMLReaderAdapter(org.xml.sax.helpers.XMLReaderAdapter) BuildException(org.apache.tools.ant.BuildException) File(java.io.File) Location(org.apache.tools.ant.Location)

Aggregations

File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 BuildException (org.apache.tools.ant.BuildException)1 Location (org.apache.tools.ant.Location)1 HandlerBase (org.xml.sax.HandlerBase)1 InputSource (org.xml.sax.InputSource)1 SAXException (org.xml.sax.SAXException)1 SAXParseException (org.xml.sax.SAXParseException)1 XMLReaderAdapter (org.xml.sax.helpers.XMLReaderAdapter)1