Example usage for org.xml.sax XMLReader getContentHandler

List of usage examples for org.xml.sax XMLReader getContentHandler

Introduction

In this page you can find the example usage for org.xml.sax XMLReader getContentHandler.

Prototype

public ContentHandler getContentHandler();

Source Link

Document

Return the current content handler.

Usage

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

/**
 * Parses the <code>InputSource</code>
 * @param input A <code>InputSource</code> object.
 * @throws SAXException//from w  ww  .  jav a2  s  . co  m
 * @throws IOException
 */
public void parse(InputSource input) throws SAXException, IOException {

    Transformer transformer = null;
    if (DEBUG) {
        if (log.isDebugEnabled())
            log.debug("parsing InputSource " + input.getSystemId());
    }

    try {
        // get a new Transformer
        transformer = this.templates.newTransformer();
        if (transformer instanceof TransformerImpl) {
            this.processor = ((TransformerImpl) transformer).getStxProcessor();
        } else {
            String msg = "An error is occured, because the given transformer is "
                    + "not an instance of TransformerImpl";
            if (log != null)
                log.fatal(msg);
            else
                System.err.println("Fatal error - " + msg);

        }
        XMLReader parent = this.getParent();

        if (parent == null) {
            parent = XMLReaderFactory.createXMLReader();
            setParent(parent);
        }
        ContentHandler handler = this.getContentHandler();

        if (handler == null) {
            handler = parent.getContentHandler();
        }
        if (handler == null) {
            throw new SAXException("no ContentHandler registered");
        }
        //init StxEmitter
        StxEmitter out = null;

        //SAX specific Implementation
        out = new SAXEmitter(handler);

        if (this.processor != null) {
            this.processor.setContentHandler(out);
            this.processor.setLexicalHandler(out);
        } else {
            throw new SAXException("Joost-Processor is not correct configured.");
        }
        if (parent == null) {
            throw new SAXException("No parent for filter");
        }
        parent.setContentHandler(this.processor);
        parent.setProperty("http://xml.org/sax/properties/lexical-handler", this.processor);
        parent.setEntityResolver(this);
        parent.setDTDHandler(this);
        parent.setErrorHandler(this);
        parent.parse(input);

    } catch (TransformerConfigurationException tE) {
        try {
            configErrListener.fatalError(tE);
        } catch (TransformerConfigurationException innerE) {
            throw new SAXException(innerE.getMessage(), innerE);
        }
    } catch (SAXException sE) {
        try {
            configErrListener.fatalError(new TransformerConfigurationException(sE.getMessage(), sE));
        } catch (TransformerConfigurationException innerE) {
            throw new SAXException(innerE.getMessage(), innerE);
        }
    } catch (IOException iE) {
        try {
            configErrListener.fatalError(new TransformerConfigurationException(iE.getMessage(), iE));
        } catch (TransformerConfigurationException innerE) {
            throw new IOException(innerE.getMessage());
        }
    }
}

From source file:org.kalypso.gmlschema.types.AbstractOldFormatMarshallingTypeHandlerAdapter.java

/**
 * @see org.kalypso.gmlschema.types.IMarshallingTypeHandler#marshal(java.lang.Object, org.xml.sax.ContentHandler,
 *      org.xml.sax.ext.LexicalHandler, java.net.URL)
 *//* w  ww . ja v  a 2 s. c o  m*/
@Override
public void marshal(final Object value, final XMLReader reader, final URL context, final String gmlVersion)
        throws SAXException {
    try {
        final Document document = m_builder.newDocument();
        final Node node = marshall(value, document, context);
        // value is encoded in xml in document object
        final StringWriter writer = new StringWriter();
        // REMARK: we do not write the dom formatted here (argument false), because later the
        // content handler will probably do the formatting (IndentContentHandler). If we format here, we will get empty
        // lines later.
        XMLHelper.writeDOM(node, "UTF-8", writer, false); //$NON-NLS-1$
        IOUtils.closeQuietly(writer);
        // value is encoded in string
        final String xmlString = writer.toString();
        final String xmlStringNoHeader = XMLUtilities.removeXMLHeader(xmlString);
        final InputSource input = new InputSource(new StringReader(xmlStringNoHeader));

        SAX_FACTORY.setNamespaceAware(true);
        final SAXParser saxParser = SAX_FACTORY.newSAXParser();
        final XMLReader xmlReader = saxParser.getXMLReader();
        xmlReader.setContentHandler(reader.getContentHandler());
        xmlReader.parse(input);
    } catch (final SAXException e) {
        throw e;
    } catch (final Exception e) {
        throw new SAXException(e);
    }
}

From source file:org.kalypso.gmlschema.types.SimpleDOMTypeHandler.java

@Override
public void marshal(final Object value, final XMLReader reader, final URL context, final String gmlVersion)
        throws SAXException {
    try {//from  w  w w.  ja v  a 2  s  .com
        final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        final DocumentBuilder builder = factory.newDocumentBuilder();
        final Document document = builder.newDocument();
        final Node node = internalMarshall(value, document, context);

        // value is encoded in xml in document object
        final StringWriter writer = new StringWriter();
        XMLHelper.writeDOM(node, "UTF-8", writer); //$NON-NLS-1$
        IOUtils.closeQuietly(writer);

        // value is encoded in string
        final String xmlString = writer.toString();
        final InputSource input = new InputSource(new StringReader(xmlString));
        final SAXParserFactory saxFac = SAXParserFactory.newInstance();
        saxFac.setNamespaceAware(true);

        final SAXParser saxParser = saxFac.newSAXParser();
        final XMLReader xmlReader = saxParser.getXMLReader();
        xmlReader.setContentHandler(reader.getContentHandler());
        xmlReader.parse(input);
    } catch (final SAXException saxe) {
        throw saxe;
    } catch (final Exception e) {
        throw new SAXException(e);
    }
}

From source file:org.kalypsodeegree_impl.io.sax.test.SaxParserTestUtils.java

public static <T> void marshallDocument(final XMLReader reader, final AbstractMarshaller<T> marshaller,
        final T objectToMarshall) throws SAXException {
    final ContentHandler contentHandler = reader.getContentHandler();
    contentHandler.startDocument();/*  w  w  w . j a v a2 s . co  m*/
    marshaller.marshall(objectToMarshall);
    contentHandler.endDocument();
}

From source file:org.nuxeo.wss.fprpc.FPRPCRequest.java

protected void parseCAMLRequest() throws MalformedFPRPCRequest {
    XMLReader reader;
    try {/*  ww w.  java  2  s  .co m*/
        reader = CAMLHandler.getXMLReader();
        reader.parse(new InputSource(httpRequest.getInputStream()));
        calls = ((CAMLHandler) reader.getContentHandler()).getParsedCalls();
    } catch (Exception e) {
        throw new MalformedFPRPCRequest("Unable to parse CAML Request");
    }
}