use of org.w3c.dom.DOMImplementation 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 org.w3c.dom.DOMImplementation in project buck by facebook.
the class WorkspaceGenerator method writeWorkspace.
public Path writeWorkspace() throws IOException {
DocumentBuilder docBuilder;
Transformer transformer;
try {
docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
transformer = TransformerFactory.newInstance().newTransformer();
} catch (ParserConfigurationException | TransformerConfigurationException e) {
throw new RuntimeException(e);
}
DOMImplementation domImplementation = docBuilder.getDOMImplementation();
final Document doc = domImplementation.createDocument(/* namespaceURI */
null, "Workspace", /* docType */
null);
doc.setXmlVersion("1.0");
Element rootElem = doc.getDocumentElement();
rootElem.setAttribute("version", "1.0");
final Stack<Element> groups = new Stack<>();
groups.push(rootElem);
FileVisitor<Map.Entry<String, WorkspaceNode>> visitor = new FileVisitor<Map.Entry<String, WorkspaceNode>>() {
@Override
public FileVisitResult preVisitDirectory(Map.Entry<String, WorkspaceNode> dir, BasicFileAttributes attrs) throws IOException {
Preconditions.checkArgument(dir.getValue() instanceof WorkspaceGroup);
Element element = doc.createElement("Group");
element.setAttribute("location", "container:");
element.setAttribute("name", dir.getKey());
groups.peek().appendChild(element);
groups.push(element);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Map.Entry<String, WorkspaceNode> file, BasicFileAttributes attrs) throws IOException {
Preconditions.checkArgument(file.getValue() instanceof WorkspaceFileRef);
WorkspaceFileRef fileRef = (WorkspaceFileRef) file.getValue();
Element element = doc.createElement("FileRef");
element.setAttribute("location", "container:" + MorePaths.relativize(MorePaths.normalize(outputDirectory), fileRef.getPath()).toString());
groups.peek().appendChild(element);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Map.Entry<String, WorkspaceNode> file, IOException exc) throws IOException {
return FileVisitResult.TERMINATE;
}
@Override
public FileVisitResult postVisitDirectory(Map.Entry<String, WorkspaceNode> dir, IOException exc) throws IOException {
groups.pop();
return FileVisitResult.CONTINUE;
}
};
walkNodeTree(visitor);
Path projectWorkspaceDir = getWorkspaceDir();
projectFilesystem.mkdirs(projectWorkspaceDir);
Path serializedWorkspace = projectWorkspaceDir.resolve("contents.xcworkspacedata");
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(outputStream);
transformer.transform(source, result);
String contentsToWrite = outputStream.toString();
if (MoreProjectFilesystems.fileContentsDiffer(new ByteArrayInputStream(contentsToWrite.getBytes(Charsets.UTF_8)), serializedWorkspace, projectFilesystem)) {
projectFilesystem.writeContentsToPath(contentsToWrite, serializedWorkspace);
}
} catch (TransformerException e) {
throw new RuntimeException(e);
}
Path xcshareddata = projectWorkspaceDir.resolve("xcshareddata");
projectFilesystem.mkdirs(xcshareddata);
Path workspaceSettingsPath = xcshareddata.resolve("WorkspaceSettings.xcsettings");
String workspaceSettings = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\"" + " \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n" + "<plist version=\"1.0\">\n" + "<dict>\n" + "\t<key>IDEWorkspaceSharedSettings_AutocreateContextsIfNeeded</key>\n" + "\t<false/>\n" + "</dict>\n" + "</plist>";
projectFilesystem.writeContentsToPath(workspaceSettings, workspaceSettingsPath);
return projectWorkspaceDir;
}
use of org.w3c.dom.DOMImplementation in project qi4j-sdk by Qi4j.
the class QuikitResolver method getLSInput.
private LSInput getLSInput() throws Exception {
DOMImplementationLS impl;
DOMImplementation docImpl = builder.getDOMImplementation();
// defaulting to the sun implementation.
if (docImpl != null && docImpl.hasFeature("LS", "3.0")) {
impl = (DOMImplementationLS) docImpl.getFeature("LS", "3.0");
} else {
DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
if (impl == null) {
System.setProperty(DOMImplementationRegistry.PROPERTY, "com.sun.org.apache.xerces.internal.dom.DOMImplementationSourceImpl");
registry = DOMImplementationRegistry.newInstance();
impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
}
}
return impl.createLSInput();
}
use of org.w3c.dom.DOMImplementation in project robovm by robovm.
the class DocumentGetElementsByTagnameNS method testGetElementsByTagNameNS1.
/**
* Runs the test case.
*
* @throws Throwable
* Any uncaught exception causes test to fail
*/
public void testGetElementsByTagNameNS1() throws Throwable {
Document doc;
Document newDoc;
DocumentType docType = null;
DOMImplementation domImpl;
NodeList childList;
String nullNS = null;
doc = (Document) load("staffNS", builder);
domImpl = doc.getImplementation();
newDoc = domImpl.createDocument(nullNS, "root", docType);
childList = newDoc.getElementsByTagNameNS("*", "*");
assertEquals("documentgetelementsbytagnameNS01", 1, childList.getLength());
}
use of org.w3c.dom.DOMImplementation in project robovm by robovm.
the class DocumentImportNode method testImportNode8.
public void testImportNode8() throws Throwable {
Document doc;
DocumentType docType;
DOMImplementation domImpl;
String nullNS = null;
doc = (Document) load("staffNS", builder);
domImpl = doc.getImplementation();
docType = domImpl.createDocumentType("test:root", nullNS, nullNS);
{
boolean success = false;
try {
doc.importNode(docType, true);
} catch (DOMException ex) {
success = (ex.code == DOMException.NOT_SUPPORTED_ERR);
}
assertTrue("throw_NOT_SUPPORTED_ERR", success);
}
}
Aggregations