Example usage for javax.xml.transform.sax SAXResult getHandler

List of usage examples for javax.xml.transform.sax SAXResult getHandler

Introduction

In this page you can find the example usage for javax.xml.transform.sax SAXResult getHandler.

Prototype

public ContentHandler getHandler() 

Source Link

Document

Get the org.xml.sax.ContentHandler that is the Result.

Usage

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

/**
 * HelperMethod for initiating StxEmitter.
 * @param result A <code>Result</code> object.
 * @return An <code>StxEmitter</code>.
 * @throws javax.xml.transform.TransformerException
 *///w ww . j a v  a2s.  c  om
public static StxEmitter initStxEmitter(Result result, Processor processor, Properties outputProperties)
        throws TransformerException {

    if (outputProperties == null)
        outputProperties = processor.outputProperties;

    if (DEBUG)
        log.debug("init StxEmitter");
    // Return the content handler for this Result object
    try {
        // Result object could be SAXResult, DOMResult, or StreamResult
        if (result instanceof SAXResult) {
            final SAXResult target = (SAXResult) result;
            final ContentHandler handler = target.getHandler();
            if (handler != null) {
                if (DEBUG)
                    log.debug("return SAX specific Implementation for " + "StxEmitter");
                //SAX specific Implementation
                return new SAXEmitter(handler);
            }
        } else if (result instanceof DOMResult) {
            if (DEBUG)
                log.debug("return DOM specific Implementation for " + "StxEmitter");
            //DOM specific Implementation
            return new DOMEmitter((DOMResult) result);
        } else if (result instanceof StreamResult) {
            if (DEBUG)
                log.debug("return StreamResult specific Implementation " + "for StxEmitter");
            // Get StreamResult
            final StreamResult target = (StreamResult) result;
            // StreamResult may have been created with a java.io.File,
            // java.io.Writer, java.io.OutputStream or just a String
            // systemId.
            // try to get a Writer from Result object
            final Writer writer = target.getWriter();
            if (writer != null) {
                if (DEBUG)
                    log.debug("get a Writer object from Result object");
                return StreamEmitter.newEmitter(writer, DEFAULT_ENCODING, outputProperties);
            }
            // or try to get an OutputStream from Result object
            final OutputStream ostream = target.getOutputStream();
            if (ostream != null) {
                if (DEBUG)
                    log.debug("get an OutputStream from Result object");
                return StreamEmitter.newEmitter(ostream, outputProperties);
            }
            // or try to get just a systemId string from Result object
            String systemId = result.getSystemId();
            if (DEBUG)
                log.debug("get a systemId string from Result object");
            if (systemId == null) {
                if (DEBUG)
                    log.debug("JAXP_NO_RESULT_ERR");
                throw new TransformerException("JAXP_NO_RESULT_ERR");
            }
            // System Id may be in one of several forms, (1) a uri
            // that starts with 'file:', (2) uri that starts with 'http:'
            // or (3) just a filename on the local system.
            OutputStream os = null;
            URL url = null;
            if (systemId.startsWith("file:")) {
                url = new URL(systemId);
                os = new FileOutputStream(url.getFile());
                return StreamEmitter.newEmitter(os, outputProperties);
            } else if (systemId.startsWith("http:")) {
                url = new URL(systemId);
                URLConnection connection = url.openConnection();
                os = connection.getOutputStream();
                return StreamEmitter.newEmitter(os, outputProperties);
            } else {
                // system id is just a filename
                File tmp = new File(systemId);
                url = tmp.toURL();
                os = new FileOutputStream(url.getFile());
                return StreamEmitter.newEmitter(os, outputProperties);
            }
        }
        // If we cannot create the file specified by the SystemId
    } catch (IOException iE) {
        if (DEBUG)
            log.debug(iE);
        throw new TransformerException(iE);
    } catch (ParserConfigurationException pE) {
        if (DEBUG)
            log.debug(pE);
        throw new TransformerException(pE);
    }
    return null;
}

From source file:org.springframework.oxm.support.AbstractMarshaller.java

/**
 * Template method for handling {@code SAXResult}s.
 * <p>This implementation delegates to {@code marshalSaxHandlers}.
 * @param graph the root of the object graph to marshal
 * @param saxResult the {@code SAXResult}
 * @throws XmlMappingException if the given object cannot be marshalled to the result
 * @see #marshalSaxHandlers(Object, org.xml.sax.ContentHandler, org.xml.sax.ext.LexicalHandler)
 *//*from www .  ja  v a  2s .  co m*/
protected void marshalSaxResult(Object graph, SAXResult saxResult) throws XmlMappingException {
    ContentHandler contentHandler = saxResult.getHandler();
    Assert.notNull(contentHandler, "ContentHandler not set on SAXResult");
    LexicalHandler lexicalHandler = saxResult.getLexicalHandler();
    marshalSaxHandlers(graph, contentHandler, lexicalHandler);
}