Search in sources :

Example 6 with LSException

use of org.w3c.dom.ls.LSException in project ACS by ACS-Community.

the class HibernateWDALImpl method initializeParser.

private void initializeParser() throws RuntimeException {
    try {
        System.setProperty(DOMImplementationRegistry.PROPERTY, "org.apache.xerces.dom.DOMImplementationSourceImpl");
        DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
        domImplementationLS = (DOMImplementationLS) registry.getDOMImplementation("LS");
        parser = domImplementationLS.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, "http://www.w3.org/2001/XMLSchema");
        //parser.getDomConfig().setParameter("http://apache.org/xml/features/xinclude", Boolean.TRUE);
        schemaResourceResolverLoader = new SchemaResourceResolverLoader(null, domImplementationLS, m_logger);
        parser.getDomConfig().setParameter("resource-resolver", schemaResourceResolverLoader);
        parser.getDomConfig().setParameter("http://apache.org/xml/features/validation/schema", Boolean.TRUE);
        parser.getDomConfig().setParameter("error-handler", new DOMErrorHandler() {

            public boolean handleError(DOMError error) {
                //((Exception) error.getRelatedException()).printStackTrace();
                throw new LSException(LSException.PARSE_ERR, error.getMessage());
            }
        });
        input = domImplementationLS.createLSInput();
        output = domImplementationLS.createLSOutput();
        // SAX parser (non-validating)
        SAXParserFactory factory = SAXParserFactory.newInstance();
        saxParser = factory.newSAXParser();
    } catch (Throwable th) {
        throw new RuntimeException("Failed to initialize parser.", th);
    }
}
Also used : DOMErrorHandler(org.w3c.dom.DOMErrorHandler) DOMError(org.w3c.dom.DOMError) DOMImplementationRegistry(org.w3c.dom.bootstrap.DOMImplementationRegistry) LSException(org.w3c.dom.ls.LSException) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 7 with LSException

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

the class LSSerializerImpl method write.

/** 
     * Serializes the specified node to the specified LSOutput and returns true if the Node 
     * was successfully serialized. 
     * 
     * @see org.w3c.dom.ls.LSSerializer#write(org.w3c.dom.Node, org.w3c.dom.ls.LSOutput)
     * @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 write(Node nodeArg, LSOutput destination) throws LSException {
    // If the destination is null
    if (destination == 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);
    }
    // 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 = getXMLSerializer(xmlVersion);
    Serializer serializer = fXMLSerializer;
    serializer.reset();
    // If the node has not been seen
    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 = destination.getEncoding();
        if (fEncoding == null) {
            fEncoding = getInputEncoding(nodeArg);
            fEncoding = fEncoding != null ? fEncoding : getXMLEncoding(nodeArg) == null ? "UTF-8" : getXMLEncoding(nodeArg);
        }
        // Note: The serializer defaults to UTF-8 when created
        if (!Encodings.isRecognizedEncoding(fEncoding)) {
            String msg = Utils.messages.createMessage(MsgKey.ER_UNSUPPORTED_ENCODING, null);
            if (fDOMErrorHandler != null) {
                fDOMErrorHandler.handleError(new DOMErrorImpl(DOMError.SEVERITY_FATAL_ERROR, msg, MsgKey.ER_UNSUPPORTED_ENCODING));
            }
            throw new LSException(LSException.SERIALIZE_ERR, msg);
        }
        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 {
        // The LSSerializer will use the LSOutput object to determine 
        // where to serialize the output to in the following order the  
        // first one that is not null and not an empty string will be    
        // used: 1.LSOutput.characterStream, 2.LSOutput.byteStream,   
        // 3. LSOutput.systemId 
        // 1.LSOutput.characterStream
        Writer writer = destination.getCharacterStream();
        if (writer == null) {
            // 2.LSOutput.byteStream
            OutputStream outputStream = destination.getByteStream();
            if (outputStream == null) {
                // 3. LSOutput.systemId
                String uri = destination.getSystemId();
                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 {
                    // Expand the System Id and obtain an absolute URI for it.
                    String absoluteURI = SystemIDResolver.getAbsoluteURI(uri);
                    URL url = new URL(absoluteURI);
                    OutputStream urlOutStream = null;
                    String protocol = url.getProtocol();
                    String host = url.getHost();
                    // lower case in scheme names (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);
                }
            } else {
                // 2.LSOutput.byteStream
                serializer.setOutputStream(outputStream);
            }
        } else {
            // 1.LSOutput.characterStream
            serializer.setWriter(writer);
        }
        // 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 (UnsupportedEncodingException ue) {
        String msg = Utils.messages.createMessage(MsgKey.ER_UNSUPPORTED_ENCODING, null);
        if (fDOMErrorHandler != null) {
            fDOMErrorHandler.handleError(new DOMErrorImpl(DOMError.SEVERITY_FATAL_ERROR, msg, MsgKey.ER_UNSUPPORTED_ENCODING, ue));
        }
        throw (LSException) createLSException(LSException.SERIALIZE_ERR, ue).fillInStackTrace();
    } 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 : OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) UnsupportedEncodingException(java.io.UnsupportedEncodingException) 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) HttpURLConnection(java.net.HttpURLConnection) FileOutputStream(java.io.FileOutputStream) LSException(org.w3c.dom.ls.LSException) StringWriter(java.io.StringWriter) Writer(java.io.Writer) LSSerializer(org.w3c.dom.ls.LSSerializer) Serializer(org.apache.xml.serializer.Serializer) DOM3Serializer(org.apache.xml.serializer.DOM3Serializer)

Example 8 with LSException

use of org.w3c.dom.ls.LSException in project ddf by codice.

the class XPathHelper method print.

/**
     * Prints a given node as a String
     *
     * @param n
     *            - the node to print as a String
     * @param encoding
     *            - the character encoding to use for the returned String
     * @return the Node as a String, null if an exception is thrown or null is passed in.
     */
public static String print(Node n, String encoding) {
    if (n == null) {
        return null;
    }
    try {
        Document document = null;
        if (n instanceof Document) {
            document = (Document) n;
        } else {
            document = n.getOwnerDocument();
        }
        StringWriter stringOut = new StringWriter();
        DOMImplementationLS domImpl = (DOMImplementationLS) document.getImplementation();
        LSSerializer serializer = domImpl.createLSSerializer();
        LSOutput lsOut = domImpl.createLSOutput();
        lsOut.setEncoding(encoding);
        lsOut.setCharacterStream(stringOut);
        serializer.write(n, lsOut);
        return stringOut.toString();
    } catch (DOMException | LSException e) {
        LOGGER.debug(e.getMessage(), e);
    }
    return null;
}
Also used : DOMException(org.w3c.dom.DOMException) StringWriter(java.io.StringWriter) DOMImplementationLS(org.w3c.dom.ls.DOMImplementationLS) LSSerializer(org.w3c.dom.ls.LSSerializer) Document(org.w3c.dom.Document) LSOutput(org.w3c.dom.ls.LSOutput) LSException(org.w3c.dom.ls.LSException)

Aggregations

LSException (org.w3c.dom.ls.LSException)8 DOMException (org.w3c.dom.DOMException)7 LSSerializer (org.w3c.dom.ls.LSSerializer)7 UnsupportedEncodingException (java.io.UnsupportedEncodingException)6 DOM3Serializer (org.apache.xml.serializer.DOM3Serializer)6 Serializer (org.apache.xml.serializer.Serializer)6 StringWriter (java.io.StringWriter)5 FileOutputStream (java.io.FileOutputStream)4 OutputStream (java.io.OutputStream)4 HttpURLConnection (java.net.HttpURLConnection)4 URL (java.net.URL)4 URLConnection (java.net.URLConnection)4 Writer (java.io.Writer)2 SAXParserFactory (javax.xml.parsers.SAXParserFactory)1 DOMError (org.w3c.dom.DOMError)1 DOMErrorHandler (org.w3c.dom.DOMErrorHandler)1 Document (org.w3c.dom.Document)1 DOMImplementationRegistry (org.w3c.dom.bootstrap.DOMImplementationRegistry)1 DOMImplementationLS (org.w3c.dom.ls.DOMImplementationLS)1 LSOutput (org.w3c.dom.ls.LSOutput)1