Search in sources :

Example 1 with DTDHandler

use of org.xml.sax.DTDHandler in project robovm by robovm.

the class TransformerIdentityImpl method createResultContentHandler.

/**
   * Create a result ContentHandler from a Result object, based
   * on the current OutputProperties.
   *
   * @param outputTarget Where the transform result should go,
   * should not be null.
   *
   * @return A valid ContentHandler that will create the
   * result tree when it is fed SAX events.
   *
   * @throws TransformerException
   */
private void createResultContentHandler(Result outputTarget) throws TransformerException {
    if (outputTarget instanceof SAXResult) {
        SAXResult saxResult = (SAXResult) outputTarget;
        m_resultContentHandler = saxResult.getHandler();
        m_resultLexicalHandler = saxResult.getLexicalHandler();
        if (m_resultContentHandler instanceof Serializer) {
            // Dubious but needed, I think.
            m_serializer = (Serializer) m_resultContentHandler;
        }
    } else if (outputTarget instanceof DOMResult) {
        DOMResult domResult = (DOMResult) outputTarget;
        Node outputNode = domResult.getNode();
        Node nextSibling = domResult.getNextSibling();
        Document doc;
        short type;
        if (null != outputNode) {
            type = outputNode.getNodeType();
            doc = (Node.DOCUMENT_NODE == type) ? (Document) outputNode : outputNode.getOwnerDocument();
        } else {
            try {
                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                dbf.setNamespaceAware(true);
                if (m_isSecureProcessing) {
                    try {
                        dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
                    } catch (ParserConfigurationException pce) {
                    }
                }
                DocumentBuilder db = dbf.newDocumentBuilder();
                doc = db.newDocument();
            } catch (ParserConfigurationException pce) {
                throw new TransformerException(pce);
            }
            outputNode = doc;
            type = outputNode.getNodeType();
            ((DOMResult) outputTarget).setNode(outputNode);
        }
        DOMBuilder domBuilder = (Node.DOCUMENT_FRAGMENT_NODE == type) ? new DOMBuilder(doc, (DocumentFragment) outputNode) : new DOMBuilder(doc, outputNode);
        if (nextSibling != null)
            domBuilder.setNextSibling(nextSibling);
        m_resultContentHandler = domBuilder;
        m_resultLexicalHandler = domBuilder;
    } else if (outputTarget instanceof StreamResult) {
        StreamResult sresult = (StreamResult) outputTarget;
        try {
            Serializer serializer = SerializerFactory.getSerializer(m_outputFormat.getProperties());
            m_serializer = serializer;
            if (null != sresult.getWriter())
                serializer.setWriter(sresult.getWriter());
            else if (null != sresult.getOutputStream())
                serializer.setOutputStream(sresult.getOutputStream());
            else if (null != sresult.getSystemId()) {
                String fileURL = sresult.getSystemId();
                if (fileURL.startsWith("file:///")) {
                    if (fileURL.substring(8).indexOf(":") > 0) {
                        fileURL = fileURL.substring(8);
                    } else {
                        fileURL = fileURL.substring(7);
                    }
                } else if (fileURL.startsWith("file:/")) {
                    if (fileURL.substring(6).indexOf(":") > 0) {
                        fileURL = fileURL.substring(6);
                    } else {
                        fileURL = fileURL.substring(5);
                    }
                }
                m_outputStream = new java.io.FileOutputStream(fileURL);
                serializer.setOutputStream(m_outputStream);
            } else
                //"No output specified!");
                throw new TransformerException(XSLMessages.createMessage(XSLTErrorResources.ER_NO_OUTPUT_SPECIFIED, null));
            m_resultContentHandler = serializer.asContentHandler();
        } catch (IOException ioe) {
            throw new TransformerException(ioe);
        }
    } else {
        //"Can't transform to a Result of type "
        throw new TransformerException(XSLMessages.createMessage(XSLTErrorResources.ER_CANNOT_TRANSFORM_TO_RESULT_TYPE, new Object[] { outputTarget.getClass().getName() }));
    // + outputTarget.getClass().getName()
    // + "!");
    }
    if (m_resultContentHandler instanceof DTDHandler)
        m_resultDTDHandler = (DTDHandler) m_resultContentHandler;
    if (m_resultContentHandler instanceof DeclHandler)
        m_resultDeclHandler = (DeclHandler) m_resultContentHandler;
    if (m_resultContentHandler instanceof LexicalHandler)
        m_resultLexicalHandler = (LexicalHandler) m_resultContentHandler;
}
Also used : DOMResult(javax.xml.transform.dom.DOMResult) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) StreamResult(javax.xml.transform.stream.StreamResult) Node(org.w3c.dom.Node) IOException(java.io.IOException) Document(org.w3c.dom.Document) DOMBuilder(org.apache.xml.utils.DOMBuilder) DeclHandler(org.xml.sax.ext.DeclHandler) SAXResult(javax.xml.transform.sax.SAXResult) DocumentBuilder(javax.xml.parsers.DocumentBuilder) LexicalHandler(org.xml.sax.ext.LexicalHandler) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) DTDHandler(org.xml.sax.DTDHandler) TransformerException(javax.xml.transform.TransformerException) Serializer(org.apache.xml.serializer.Serializer)

Example 2 with DTDHandler

use of org.xml.sax.DTDHandler in project j2objc by google.

the class TransformerIdentityImpl method startElement.

/**
   * Receive notification of the start of an element.
   *
   * <p>By default, do nothing.  Application writers may override this
   * method in a subclass to take specific actions at the start of
   * each element (such as allocating a new tree node or writing
   * output to a file).</p>
   *
   * @param uri The Namespace URI, or the empty string if the
   *        element has no Namespace URI or if Namespace
   *        processing is not being performed.
   * @param localName The local name (without prefix), or the
   *        empty string if Namespace processing is not being
   *        performed.
   * @param qName The qualified name (with prefix), or the
   *        empty string if qualified names are not available.
   * @param attributes The specified or defaulted attributes.
   * @throws org.xml.sax.SAXException Any SAX exception, possibly
   *            wrapping another exception.
   * @see org.xml.sax.ContentHandler#startElement
   *
   * @throws SAXException
   */
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
    if (!m_foundFirstElement && null != m_serializer) {
        m_foundFirstElement = true;
        Serializer newSerializer;
        try {
            newSerializer = SerializerSwitcher.switchSerializerIfHTML(uri, localName, m_outputFormat.getProperties(), m_serializer);
        } catch (TransformerException te) {
            throw new SAXException(te);
        }
        if (newSerializer != m_serializer) {
            try {
                m_resultContentHandler = newSerializer.asContentHandler();
            } catch (// why?
            IOException ioe) {
                throw new SAXException(ioe);
            }
            if (m_resultContentHandler instanceof DTDHandler)
                m_resultDTDHandler = (DTDHandler) m_resultContentHandler;
            if (m_resultContentHandler instanceof LexicalHandler)
                m_resultLexicalHandler = (LexicalHandler) m_resultContentHandler;
            m_serializer = newSerializer;
        }
    }
    flushStartDoc();
    m_resultContentHandler.startElement(uri, localName, qName, attributes);
}
Also used : LexicalHandler(org.xml.sax.ext.LexicalHandler) IOException(java.io.IOException) DTDHandler(org.xml.sax.DTDHandler) TransformerException(javax.xml.transform.TransformerException) Serializer(org.apache.xml.serializer.Serializer) SAXException(org.xml.sax.SAXException)

Example 3 with DTDHandler

use of org.xml.sax.DTDHandler in project j2objc by google.

the class TransformerIdentityImpl method createResultContentHandler.

/**
   * Create a result ContentHandler from a Result object, based
   * on the current OutputProperties.
   *
   * @param outputTarget Where the transform result should go,
   * should not be null.
   *
   * @return A valid ContentHandler that will create the
   * result tree when it is fed SAX events.
   *
   * @throws TransformerException
   */
private void createResultContentHandler(Result outputTarget) throws TransformerException {
    if (outputTarget instanceof SAXResult) {
        SAXResult saxResult = (SAXResult) outputTarget;
        m_resultContentHandler = saxResult.getHandler();
        m_resultLexicalHandler = saxResult.getLexicalHandler();
        if (m_resultContentHandler instanceof Serializer) {
            // Dubious but needed, I think.
            m_serializer = (Serializer) m_resultContentHandler;
        }
    } else if (outputTarget instanceof DOMResult) {
        DOMResult domResult = (DOMResult) outputTarget;
        Node outputNode = domResult.getNode();
        Node nextSibling = domResult.getNextSibling();
        Document doc;
        short type;
        if (null != outputNode) {
            type = outputNode.getNodeType();
            doc = (Node.DOCUMENT_NODE == type) ? (Document) outputNode : outputNode.getOwnerDocument();
        } else {
            try {
                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                dbf.setNamespaceAware(true);
                if (m_isSecureProcessing) {
                    try {
                        dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
                    } catch (ParserConfigurationException pce) {
                    }
                }
                DocumentBuilder db = dbf.newDocumentBuilder();
                doc = db.newDocument();
            } catch (ParserConfigurationException pce) {
                throw new TransformerException(pce);
            }
            outputNode = doc;
            type = outputNode.getNodeType();
            ((DOMResult) outputTarget).setNode(outputNode);
        }
        DOMBuilder domBuilder = (Node.DOCUMENT_FRAGMENT_NODE == type) ? new DOMBuilder(doc, (DocumentFragment) outputNode) : new DOMBuilder(doc, outputNode);
        if (nextSibling != null)
            domBuilder.setNextSibling(nextSibling);
        m_resultContentHandler = domBuilder;
        m_resultLexicalHandler = domBuilder;
    } else if (outputTarget instanceof StreamResult) {
        StreamResult sresult = (StreamResult) outputTarget;
        try {
            Serializer serializer = SerializerFactory.getSerializer(m_outputFormat.getProperties());
            m_serializer = serializer;
            if (null != sresult.getWriter())
                serializer.setWriter(sresult.getWriter());
            else if (null != sresult.getOutputStream())
                serializer.setOutputStream(sresult.getOutputStream());
            else if (null != sresult.getSystemId()) {
                String fileURL = sresult.getSystemId();
                if (fileURL.startsWith("file:///")) {
                    if (fileURL.substring(8).indexOf(":") > 0) {
                        fileURL = fileURL.substring(8);
                    } else {
                        fileURL = fileURL.substring(7);
                    }
                } else if (fileURL.startsWith("file:/")) {
                    if (fileURL.substring(6).indexOf(":") > 0) {
                        fileURL = fileURL.substring(6);
                    } else {
                        fileURL = fileURL.substring(5);
                    }
                }
                m_outputStream = new java.io.FileOutputStream(fileURL);
                serializer.setOutputStream(m_outputStream);
            } else
                //"No output specified!");
                throw new TransformerException(XSLMessages.createMessage(XSLTErrorResources.ER_NO_OUTPUT_SPECIFIED, null));
            m_resultContentHandler = serializer.asContentHandler();
        } catch (IOException ioe) {
            throw new TransformerException(ioe);
        }
    } else {
        //"Can't transform to a Result of type "
        throw new TransformerException(XSLMessages.createMessage(XSLTErrorResources.ER_CANNOT_TRANSFORM_TO_RESULT_TYPE, new Object[] { outputTarget.getClass().getName() }));
    // + outputTarget.getClass().getName()
    // + "!");
    }
    if (m_resultContentHandler instanceof DTDHandler)
        m_resultDTDHandler = (DTDHandler) m_resultContentHandler;
    if (m_resultContentHandler instanceof DeclHandler)
        m_resultDeclHandler = (DeclHandler) m_resultContentHandler;
    if (m_resultContentHandler instanceof LexicalHandler)
        m_resultLexicalHandler = (LexicalHandler) m_resultContentHandler;
}
Also used : DOMResult(javax.xml.transform.dom.DOMResult) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) StreamResult(javax.xml.transform.stream.StreamResult) Node(org.w3c.dom.Node) IOException(java.io.IOException) Document(org.w3c.dom.Document) DOMBuilder(org.apache.xml.utils.DOMBuilder) DeclHandler(org.xml.sax.ext.DeclHandler) SAXResult(javax.xml.transform.sax.SAXResult) DocumentBuilder(javax.xml.parsers.DocumentBuilder) LexicalHandler(org.xml.sax.ext.LexicalHandler) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) DTDHandler(org.xml.sax.DTDHandler) TransformerException(javax.xml.transform.TransformerException) Serializer(org.apache.xml.serializer.Serializer)

Example 4 with DTDHandler

use of org.xml.sax.DTDHandler in project robovm by robovm.

the class TransformerIdentityImpl method startElement.

/**
   * Receive notification of the start of an element.
   *
   * <p>By default, do nothing.  Application writers may override this
   * method in a subclass to take specific actions at the start of
   * each element (such as allocating a new tree node or writing
   * output to a file).</p>
   *
   * @param uri The Namespace URI, or the empty string if the
   *        element has no Namespace URI or if Namespace
   *        processing is not being performed.
   * @param localName The local name (without prefix), or the
   *        empty string if Namespace processing is not being
   *        performed.
   * @param qName The qualified name (with prefix), or the
   *        empty string if qualified names are not available.
   * @param attributes The specified or defaulted attributes.
   * @throws org.xml.sax.SAXException Any SAX exception, possibly
   *            wrapping another exception.
   * @see org.xml.sax.ContentHandler#startElement
   *
   * @throws SAXException
   */
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
    if (!m_foundFirstElement && null != m_serializer) {
        m_foundFirstElement = true;
        Serializer newSerializer;
        try {
            newSerializer = SerializerSwitcher.switchSerializerIfHTML(uri, localName, m_outputFormat.getProperties(), m_serializer);
        } catch (TransformerException te) {
            throw new SAXException(te);
        }
        if (newSerializer != m_serializer) {
            try {
                m_resultContentHandler = newSerializer.asContentHandler();
            } catch (// why?
            IOException ioe) {
                throw new SAXException(ioe);
            }
            if (m_resultContentHandler instanceof DTDHandler)
                m_resultDTDHandler = (DTDHandler) m_resultContentHandler;
            if (m_resultContentHandler instanceof LexicalHandler)
                m_resultLexicalHandler = (LexicalHandler) m_resultContentHandler;
            m_serializer = newSerializer;
        }
    }
    flushStartDoc();
    m_resultContentHandler.startElement(uri, localName, qName, attributes);
}
Also used : LexicalHandler(org.xml.sax.ext.LexicalHandler) IOException(java.io.IOException) DTDHandler(org.xml.sax.DTDHandler) TransformerException(javax.xml.transform.TransformerException) Serializer(org.apache.xml.serializer.Serializer) SAXException(org.xml.sax.SAXException)

Aggregations

IOException (java.io.IOException)4 TransformerException (javax.xml.transform.TransformerException)4 Serializer (org.apache.xml.serializer.Serializer)4 DTDHandler (org.xml.sax.DTDHandler)4 LexicalHandler (org.xml.sax.ext.LexicalHandler)4 DocumentBuilder (javax.xml.parsers.DocumentBuilder)2 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 DOMResult (javax.xml.transform.dom.DOMResult)2 SAXResult (javax.xml.transform.sax.SAXResult)2 StreamResult (javax.xml.transform.stream.StreamResult)2 DOMBuilder (org.apache.xml.utils.DOMBuilder)2 Document (org.w3c.dom.Document)2 Node (org.w3c.dom.Node)2 SAXException (org.xml.sax.SAXException)2 DeclHandler (org.xml.sax.ext.DeclHandler)2