use of javax.xml.parsers.DocumentBuilder in project camel by apache.
the class XmlConverter method toDOMSourceFromStream.
@Converter
public DOMSource toDOMSourceFromStream(StreamSource source, Exchange exchange) throws ParserConfigurationException, IOException, SAXException {
Document document;
String systemId = source.getSystemId();
DocumentBuilder builder = getDocumentBuilderFactory(exchange).newDocumentBuilder();
Reader reader = source.getReader();
if (reader != null) {
document = builder.parse(new InputSource(reader));
} else {
InputStream inputStream = source.getInputStream();
if (inputStream != null) {
InputSource inputsource = new InputSource(inputStream);
inputsource.setSystemId(systemId);
document = builder.parse(inputsource);
} else {
throw new IOException("No input stream or reader available on StreamSource: " + source);
}
}
return new DOMSource(document, systemId);
}
use of javax.xml.parsers.DocumentBuilder in project camel by apache.
the class JibxDataFormatMarshallTest method testMarshall.
@Test
public void testMarshall() throws InterruptedException, ParserConfigurationException, IOException, SAXException {
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMessageCount(1);
PurchaseOrder purchaseOrder = new PurchaseOrder();
String name = "foo";
purchaseOrder.setName(name);
double price = 49;
purchaseOrder.setPrice(price);
double amount = 3;
purchaseOrder.setAmount(amount);
template.sendBody("direct:start", purchaseOrder);
assertMockEndpointsSatisfied();
String body = mock.getReceivedExchanges().get(0).getIn().getBody(String.class);
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Element root = builder.parse(new InputSource(new StringReader(body))).getDocumentElement();
assertEquals(name, root.getAttribute("name"));
assertEquals(price + "", root.getAttribute("price"));
assertEquals(amount + "", root.getAttribute("amount"));
}
use of javax.xml.parsers.DocumentBuilder in project camel by apache.
the class JibxDataFormatMarshallWithBindingNameTest method testMarshall.
@Test
public void testMarshall() throws InterruptedException, ParserConfigurationException, IOException, SAXException {
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMessageCount(1);
PurchaseOrder purchaseOrder = new PurchaseOrder();
String name = "foo";
purchaseOrder.setName(name);
double price = 49;
purchaseOrder.setPrice(price);
double amount = 3;
purchaseOrder.setAmount(amount);
template.sendBody("direct:start", purchaseOrder);
assertMockEndpointsSatisfied();
String body = mock.getReceivedExchanges().get(0).getIn().getBody(String.class);
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Element root = builder.parse(new InputSource(new StringReader(body))).getDocumentElement();
assertEquals(name, root.getAttribute("name"));
assertEquals(price + "", root.getAttribute("price"));
assertEquals(amount + "", root.getAttribute("amount"));
}
use of javax.xml.parsers.DocumentBuilder in project camel by apache.
the class JibxDataFormatSpringDslTest method testMarshall.
@Test
public void testMarshall() throws InterruptedException, ParserConfigurationException, IOException, SAXException {
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMessageCount(1);
PurchaseOrder purchaseOrder = new PurchaseOrder();
String name = "foo";
purchaseOrder.setName(name);
double price = 49;
purchaseOrder.setPrice(price);
double amount = 3;
purchaseOrder.setAmount(amount);
template.sendBody("direct:marshall", purchaseOrder);
assertMockEndpointsSatisfied();
String body = mock.getReceivedExchanges().get(0).getIn().getBody(String.class);
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Element root = builder.parse(new InputSource(new StringReader(body))).getDocumentElement();
assertEquals(name, root.getAttribute("name"));
assertEquals(price + "", root.getAttribute("price"));
assertEquals(amount + "", root.getAttribute("amount"));
}
use of javax.xml.parsers.DocumentBuilder in project hadoop by apache.
the class HostsFileReader method readXmlFileToMapWithFileInputStream.
public static void readXmlFileToMapWithFileInputStream(String type, String filename, InputStream fileInputStream, Map<String, Integer> map) throws IOException {
Document dom;
DocumentBuilderFactory builder = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = builder.newDocumentBuilder();
dom = db.parse(fileInputStream);
// Examples:
// <host><name>host1</name></host>
// <host><name>host2</name><timeout>123</timeout></host>
// <host><name>host3</name><timeout>-1</timeout></host>
// <host><name>host4, host5,host6</name><timeout>1800</timeout></host>
Element doc = dom.getDocumentElement();
NodeList nodes = doc.getElementsByTagName("host");
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element e = (Element) node;
// Support both single host and comma-separated list of hosts.
String v = readFirstTagValue(e, "name");
String[] hosts = StringUtils.getTrimmedStrings(v);
String str = readFirstTagValue(e, "timeout");
Integer timeout = (str == null) ? null : Integer.parseInt(str);
for (String host : hosts) {
map.put(host, timeout);
LOG.info("Adding a node \"" + host + "\" to the list of " + type + " hosts from " + filename);
}
}
}
} catch (IOException | SAXException | ParserConfigurationException e) {
LOG.fatal("error parsing " + filename, e);
throw new RuntimeException(e);
} finally {
fileInputStream.close();
}
}
Aggregations