Search in sources :

Example 6 with LSSerializer

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);
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) DOMImplementationLS(org.w3c.dom.ls.DOMImplementationLS) LSSerializer(org.w3c.dom.ls.LSSerializer) Document(org.w3c.dom.Document) IOException(java.io.IOException)

Example 7 with LSSerializer

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);
}
Also used : Path(java.nio.file.Path) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) DOMImplementationLS(org.w3c.dom.ls.DOMImplementationLS) LSSerializer(org.w3c.dom.ls.LSSerializer) IOException(java.io.IOException) Document(org.w3c.dom.Document) LinkedList(java.util.LinkedList) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) DOMImplementationRegistry(org.w3c.dom.bootstrap.DOMImplementationRegistry) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 8 with LSSerializer

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());
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) DOMImplementationLS(org.w3c.dom.ls.DOMImplementationLS) LSSerializer(org.w3c.dom.ls.LSSerializer) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 9 with LSSerializer

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;
}
Also used : DOMImplementationLS(org.w3c.dom.ls.DOMImplementationLS) NodeList(org.w3c.dom.NodeList) LSSerializer(org.w3c.dom.ls.LSSerializer) MutableString(net.drewke.tdme.utils.MutableString)

Example 10 with LSSerializer

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());
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) DOMImplementationLS(org.w3c.dom.ls.DOMImplementationLS) LSSerializer(org.w3c.dom.ls.LSSerializer) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Aggregations

LSSerializer (org.w3c.dom.ls.LSSerializer)25 DOMImplementationLS (org.w3c.dom.ls.DOMImplementationLS)18 DOMException (org.w3c.dom.DOMException)8 StringWriter (java.io.StringWriter)7 DocumentBuilder (javax.xml.parsers.DocumentBuilder)7 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)7 Document (org.w3c.dom.Document)7 LSException (org.w3c.dom.ls.LSException)7 UnsupportedEncodingException (java.io.UnsupportedEncodingException)6 DOM3Serializer (org.apache.xml.serializer.DOM3Serializer)6 Serializer (org.apache.xml.serializer.Serializer)6 LSOutput (org.w3c.dom.ls.LSOutput)5 FileOutputStream (java.io.FileOutputStream)4 IOException (java.io.IOException)4 OutputStream (java.io.OutputStream)4 HttpURLConnection (java.net.HttpURLConnection)4 URL (java.net.URL)4 URLConnection (java.net.URLConnection)4 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)4 Element (org.w3c.dom.Element)4