Search in sources :

Example 26 with DOMException

use of org.w3c.dom.DOMException 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 27 with DOMException

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

the class DocumentImpl method adoptNode.

/**
     * Detaches the node from its parent (if any) and changes its document to
     * this document. The node's subtree and attributes will remain attached,
     * but their document will be changed to this document.
     */
public Node adoptNode(Node node) {
    if (!(node instanceof NodeImpl)) {
        // the API specifies this quiet failure
        return null;
    }
    NodeImpl nodeImpl = (NodeImpl) node;
    switch(nodeImpl.getNodeType()) {
        case Node.ATTRIBUTE_NODE:
            AttrImpl attr = (AttrImpl) node;
            if (attr.ownerElement != null) {
                attr.ownerElement.removeAttributeNode(attr);
            }
            break;
        case Node.DOCUMENT_FRAGMENT_NODE:
        case Node.ENTITY_REFERENCE_NODE:
        case Node.PROCESSING_INSTRUCTION_NODE:
        case Node.TEXT_NODE:
        case Node.CDATA_SECTION_NODE:
        case Node.COMMENT_NODE:
        case Node.ELEMENT_NODE:
            break;
        case Node.DOCUMENT_NODE:
        case Node.DOCUMENT_TYPE_NODE:
        case Node.ENTITY_NODE:
        case Node.NOTATION_NODE:
            throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "Cannot adopt nodes of type " + nodeImpl.getNodeType());
        default:
            throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "Unsupported node type " + node.getNodeType());
    }
    Node parent = nodeImpl.getParentNode();
    if (parent != null) {
        parent.removeChild(nodeImpl);
    }
    changeDocumentToThis(nodeImpl);
    notifyUserDataHandlers(UserDataHandler.NODE_ADOPTED, node, null);
    return nodeImpl;
}
Also used : DOMException(org.w3c.dom.DOMException) Node(org.w3c.dom.Node)

Example 28 with DOMException

use of org.w3c.dom.DOMException 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 29 with DOMException

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

the class CreateAttributeNS method testCreateAttributeNS4.

public void testCreateAttributeNS4() throws Throwable {
    String namespaceURI = "http://www.w3.org/XML/1998/namespaces";
    String qualifiedName = "xml:attr1";
    Document doc;
    doc = (Document) load("staffNS", builder);
    {
        boolean success = false;
        try {
            doc.createAttributeNS(namespaceURI, qualifiedName);
        } catch (DOMException ex) {
            success = (ex.code == DOMException.NAMESPACE_ERR);
        }
        assertTrue("throw_NAMESPACE_ERR", success);
    }
}
Also used : DOMException(org.w3c.dom.DOMException) Document(org.w3c.dom.Document)

Example 30 with DOMException

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

the class CreateAttributeNS method testCreateAttributeNS1.

/**
     * Runs the test case.
     *
     * @throws Throwable
     *             Any uncaught exception causes test to fail
     */
public void testCreateAttributeNS1() throws Throwable {
    String namespaceURI = "http://www.ecommerce.org/";
    String malformedName = "prefix::local";
    Document doc;
    doc = (Document) load("staffNS", builder);
    {
        boolean success = false;
        try {
            doc.createAttributeNS(namespaceURI, malformedName);
        } catch (DOMException ex) {
            success = (ex.code == DOMException.NAMESPACE_ERR);
        }
        assertTrue("throw_NAMESPACE_ERR", success);
    }
}
Also used : DOMException(org.w3c.dom.DOMException) Document(org.w3c.dom.Document)

Aggregations

DOMException (org.w3c.dom.DOMException)323 Document (org.w3c.dom.Document)165 Element (org.w3c.dom.Element)131 Node (org.w3c.dom.Node)73 NodeList (org.w3c.dom.NodeList)57 DocumentBuilder (javax.xml.parsers.DocumentBuilder)42 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)42 Attr (org.w3c.dom.Attr)40 DOMImplementation (org.w3c.dom.DOMImplementation)37 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)28 FrameworkException (org.structr.common.error.FrameworkException)27 IOException (java.io.IOException)26 NamedNodeMap (org.w3c.dom.NamedNodeMap)25 DocumentType (org.w3c.dom.DocumentType)19 ArrayList (java.util.ArrayList)17 Text (org.w3c.dom.Text)17 DOMNode (org.structr.web.entity.dom.DOMNode)16 XPathExpressionException (javax.xml.xpath.XPathExpressionException)15 SAXException (org.xml.sax.SAXException)13 TransformerException (javax.xml.transform.TransformerException)12