Search in sources :

Example 6 with DocumentBuilder

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);
}
Also used : InputSource(org.xml.sax.InputSource) DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) XMLStreamReader(javax.xml.stream.XMLStreamReader) Reader(java.io.Reader) XMLReader(org.xml.sax.XMLReader) InputStreamReader(java.io.InputStreamReader) StringReader(java.io.StringReader) IOException(java.io.IOException) Document(org.w3c.dom.Document) Converter(org.apache.camel.Converter)

Example 7 with DocumentBuilder

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"));
}
Also used : InputSource(org.xml.sax.InputSource) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Element(org.w3c.dom.Element) StringReader(java.io.StringReader) PurchaseOrder(org.apache.camel.dataformat.jibx.model.PurchaseOrder) Test(org.junit.Test)

Example 8 with DocumentBuilder

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"));
}
Also used : InputSource(org.xml.sax.InputSource) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Element(org.w3c.dom.Element) StringReader(java.io.StringReader) PurchaseOrder(org.apache.camel.dataformat.jibx.model.PurchaseOrder) Test(org.junit.Test)

Example 9 with DocumentBuilder

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"));
}
Also used : InputSource(org.xml.sax.InputSource) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Element(org.w3c.dom.Element) StringReader(java.io.StringReader) PurchaseOrder(org.apache.camel.dataformat.jibx.model.PurchaseOrder) Test(org.junit.Test)

Example 10 with DocumentBuilder

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();
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Aggregations

DocumentBuilder (javax.xml.parsers.DocumentBuilder)883 Document (org.w3c.dom.Document)694 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)622 Element (org.w3c.dom.Element)339 IOException (java.io.IOException)276 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)276 NodeList (org.w3c.dom.NodeList)267 InputSource (org.xml.sax.InputSource)238 SAXException (org.xml.sax.SAXException)235 Node (org.w3c.dom.Node)199 StringReader (java.io.StringReader)167 Test (org.junit.Test)127 DOMSource (javax.xml.transform.dom.DOMSource)102 File (java.io.File)99 ByteArrayInputStream (java.io.ByteArrayInputStream)86 InputStream (java.io.InputStream)73 ArrayList (java.util.ArrayList)72 StreamResult (javax.xml.transform.stream.StreamResult)65 Transformer (javax.xml.transform.Transformer)59 SAXParseException (org.xml.sax.SAXParseException)56