Search in sources :

Example 6 with EntityReference

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

the class XsltXPathConformanceTestSuite method emitNode.

private void emitNode(XmlSerializer serializer, Node node) throws IOException {
    if (node == null) {
        throw new UnsupportedOperationException("Cannot emit null nodes");
    } else if (node.getNodeType() == Node.ELEMENT_NODE) {
        Element element = (Element) node;
        serializer.startTag(element.getNamespaceURI(), element.getLocalName());
        emitAttributes(serializer, element);
        emitChildren(serializer, element);
        serializer.endTag(element.getNamespaceURI(), element.getLocalName());
    } else if (node.getNodeType() == Node.TEXT_NODE || node.getNodeType() == Node.CDATA_SECTION_NODE) {
        // TODO: is it okay to trim whitespace in general? This may cause
        //     false positives for elements like HTML's <pre> tag
        String trimmed = node.getTextContent().trim();
        if (trimmed.length() > 0) {
            serializer.text(trimmed);
        }
    } else if (node.getNodeType() == Node.DOCUMENT_NODE) {
        Document document = (Document) node;
        serializer.startDocument("UTF-8", true);
        emitNode(serializer, document.getDocumentElement());
        serializer.endDocument();
    } else if (node.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) {
        ProcessingInstruction processingInstruction = (ProcessingInstruction) node;
        String data = processingInstruction.getData();
        String target = processingInstruction.getTarget();
        serializer.processingInstruction(target + " " + data);
    } else if (node.getNodeType() == Node.COMMENT_NODE) {
    // ignore!
    } else if (node.getNodeType() == Node.ENTITY_REFERENCE_NODE) {
        EntityReference entityReference = (EntityReference) node;
        serializer.entityRef(entityReference.getNodeName());
    } else {
        throw new UnsupportedOperationException("Cannot emit " + node + " of type " + node.getNodeType());
    }
}
Also used : Element(org.w3c.dom.Element) EntityReference(org.w3c.dom.EntityReference) Document(org.w3c.dom.Document) ProcessingInstruction(org.w3c.dom.ProcessingInstruction)

Example 7 with EntityReference

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

the class ImportNode method testImportNode10.

// Assumes validation.
//    public void testImportNode9() throws Throwable {
//        Document doc;
//        Document aNewDoc;
//
//        NamedNodeMap entityList;
//        Entity entity2;
//        Entity entity1;
//        Document ownerDocument;
//        DocumentType docType;
//        String system;
//        String entityName;
//        String publicVal;
//        String notationName;
//        doc = (Document) load("staffNS", builder);
//        aNewDoc = (Document) load("staffNS", builder);
//        docType = aNewDoc.getDoctype();
//        entityList = docType.getEntities();
//        assertNotNull("entitiesNotNull", entityList);
//        entity2 = (Entity) entityList.getNamedItem("ent6");
//        entity1 = (Entity) doc.importNode(entity2, false);
//        ownerDocument = entity1.getOwnerDocument();
//        docType = ownerDocument.getDoctype();
//        system = docType.getSystemId();
//        assertURIEquals("dtdSystemId", null, null, null, "staffNS.dtd", null,
//                null, null, null, system);
//        entityName = entity1.getNodeName();
//        assertEquals("entityName", "ent6", entityName);
//        publicVal = entity1.getPublicId();
//        assertEquals("entityPublicId", "uri", publicVal);
//        system = entity1.getSystemId();
//        assertURIEquals("entitySystemId", null, null, null, "file", null, null,
//                null, null, system);
//        notationName = entity1.getNotationName();
//        assertEquals("notationName", "notation2", notationName);
//    }
public void testImportNode10() throws Throwable {
    Document doc;
    Document aNewDoc;
    EntityReference entRef;
    Node aNode;
    Document ownerDocument;
    DocumentType docType;
    String system;
    String name;
    doc = (Document) load("staffNS", builder);
    aNewDoc = (Document) load("staffNS", builder);
    entRef = aNewDoc.createEntityReference("entRef1");
    assertNotNull("createdEntRefNotNull", entRef);
    entRef.setNodeValue("entRef1Value");
    aNode = doc.importNode(entRef, false);
    ownerDocument = aNode.getOwnerDocument();
    docType = ownerDocument.getDoctype();
    system = docType.getSystemId();
    assertURIEquals("systemId", null, null, null, "staffNS.dtd", null, null, null, null, system);
    name = aNode.getNodeName();
    assertEquals("nodeName", "entRef1", name);
}
Also used : Node(org.w3c.dom.Node) EntityReference(org.w3c.dom.EntityReference) DocumentType(org.w3c.dom.DocumentType) Document(org.w3c.dom.Document)

Example 8 with EntityReference

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

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 9 with EntityReference

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

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());
            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();
                if (attrName.equals("xmlns") || attrName.startsWith("xmlns:")) {
                    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.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 10 with EntityReference

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

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