Search in sources :

Example 1 with LSSerializer

use of org.w3c.dom.ls.LSSerializer in project robovm by robovm.

the class LSSerializerImpl method writeToString.

/** 
     * Serializes the specified node and returns a String with the serialized
     * data to the caller.  
     * 
     * @see org.w3c.dom.ls.LSSerializer#writeToString(org.w3c.dom.Node)
     * @since DOM Level 3
     * @param nodeArg The Node to serialize.
     * @throws org.w3c.dom.ls.LSException SERIALIZE_ERR: Raised if the 
     * LSSerializer was unable to serialize the node.
     *      
     */
public String writeToString(Node nodeArg) throws DOMException, LSException {
    // return null is nodeArg is null.  Should an Exception be thrown instead?
    if (nodeArg == null) {
        return null;
    }
    // Should we reset the serializer configuration before each write operation?
    // Obtain a reference to the serializer to use
    Serializer serializer = fXMLSerializer;
    serializer.reset();
    if (nodeArg != fVisitedNode) {
        // Determine the XML Document version of the Node 
        String xmlVersion = getXMLVersion(nodeArg);
        serializer.getOutputFormat().setProperty("version", xmlVersion);
        // Set the output encoding and xml version properties
        fDOMConfigProperties.setProperty(DOMConstants.S_XERCES_PROPERTIES_NS + DOMConstants.S_XML_VERSION, xmlVersion);
        fDOMConfigProperties.setProperty(DOMConstants.S_XSL_OUTPUT_ENCODING, "UTF-16");
        // serialized.
        if ((nodeArg.getNodeType() != Node.DOCUMENT_NODE || nodeArg.getNodeType() != Node.ELEMENT_NODE || nodeArg.getNodeType() != Node.ENTITY_NODE) && ((fFeatures & XMLDECL) != 0)) {
            fDOMConfigProperties.setProperty(DOMConstants.S_XSL_OUTPUT_OMIT_XML_DECL, DOMConstants.DOM3_DEFAULT_FALSE);
        }
        fVisitedNode = nodeArg;
    }
    // Update the serializer properties
    fXMLSerializer.setOutputFormat(fDOMConfigProperties);
    // StringWriter to Output to
    StringWriter output = new StringWriter();
    // 
    try {
        // Set the Serializer's Writer to a StringWriter
        serializer.setWriter(output);
        // Use this hack till Xalan support JAXP1.3
        if (fDOMSerializer == null) {
            fDOMSerializer = (DOM3Serializer) serializer.asDOM3Serializer();
        }
        // Set the error handler on the DOM3Serializer interface implementation
        if (fDOMErrorHandler != null) {
            fDOMSerializer.setErrorHandler(fDOMErrorHandler);
        }
        // Set the filter on the DOM3Serializer interface implementation
        if (fSerializerFilter != null) {
            fDOMSerializer.setNodeFilter(fSerializerFilter);
        }
        // Set the NewLine character to be used
        fDOMSerializer.setNewLine(fEndOfLine.toCharArray());
        // Serializer your DOM, where node is an org.w3c.dom.Node
        fDOMSerializer.serializeDOM3(nodeArg);
    } catch (LSException lse) {
        // Rethrow LSException.
        throw lse;
    } catch (RuntimeException e) {
        throw (LSException) createLSException(LSException.SERIALIZE_ERR, e).fillInStackTrace();
    } catch (Exception e) {
        if (fDOMErrorHandler != null) {
            fDOMErrorHandler.handleError(new DOMErrorImpl(DOMError.SEVERITY_FATAL_ERROR, e.getMessage(), null, e));
        }
        throw (LSException) createLSException(LSException.SERIALIZE_ERR, e).fillInStackTrace();
    }
    // return the serialized string
    return output.toString();
}
Also used : StringWriter(java.io.StringWriter) LSException(org.w3c.dom.ls.LSException) LSException(org.w3c.dom.ls.LSException) DOMException(org.w3c.dom.DOMException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) LSSerializer(org.w3c.dom.ls.LSSerializer) Serializer(org.apache.xml.serializer.Serializer) DOM3Serializer(org.apache.xml.serializer.DOM3Serializer)

Example 2 with LSSerializer

use of org.w3c.dom.ls.LSSerializer in project robovm by robovm.

the class LSSerializerImpl method writeToURI.

/** 
     * Serializes the specified node to the specified URI and returns true if the Node 
     * was successfully serialized. 
     * 
     * @see org.w3c.dom.ls.LSSerializer#writeToURI(org.w3c.dom.Node, String)
     * @since DOM Level 3
     * @param nodeArg The Node to serialize.
     * @throws org.w3c.dom.ls.LSException SERIALIZE_ERR: Raised if the 
     * LSSerializer was unable to serialize the node.
     *      
     */
public boolean writeToURI(Node nodeArg, String uri) throws LSException {
    // If nodeArg is null, return false.  Should we throw and LSException instead?
    if (nodeArg == null) {
        return false;
    }
    // Obtain a reference to the serializer to use
    Serializer serializer = fXMLSerializer;
    serializer.reset();
    if (nodeArg != fVisitedNode) {
        // Determine the XML Document version of the Node 
        String xmlVersion = getXMLVersion(nodeArg);
        // Determine the encoding: 1.LSOutput.encoding,
        // 2.Document.inputEncoding, 3.Document.xmlEncoding.
        fEncoding = getInputEncoding(nodeArg);
        if (fEncoding == null) {
            fEncoding = fEncoding != null ? fEncoding : getXMLEncoding(nodeArg) == null ? "UTF-8" : getXMLEncoding(nodeArg);
        }
        serializer.getOutputFormat().setProperty("version", xmlVersion);
        // Set the output encoding and xml version properties
        fDOMConfigProperties.setProperty(DOMConstants.S_XERCES_PROPERTIES_NS + DOMConstants.S_XML_VERSION, xmlVersion);
        fDOMConfigProperties.setProperty(DOMConstants.S_XSL_OUTPUT_ENCODING, fEncoding);
        // serialized.
        if ((nodeArg.getNodeType() != Node.DOCUMENT_NODE || nodeArg.getNodeType() != Node.ELEMENT_NODE || nodeArg.getNodeType() != Node.ENTITY_NODE) && ((fFeatures & XMLDECL) != 0)) {
            fDOMConfigProperties.setProperty(DOMConstants.S_XSL_OUTPUT_OMIT_XML_DECL, DOMConstants.DOM3_DEFAULT_FALSE);
        }
        fVisitedNode = nodeArg;
    }
    // Update the serializer properties
    fXMLSerializer.setOutputFormat(fDOMConfigProperties);
    // 
    try {
        // "unsupported-encoding" fatal error is raised. ??
        if (uri == null) {
            String msg = Utils.messages.createMessage(MsgKey.ER_NO_OUTPUT_SPECIFIED, null);
            if (fDOMErrorHandler != null) {
                fDOMErrorHandler.handleError(new DOMErrorImpl(DOMError.SEVERITY_FATAL_ERROR, msg, MsgKey.ER_NO_OUTPUT_SPECIFIED));
            }
            throw new LSException(LSException.SERIALIZE_ERR, msg);
        } else {
            // REVISIT: Can this be used to get an absolute expanded URI
            String absoluteURI = SystemIDResolver.getAbsoluteURI(uri);
            URL url = new URL(absoluteURI);
            OutputStream urlOutStream = null;
            String protocol = url.getProtocol();
            String host = url.getHost();
            // (e.g., allow "HTTP" as well as "http").
            if (protocol.equalsIgnoreCase("file") && (host == null || host.length() == 0 || host.equals("localhost"))) {
                // do we also need to check for host.equals(hostname)
                urlOutStream = new FileOutputStream(getPathWithoutEscapes(url.getPath()));
            } else {
                // This should support URL's whose schemes are mentioned in
                // RFC1738 other than file
                URLConnection urlCon = url.openConnection();
                urlCon.setDoInput(false);
                urlCon.setDoOutput(true);
                urlCon.setUseCaches(false);
                urlCon.setAllowUserInteraction(false);
                // When writing to a HTTP URI, a HTTP PUT is performed.
                if (urlCon instanceof HttpURLConnection) {
                    HttpURLConnection httpCon = (HttpURLConnection) urlCon;
                    httpCon.setRequestMethod("PUT");
                }
                urlOutStream = urlCon.getOutputStream();
            }
            // set the OutputStream to that obtained from the systemId
            serializer.setOutputStream(urlOutStream);
        }
        // Use this hack till Xalan support JAXP1.3
        if (fDOMSerializer == null) {
            fDOMSerializer = (DOM3Serializer) serializer.asDOM3Serializer();
        }
        // Set the error handler on the DOM3Serializer interface implementation
        if (fDOMErrorHandler != null) {
            fDOMSerializer.setErrorHandler(fDOMErrorHandler);
        }
        // Set the filter on the DOM3Serializer interface implementation
        if (fSerializerFilter != null) {
            fDOMSerializer.setNodeFilter(fSerializerFilter);
        }
        // Set the NewLine character to be used
        fDOMSerializer.setNewLine(fEndOfLine.toCharArray());
        // Serializer your DOM, where node is an org.w3c.dom.Node
        // Assuming that Xalan's serializer can serialize any type of DOM
        // node
        fDOMSerializer.serializeDOM3(nodeArg);
    } catch (LSException lse) {
        // Rethrow LSException.
        throw lse;
    } catch (RuntimeException e) {
        throw (LSException) createLSException(LSException.SERIALIZE_ERR, e).fillInStackTrace();
    } catch (Exception e) {
        if (fDOMErrorHandler != null) {
            fDOMErrorHandler.handleError(new DOMErrorImpl(DOMError.SEVERITY_FATAL_ERROR, e.getMessage(), null, e));
        }
        throw (LSException) createLSException(LSException.SERIALIZE_ERR, e).fillInStackTrace();
    }
    return true;
}
Also used : HttpURLConnection(java.net.HttpURLConnection) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) LSException(org.w3c.dom.ls.LSException) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) LSException(org.w3c.dom.ls.LSException) DOMException(org.w3c.dom.DOMException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) LSSerializer(org.w3c.dom.ls.LSSerializer) Serializer(org.apache.xml.serializer.Serializer) DOM3Serializer(org.apache.xml.serializer.DOM3Serializer)

Example 3 with LSSerializer

use of org.w3c.dom.ls.LSSerializer in project camel by apache.

the class XmlSignatureHelper method transformNonTextNodeToOutputStream.

/**
     * Serializes a node using a certain character encoding.
     * 
     * @param node
     *            DOM node to serialize
     * @param os
     *            output stream, to which the node is serialized
     * @param omitXmlDeclaration
     *            indicator whether to omit the XML declaration or not
     * @param encoding
     *            character encoding, can be <code>null</code>, if
     *            <code>null</code> then "UTF-8" is used
     * @throws Exception
     */
public static void transformNonTextNodeToOutputStream(Node node, OutputStream os, boolean omitXmlDeclaration, String encoding) throws Exception {
    // therefore we switched to DOMImplementationLS
    if (encoding == null) {
        encoding = "UTF-8";
    }
    DOMImplementationRegistry domImplementationRegistry = DOMImplementationRegistry.newInstance();
    DOMImplementationLS domImplementationLS = (DOMImplementationLS) domImplementationRegistry.getDOMImplementation("LS");
    LSOutput lsOutput = domImplementationLS.createLSOutput();
    lsOutput.setEncoding(encoding);
    lsOutput.setByteStream(os);
    LSSerializer lss = domImplementationLS.createLSSerializer();
    lss.getDomConfig().setParameter("xml-declaration", !omitXmlDeclaration);
    lss.write(node, lsOutput);
}
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)

Example 4 with LSSerializer

use of org.w3c.dom.ls.LSSerializer in project j2objc by google.

the class LSSerializerImpl method writeToString.

/** 
     * Serializes the specified node and returns a String with the serialized
     * data to the caller.  
     * 
     * @see org.w3c.dom.ls.LSSerializer#writeToString(org.w3c.dom.Node)
     * @since DOM Level 3
     * @param nodeArg The Node to serialize.
     * @throws org.w3c.dom.ls.LSException SERIALIZE_ERR: Raised if the 
     * LSSerializer was unable to serialize the node.
     *      
     */
public String writeToString(Node nodeArg) throws DOMException, LSException {
    // return null is nodeArg is null.  Should an Exception be thrown instead?
    if (nodeArg == null) {
        return null;
    }
    // Should we reset the serializer configuration before each write operation?
    // Obtain a reference to the serializer to use
    Serializer serializer = fXMLSerializer;
    serializer.reset();
    if (nodeArg != fVisitedNode) {
        // Determine the XML Document version of the Node 
        String xmlVersion = getXMLVersion(nodeArg);
        serializer.getOutputFormat().setProperty("version", xmlVersion);
        // Set the output encoding and xml version properties
        fDOMConfigProperties.setProperty(DOMConstants.S_XERCES_PROPERTIES_NS + DOMConstants.S_XML_VERSION, xmlVersion);
        fDOMConfigProperties.setProperty(DOMConstants.S_XSL_OUTPUT_ENCODING, "UTF-16");
        // serialized.
        if ((nodeArg.getNodeType() != Node.DOCUMENT_NODE || nodeArg.getNodeType() != Node.ELEMENT_NODE || nodeArg.getNodeType() != Node.ENTITY_NODE) && ((fFeatures & XMLDECL) != 0)) {
            fDOMConfigProperties.setProperty(DOMConstants.S_XSL_OUTPUT_OMIT_XML_DECL, DOMConstants.DOM3_DEFAULT_FALSE);
        }
        fVisitedNode = nodeArg;
    }
    // Update the serializer properties
    fXMLSerializer.setOutputFormat(fDOMConfigProperties);
    // StringWriter to Output to
    StringWriter output = new StringWriter();
    // 
    try {
        // Set the Serializer's Writer to a StringWriter
        serializer.setWriter(output);
        // Use this hack till Xalan support JAXP1.3
        if (fDOMSerializer == null) {
            fDOMSerializer = (DOM3Serializer) serializer.asDOM3Serializer();
        }
        // Set the error handler on the DOM3Serializer interface implementation
        if (fDOMErrorHandler != null) {
            fDOMSerializer.setErrorHandler(fDOMErrorHandler);
        }
        // Set the filter on the DOM3Serializer interface implementation
        if (fSerializerFilter != null) {
            fDOMSerializer.setNodeFilter(fSerializerFilter);
        }
        // Set the NewLine character to be used
        fDOMSerializer.setNewLine(fEndOfLine.toCharArray());
        // Serializer your DOM, where node is an org.w3c.dom.Node
        fDOMSerializer.serializeDOM3(nodeArg);
    } catch (LSException lse) {
        // Rethrow LSException.
        throw lse;
    } catch (RuntimeException e) {
        throw (LSException) createLSException(LSException.SERIALIZE_ERR, e).fillInStackTrace();
    } catch (Exception e) {
        if (fDOMErrorHandler != null) {
            fDOMErrorHandler.handleError(new DOMErrorImpl(DOMError.SEVERITY_FATAL_ERROR, e.getMessage(), null, e));
        }
        throw (LSException) createLSException(LSException.SERIALIZE_ERR, e).fillInStackTrace();
    }
    // return the serialized string
    return output.toString();
}
Also used : StringWriter(java.io.StringWriter) LSException(org.w3c.dom.ls.LSException) LSException(org.w3c.dom.ls.LSException) DOMException(org.w3c.dom.DOMException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) LSSerializer(org.w3c.dom.ls.LSSerializer) Serializer(org.apache.xml.serializer.Serializer) DOM3Serializer(org.apache.xml.serializer.DOM3Serializer)

Example 5 with LSSerializer

use of org.w3c.dom.ls.LSSerializer in project j2objc by google.

the class LSSerializerImpl method writeToURI.

/** 
     * Serializes the specified node to the specified URI and returns true if the Node 
     * was successfully serialized. 
     * 
     * @see org.w3c.dom.ls.LSSerializer#writeToURI(org.w3c.dom.Node, String)
     * @since DOM Level 3
     * @param nodeArg The Node to serialize.
     * @throws org.w3c.dom.ls.LSException SERIALIZE_ERR: Raised if the 
     * LSSerializer was unable to serialize the node.
     *      
     */
public boolean writeToURI(Node nodeArg, String uri) throws LSException {
    // If nodeArg is null, return false.  Should we throw and LSException instead?
    if (nodeArg == null) {
        return false;
    }
    // Obtain a reference to the serializer to use
    Serializer serializer = fXMLSerializer;
    serializer.reset();
    if (nodeArg != fVisitedNode) {
        // Determine the XML Document version of the Node 
        String xmlVersion = getXMLVersion(nodeArg);
        // Determine the encoding: 1.LSOutput.encoding,
        // 2.Document.inputEncoding, 3.Document.xmlEncoding.
        fEncoding = getInputEncoding(nodeArg);
        if (fEncoding == null) {
            fEncoding = fEncoding != null ? fEncoding : getXMLEncoding(nodeArg) == null ? "UTF-8" : getXMLEncoding(nodeArg);
        }
        serializer.getOutputFormat().setProperty("version", xmlVersion);
        // Set the output encoding and xml version properties
        fDOMConfigProperties.setProperty(DOMConstants.S_XERCES_PROPERTIES_NS + DOMConstants.S_XML_VERSION, xmlVersion);
        fDOMConfigProperties.setProperty(DOMConstants.S_XSL_OUTPUT_ENCODING, fEncoding);
        // serialized.
        if ((nodeArg.getNodeType() != Node.DOCUMENT_NODE || nodeArg.getNodeType() != Node.ELEMENT_NODE || nodeArg.getNodeType() != Node.ENTITY_NODE) && ((fFeatures & XMLDECL) != 0)) {
            fDOMConfigProperties.setProperty(DOMConstants.S_XSL_OUTPUT_OMIT_XML_DECL, DOMConstants.DOM3_DEFAULT_FALSE);
        }
        fVisitedNode = nodeArg;
    }
    // Update the serializer properties
    fXMLSerializer.setOutputFormat(fDOMConfigProperties);
    // 
    try {
        // "unsupported-encoding" fatal error is raised. ??
        if (uri == null) {
            String msg = Utils.messages.createMessage(MsgKey.ER_NO_OUTPUT_SPECIFIED, null);
            if (fDOMErrorHandler != null) {
                fDOMErrorHandler.handleError(new DOMErrorImpl(DOMError.SEVERITY_FATAL_ERROR, msg, MsgKey.ER_NO_OUTPUT_SPECIFIED));
            }
            throw new LSException(LSException.SERIALIZE_ERR, msg);
        } else {
            // REVISIT: Can this be used to get an absolute expanded URI
            String absoluteURI = SystemIDResolver.getAbsoluteURI(uri);
            URL url = new URL(absoluteURI);
            OutputStream urlOutStream = null;
            String protocol = url.getProtocol();
            String host = url.getHost();
            // (e.g., allow "HTTP" as well as "http").
            if (protocol.equalsIgnoreCase("file") && (host == null || host.length() == 0 || host.equals("localhost"))) {
                // do we also need to check for host.equals(hostname)
                urlOutStream = new FileOutputStream(getPathWithoutEscapes(url.getPath()));
            } else {
                // This should support URL's whose schemes are mentioned in
                // RFC1738 other than file
                URLConnection urlCon = url.openConnection();
                urlCon.setDoInput(false);
                urlCon.setDoOutput(true);
                urlCon.setUseCaches(false);
                urlCon.setAllowUserInteraction(false);
                // When writing to a HTTP URI, a HTTP PUT is performed.
                if (urlCon instanceof HttpURLConnection) {
                    HttpURLConnection httpCon = (HttpURLConnection) urlCon;
                    httpCon.setRequestMethod("PUT");
                }
                urlOutStream = urlCon.getOutputStream();
            }
            // set the OutputStream to that obtained from the systemId
            serializer.setOutputStream(urlOutStream);
        }
        // Use this hack till Xalan support JAXP1.3
        if (fDOMSerializer == null) {
            fDOMSerializer = (DOM3Serializer) serializer.asDOM3Serializer();
        }
        // Set the error handler on the DOM3Serializer interface implementation
        if (fDOMErrorHandler != null) {
            fDOMSerializer.setErrorHandler(fDOMErrorHandler);
        }
        // Set the filter on the DOM3Serializer interface implementation
        if (fSerializerFilter != null) {
            fDOMSerializer.setNodeFilter(fSerializerFilter);
        }
        // Set the NewLine character to be used
        fDOMSerializer.setNewLine(fEndOfLine.toCharArray());
        // Serializer your DOM, where node is an org.w3c.dom.Node
        // Assuming that Xalan's serializer can serialize any type of DOM
        // node
        fDOMSerializer.serializeDOM3(nodeArg);
    } catch (LSException lse) {
        // Rethrow LSException.
        throw lse;
    } catch (RuntimeException e) {
        throw (LSException) createLSException(LSException.SERIALIZE_ERR, e).fillInStackTrace();
    } catch (Exception e) {
        if (fDOMErrorHandler != null) {
            fDOMErrorHandler.handleError(new DOMErrorImpl(DOMError.SEVERITY_FATAL_ERROR, e.getMessage(), null, e));
        }
        throw (LSException) createLSException(LSException.SERIALIZE_ERR, e).fillInStackTrace();
    }
    return true;
}
Also used : HttpURLConnection(java.net.HttpURLConnection) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) LSException(org.w3c.dom.ls.LSException) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) LSException(org.w3c.dom.ls.LSException) DOMException(org.w3c.dom.DOMException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) LSSerializer(org.w3c.dom.ls.LSSerializer) Serializer(org.apache.xml.serializer.Serializer) DOM3Serializer(org.apache.xml.serializer.DOM3Serializer)

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