Search in sources :

Example 1 with DOMImplementationLS

use of org.w3c.dom.ls.DOMImplementationLS 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 2 with DOMImplementationLS

use of org.w3c.dom.ls.DOMImplementationLS 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 3 with DOMImplementationLS

use of org.w3c.dom.ls.DOMImplementationLS 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 4 with DOMImplementationLS

use of org.w3c.dom.ls.DOMImplementationLS 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 5 with DOMImplementationLS

use of org.w3c.dom.ls.DOMImplementationLS in project android by JetBrains.

the class ThemePreviewBuilder method printDebug.

private static void printDebug(@NotNull PrintStream out, @NotNull Document document) {
    try {
        DOMImplementationRegistry reg = DOMImplementationRegistry.newInstance();
        DOMImplementationLS impl = (DOMImplementationLS) reg.getDOMImplementation("LS");
        LSSerializer serializer = impl.createLSSerializer();
        LSOutput lsOutput = impl.createLSOutput();
        lsOutput.setEncoding("UTF-8");
        lsOutput.setByteStream(out);
        serializer.write(document, lsOutput);
    } catch (ClassNotFoundException e) {
        e.printStackTrace(out);
    } catch (InstantiationException e) {
        e.printStackTrace(out);
    } catch (IllegalAccessException e) {
        e.printStackTrace(out);
    }
}
Also used : DOMImplementationLS(org.w3c.dom.ls.DOMImplementationLS) DOMImplementationRegistry(org.w3c.dom.bootstrap.DOMImplementationRegistry) LSSerializer(org.w3c.dom.ls.LSSerializer) LSOutput(org.w3c.dom.ls.LSOutput)

Aggregations

DOMImplementationLS (org.w3c.dom.ls.DOMImplementationLS)53 LSSerializer (org.w3c.dom.ls.LSSerializer)42 Document (org.w3c.dom.Document)22 DOMImplementationRegistry (org.w3c.dom.bootstrap.DOMImplementationRegistry)21 LSOutput (org.w3c.dom.ls.LSOutput)18 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)15 IOException (java.io.IOException)13 DocumentBuilder (javax.xml.parsers.DocumentBuilder)13 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)13 Node (org.w3c.dom.Node)12 DOMImplementation (org.w3c.dom.DOMImplementation)10 StringWriter (java.io.StringWriter)9 Element (org.w3c.dom.Element)9 LSParser (org.w3c.dom.ls.LSParser)8 InputSource (org.xml.sax.InputSource)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 StringReader (java.io.StringReader)5 TransformerException (javax.xml.transform.TransformerException)5 NodeList (org.w3c.dom.NodeList)5 LSInput (org.w3c.dom.ls.LSInput)5