Search in sources :

Example 21 with LexicalHandler

use of org.xml.sax.ext.LexicalHandler in project j2objc by google.

the class TransformerImpl method createSerializationHandler.

/**
     * Create a ContentHandler from a Result object and an OutputProperties.
     *
     * @param outputTarget Where the transform result should go,
     * should not be null.
     * @param format The OutputProperties object that will contain
     * instructions on how to serialize the output.
     *
     * @return A valid ContentHandler that will create the
     * result tree when it is fed SAX events.
     *
     * @throws TransformerException
     */
public SerializationHandler createSerializationHandler(Result outputTarget, OutputProperties format) throws TransformerException {
    SerializationHandler xoh;
    // If the Result object contains a Node, then create
    // a ContentHandler that will add nodes to the input node.
    org.w3c.dom.Node outputNode = null;
    if (outputTarget instanceof DOMResult) {
        outputNode = ((DOMResult) outputTarget).getNode();
        org.w3c.dom.Node nextSibling = ((DOMResult) outputTarget).getNextSibling();
        org.w3c.dom.Document doc;
        short type;
        if (null != outputNode) {
            type = outputNode.getNodeType();
            doc = (org.w3c.dom.Node.DOCUMENT_NODE == type) ? (org.w3c.dom.Document) outputNode : outputNode.getOwnerDocument();
        } else {
            boolean isSecureProcessing = m_stylesheetRoot.isSecureProcessing();
            doc = org.apache.xml.utils.DOMHelper.createDocument(isSecureProcessing);
            outputNode = doc;
            type = outputNode.getNodeType();
            ((DOMResult) outputTarget).setNode(outputNode);
        }
        DOMBuilder handler = (org.w3c.dom.Node.DOCUMENT_FRAGMENT_NODE == type) ? new DOMBuilder(doc, (org.w3c.dom.DocumentFragment) outputNode) : new DOMBuilder(doc, outputNode);
        if (nextSibling != null)
            handler.setNextSibling(nextSibling);
        String encoding = format.getProperty(OutputKeys.ENCODING);
        xoh = new ToXMLSAXHandler(handler, (LexicalHandler) handler, encoding);
    } else if (outputTarget instanceof SAXResult) {
        ContentHandler handler = ((SAXResult) outputTarget).getHandler();
        if (null == handler)
            throw new IllegalArgumentException("handler can not be null for a SAXResult");
        LexicalHandler lexHandler;
        if (handler instanceof LexicalHandler)
            lexHandler = (LexicalHandler) handler;
        else
            lexHandler = null;
        String encoding = format.getProperty(OutputKeys.ENCODING);
        String method = format.getProperty(OutputKeys.METHOD);
        ToXMLSAXHandler toXMLSAXHandler = new ToXMLSAXHandler(handler, lexHandler, encoding);
        toXMLSAXHandler.setShouldOutputNSAttr(false);
        xoh = toXMLSAXHandler;
        String publicID = format.getProperty(OutputKeys.DOCTYPE_PUBLIC);
        String systemID = format.getProperty(OutputKeys.DOCTYPE_SYSTEM);
        if (systemID != null)
            xoh.setDoctypeSystem(systemID);
        if (publicID != null)
            xoh.setDoctypePublic(publicID);
        if (handler instanceof TransformerClient) {
            XalanTransformState state = new XalanTransformState();
            ((TransformerClient) handler).setTransformState(state);
            ((ToSAXHandler) xoh).setTransformState(state);
        }
    } else // result tree to either a stream or a writer.
    if (outputTarget instanceof StreamResult) {
        StreamResult sresult = (StreamResult) outputTarget;
        try {
            SerializationHandler serializer = (SerializationHandler) SerializerFactory.getSerializer(format.getProperties());
            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);
                xoh = serializer;
            } else
                //"No output specified!");
                throw new TransformerException(XSLMessages.createMessage(XSLTErrorResources.ER_NO_OUTPUT_SPECIFIED, null));
            // handler = serializer.asContentHandler();
            //  this.setSerializer(serializer);
            xoh = serializer;
        }//        }
         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()
    //+ "!");
    }
    // before we forget, lets make the created handler hold a reference
    // to the current TransformImpl object
    xoh.setTransformer(this);
    SourceLocator srcLocator = getStylesheet();
    xoh.setSourceLocator(srcLocator);
    return xoh;
}
Also used : DOMResult(javax.xml.transform.dom.DOMResult) SerializationHandler(org.apache.xml.serializer.SerializationHandler) DOMBuilder(org.apache.xml.utils.DOMBuilder) ContentHandler(org.xml.sax.ContentHandler) LexicalHandler(org.xml.sax.ext.LexicalHandler) TransformerException(javax.xml.transform.TransformerException) StreamResult(javax.xml.transform.stream.StreamResult) IOException(java.io.IOException) SAXResult(javax.xml.transform.sax.SAXResult) SourceLocator(javax.xml.transform.SourceLocator) SAXSourceLocator(org.apache.xml.utils.SAXSourceLocator) XObject(org.apache.xpath.objects.XObject) ToXMLSAXHandler(org.apache.xml.serializer.ToXMLSAXHandler)

Example 22 with LexicalHandler

use of org.xml.sax.ext.LexicalHandler in project robovm by robovm.

the class TreeWalker method startNode.

/**
   * Start processing given node
   *
   *
   * @param node Node to process
   *
   * @throws org.xml.sax.SAXException
   */
protected void startNode(Node node) throws org.xml.sax.SAXException {
    if (node instanceof Locator) {
        Locator loc = (Locator) node;
        m_locator.setColumnNumber(loc.getColumnNumber());
        m_locator.setLineNumber(loc.getLineNumber());
        m_locator.setPublicId(loc.getPublicId());
        m_locator.setSystemId(loc.getSystemId());
    } else {
        m_locator.setColumnNumber(0);
        m_locator.setLineNumber(0);
    }
    switch(node.getNodeType()) {
        case Node.COMMENT_NODE:
            {
                String data = ((Comment) node).getData();
                if (m_contentHandler instanceof LexicalHandler) {
                    LexicalHandler lh = ((LexicalHandler) this.m_contentHandler);
                    lh.comment(data.toCharArray(), 0, data.length());
                }
            }
            break;
        case Node.DOCUMENT_FRAGMENT_NODE:
            // ??;
            break;
        case Node.DOCUMENT_NODE:
            break;
        case Node.ELEMENT_NODE:
            Element elem_node = (Element) node;
            {
                // Make sure the namespace node
                // for the element itself is declared
                // to the ContentHandler
                String uri = elem_node.getNamespaceURI();
                if (uri != null) {
                    String prefix = elem_node.getPrefix();
                    if (prefix == null)
                        prefix = "";
                    this.m_contentHandler.startPrefixMapping(prefix, uri);
                }
            }
            NamedNodeMap atts = elem_node.getAttributes();
            int nAttrs = atts.getLength();
            // each attribute is declared to the ContentHandler
            for (int i = 0; i < nAttrs; i++) {
                final Node attr = atts.item(i);
                final String attrName = attr.getNodeName();
                final int colon = attrName.indexOf(':');
                final String prefix;
                // System.out.println("TreeWalker#startNode: attr["+i+"] = "+attrName+", "+attr.getNodeValue());
                if (attrName.equals("xmlns") || attrName.startsWith("xmlns:")) {
                    // to "Steven Murray" <smurray@ebt.com>.
                    if (colon < 0)
                        prefix = "";
                    else
                        prefix = attrName.substring(colon + 1);
                    this.m_contentHandler.startPrefixMapping(prefix, attr.getNodeValue());
                } else if (colon > 0) {
                    prefix = attrName.substring(0, colon);
                    String uri = attr.getNamespaceURI();
                    if (uri != null)
                        this.m_contentHandler.startPrefixMapping(prefix, uri);
                }
            }
            String ns = m_dh.getNamespaceOfNode(node);
            if (null == ns)
                ns = "";
            this.m_contentHandler.startElement(ns, m_dh.getLocalNameOfNode(node), node.getNodeName(), new AttList(atts, m_dh));
            break;
        case Node.PROCESSING_INSTRUCTION_NODE:
            {
                ProcessingInstruction pi = (ProcessingInstruction) node;
                String name = pi.getNodeName();
                // String data = pi.getData();
                if (name.equals("xslt-next-is-raw")) {
                    nextIsRaw = true;
                } else {
                    this.m_contentHandler.processingInstruction(pi.getNodeName(), pi.getData());
                }
            }
            break;
        case Node.CDATA_SECTION_NODE:
            {
                boolean isLexH = (m_contentHandler instanceof LexicalHandler);
                LexicalHandler lh = isLexH ? ((LexicalHandler) this.m_contentHandler) : null;
                if (isLexH) {
                    lh.startCDATA();
                }
                dispatachChars(node);
                {
                    if (isLexH) {
                        lh.endCDATA();
                    }
                }
            }
            break;
        case Node.TEXT_NODE:
            {
                if (nextIsRaw) {
                    nextIsRaw = false;
                    m_contentHandler.processingInstruction(javax.xml.transform.Result.PI_DISABLE_OUTPUT_ESCAPING, "");
                    dispatachChars(node);
                    m_contentHandler.processingInstruction(javax.xml.transform.Result.PI_ENABLE_OUTPUT_ESCAPING, "");
                } else {
                    dispatachChars(node);
                }
            }
            break;
        case Node.ENTITY_REFERENCE_NODE:
            {
                EntityReference eref = (EntityReference) node;
                if (m_contentHandler instanceof LexicalHandler) {
                    ((LexicalHandler) this.m_contentHandler).startEntity(eref.getNodeName());
                } else {
                // warning("Can not output entity to a pure SAX ContentHandler");
                }
            }
            break;
        default:
    }
}
Also used : Locator(org.xml.sax.Locator) AttList(org.apache.xml.serializer.utils.AttList) NamedNodeMap(org.w3c.dom.NamedNodeMap) LexicalHandler(org.xml.sax.ext.LexicalHandler) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) EntityReference(org.w3c.dom.EntityReference) ProcessingInstruction(org.w3c.dom.ProcessingInstruction)

Example 23 with LexicalHandler

use of org.xml.sax.ext.LexicalHandler in project robovm by robovm.

the class DTMTreeWalker method endNode.

/**
   * End processing of given node 
   *
   *
   * @param node Node we just finished processing
   *
   * @throws org.xml.sax.SAXException
   */
protected void endNode(int node) throws org.xml.sax.SAXException {
    switch(m_dtm.getNodeType(node)) {
        case DTM.DOCUMENT_NODE:
            this.m_contentHandler.endDocument();
            break;
        case DTM.ELEMENT_NODE:
            String ns = m_dtm.getNamespaceURI(node);
            if (null == ns)
                ns = "";
            this.m_contentHandler.endElement(ns, m_dtm.getLocalName(node), m_dtm.getNodeName(node));
            for (int nsn = m_dtm.getFirstNamespaceNode(node, true); DTM.NULL != nsn; nsn = m_dtm.getNextNamespaceNode(node, nsn, true)) {
                // String prefix = m_dtm.getPrefix(nsn);
                String prefix = m_dtm.getNodeNameX(nsn);
                this.m_contentHandler.endPrefixMapping(prefix);
            }
            break;
        case DTM.CDATA_SECTION_NODE:
            break;
        case DTM.ENTITY_REFERENCE_NODE:
            {
                if (m_contentHandler instanceof LexicalHandler) {
                    LexicalHandler lh = ((LexicalHandler) this.m_contentHandler);
                    lh.endEntity(m_dtm.getNodeName(node));
                }
            }
            break;
        default:
    }
}
Also used : LexicalHandler(org.xml.sax.ext.LexicalHandler) XMLString(org.apache.xml.utils.XMLString)

Example 24 with LexicalHandler

use of org.xml.sax.ext.LexicalHandler 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)

Example 25 with LexicalHandler

use of org.xml.sax.ext.LexicalHandler in project robovm by robovm.

the class TransformerImpl method createSerializationHandler.

/**
     * Create a ContentHandler from a Result object and an OutputProperties.
     *
     * @param outputTarget Where the transform result should go,
     * should not be null.
     * @param format The OutputProperties object that will contain
     * instructions on how to serialize the output.
     *
     * @return A valid ContentHandler that will create the
     * result tree when it is fed SAX events.
     *
     * @throws TransformerException
     */
public SerializationHandler createSerializationHandler(Result outputTarget, OutputProperties format) throws TransformerException {
    SerializationHandler xoh;
    // If the Result object contains a Node, then create
    // a ContentHandler that will add nodes to the input node.
    org.w3c.dom.Node outputNode = null;
    if (outputTarget instanceof DOMResult) {
        outputNode = ((DOMResult) outputTarget).getNode();
        org.w3c.dom.Node nextSibling = ((DOMResult) outputTarget).getNextSibling();
        org.w3c.dom.Document doc;
        short type;
        if (null != outputNode) {
            type = outputNode.getNodeType();
            doc = (org.w3c.dom.Node.DOCUMENT_NODE == type) ? (org.w3c.dom.Document) outputNode : outputNode.getOwnerDocument();
        } else {
            boolean isSecureProcessing = m_stylesheetRoot.isSecureProcessing();
            doc = org.apache.xml.utils.DOMHelper.createDocument(isSecureProcessing);
            outputNode = doc;
            type = outputNode.getNodeType();
            ((DOMResult) outputTarget).setNode(outputNode);
        }
        DOMBuilder handler = (org.w3c.dom.Node.DOCUMENT_FRAGMENT_NODE == type) ? new DOMBuilder(doc, (org.w3c.dom.DocumentFragment) outputNode) : new DOMBuilder(doc, outputNode);
        if (nextSibling != null)
            handler.setNextSibling(nextSibling);
        String encoding = format.getProperty(OutputKeys.ENCODING);
        xoh = new ToXMLSAXHandler(handler, (LexicalHandler) handler, encoding);
    } else if (outputTarget instanceof SAXResult) {
        ContentHandler handler = ((SAXResult) outputTarget).getHandler();
        if (null == handler)
            throw new IllegalArgumentException("handler can not be null for a SAXResult");
        LexicalHandler lexHandler;
        if (handler instanceof LexicalHandler)
            lexHandler = (LexicalHandler) handler;
        else
            lexHandler = null;
        String encoding = format.getProperty(OutputKeys.ENCODING);
        String method = format.getProperty(OutputKeys.METHOD);
        ToXMLSAXHandler toXMLSAXHandler = new ToXMLSAXHandler(handler, lexHandler, encoding);
        toXMLSAXHandler.setShouldOutputNSAttr(false);
        xoh = toXMLSAXHandler;
        String publicID = format.getProperty(OutputKeys.DOCTYPE_PUBLIC);
        String systemID = format.getProperty(OutputKeys.DOCTYPE_SYSTEM);
        if (systemID != null)
            xoh.setDoctypeSystem(systemID);
        if (publicID != null)
            xoh.setDoctypePublic(publicID);
        if (handler instanceof TransformerClient) {
            XalanTransformState state = new XalanTransformState();
            ((TransformerClient) handler).setTransformState(state);
            ((ToSAXHandler) xoh).setTransformState(state);
        }
    } else // result tree to either a stream or a writer.
    if (outputTarget instanceof StreamResult) {
        StreamResult sresult = (StreamResult) outputTarget;
        try {
            SerializationHandler serializer = (SerializationHandler) SerializerFactory.getSerializer(format.getProperties());
            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);
                xoh = serializer;
            } else
                //"No output specified!");
                throw new TransformerException(XSLMessages.createMessage(XSLTErrorResources.ER_NO_OUTPUT_SPECIFIED, null));
            // handler = serializer.asContentHandler();
            //  this.setSerializer(serializer);
            xoh = serializer;
        }//        }
         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()
    //+ "!");
    }
    // before we forget, lets make the created handler hold a reference
    // to the current TransformImpl object
    xoh.setTransformer(this);
    SourceLocator srcLocator = getStylesheet();
    xoh.setSourceLocator(srcLocator);
    return xoh;
}
Also used : DOMResult(javax.xml.transform.dom.DOMResult) SerializationHandler(org.apache.xml.serializer.SerializationHandler) DOMBuilder(org.apache.xml.utils.DOMBuilder) ContentHandler(org.xml.sax.ContentHandler) LexicalHandler(org.xml.sax.ext.LexicalHandler) TransformerException(javax.xml.transform.TransformerException) StreamResult(javax.xml.transform.stream.StreamResult) IOException(java.io.IOException) SAXResult(javax.xml.transform.sax.SAXResult) SourceLocator(javax.xml.transform.SourceLocator) SAXSourceLocator(org.apache.xml.utils.SAXSourceLocator) XObject(org.apache.xpath.objects.XObject) ToXMLSAXHandler(org.apache.xml.serializer.ToXMLSAXHandler)

Aggregations

LexicalHandler (org.xml.sax.ext.LexicalHandler)34 Node (org.w3c.dom.Node)14 IOException (java.io.IOException)11 TransformerException (javax.xml.transform.TransformerException)10 ContentHandler (org.xml.sax.ContentHandler)9 Element (org.w3c.dom.Element)8 EntityReference (org.w3c.dom.EntityReference)8 NamedNodeMap (org.w3c.dom.NamedNodeMap)8 SAXException (org.xml.sax.SAXException)8 DTDHandler (org.xml.sax.DTDHandler)6 SAXResult (javax.xml.transform.sax.SAXResult)5 DocumentBuilder (javax.xml.parsers.DocumentBuilder)4 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)4 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)4 DOMResult (javax.xml.transform.dom.DOMResult)4 StreamResult (javax.xml.transform.stream.StreamResult)4 SerializationHandler (org.apache.xml.serializer.SerializationHandler)4 Serializer (org.apache.xml.serializer.Serializer)4 ToXMLSAXHandler (org.apache.xml.serializer.ToXMLSAXHandler)4 DOMBuilder (org.apache.xml.utils.DOMBuilder)4