Search in sources :

Example 1 with Locator

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

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 2 with Locator

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

the class DOM3TreeWalker 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;
        fLocator.setColumnNumber(loc.getColumnNumber());
        fLocator.setLineNumber(loc.getLineNumber());
        fLocator.setPublicId(loc.getPublicId());
        fLocator.setSystemId(loc.getSystemId());
    } else {
        fLocator.setColumnNumber(0);
        fLocator.setLineNumber(0);
    }
    switch(node.getNodeType()) {
        case Node.DOCUMENT_TYPE_NODE:
            serializeDocType((DocumentType) node, true);
            break;
        case Node.COMMENT_NODE:
            serializeComment((Comment) node);
            break;
        case Node.DOCUMENT_FRAGMENT_NODE:
            // Children are traversed
            break;
        case Node.DOCUMENT_NODE:
            break;
        case Node.ELEMENT_NODE:
            serializeElement((Element) node, true);
            break;
        case Node.PROCESSING_INSTRUCTION_NODE:
            serializePI((ProcessingInstruction) node);
            break;
        case Node.CDATA_SECTION_NODE:
            serializeCDATASection((CDATASection) node);
            break;
        case Node.TEXT_NODE:
            serializeText((Text) node);
            break;
        case Node.ENTITY_REFERENCE_NODE:
            serializeEntityReference((EntityReference) node, true);
            break;
        default:
    }
}
Also used : Locator(org.xml.sax.Locator)

Example 3 with Locator

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

the class DOM3TreeWalker 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;
        fLocator.setColumnNumber(loc.getColumnNumber());
        fLocator.setLineNumber(loc.getLineNumber());
        fLocator.setPublicId(loc.getPublicId());
        fLocator.setSystemId(loc.getSystemId());
    } else {
        fLocator.setColumnNumber(0);
        fLocator.setLineNumber(0);
    }
    switch(node.getNodeType()) {
        case Node.DOCUMENT_TYPE_NODE:
            serializeDocType((DocumentType) node, true);
            break;
        case Node.COMMENT_NODE:
            serializeComment((Comment) node);
            break;
        case Node.DOCUMENT_FRAGMENT_NODE:
            // Children are traversed
            break;
        case Node.DOCUMENT_NODE:
            break;
        case Node.ELEMENT_NODE:
            serializeElement((Element) node, true);
            break;
        case Node.PROCESSING_INSTRUCTION_NODE:
            serializePI((ProcessingInstruction) node);
            break;
        case Node.CDATA_SECTION_NODE:
            serializeCDATASection((CDATASection) node);
            break;
        case Node.TEXT_NODE:
            serializeText((Text) node);
            break;
        case Node.ENTITY_REFERENCE_NODE:
            serializeEntityReference((EntityReference) node, true);
            break;
        default:
    }
}
Also used : Locator(org.xml.sax.Locator)

Example 4 with Locator

use of org.xml.sax.Locator 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 (m_contentHandler instanceof NodeConsumer) {
        ((NodeConsumer) m_contentHandler).setOriginatingNode(node);
    }
    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:
            NamedNodeMap atts = ((Element) node).getAttributes();
            int nAttrs = atts.getLength();
            for (int i = 0; i < nAttrs; i++) {
                Node attr = atts.item(i);
                String attrName = attr.getNodeName();
                // System.out.println("TreeWalker#startNode: attr["+i+"] = "+attrName+", "+attr.getNodeValue());
                if (attrName.equals("xmlns") || attrName.startsWith("xmlns:")) {
                    // System.out.println("TreeWalker#startNode: attr["+i+"] = "+attrName+", "+attr.getNodeValue());
                    int index;
                    // Use "" instead of null, as Xerces likes "" for the 
                    // name of the default namespace.  Fix attributed 
                    // to "Steven Murray" <smurray@ebt.com>.
                    String prefix = (index = attrName.indexOf(":")) < 0 ? "" : attrName.substring(index + 1);
                    this.m_contentHandler.startPrefixMapping(prefix, attr.getNodeValue());
                }
            }
            // System.out.println("m_dh.getNamespaceOfNode(node): "+m_dh.getNamespaceOfNode(node));
            // System.out.println("m_dh.getLocalNameOfNode(node): "+m_dh.getLocalNameOfNode(node));
            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) 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 5 with Locator

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

the class XMLFilterImplTest method testSetDocumentLocator.

public void testSetDocumentLocator() {
    Locator l = new LocatorImpl();
    child.setDocumentLocator(l);
    assertEquals(logger.size(), 1);
    assertEquals("setDocumentLocator", logger.getMethod());
    assertEquals(new Object[] { l }, logger.getArgs());
    child.setDocumentLocator(null);
    assertEquals(logger.size(), 2);
    assertEquals("setDocumentLocator", logger.getMethod());
    assertEquals(new Object[] { null }, logger.getArgs());
}
Also used : Locator(org.xml.sax.Locator) LocatorImpl(org.xml.sax.helpers.LocatorImpl)

Aggregations

Locator (org.xml.sax.Locator)16 Element (org.w3c.dom.Element)7 Node (org.w3c.dom.Node)7 EntityReference (org.w3c.dom.EntityReference)4 NamedNodeMap (org.w3c.dom.NamedNodeMap)4 ProcessingInstruction (org.w3c.dom.ProcessingInstruction)4 Attributes (org.xml.sax.Attributes)4 LexicalHandler (org.xml.sax.ext.LexicalHandler)4 Stack (java.util.Stack)3 DocumentBuilder (javax.xml.parsers.DocumentBuilder)3 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)3 SAXParser (javax.xml.parsers.SAXParser)3 SAXParserFactory (javax.xml.parsers.SAXParserFactory)3 Document (org.w3c.dom.Document)3 DefaultHandler (org.xml.sax.helpers.DefaultHandler)3 StringReader (java.io.StringReader)2 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)2 TransformerException (javax.xml.transform.TransformerException)2 ElemExtensionCall (org.apache.xalan.templates.ElemExtensionCall)2 ElemLiteralResult (org.apache.xalan.templates.ElemLiteralResult)2