use of javax.xml.parsers.DocumentBuilder 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);
}
}
use of javax.xml.parsers.DocumentBuilder 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);
}
use of javax.xml.parsers.DocumentBuilder 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);
}
use of javax.xml.parsers.DocumentBuilder 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;
}
use of javax.xml.parsers.DocumentBuilder in project camel by apache.
the class XmlConverter method toDOMSource.
@Converter
public DOMSource toDOMSource(InputStream is, Exchange exchange) throws ParserConfigurationException, IOException, SAXException {
InputSource source = new InputSource(is);
String systemId = source.getSystemId();
DocumentBuilder builder = getDocumentBuilderFactory(exchange).newDocumentBuilder();
Document document = builder.parse(source);
return new DOMSource(document, systemId);
}
Aggregations