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);
}
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");
}
}
}
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);
}
}
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);
}
}
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());
}
Aggregations