use of org.w3c.dom.ls.LSSerializer in project SIMRacingApps by SIMRacingApps.
the class ConfigPersister method load.
public void load(File f) throws ConfigPersisterException {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(f);
DOMImplementationLS domImplementation = (DOMImplementationLS) doc.getImplementation();
LSSerializer lsSerializer = domImplementation.createLSSerializer();
String configString = lsSerializer.writeToString(doc);
_config = (Config) _xstream.fromXML(convertToCurrent(configString));
setConfigPath(f);
} catch (Exception e) {
throw new ConfigPersisterException(e);
}
}
use of org.w3c.dom.ls.LSSerializer in project zxing by zxing.
the class HtmlAssetTranslator method translateOneFile.
private static void translateOneFile(String language, Path targetHtmlDir, Path sourceFile, String translationTextTranslated) throws IOException {
Path destFile = targetHtmlDir.resolve(sourceFile.getFileName());
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
Document document;
try {
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse(sourceFile.toFile());
} catch (ParserConfigurationException pce) {
throw new IllegalStateException(pce);
} catch (SAXException sae) {
throw new IOException(sae);
}
Element rootElement = document.getDocumentElement();
rootElement.normalize();
Queue<Node> nodes = new LinkedList<>();
nodes.add(rootElement);
while (!nodes.isEmpty()) {
Node node = nodes.poll();
if (shouldTranslate(node)) {
NodeList children = node.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
nodes.add(children.item(i));
}
}
if (node.getNodeType() == Node.TEXT_NODE) {
String text = node.getTextContent();
if (!text.trim().isEmpty()) {
text = StringsResourceTranslator.translateString(text, language);
node.setTextContent(' ' + text + ' ');
}
}
}
Node translateText = document.createTextNode(translationTextTranslated);
Node paragraph = document.createElement("p");
paragraph.appendChild(translateText);
Node body = rootElement.getElementsByTagName("body").item(0);
body.appendChild(paragraph);
DOMImplementationRegistry registry;
try {
registry = DOMImplementationRegistry.newInstance();
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
throw new IllegalStateException(e);
}
DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
LSSerializer writer = impl.createLSSerializer();
String fileAsString = writer.writeToString(document);
// Replace default XML header with HTML DOCTYPE
fileAsString = fileAsString.replaceAll("<\\?xml[^>]+>", "<!DOCTYPE HTML>");
Files.write(destFile, Collections.singleton(fileAsString), StandardCharsets.UTF_8);
}
use of org.w3c.dom.ls.LSSerializer in project CloudStack-archive by CloudStack-extras.
the class VsmResponse method printResponse.
// Helper routine to check for the response received.
protected void printResponse() {
try {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
DOMImplementationLS ls = (DOMImplementationLS) docBuilder.getDOMImplementation();
LSSerializer lss = ls.createLSSerializer();
System.out.println(lss.writeToString(_docResponse));
} catch (ParserConfigurationException e) {
s_logger.error("Error parsing the repsonse : " + e.toString());
}
}
use of org.w3c.dom.ls.LSSerializer in project tdme by andreasdr.
the class GUIParser method getInnerXml.
/**
* Get inner XML
* see: http://stackoverflow.com/questions/3300839/get-a-nodes-inner-xml-as-string-in-java-dom
* @param node
* @return string
*/
private static String getInnerXml(Node node) {
DOMImplementationLS lsImpl = (DOMImplementationLS) node.getOwnerDocument().getImplementation().getFeature("LS", "3.0");
LSSerializer lsSerializer = lsImpl.createLSSerializer();
NodeList childNodes = node.getChildNodes();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < childNodes.getLength(); i++) {
sb.append(lsSerializer.writeToString(childNodes.item(i)));
}
String result = sb.toString();
result = result.replace("<?xml version=\"1.0\" encoding=\"UTF-16\"?>", "");
return result;
}
use of org.w3c.dom.ls.LSSerializer in project cloudstack by apache.
the class VsmResponse method printResponse.
// Helper routine to check for the response received.
protected void printResponse() {
try {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
DOMImplementationLS ls = (DOMImplementationLS) docBuilder.getDOMImplementation();
LSSerializer lss = ls.createLSSerializer();
System.out.println(lss.writeToString(_docResponse));
} catch (ParserConfigurationException e) {
s_logger.error("Error parsing the repsonse : " + e.toString());
}
}
Aggregations