Search in sources :

Example 61 with NamedNodeMap

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

the class NamedNodeMapSetNamedItemNS method testSetNamedItemNS1.

/**
     * Runs the test case.
     *
     * @throws Throwable
     *             Any uncaught exception causes test to fail
     */
public void testSetNamedItemNS1() throws Throwable {
    Document doc;
    NamedNodeMap attributes;
    Node element;
    Attr attribute;
    Attr newAttr1;
    NodeList elementList;
    String attrName;
    doc = (Document) load("staffNS", builder);
    elementList = doc.getElementsByTagNameNS("http://www.nist.gov", "address");
    element = elementList.item(0);
    attributes = element.getAttributes();
    newAttr1 = doc.createAttributeNS("http://www.w3.org/DOM/L1", "streets");
    ((Element) /* Node */
    element).setAttributeNodeNS(newAttr1);
    attribute = (Attr) attributes.getNamedItemNS("http://www.w3.org/DOM/L1", "streets");
    attrName = attribute.getNodeName();
    assertEquals("namednodemapsetnameditemns01", "streets", attrName);
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) Attr(org.w3c.dom.Attr)

Example 62 with NamedNodeMap

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

the class RemoveNamedItemNS method testRemoveNamedItemNS2.

public void testRemoveNamedItemNS2() throws Throwable {
    String namespaceURI = "http://www.usa.com";
    String localName = "domest";
    Document doc;
    NodeList elementList;
    Node testAddress;
    NamedNodeMap attributes;
    doc = (Document) load("staffNS", builder);
    elementList = doc.getElementsByTagName("address");
    testAddress = elementList.item(1);
    attributes = testAddress.getAttributes();
    {
        boolean success = false;
        try {
            attributes.removeNamedItemNS(namespaceURI, localName);
        } catch (DOMException ex) {
            success = (ex.code == DOMException.NOT_FOUND_ERR);
        }
        assertTrue("throw_NOT_FOUND_ERR", success);
    }
}
Also used : DOMException(org.w3c.dom.DOMException) NamedNodeMap(org.w3c.dom.NamedNodeMap) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document)

Example 63 with NamedNodeMap

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

the class DocumentImpl method shallowCopy.

/**
     * Returns a shallow copy of the given node. If the node is an element node,
     * its attributes are always copied.
     *
     * @param node a node belonging to any document or DOM implementation.
     * @param operation the operation type to use when notifying user data
     *     handlers of copied element attributes. It is the caller's
     *     responsibility to notify user data handlers of the returned node.
     * @return a new node whose document is this document and whose DOM
     *     implementation is this DOM implementation.
     */
private NodeImpl shallowCopy(short operation, Node node) {
    switch(node.getNodeType()) {
        case Node.ATTRIBUTE_NODE:
            AttrImpl attr = (AttrImpl) node;
            AttrImpl attrCopy;
            if (attr.namespaceAware) {
                attrCopy = createAttributeNS(attr.getNamespaceURI(), attr.getLocalName());
                attrCopy.setPrefix(attr.getPrefix());
            } else {
                attrCopy = createAttribute(attr.getName());
            }
            attrCopy.setNodeValue(attr.getValue());
            return attrCopy;
        case Node.CDATA_SECTION_NODE:
            return createCDATASection(((CharacterData) node).getData());
        case Node.COMMENT_NODE:
            return createComment(((Comment) node).getData());
        case Node.DOCUMENT_FRAGMENT_NODE:
            return createDocumentFragment();
        case Node.DOCUMENT_NODE:
        case Node.DOCUMENT_TYPE_NODE:
            throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "Cannot copy node of type " + node.getNodeType());
        case Node.ELEMENT_NODE:
            ElementImpl element = (ElementImpl) node;
            ElementImpl elementCopy;
            if (element.namespaceAware) {
                elementCopy = createElementNS(element.getNamespaceURI(), element.getLocalName());
                elementCopy.setPrefix(element.getPrefix());
            } else {
                elementCopy = createElement(element.getTagName());
            }
            NamedNodeMap attributes = element.getAttributes();
            for (int i = 0; i < attributes.getLength(); i++) {
                AttrImpl elementAttr = (AttrImpl) attributes.item(i);
                AttrImpl elementAttrCopy = (AttrImpl) shallowCopy(operation, elementAttr);
                notifyUserDataHandlers(operation, elementAttr, elementAttrCopy);
                if (elementAttr.namespaceAware) {
                    elementCopy.setAttributeNodeNS(elementAttrCopy);
                } else {
                    elementCopy.setAttributeNode(elementAttrCopy);
                }
            }
            return elementCopy;
        case Node.ENTITY_NODE:
        case Node.NOTATION_NODE:
            // TODO: implement this when we support these node types
            throw new UnsupportedOperationException();
        case Node.ENTITY_REFERENCE_NODE:
            /*
             * When we support entities in the doctype, this will need to
             * behave differently for clones vs. imports. Clones copy
             * entities by value, copying the referenced subtree from the
             * original document. Imports copy entities by reference,
             * possibly referring to a different subtree in the new
             * document.
             */
            return createEntityReference(node.getNodeName());
        case Node.PROCESSING_INSTRUCTION_NODE:
            ProcessingInstruction pi = (ProcessingInstruction) node;
            return createProcessingInstruction(pi.getTarget(), pi.getData());
        case Node.TEXT_NODE:
            return createTextNode(((Text) node).getData());
        default:
            throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "Unsupported node type " + node.getNodeType());
    }
}
Also used : DOMException(org.w3c.dom.DOMException) NamedNodeMap(org.w3c.dom.NamedNodeMap) ProcessingInstruction(org.w3c.dom.ProcessingInstruction)

Example 64 with NamedNodeMap

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

the class NodeImpl method lookupNamespaceURI.

public final String lookupNamespaceURI(String prefix) {
    NodeImpl target = getNamespacingElement();
    for (NodeImpl node = target; node != null; node = node.getContainingElement()) {
        // check this element's namespace first
        String nodePrefix = node.getPrefix();
        if (node.getNamespaceURI() != null) {
            if (// null => default prefix
            prefix == null ? nodePrefix == null : prefix.equals(nodePrefix)) {
                return node.getNamespaceURI();
            }
        }
        //          non default: xmlns:specifiedPrefix="http://resultUri"
        if (!node.hasAttributes()) {
            continue;
        }
        NamedNodeMap attributes = node.getAttributes();
        for (int i = 0, length = attributes.getLength(); i < length; i++) {
            Node attr = attributes.item(i);
            if (!"http://www.w3.org/2000/xmlns/".equals(attr.getNamespaceURI())) {
                continue;
            }
            if (// null => default prefix
            prefix == null ? "xmlns".equals(attr.getNodeName()) : "xmlns".equals(attr.getPrefix()) && prefix.equals(attr.getLocalName())) {
                String value = attr.getNodeValue();
                return value.length() > 0 ? value : null;
            }
        }
    }
    return null;
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) Node(org.w3c.dom.Node)

Example 65 with NamedNodeMap

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

the class DOMHelper method getNamespaceOfNode.

/**
   * Returns the namespace of the given node. Differs from simply getting
   * the node's prefix and using getNamespaceForPrefix in that it attempts
   * to cache some of the data in NSINFO objects, to avoid repeated lookup.
   * TODO: Should we consider moving that logic into getNamespaceForPrefix?
   *
   * @param n Node to be examined.
   *
   * @return String containing the Namespace Name (uri) for this node.
   * Note that this is undefined for any nodes other than Elements and
   * Attributes.
   */
public String getNamespaceOfNode(Node n) {
    String namespaceOfPrefix;
    boolean hasProcessedNS;
    NSInfo nsInfo;
    short ntype = n.getNodeType();
    if (Node.ATTRIBUTE_NODE != ntype) {
        // return value
        Object nsObj = m_NSInfos.get(n);
        nsInfo = (nsObj == null) ? null : (NSInfo) nsObj;
        hasProcessedNS = (nsInfo == null) ? false : nsInfo.m_hasProcessedNS;
    } else {
        hasProcessedNS = false;
        nsInfo = null;
    }
    if (hasProcessedNS) {
        namespaceOfPrefix = nsInfo.m_namespace;
    } else {
        namespaceOfPrefix = null;
        String nodeName = n.getNodeName();
        int indexOfNSSep = nodeName.indexOf(':');
        String prefix;
        if (Node.ATTRIBUTE_NODE == ntype) {
            if (indexOfNSSep > 0) {
                prefix = nodeName.substring(0, indexOfNSSep);
            } else {
                // there isn't a prefix, we're done.
                return namespaceOfPrefix;
            }
        } else {
            prefix = (indexOfNSSep >= 0) ? nodeName.substring(0, indexOfNSSep) : "";
        }
        boolean ancestorsHaveXMLNS = false;
        boolean nHasXMLNS = false;
        if (prefix.equals("xml")) {
            namespaceOfPrefix = QName.S_XMLNAMESPACEURI;
        } else {
            int parentType;
            Node parent = n;
            while ((null != parent) && (null == namespaceOfPrefix)) {
                if ((null != nsInfo) && (nsInfo.m_ancestorHasXMLNSAttrs == NSInfo.ANCESTORNOXMLNS)) {
                    break;
                }
                parentType = parent.getNodeType();
                if ((null == nsInfo) || nsInfo.m_hasXMLNSAttrs) {
                    boolean elementHasXMLNS = false;
                    if (parentType == Node.ELEMENT_NODE) {
                        NamedNodeMap nnm = parent.getAttributes();
                        for (int i = 0; i < nnm.getLength(); i++) {
                            Node attr = nnm.item(i);
                            String aname = attr.getNodeName();
                            if (aname.charAt(0) == 'x') {
                                boolean isPrefix = aname.startsWith("xmlns:");
                                if (aname.equals("xmlns") || isPrefix) {
                                    if (n == parent)
                                        nHasXMLNS = true;
                                    elementHasXMLNS = true;
                                    ancestorsHaveXMLNS = true;
                                    String p = isPrefix ? aname.substring(6) : "";
                                    if (p.equals(prefix)) {
                                        namespaceOfPrefix = attr.getNodeValue();
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    if ((Node.ATTRIBUTE_NODE != parentType) && (null == nsInfo) && (n != parent)) {
                        nsInfo = elementHasXMLNS ? m_NSInfoUnProcWithXMLNS : m_NSInfoUnProcWithoutXMLNS;
                        m_NSInfos.put(parent, nsInfo);
                    }
                }
                if (Node.ATTRIBUTE_NODE == parentType) {
                    parent = getParentOfNode(parent);
                } else {
                    m_candidateNoAncestorXMLNS.addElement(parent);
                    m_candidateNoAncestorXMLNS.addElement(nsInfo);
                    parent = parent.getParentNode();
                }
                if (null != parent) {
                    // return value
                    Object nsObj = m_NSInfos.get(parent);
                    nsInfo = (nsObj == null) ? null : (NSInfo) nsObj;
                }
            }
            int nCandidates = m_candidateNoAncestorXMLNS.size();
            if (nCandidates > 0) {
                if ((false == ancestorsHaveXMLNS) && (null == parent)) {
                    for (int i = 0; i < nCandidates; i += 2) {
                        Object candidateInfo = m_candidateNoAncestorXMLNS.elementAt(i + 1);
                        if (candidateInfo == m_NSInfoUnProcWithoutXMLNS) {
                            m_NSInfos.put(m_candidateNoAncestorXMLNS.elementAt(i), m_NSInfoUnProcNoAncestorXMLNS);
                        } else if (candidateInfo == m_NSInfoNullWithoutXMLNS) {
                            m_NSInfos.put(m_candidateNoAncestorXMLNS.elementAt(i), m_NSInfoNullNoAncestorXMLNS);
                        }
                    }
                }
                m_candidateNoAncestorXMLNS.removeAllElements();
            }
        }
        if (Node.ATTRIBUTE_NODE != ntype) {
            if (null == namespaceOfPrefix) {
                if (ancestorsHaveXMLNS) {
                    if (nHasXMLNS)
                        m_NSInfos.put(n, m_NSInfoNullWithXMLNS);
                    else
                        m_NSInfos.put(n, m_NSInfoNullWithoutXMLNS);
                } else {
                    m_NSInfos.put(n, m_NSInfoNullNoAncestorXMLNS);
                }
            } else {
                m_NSInfos.put(n, new NSInfo(namespaceOfPrefix, nHasXMLNS));
            }
        }
    }
    return namespaceOfPrefix;
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) Node(org.w3c.dom.Node)

Aggregations

NamedNodeMap (org.w3c.dom.NamedNodeMap)993 Node (org.w3c.dom.Node)690 NodeList (org.w3c.dom.NodeList)339 Attr (org.w3c.dom.Attr)295 Element (org.w3c.dom.Element)238 Document (org.w3c.dom.Document)154 ArrayList (java.util.ArrayList)92 HashMap (java.util.HashMap)91 DocumentBuilder (javax.xml.parsers.DocumentBuilder)59 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)58 IOException (java.io.IOException)53 SAXException (org.xml.sax.SAXException)41 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)40 List (java.util.List)36 Map (java.util.Map)33 File (java.io.File)27 InputStream (java.io.InputStream)26 CMNamedNodeMap (org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap)25 DOMException (org.w3c.dom.DOMException)25 ByteArrayInputStream (java.io.ByteArrayInputStream)19