Search in sources :

Example 91 with XMLReader

use of org.xml.sax.XMLReader in project hbase by apache.

the class TestTableScan method testScanUsingListenerUnmarshallerXML.

/**
   * An example to scan using listener in unmarshaller for XML.
   * @throws Exception the exception
   */
@Test
public void testScanUsingListenerUnmarshallerXML() throws Exception {
    StringBuilder builder = new StringBuilder();
    builder.append("/*");
    builder.append("?");
    builder.append(Constants.SCAN_COLUMN + "=" + COLUMN_1);
    builder.append("&");
    builder.append(Constants.SCAN_LIMIT + "=10");
    Response response = client.get("/" + TABLE + builder.toString(), Constants.MIMETYPE_XML);
    assertEquals(200, response.getCode());
    assertEquals(Constants.MIMETYPE_XML, response.getHeader("content-type"));
    JAXBContext context = JAXBContext.newInstance(ClientSideCellSetModel.class, RowModel.class, CellModel.class);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    final ClientSideCellSetModel.Listener listener = new ClientSideCellSetModel.Listener() {

        @Override
        public void handleRowModel(ClientSideCellSetModel helper, RowModel row) {
            assertTrue(row.getKey() != null);
            assertTrue(row.getCells().size() > 0);
        }
    };
    // install the callback on all ClientSideCellSetModel instances
    unmarshaller.setListener(new Unmarshaller.Listener() {

        public void beforeUnmarshal(Object target, Object parent) {
            if (target instanceof ClientSideCellSetModel) {
                ((ClientSideCellSetModel) target).setCellSetModelListener(listener);
            }
        }

        public void afterUnmarshal(Object target, Object parent) {
            if (target instanceof ClientSideCellSetModel) {
                ((ClientSideCellSetModel) target).setCellSetModelListener(null);
            }
        }
    });
    // create a new XML parser
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setNamespaceAware(true);
    XMLReader reader = factory.newSAXParser().getXMLReader();
    reader.setContentHandler(unmarshaller.getUnmarshallerHandler());
    assertFalse(ClientSideCellSetModel.listenerInvoked);
    reader.parse(new InputSource(response.getStream()));
    assertTrue(ClientSideCellSetModel.listenerInvoked);
}
Also used : InputSource(org.xml.sax.InputSource) JAXBContext(javax.xml.bind.JAXBContext) Response(org.apache.hadoop.hbase.rest.client.Response) RowModel(org.apache.hadoop.hbase.rest.model.RowModel) Unmarshaller(javax.xml.bind.Unmarshaller) XMLReader(org.xml.sax.XMLReader) SAXParserFactory(javax.xml.parsers.SAXParserFactory) Test(org.junit.Test)

Example 92 with XMLReader

use of org.xml.sax.XMLReader in project camel by apache.

the class TidyMarkupDataFormat method asStringTidyMarkup.

/**
     * Return the tidy markup as a string
     * 
     * @param inputStream
     * @return String of XML
     * @throws CamelException
     */
public String asStringTidyMarkup(InputStream inputStream) throws CamelException {
    XMLReader parser = createTagSoupParser();
    StringWriter w = new StringWriter();
    parser.setContentHandler(createContentHandler(w));
    try {
        parser.parse(new InputSource(inputStream));
        return w.toString();
    } catch (Exception e) {
        throw new CamelException("Failed to convert the HTML to tidy Markup", e);
    } finally {
        try {
            inputStream.close();
        } catch (Exception e) {
            LOG.warn("Failed to close the inputStream");
        }
    }
}
Also used : InputSource(org.xml.sax.InputSource) StringWriter(java.io.StringWriter) CamelException(org.apache.camel.CamelException) XMLReader(org.xml.sax.XMLReader) CamelException(org.apache.camel.CamelException)

Example 93 with XMLReader

use of org.xml.sax.XMLReader in project camel by apache.

the class TidyMarkupDataFormat method asNodeTidyMarkup.

/**
     * Return the HTML Markup as an {@link org.w3c.dom.Node}
     * 
     * @param inputStream
     *            The input Stream to convert
     * @return org.w3c.dom.Node The HTML Markup as a DOM Node
     * @throws CamelException
     */
public Node asNodeTidyMarkup(InputStream inputStream) throws CamelException {
    XMLReader parser = createTagSoupParser();
    StringWriter w = new StringWriter();
    parser.setContentHandler(createContentHandler(w));
    try {
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        DOMResult result = new DOMResult();
        transformer.transform(new SAXSource(parser, new InputSource(inputStream)), result);
        return result.getNode();
    } catch (Exception e) {
        throw new CamelException("Failed to convert the HTML to tidy Markup", e);
    }
}
Also used : InputSource(org.xml.sax.InputSource) Transformer(javax.xml.transform.Transformer) DOMResult(javax.xml.transform.dom.DOMResult) SAXSource(javax.xml.transform.sax.SAXSource) StringWriter(java.io.StringWriter) CamelException(org.apache.camel.CamelException) XMLReader(org.xml.sax.XMLReader) CamelException(org.apache.camel.CamelException)

Example 94 with XMLReader

use of org.xml.sax.XMLReader in project camel by apache.

the class StAXProcessor method process.

@Override
public void process(Exchange exchange) throws Exception {
    InputSource is = exchange.getIn().getMandatoryBody(InputSource.class);
    XMLStreamReader stream = exchange.getIn().getMandatoryBody(XMLStreamReader.class);
    XMLReader reader = new StaxStreamXMLReader(stream);
    ContentHandler handler;
    if (contentHandlerClass != null) {
        handler = contentHandlerClass.newInstance();
    } else {
        handler = contentHandler;
    }
    reader.setContentHandler(handler);
    reader.parse(is);
    if (ExchangeHelper.isOutCapable(exchange)) {
        // preserve headers
        exchange.getOut().setHeaders(exchange.getIn().getHeaders());
        exchange.getOut().setBody(handler);
    } else {
        exchange.getIn().setBody(handler);
    }
}
Also used : InputSource(org.xml.sax.InputSource) XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLReader(org.xml.sax.XMLReader) ContentHandler(org.xml.sax.ContentHandler)

Example 95 with XMLReader

use of org.xml.sax.XMLReader in project camel by apache.

the class XmlRpcDataFormat method unmarshalRequest.

protected Object unmarshalRequest(Exchange exchange, InputStream stream) throws Exception {
    InputSource isource = new InputSource(stream);
    XMLReader xr = newXMLReader();
    XmlRpcRequestParser xp;
    try {
        xp = new XmlRpcRequestParser(xmlRpcStreamRequestConfig, typeFactory);
        xr.setContentHandler(xp);
        xr.parse(isource);
    } catch (SAXException e) {
        throw new XmlRpcClientException("Failed to parse server's response: " + e.getMessage(), e);
    } catch (IOException e) {
        throw new XmlRpcClientException("Failed to read server's response: " + e.getMessage(), e);
    }
    return new XmlRpcRequestImpl(xp.getMethodName(), xp.getParams());
}
Also used : XmlRpcRequestParser(org.apache.xmlrpc.parser.XmlRpcRequestParser) InputSource(org.xml.sax.InputSource) XmlRpcRequestImpl(org.apache.camel.component.xmlrpc.XmlRpcRequestImpl) XmlRpcClientException(org.apache.xmlrpc.client.XmlRpcClientException) IOException(java.io.IOException) XMLReader(org.xml.sax.XMLReader) SAXException(org.xml.sax.SAXException)

Aggregations

XMLReader (org.xml.sax.XMLReader)196 InputSource (org.xml.sax.InputSource)160 SAXException (org.xml.sax.SAXException)66 IOException (java.io.IOException)65 SAXSource (javax.xml.transform.sax.SAXSource)38 SAXParserFactory (javax.xml.parsers.SAXParserFactory)37 SAXParser (javax.xml.parsers.SAXParser)36 StringReader (java.io.StringReader)35 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)28 ExpatReader (org.apache.harmony.xml.ExpatReader)24 InputStream (java.io.InputStream)20 ContentHandler (org.xml.sax.ContentHandler)20 TransformerException (javax.xml.transform.TransformerException)17 DOMSource (javax.xml.transform.dom.DOMSource)15 StreamSource (javax.xml.transform.stream.StreamSource)14 ByteArrayInputStream (java.io.ByteArrayInputStream)13 InputStreamReader (java.io.InputStreamReader)9 Source (javax.xml.transform.Source)9 Transformer (javax.xml.transform.Transformer)9 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)9