Search in sources :

Example 1 with DocumentBuilderFactory

use of javax.xml.parsers.DocumentBuilderFactory in project camel by apache.

the class CMSenderOneMessageImpl method createXml.

private String createXml(final CMMessage message) {
    try {
        final ByteArrayOutputStream xml = new ByteArrayOutputStream();
        final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        // Get the DocumentBuilder
        final DocumentBuilder docBuilder = factory.newDocumentBuilder();
        // Create blank DOM Document
        final DOMImplementation impl = docBuilder.getDOMImplementation();
        final Document doc = impl.createDocument(null, "MESSAGES", null);
        // ROOT Element es MESSAGES
        final Element root = doc.getDocumentElement();
        // AUTHENTICATION element
        final Element authenticationElement = doc.createElement("AUTHENTICATION");
        final Element productTokenElement = doc.createElement("PRODUCTTOKEN");
        authenticationElement.appendChild(productTokenElement);
        final Text productTokenValue = doc.createTextNode("" + productToken);
        productTokenElement.appendChild(productTokenValue);
        root.appendChild(authenticationElement);
        // MSG Element
        final Element msgElement = doc.createElement("MSG");
        root.appendChild(msgElement);
        // <FROM>VALUE</FROM>
        final Element fromElement = doc.createElement("FROM");
        fromElement.appendChild(doc.createTextNode(message.getSender()));
        msgElement.appendChild(fromElement);
        // <BODY>VALUE</BODY>
        final Element bodyElement = doc.createElement("BODY");
        bodyElement.appendChild(doc.createTextNode(message.getMessage()));
        msgElement.appendChild(bodyElement);
        // <TO>VALUE</TO>
        final Element toElement = doc.createElement("TO");
        toElement.appendChild(doc.createTextNode(message.getPhoneNumber()));
        msgElement.appendChild(toElement);
        // false
        if (message.isUnicode()) {
            final Element dcsElement = doc.createElement("DCS");
            dcsElement.appendChild(doc.createTextNode("8"));
            msgElement.appendChild(dcsElement);
        }
        // <REFERENCE>VALUE</REFERENCE> -Alfanum
        final String id = message.getIdAsString();
        if (id != null && !id.isEmpty()) {
            final Element refElement = doc.createElement("REFERENCE");
            refElement.appendChild(doc.createTextNode("" + message.getIdAsString()));
            msgElement.appendChild(refElement);
        }
        // <MAXIMUMNUMBEROFMESSAGEPARTS>8</MAXIMUMNUMBEROFMESSAGEPARTS>
        if (message.isMultipart()) {
            final Element minMessagePartsElement = doc.createElement("MINIMUMNUMBEROFMESSAGEPARTS");
            minMessagePartsElement.appendChild(doc.createTextNode("1"));
            msgElement.appendChild(minMessagePartsElement);
            final Element maxMessagePartsElement = doc.createElement("MAXIMUMNUMBEROFMESSAGEPARTS");
            maxMessagePartsElement.appendChild(doc.createTextNode(Integer.toString(message.getMultiparts())));
            msgElement.appendChild(maxMessagePartsElement);
        }
        // Creatate XML as String
        final Transformer aTransformer = TransformerFactory.newInstance().newTransformer();
        aTransformer.setOutputProperty(OutputKeys.INDENT, "yes");
        final Source src = new DOMSource(doc);
        final Result dest = new StreamResult(xml);
        aTransformer.transform(src, dest);
        return xml.toString();
    } catch (final TransformerException e) {
        throw new XMLConstructionException(String.format("Cant serialize CMMessage %s", message), e);
    } catch (final ParserConfigurationException e) {
        throw new XMLConstructionException(String.format("Cant serialize CMMessage %s", message), e);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) Element(org.w3c.dom.Element) DOMImplementation(org.w3c.dom.DOMImplementation) Text(org.w3c.dom.Text) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(org.w3c.dom.Document) XMLConstructionException(org.apache.camel.component.cm.exceptions.XMLConstructionException) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) TransformerException(javax.xml.transform.TransformerException)

Example 2 with DocumentBuilderFactory

use of javax.xml.parsers.DocumentBuilderFactory in project camel by apache.

the class ConverterTest method testFallbackConverter.

@Test
public void testFallbackConverter() throws Exception {
    CamelContext context = new DefaultCamelContext();
    Exchange exchange = new DefaultExchange(context);
    MessageContentsList list = new MessageContentsList();
    NodeListWrapper nl = new NodeListWrapper(new ArrayList<Element>());
    list.add(nl);
    exchange.getIn().setBody(list);
    Node node = exchange.getIn().getBody(Node.class);
    assertNull(node);
    File file = new File("src/test/resources/org/apache/camel/component/cxf/converter/test.xml");
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    documentBuilderFactory.setNamespaceAware(true);
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    Document document = documentBuilder.parse(file);
    document.getDocumentElement().normalize();
    List<Element> elements = new ArrayList<Element>();
    elements.add(document.getDocumentElement());
    nl = new NodeListWrapper(elements);
    list.clear();
    list.add(nl);
    exchange.getIn().setBody(list);
    node = exchange.getIn().getBody(Node.class);
    assertNotNull(node);
}
Also used : CamelContext(org.apache.camel.CamelContext) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) DefaultExchange(org.apache.camel.impl.DefaultExchange) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) MessageContentsList(org.apache.cxf.message.MessageContentsList) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) Document(org.w3c.dom.Document) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) DocumentBuilder(javax.xml.parsers.DocumentBuilder) File(java.io.File) Test(org.junit.Test)

Example 3 with DocumentBuilderFactory

use of javax.xml.parsers.DocumentBuilderFactory in project camel by apache.

the class CxfPayloadConverterTest method setUp.

@Override
@Before
public void setUp() throws Exception {
    super.setUp();
    File file = new File("src/test/resources/org/apache/camel/component/cxf/converter/test.xml");
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    documentBuilderFactory.setNamespaceAware(true);
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    document = documentBuilder.parse(file);
    document.getDocumentElement().normalize();
    List<Source> body = new ArrayList<Source>();
    body.add(new DOMSource(document.getDocumentElement()));
    List<Source> staxbody = new ArrayList<Source>();
    staxbody.add(new StAXSource(StaxUtils.createXMLStreamReader(new FileInputStream(file), "utf-8")));
    payload = new CxfPayload<String[]>(new ArrayList<String[]>(), body, null);
    emptyPayload = new CxfPayload<String[]>(new ArrayList<String[]>(), new ArrayList<Source>(), null);
    staxpayload = new CxfPayload<String[]>(new ArrayList<String[]>(), staxbody, null);
    inputStream = new FileInputStream(file);
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ArrayList(java.util.ArrayList) StAXSource(javax.xml.transform.stax.StAXSource) File(java.io.File) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) SAXSource(javax.xml.transform.sax.SAXSource) StAXSource(javax.xml.transform.stax.StAXSource) FileInputStream(java.io.FileInputStream) Before(org.junit.Before)

Example 4 with DocumentBuilderFactory

use of javax.xml.parsers.DocumentBuilderFactory in project camel by apache.

the class DefaultCxfBindingTest method getDocument.

private Document getDocument(String soapMessage) throws Exception {
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    documentBuilderFactory.setNamespaceAware(true);
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    Document document = documentBuilder.parse(IOConverter.toInputStream(soapMessage, null));
    document.getDocumentElement().normalize();
    return document;
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Document(org.w3c.dom.Document)

Example 5 with DocumentBuilderFactory

use of javax.xml.parsers.DocumentBuilderFactory in project groovy by apache.

the class DOMBuilder method newInstance.

public static DOMBuilder newInstance(boolean validating, boolean namespaceAware) throws ParserConfigurationException {
    DocumentBuilderFactory factory = FactorySupport.createDocumentBuilderFactory();
    factory.setNamespaceAware(namespaceAware);
    factory.setValidating(validating);
    return new DOMBuilder(factory.newDocumentBuilder());
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory)

Aggregations

DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)759 DocumentBuilder (javax.xml.parsers.DocumentBuilder)622 Document (org.w3c.dom.Document)526 Element (org.w3c.dom.Element)244 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)232 NodeList (org.w3c.dom.NodeList)209 IOException (java.io.IOException)197 InputSource (org.xml.sax.InputSource)193 SAXException (org.xml.sax.SAXException)173 Node (org.w3c.dom.Node)145 StringReader (java.io.StringReader)142 Test (org.junit.Test)117 File (java.io.File)86 DOMSource (javax.xml.transform.dom.DOMSource)77 ByteArrayInputStream (java.io.ByteArrayInputStream)72 InputStream (java.io.InputStream)63 StreamResult (javax.xml.transform.stream.StreamResult)50 ArrayList (java.util.ArrayList)47 SAXParseException (org.xml.sax.SAXParseException)46 Transformer (javax.xml.transform.Transformer)44