Search in sources :

Example 31 with DOMImplementationLS

use of org.w3c.dom.ls.DOMImplementationLS in project latexdraw by arnobl.

the class SVGDocument method saveSVGDocument.

/**
 * Serialise the given SVG document.
 * @param path The file of the future serialised document.
 * @return True: the document has been successfully saved.
 * @since 2.0
 */
public boolean saveSVGDocument(final String path) {
    if (path == null)
        return false;
    boolean ok = true;
    try {
        // $NON-NLS-1$
        final DOMImplementationLS impl = (DOMImplementationLS) DOMImplementationRegistry.newInstance().getDOMImplementation("XML 3.0 LS 3.0");
        final LSSerializer serializer = impl.createLSSerializer();
        // $NON-NLS-1$
        serializer.getDomConfig().setParameter("format-pretty-print", true);
        // $NON-NLS-1$
        serializer.getDomConfig().setParameter("namespaces", false);
        final LSOutput output = impl.createLSOutput();
        final Charset charset = Charset.defaultCharset();
        try (final OutputStreamWriter fw = new OutputStreamWriter(new FileOutputStream(path), charset.newEncoder())) {
            output.setEncoding(charset.name());
            output.setCharacterStream(fw);
            serializer.write(getDocumentElement(), output);
        }
    } catch (final ClassNotFoundException | InstantiationException | IllegalAccessException | ClassCastException | IOException ex) {
        BadaboomCollector.INSTANCE.add(ex);
        ok = false;
    }
    return ok;
}
Also used : DOMImplementationLS(org.w3c.dom.ls.DOMImplementationLS) LSSerializer(org.w3c.dom.ls.LSSerializer) Charset(java.nio.charset.Charset) IOException(java.io.IOException) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) LSOutput(org.w3c.dom.ls.LSOutput)

Example 32 with DOMImplementationLS

use of org.w3c.dom.ls.DOMImplementationLS in project webtools.sourceediting by eclipse.

the class AbstractPsychoPathWTPTest method formatResultString.

protected String formatResultString(String resultFile) throws Exception {
    DOMLoader domloader = new XercesLoader(null);
    domloader.set_validating(false);
    InputStream is = bundle.getEntry(resultFile).openStream();
    Document resultDoc = domloader.load(is);
    DOMImplementationLS domLS = (DOMImplementationLS) resultDoc.getImplementation().getFeature("LS", "3.0");
    LSSerializer serializer = domLS.createLSSerializer();
    String actual = serializer.writeToString(resultDoc.getDocumentElement());
    actual = actual.replace("<?xml version=\"1.0\" encoding=\"UTF-16\"?>", "");
    return actual.trim();
}
Also used : DOMLoader(org.eclipse.wst.xml.xpath2.processor.DOMLoader) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) DOMImplementationLS(org.w3c.dom.ls.DOMImplementationLS) LSSerializer(org.w3c.dom.ls.LSSerializer) XercesLoader(org.eclipse.wst.xml.xpath2.processor.XercesLoader) Document(org.w3c.dom.Document)

Example 33 with DOMImplementationLS

use of org.w3c.dom.ls.DOMImplementationLS in project webtools.sourceediting by eclipse.

the class AbstractPsychoPathTest method buildXMLResultString.

protected String buildXMLResultString(ResultSequence rs) throws Exception {
    DOMImplementationLS domLS = (DOMImplementationLS) domDoc.getImplementation().getFeature("LS", "3.0");
    LSOutput outputText = domLS.createLSOutput();
    LSSerializer serializer = null;
    ClassLoader originalLoader = Thread.currentThread().getContextClassLoader();
    DelegatingLoader newContext = new DelegatingLoader(originalLoader);
    Thread.currentThread().setContextClassLoader(newContext);
    try {
        serializer = domLS.createLSSerializer();
    } finally {
        Thread.currentThread().setContextClassLoader(originalLoader);
    }
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    outputText.setByteStream(outputStream);
    String actual = new String();
    Iterator iterator = rs.iterator();
    boolean queueSpace = false;
    while (iterator.hasNext()) {
        AnyType aat = (AnyType) iterator.next();
        if (aat instanceof NodeType) {
            NodeType nodeType = (NodeType) aat;
            Node node = nodeType.node_value();
            serializer.write(node, outputText);
            queueSpace = false;
        } else {
            if (queueSpace)
                outputText.getByteStream().write(32);
            outputText.getByteStream().write(aat.getStringValue().getBytes("UTF-8"));
            queueSpace = true;
        }
    }
    actual = outputStream.toString("UTF-8");
    actual = actual.replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "");
    actual = actual.replace("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>", "");
    outputStream.close();
    return actual.trim();
}
Also used : DOMImplementationLS(org.w3c.dom.ls.DOMImplementationLS) NodeType(org.eclipse.wst.xml.xpath2.processor.internal.types.NodeType) Node(org.w3c.dom.Node) Iterator(java.util.Iterator) LSSerializer(org.w3c.dom.ls.LSSerializer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) LSOutput(org.w3c.dom.ls.LSOutput) AnyType(org.eclipse.wst.xml.xpath2.processor.internal.types.AnyType)

Example 34 with DOMImplementationLS

use of org.w3c.dom.ls.DOMImplementationLS in project assertj-core by joel-costigliola.

the class XmlStringPrettyFormatter method prettyFormat.

private static String prettyFormat(Document document, boolean keepXmlDeclaration) {
    try {
        DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
        DOMImplementationLS domImplementation = (DOMImplementationLS) registry.getDOMImplementation("LS");
        Writer stringWriter = new StringWriter();
        LSOutput formattedOutput = domImplementation.createLSOutput();
        formattedOutput.setCharacterStream(stringWriter);
        LSSerializer domSerializer = domImplementation.createLSSerializer();
        domSerializer.getDomConfig().setParameter("format-pretty-print", true);
        // Set this to true if the declaration is needed to be in the output.
        domSerializer.getDomConfig().setParameter("xml-declaration", keepXmlDeclaration);
        domSerializer.write(document, formattedOutput);
        return stringWriter.toString();
    } catch (Exception e) {
        throw new RuntimeException(FORMAT_ERROR, e);
    }
}
Also used : StringWriter(java.io.StringWriter) DOMImplementationLS(org.w3c.dom.ls.DOMImplementationLS) DOMImplementationRegistry(org.w3c.dom.bootstrap.DOMImplementationRegistry) LSSerializer(org.w3c.dom.ls.LSSerializer) LSOutput(org.w3c.dom.ls.LSOutput) StringWriter(java.io.StringWriter) Writer(java.io.Writer)

Example 35 with DOMImplementationLS

use of org.w3c.dom.ls.DOMImplementationLS in project syncope by apache.

the class MavenUtils method createSettingsWithProxy.

public static File createSettingsWithProxy(final String path, final String proxyHost, final String proxyPort, final String proxyUser, final String proxyPassword) throws Exception {
    final File settingsXML = new File(System.getProperty(MAVEN_HOME_PROPERTY) + (System.getProperty(MAVEN_HOME_PROPERTY).endsWith("/") ? "conf/settings.xml" : "/conf/settings.xml"));
    final File tempSettingsXML = new File(path + (path.endsWith("/") ? "settings_temp.xml" : "/settings_temp.xml"));
    if (settingsXML.canRead() && !tempSettingsXML.exists()) {
        tempSettingsXML.createNewFile();
        DOMImplementationRegistry reg = DOMImplementationRegistry.newInstance();
        DOMImplementationLS domImpl = (DOMImplementationLS) reg.getDOMImplementation("LS");
        LSInput lsinput = domImpl.createLSInput();
        lsinput.setByteStream(new FileInputStream(settingsXML));
        LSParser parser = domImpl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);
        // parse settings.xml
        final Document settings = parser.parse(lsinput);
        final Element proxies = (Element) settings.getDocumentElement().getElementsByTagName("proxies").item(0);
        final Element proxy = settings.createElement("proxy");
        final Element id = settings.createElement("id");
        final Element active = settings.createElement("active");
        final Element protocol = settings.createElement("protocol");
        final Element host = settings.createElement("host");
        final Element port = settings.createElement("port");
        final Element nonProxyHosts = settings.createElement("nonProxyHosts");
        id.appendChild(settings.createTextNode("optional"));
        active.appendChild(settings.createTextNode("true"));
        protocol.appendChild(settings.createTextNode("http"));
        host.appendChild(settings.createTextNode(proxyHost));
        port.appendChild(settings.createTextNode(proxyPort));
        proxy.appendChild(id);
        proxy.appendChild(active);
        proxy.appendChild(protocol);
        // create username and password tags only if required
        if (proxyUser != null && !proxyUser.isEmpty() && proxyPassword != null) {
            final Element username = settings.createElement("username");
            final Element password = settings.createElement("password");
            username.appendChild(settings.createTextNode(proxyUser));
            password.appendChild(settings.createTextNode(proxyPassword));
            proxy.appendChild(username);
            proxy.appendChild(password);
        }
        proxy.appendChild(host);
        proxy.appendChild(port);
        proxy.appendChild(nonProxyHosts);
        proxies.appendChild(proxy);
        FileSystemUtils.writeXML(settings, Files.newOutputStream(tempSettingsXML.toPath()));
    }
    return tempSettingsXML;
}
Also used : DOMImplementationLS(org.w3c.dom.ls.DOMImplementationLS) LSInput(org.w3c.dom.ls.LSInput) Element(org.w3c.dom.Element) DOMImplementationRegistry(org.w3c.dom.bootstrap.DOMImplementationRegistry) LSParser(org.w3c.dom.ls.LSParser) Document(org.w3c.dom.Document) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

DOMImplementationLS (org.w3c.dom.ls.DOMImplementationLS)54 LSSerializer (org.w3c.dom.ls.LSSerializer)43 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)14 IOException (java.io.IOException)13 DocumentBuilder (javax.xml.parsers.DocumentBuilder)13 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)12 Node (org.w3c.dom.Node)12 DOMImplementation (org.w3c.dom.DOMImplementation)10 Element (org.w3c.dom.Element)10 StringWriter (java.io.StringWriter)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