Search in sources :

Example 61 with Document

use of org.w3c.dom.Document in project camel by apache.

the class JsonToXmlAttributesTest method shouldCreateElementWithAttribute.

@Test
public void shouldCreateElementWithAttribute() throws ParserConfigurationException, IOException, SAXException {
    // Given
    InputStream inStream = getClass().getResourceAsStream("jsonToXmlElementWithAttributeMessage.json");
    String in = context.getTypeConverter().convertTo(String.class, inStream);
    // When
    String xml = template.requestBody("direct:unmarshal", in, String.class);
    // Then
    Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(xml.getBytes()));
    NodeList nodeList = document.getDocumentElement().getElementsByTagName("element");
    assertEquals(1, nodeList.getLength());
    Element element = (Element) nodeList.item(0);
    assertEquals("elementContent", element.getTextContent());
    assertEquals("attributeValue", element.getAttribute("attribute"));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) Test(org.junit.Test)

Example 62 with Document

use of org.w3c.dom.Document in project camel by apache.

the class SpringXmlJsonDataFormatTest method testSomeOptionsToXML.

@Test
public void testSomeOptionsToXML() throws Exception {
    InputStream inStream = getClass().getResourceAsStream("testMessage1.json");
    String in = context.getTypeConverter().convertTo(String.class, inStream);
    MockEndpoint mockXML = getMockEndpoint("mock:xmlWithOptions");
    mockXML.expectedMessageCount(1);
    mockXML.message(0).body().isInstanceOf(String.class);
    Object marshalled = template.requestBody("direct:unmarshalWithOptions", in);
    Document document = context.getTypeConverter().convertTo(Document.class, marshalled);
    assertEquals("The XML document doesn't carry newRoot as the root name", "newRoot", document.getDocumentElement().getLocalName());
    // with expandable properties, array elements are converted to XML as a
    // sequence of repetitive XML elements with the local name equal to the
    // JSON key
    // for example: { number: [1,2,3] }, normally converted to:
    // <number><e>1</e><e>2</e><e>3</e></number> (where e can be modified by
    // setting elementName)
    // would be converted to
    // <number>1</number><number>2</number><number>3</number>, if number is
    // set as an expandable property
    assertEquals("The number of direct child elements of newRoot with tag d (expandable property) is not 3", 3, document.getDocumentElement().getElementsByTagName("d").getLength());
    assertEquals("The number of direct child elements of newRoot with tag e (expandable property) is not 3", 3, document.getDocumentElement().getElementsByTagName("e").getLength());
    mockXML.assertIsSatisfied();
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) InputStream(java.io.InputStream) JSONObject(net.sf.json.JSONObject) Document(org.w3c.dom.Document) Test(org.junit.Test)

Example 63 with Document

use of org.w3c.dom.Document in project camel by apache.

the class XmlJsonDataFormatTest method testUnmarshalToXMLInlineOptions.

@Test
public void testUnmarshalToXMLInlineOptions() throws Exception {
    InputStream inStream = getClass().getResourceAsStream("testMessage1.json");
    String in = context.getTypeConverter().convertTo(String.class, inStream);
    MockEndpoint mockXML = getMockEndpoint("mock:xmlInlineOptions");
    mockXML.expectedMessageCount(1);
    mockXML.message(0).body().isInstanceOf(String.class);
    Object marshalled = template.requestBody("direct:unmarshalInlineOptions", in);
    Document document = context.getTypeConverter().convertTo(Document.class, marshalled);
    assertEquals("The XML document doesn't carry newRoot as the root name", "newRoot", document.getDocumentElement().getLocalName());
    assertEquals("The number of direct child elements of newRoot with tag d (expandable property) is not 3", 3, document.getDocumentElement().getElementsByTagName("d").getLength());
    assertEquals("The number of direct child elements of newRoot with tag e (expandable property) is not 3", 3, document.getDocumentElement().getElementsByTagName("e").getLength());
    mockXML.assertIsSatisfied();
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) InputStream(java.io.InputStream) JSONObject(net.sf.json.JSONObject) Document(org.w3c.dom.Document) Test(org.junit.Test)

Example 64 with Document

use of org.w3c.dom.Document in project camel by apache.

the class XmlJsonDataFormatTest method testMarshalXMLSources.

@Test
public void testMarshalXMLSources() throws Exception {
    InputStream inStream = getClass().getResourceAsStream("testMessage1.xml");
    DOMSource inDOM = context.getTypeConverter().convertTo(DOMSource.class, inStream);
    inStream = getClass().getResourceAsStream("testMessage1.xml");
    SAXSource inSAX = context.getTypeConverter().convertTo(SAXSource.class, inStream);
    inStream = getClass().getResourceAsStream("testMessage1.xml");
    Document inDocument = context.getTypeConverter().convertTo(Document.class, inStream);
    // save the expected body of the message to set it later
    Object expectedBody = template.requestBody("direct:marshal", inDOM);
    MockEndpoint mockJSON = getMockEndpoint("mock:json");
    // reset the mock endpoint to get rid of the previous message
    mockJSON.reset();
    // all three messages should arrive, should be of type byte[] and
    // identical to one another
    mockJSON.expectedMessageCount(3);
    mockJSON.allMessages().body().isInstanceOf(byte[].class);
    mockJSON.expectedBodiesReceived(Arrays.asList(expectedBody, expectedBody, expectedBody));
    // start bombarding the route
    Object json = template.requestBody("direct:marshal", inDOM);
    String jsonString = context.getTypeConverter().convertTo(String.class, json);
    JSONObject obj = (JSONObject) JSONSerializer.toJSON(jsonString);
    assertEquals("JSONObject doesn't contain 7 keys", 7, obj.entrySet().size());
    template.requestBody("direct:marshal", inSAX);
    template.requestBody("direct:marshal", inDocument);
    mockJSON.assertIsSatisfied();
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) SAXSource(javax.xml.transform.sax.SAXSource) JSONObject(net.sf.json.JSONObject) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) InputStream(java.io.InputStream) JSONObject(net.sf.json.JSONObject) Document(org.w3c.dom.Document) Test(org.junit.Test)

Example 65 with Document

use of org.w3c.dom.Document in project camel by apache.

the class XmlJsonOptionsTest method testSomeOptionsToXML.

@Test
public void testSomeOptionsToXML() throws Exception {
    InputStream inStream = getClass().getClassLoader().getResourceAsStream("org/apache/camel/dataformat/xmljson/testMessage1.json");
    String in = context.getTypeConverter().convertTo(String.class, inStream);
    MockEndpoint mockXML = getMockEndpoint("mock:xml");
    mockXML.expectedMessageCount(1);
    mockXML.message(0).body().isInstanceOf(String.class);
    Object marshalled = template.requestBody("direct:unmarshal", in);
    Document document = context.getTypeConverter().convertTo(Document.class, marshalled);
    assertEquals("The XML document doesn't carry newRoot as the root name", "newRoot", document.getDocumentElement().getLocalName());
    // with expandable properties, array elements are converted to XML as a
    // sequence of repetitive XML elements with the local name equal to the
    // JSON key
    // for example: { number: [1,2,3] }, normally converted to:
    // <number><e>1</e><e>2</e><e>3</e></number> (where e can be modified by
    // setting elementName)
    // would be converted to
    // <number>1</number><number>2</number><number>3</number>, if number is
    // set as an expandable property
    assertEquals("The number of direct child elements of newRoot with tag d (expandable property) is not 3", 3, document.getDocumentElement().getElementsByTagName("d").getLength());
    assertEquals("The number of direct child elements of newRoot with tag e (expandable property) is not 3", 3, document.getDocumentElement().getElementsByTagName("e").getLength());
    mockXML.assertIsSatisfied();
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) InputStream(java.io.InputStream) JSONObject(net.sf.json.JSONObject) Document(org.w3c.dom.Document) Test(org.junit.Test)

Aggregations

Document (org.w3c.dom.Document)3242 Element (org.w3c.dom.Element)1380 DocumentBuilder (javax.xml.parsers.DocumentBuilder)835 NodeList (org.w3c.dom.NodeList)718 Node (org.w3c.dom.Node)644 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)632 IOException (java.io.IOException)530 Test (org.junit.Test)485 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)384 SAXException (org.xml.sax.SAXException)370 InputSource (org.xml.sax.InputSource)292 File (java.io.File)268 InputStream (java.io.InputStream)249 ArrayList (java.util.ArrayList)244 StringReader (java.io.StringReader)233 DOMSource (javax.xml.transform.dom.DOMSource)230 ByteArrayInputStream (java.io.ByteArrayInputStream)202 Attr (org.w3c.dom.Attr)147 HashMap (java.util.HashMap)139 DOMException (org.w3c.dom.DOMException)136