Search in sources :

Example 11 with LexicalHandler

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

the class TreeWalker method endNode.

/**
   * End processing of given node 
   *
   *
   * @param node Node we just finished processing
   *
   * @throws org.xml.sax.SAXException
   */
protected void endNode(Node node) throws org.xml.sax.SAXException {
    switch(node.getNodeType()) {
        case Node.DOCUMENT_NODE:
            break;
        case Node.ELEMENT_NODE:
            String ns = m_dh.getNamespaceOfNode(node);
            if (null == ns)
                ns = "";
            this.m_contentHandler.endElement(ns, m_dh.getLocalNameOfNode(node), node.getNodeName());
            if (m_Serializer == null) {
                // Don't bother with endPrefixMapping calls if the ContentHandler is a
                // SerializationHandler because SerializationHandler's ignore the
                // endPrefixMapping() calls anyways. . . .  This is an optimization.    
                Element elem_node = (Element) node;
                NamedNodeMap atts = elem_node.getAttributes();
                int nAttrs = atts.getLength();
                // of the startPrefixMapping calls
                for (int i = (nAttrs - 1); 0 <= i; i--) {
                    final Node attr = atts.item(i);
                    final String attrName = attr.getNodeName();
                    final int colon = attrName.indexOf(':');
                    final String prefix;
                    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.endPrefixMapping(prefix);
                    } else if (colon > 0) {
                        prefix = attrName.substring(0, colon);
                        this.m_contentHandler.endPrefixMapping(prefix);
                    }
                }
                {
                    String uri = elem_node.getNamespaceURI();
                    if (uri != null) {
                        String prefix = elem_node.getPrefix();
                        if (prefix == null)
                            prefix = "";
                        this.m_contentHandler.endPrefixMapping(prefix);
                    }
                }
            }
            break;
        case Node.CDATA_SECTION_NODE:
            break;
        case Node.ENTITY_REFERENCE_NODE:
            {
                EntityReference eref = (EntityReference) node;
                if (m_contentHandler instanceof LexicalHandler) {
                    LexicalHandler lh = ((LexicalHandler) this.m_contentHandler);
                    lh.endEntity(eref.getNodeName());
                }
            }
            break;
        default:
    }
}
Also used : 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)

Example 12 with LexicalHandler

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

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 13 with LexicalHandler

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

the class DTMTreeWalker method startNode.

/**
   * Start processing given node
   *
   *
   * @param node Node to process
   *
   * @throws org.xml.sax.SAXException
   */
protected void startNode(int node) throws org.xml.sax.SAXException {
    if (m_contentHandler instanceof NodeConsumer) {
    // %TBD%
    //      ((NodeConsumer) m_contentHandler).setOriginatingNode(node);
    }
    switch(m_dtm.getNodeType(node)) {
        case DTM.COMMENT_NODE:
            {
                XMLString data = m_dtm.getStringValue(node);
                if (m_contentHandler instanceof LexicalHandler) {
                    LexicalHandler lh = ((LexicalHandler) this.m_contentHandler);
                    data.dispatchAsComment(lh);
                }
            }
            break;
        case DTM.DOCUMENT_FRAGMENT_NODE:
            // ??;
            break;
        case DTM.DOCUMENT_NODE:
            this.m_contentHandler.startDocument();
            break;
        case DTM.ELEMENT_NODE:
            DTM dtm = m_dtm;
            for (int nsn = dtm.getFirstNamespaceNode(node, true); DTM.NULL != nsn; nsn = dtm.getNextNamespaceNode(node, nsn, true)) {
                // String prefix = dtm.getPrefix(nsn);
                String prefix = dtm.getNodeNameX(nsn);
                this.m_contentHandler.startPrefixMapping(prefix, dtm.getNodeValue(nsn));
            }
            // System.out.println("m_dh.getNamespaceOfNode(node): "+m_dh.getNamespaceOfNode(node));
            // System.out.println("m_dh.getLocalNameOfNode(node): "+m_dh.getLocalNameOfNode(node));
            String ns = dtm.getNamespaceURI(node);
            if (null == ns)
                ns = "";
            // %OPT% !!
            org.xml.sax.helpers.AttributesImpl attrs = new org.xml.sax.helpers.AttributesImpl();
            for (int i = dtm.getFirstAttribute(node); i != DTM.NULL; i = dtm.getNextAttribute(i)) {
                attrs.addAttribute(dtm.getNamespaceURI(i), dtm.getLocalName(i), dtm.getNodeName(i), "CDATA", dtm.getNodeValue(i));
            }
            this.m_contentHandler.startElement(ns, m_dtm.getLocalName(node), m_dtm.getNodeName(node), attrs);
            break;
        case DTM.PROCESSING_INSTRUCTION_NODE:
            {
                String name = m_dtm.getNodeName(node);
                // String data = pi.getData();
                if (name.equals("xslt-next-is-raw")) {
                    nextIsRaw = true;
                } else {
                    this.m_contentHandler.processingInstruction(name, m_dtm.getNodeValue(node));
                }
            }
            break;
        case DTM.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 DTM.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 DTM.ENTITY_REFERENCE_NODE:
            {
                if (m_contentHandler instanceof LexicalHandler) {
                    ((LexicalHandler) this.m_contentHandler).startEntity(m_dtm.getNodeName(node));
                } else {
                // warning("Can not output entity to a pure SAX ContentHandler");
                }
            }
            break;
        default:
    }
}
Also used : NodeConsumer(org.apache.xml.utils.NodeConsumer) LexicalHandler(org.xml.sax.ext.LexicalHandler) XMLString(org.apache.xml.utils.XMLString) XMLString(org.apache.xml.utils.XMLString) DTM(org.apache.xml.dtm.DTM)

Example 14 with LexicalHandler

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

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

the class TransformerIdentityImpl method transform.

/**
   * Process the source tree to the output result.
   * @param source  The input for the source tree.
   *
   * @param outputTarget The output target.
   *
   * @throws TransformerException If an unrecoverable error occurs
   * during the course of the transformation.
   */
public void transform(Source source, Result outputTarget) throws TransformerException {
    createResultContentHandler(outputTarget);
    /*
     * According to JAXP1.2, new SAXSource()/StreamSource()
     * should create an empty input tree, with a default root node. 
     * new DOMSource()creates an empty document using DocumentBuilder.
     * newDocument(); Use DocumentBuilder.newDocument() for all 3 situations,
     * since there is no clear spec. how to create an empty tree when
     * both SAXSource() and StreamSource() are used.
     */
    if ((source instanceof StreamSource && source.getSystemId() == null && ((StreamSource) source).getInputStream() == null && ((StreamSource) source).getReader() == null) || (source instanceof SAXSource && ((SAXSource) source).getInputSource() == null && ((SAXSource) source).getXMLReader() == null) || (source instanceof DOMSource && ((DOMSource) source).getNode() == null)) {
        try {
            DocumentBuilderFactory builderF = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = builderF.newDocumentBuilder();
            String systemID = source.getSystemId();
            source = new DOMSource(builder.newDocument());
            // Copy system ID from original, empty Source to new Source
            if (systemID != null) {
                source.setSystemId(systemID);
            }
        } catch (ParserConfigurationException e) {
            throw new TransformerException(e.getMessage());
        }
    }
    try {
        if (source instanceof DOMSource) {
            DOMSource dsource = (DOMSource) source;
            m_systemID = dsource.getSystemId();
            Node dNode = dsource.getNode();
            if (null != dNode) {
                try {
                    if (dNode.getNodeType() == Node.ATTRIBUTE_NODE)
                        this.startDocument();
                    try {
                        if (dNode.getNodeType() == Node.ATTRIBUTE_NODE) {
                            String data = dNode.getNodeValue();
                            char[] chars = data.toCharArray();
                            characters(chars, 0, chars.length);
                        } else {
                            org.apache.xml.serializer.TreeWalker walker;
                            walker = new org.apache.xml.serializer.TreeWalker(this, m_systemID);
                            walker.traverse(dNode);
                        }
                    } finally {
                        if (dNode.getNodeType() == Node.ATTRIBUTE_NODE)
                            this.endDocument();
                    }
                } catch (SAXException se) {
                    throw new TransformerException(se);
                }
                return;
            } else {
                String messageStr = XSLMessages.createMessage(XSLTErrorResources.ER_ILLEGAL_DOMSOURCE_INPUT, null);
                throw new IllegalArgumentException(messageStr);
            }
        }
        InputSource xmlSource = SAXSource.sourceToInputSource(source);
        if (null == xmlSource) {
            //"Can't transform a Source of type "
            throw new TransformerException(XSLMessages.createMessage(XSLTErrorResources.ER_CANNOT_TRANSFORM_SOURCE_TYPE, new Object[] { source.getClass().getName() }));
        //+ source.getClass().getName() + "!");
        }
        if (null != xmlSource.getSystemId())
            m_systemID = xmlSource.getSystemId();
        XMLReader reader = null;
        boolean managedReader = false;
        try {
            if (source instanceof SAXSource) {
                reader = ((SAXSource) source).getXMLReader();
            }
            if (null == reader) {
                try {
                    reader = XMLReaderManager.getInstance().getXMLReader();
                    managedReader = true;
                } catch (SAXException se) {
                    throw new TransformerException(se);
                }
            } else {
                try {
                    reader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
                } catch (org.xml.sax.SAXException se) {
                // We don't care.
                }
            }
            // Get the input content handler, which will handle the 
            // parse events and create the source tree. 
            ContentHandler inputHandler = this;
            reader.setContentHandler(inputHandler);
            if (inputHandler instanceof org.xml.sax.DTDHandler)
                reader.setDTDHandler((org.xml.sax.DTDHandler) inputHandler);
            try {
                if (inputHandler instanceof org.xml.sax.ext.LexicalHandler)
                    reader.setProperty("http://xml.org/sax/properties/lexical-handler", inputHandler);
                if (inputHandler instanceof org.xml.sax.ext.DeclHandler)
                    reader.setProperty("http://xml.org/sax/properties/declaration-handler", inputHandler);
            } catch (org.xml.sax.SAXException se) {
            }
            try {
                if (inputHandler instanceof org.xml.sax.ext.LexicalHandler)
                    reader.setProperty("http://xml.org/sax/handlers/LexicalHandler", inputHandler);
                if (inputHandler instanceof org.xml.sax.ext.DeclHandler)
                    reader.setProperty("http://xml.org/sax/handlers/DeclHandler", inputHandler);
            } catch (org.xml.sax.SAXNotRecognizedException snre) {
            }
            reader.parse(xmlSource);
        } catch (org.apache.xml.utils.WrappedRuntimeException wre) {
            Throwable throwable = wre.getException();
            while (throwable instanceof org.apache.xml.utils.WrappedRuntimeException) {
                throwable = ((org.apache.xml.utils.WrappedRuntimeException) throwable).getException();
            }
            throw new TransformerException(wre.getException());
        } catch (org.xml.sax.SAXException se) {
            throw new TransformerException(se);
        } catch (IOException ioe) {
            throw new TransformerException(ioe);
        } finally {
            if (managedReader) {
                XMLReaderManager.getInstance().releaseXMLReader(reader);
            }
        }
    } finally {
        if (null != m_outputStream) {
            try {
                m_outputStream.close();
            } catch (IOException ioe) {
            }
            m_outputStream = null;
        }
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Node(org.w3c.dom.Node) ContentHandler(org.xml.sax.ContentHandler) SAXException(org.xml.sax.SAXException) DeclHandler(org.xml.sax.ext.DeclHandler) LexicalHandler(org.xml.sax.ext.LexicalHandler) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) TransformerException(javax.xml.transform.TransformerException) XMLReader(org.xml.sax.XMLReader) StreamSource(javax.xml.transform.stream.StreamSource) IOException(java.io.IOException) SAXSource(javax.xml.transform.sax.SAXSource) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SAXException(org.xml.sax.SAXException) DTDHandler(org.xml.sax.DTDHandler)

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