Example usage for javax.xml.transform ErrorListener error

List of usage examples for javax.xml.transform ErrorListener error

Introduction

In this page you can find the example usage for javax.xml.transform ErrorListener error.

Prototype

public abstract void error(TransformerException exception) throws TransformerException;

Source Link

Document

Receive notification of a recoverable error.

Usage

From source file:net.sf.joost.trax.TrAXHelper.java

/**
* Converts a supplied <code>Source</code> to a <code>SAXSource</code>.
* @param source The supplied input source
* @param errorListener an ErrorListener object
* @return a <code>SAXSource</code>
*///from w  w w  .jav a 2s. co m
public static SAXSource getSAXSource(Source source, ErrorListener errorListener) throws TransformerException {

    if (DEBUG)
        log.debug("getting a SAXSource from a Source");
    //SAXSource
    if (source instanceof SAXSource) {
        if (DEBUG)
            log.debug("source is an instance of SAXSource, so simple return");
        return (SAXSource) source;
    }
    //DOMSource
    if (source instanceof DOMSource) {
        if (DEBUG)
            log.debug("source is an instance of DOMSource");
        InputSource is = new InputSource();
        Node startNode = ((DOMSource) source).getNode();
        Document doc;
        if (startNode instanceof Document) {
            doc = (Document) startNode;
        } else {
            doc = startNode.getOwnerDocument();
        }
        if (DEBUG)
            log.debug("using DOMDriver");
        DOMDriver driver = new DOMDriver();
        driver.setDocument(doc);
        is.setSystemId(source.getSystemId());
        driver.setSystemId(source.getSystemId());
        return new SAXSource(driver, is);
    }
    //StreamSource
    if (source instanceof StreamSource) {
        if (DEBUG)
            log.debug("source is an instance of StreamSource");
        InputSource isource = getInputSourceForStreamSources(source, errorListener);
        return new SAXSource(isource);
    } else {
        String errMsg = "Unknown type of source";
        if (log != null)
            log.error(errMsg);
        IllegalArgumentException iE = new IllegalArgumentException(errMsg);
        TransformerConfigurationException tE = new TransformerConfigurationException(iE.getMessage(), iE);
        if (errorListener != null)
            errorListener.error(tE);
        else
            throw tE;
        return null;
    }
}