Search in sources :

Example 16 with EntityReference

use of org.w3c.dom.EntityReference 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 17 with EntityReference

use of org.w3c.dom.EntityReference in project aries by apache.

the class SpringOsgiNamespaceHandler method getTextValue.

public static String getTextValue(Element valueEle) {
    Assert.notNull(valueEle, "Element must not be null");
    StringBuilder sb = new StringBuilder();
    NodeList nl = valueEle.getChildNodes();
    for (int i = 0, l = nl.getLength(); i < l; ++i) {
        Node item = nl.item(i);
        if (item instanceof CharacterData && !(item instanceof Comment) || item instanceof EntityReference) {
            sb.append(item.getNodeValue());
        }
    }
    return sb.toString();
}
Also used : Comment(org.w3c.dom.Comment) CharacterData(org.w3c.dom.CharacterData) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) EntityReference(org.w3c.dom.EntityReference)

Example 18 with EntityReference

use of org.w3c.dom.EntityReference in project aries by apache.

the class CmNamespaceHandler method getTextValue.

private static String getTextValue(Element element) {
    StringBuffer value = new StringBuffer();
    NodeList nl = element.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node item = nl.item(i);
        if ((item instanceof CharacterData && !(item instanceof Comment)) || item instanceof EntityReference) {
            value.append(item.getNodeValue());
        }
    }
    return value.toString();
}
Also used : Comment(org.w3c.dom.Comment) CharacterData(org.w3c.dom.CharacterData) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) EntityReference(org.w3c.dom.EntityReference)

Example 19 with EntityReference

use of org.w3c.dom.EntityReference in project knime-core by knime.

the class XMLXpathCellReader method readXML.

/**
 * {@inheritDoc}
 */
@Override
public XMLValue<Document> readXML() throws IOException {
    if (!m_xpathMatcher.nodeMatches()) {
        return null;
    }
    try {
        while (m_parser.hasNext()) {
            switch(m_parser.getEventType()) {
                case XMLStreamConstants.START_ELEMENT:
                    updateBasePath();
                    updateXmlSpaceDefinition();
                    updateXmlLangDefinition();
                    pushNamespaceContext();
                    for (int i = 0; i < m_docs.size(); i++) {
                        Element element = createElement(m_docs.get(i));
                        m_currNodes.get(i).appendChild(element);
                        m_currNodes.set(i, element);
                    }
                    boolean match = m_xpathMatcher.startElement(m_parser.getName());
                    if (match) {
                        Document doc = m_builder.newDocument();
                        Element element = createElement(doc);
                        m_namespaceStack.stream().flatMap(e -> e.stream()).forEach(p -> element.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, p.getFirst(), p.getSecond()));
                        // (see: http://www.w3.org/XML/1998/namespace)
                        if (!m_base.isEmpty() && null != m_base.get(0)) {
                            element.setAttributeNS(XMLConstants.XML_NS_URI, "xml:base", m_base.get(0));
                        }
                        if (!m_space.isEmpty() && null != m_space.get(0)) {
                            element.setAttributeNS(XMLConstants.XML_NS_URI, "xml:space", m_space.get(0));
                        }
                        if (!m_lang.isEmpty() && null != m_lang.get(0)) {
                            element.setAttributeNS(XMLConstants.XML_NS_URI, "xml:lang", m_lang.get(0));
                        }
                        doc.appendChild(element);
                        m_docs.add(doc);
                        m_currNodes.add(element);
                    }
                    break;
                case XMLStreamConstants.END_ELEMENT:
                    if (!m_reentrent) {
                        m_xpathMatcher.endElement();
                        if (!m_base.isEmpty()) {
                            m_base.remove(0);
                        }
                        if (!m_space.isEmpty()) {
                            m_space.remove(0);
                        }
                        if (!m_lang.isEmpty()) {
                            m_lang.remove(0);
                        }
                    }
                    for (int i = m_docs.size() - 1; i >= 0; i--) {
                        Node curr = m_currNodes.get(i);
                        Node first = m_docs.get(i).getFirstChild();
                        if (curr.isSameNode(first)) {
                            XMLValue<Document> cell = null;
                            cell = createDataCell(m_docs.get(i));
                            m_docs.remove(i);
                            m_currNodes.remove(i);
                            m_reentrent = true;
                            return cell;
                        } else {
                            m_currNodes.set(i, curr.getParentNode());
                        }
                    }
                    popNamespaceContext();
                    m_reentrent = false;
                    break;
                case XMLStreamConstants.CHARACTERS:
                    if (!m_parser.isWhiteSpace() || (m_parser.isWhiteSpace() && !m_space.isEmpty() && null != m_space.get(0) && m_space.get(0).equals("preserve"))) {
                        for (int i = 0; i < m_docs.size(); i++) {
                            String str = m_parser.getText();
                            Text text = m_docs.get(i).createTextNode(str);
                            m_currNodes.get(i).appendChild(text);
                        }
                    }
                    break;
                case XMLStreamConstants.SPACE:
                    if (!m_space.isEmpty() && null != m_space.get(0) && m_space.get(0).equals("preserve")) {
                        for (int i = 0; i < m_docs.size(); i++) {
                            String str = m_parser.getText();
                            Text text = m_docs.get(i).createTextNode(str);
                            m_currNodes.get(i).appendChild(text);
                        }
                    }
                    break;
                case XMLStreamConstants.COMMENT:
                    for (int i = 0; i < m_docs.size(); i++) {
                        String str = m_parser.getText();
                        Comment comment = m_docs.get(i).createComment(str);
                        m_currNodes.get(i).appendChild(comment);
                    }
                    break;
                case XMLStreamConstants.ENTITY_REFERENCE:
                    for (int i = 0; i < m_docs.size(); i++) {
                        String str = m_parser.getText();
                        EntityReference ref = m_docs.get(i).createEntityReference(str);
                        m_currNodes.get(i).appendChild(ref);
                    }
                    break;
                case XMLStreamConstants.PROCESSING_INSTRUCTION:
                    for (int i = 0; i < m_docs.size(); i++) {
                        String piTarget = m_parser.getPITarget();
                        String piName = m_parser.getPIData();
                        ProcessingInstruction pi = m_docs.get(i).createProcessingInstruction(piTarget, piName);
                        m_currNodes.get(i).appendChild(pi);
                    }
                    break;
                case XMLStreamConstants.DTD:
                    // not needed use DOM-Reader for full documents
                    break;
                case XMLStreamConstants.END_DOCUMENT:
                    // never called
                    m_parser.close();
                default:
                    break;
            }
            m_parser.next();
        }
        return null;
    } catch (XMLStreamException e) {
        throw new IOException(e);
    }
}
Also used : Text(org.w3c.dom.Text) ProcessingInstruction(org.w3c.dom.ProcessingInstruction) XMLInputFactory(javax.xml.stream.XMLInputFactory) Deque(java.util.Deque) ArrayList(java.util.ArrayList) Pair(org.knime.core.util.Pair) XMLValue(org.knime.core.data.xml.XMLValue) EntityReference(org.w3c.dom.EntityReference) XMLStreamConstants(javax.xml.stream.XMLStreamConstants) XMLStreamReader(javax.xml.stream.XMLStreamReader) Document(org.w3c.dom.Document) XMLStreamException(javax.xml.stream.XMLStreamException) Node(org.w3c.dom.Node) LinkedList(java.util.LinkedList) XMLConstants(javax.xml.XMLConstants) XMLCellFactory(org.knime.core.data.xml.XMLCellFactory) KNIMEConstants(org.knime.core.node.KNIMEConstants) IOException(java.io.IOException) Comment(org.w3c.dom.Comment) List(java.util.List) Element(org.w3c.dom.Element) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) ArrayDeque(java.util.ArrayDeque) Collections(java.util.Collections) InputStream(java.io.InputStream) Comment(org.w3c.dom.Comment) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) Text(org.w3c.dom.Text) IOException(java.io.IOException) Document(org.w3c.dom.Document) XMLStreamException(javax.xml.stream.XMLStreamException) EntityReference(org.w3c.dom.EntityReference) ProcessingInstruction(org.w3c.dom.ProcessingInstruction)

Example 20 with EntityReference

use of org.w3c.dom.EntityReference in project robovm by robovm.

the class DomTest method testEntityReferenceSetTextContent.

/**
     * Tests setTextContent on entity references. Although the other tests can
     * act on a parsed DOM, this needs to use a programmatically constructed DOM
     * because the parser may have replaced the entity reference with the
     * corresponding text.
     */
public void testEntityReferenceSetTextContent() throws TransformerException {
    document = builder.newDocument();
    Element root = document.createElement("menu");
    document.appendChild(root);
    EntityReference entityReference = document.createEntityReference("sp");
    root.appendChild(entityReference);
    try {
        entityReference.setTextContent("Lite Syrup");
        fail();
    } catch (DOMException e) {
    }
}
Also used : DOMException(org.w3c.dom.DOMException) Element(org.w3c.dom.Element) EntityReference(org.w3c.dom.EntityReference)

Aggregations

EntityReference (org.w3c.dom.EntityReference)32 Node (org.w3c.dom.Node)24 NodeList (org.w3c.dom.NodeList)17 Element (org.w3c.dom.Element)14 Comment (org.w3c.dom.Comment)12 CharacterData (org.w3c.dom.CharacterData)10 NamedNodeMap (org.w3c.dom.NamedNodeMap)8 LexicalHandler (org.xml.sax.ext.LexicalHandler)8 Document (org.w3c.dom.Document)7 ProcessingInstruction (org.w3c.dom.ProcessingInstruction)7 Locator (org.xml.sax.Locator)4 Text (org.w3c.dom.Text)3 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)2 AttList (org.apache.xml.serializer.utils.AttList)2 DOMException (org.w3c.dom.DOMException)2 DocumentType (org.w3c.dom.DocumentType)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 StringReader (java.io.StringReader)1 ArrayDeque (java.util.ArrayDeque)1