Example usage for org.w3c.dom DOMException HIERARCHY_REQUEST_ERR

List of usage examples for org.w3c.dom DOMException HIERARCHY_REQUEST_ERR

Introduction

In this page you can find the example usage for org.w3c.dom DOMException HIERARCHY_REQUEST_ERR.

Prototype

short HIERARCHY_REQUEST_ERR

To view the source code for org.w3c.dom DOMException HIERARCHY_REQUEST_ERR.

Click Source Link

Document

If any Node is inserted somewhere it doesn't belong.

Usage

From source file:com.gargoylesoftware.htmlunit.html.DomNode.java

/**
 * Check for insertion errors for a new child node. This is overridden by derived
 * classes to enforce which types of children are allowed.
 *
 * @param newChild the new child node that is being inserted below this node
 * @throws DOMException HIERARCHY_REQUEST_ERR: Raised if this node is of a type that does
 * not allow children of the type of the newChild node, or if the node to insert is one of
 * this node's ancestors or this node itself, or if this node is of type Document and the
 * DOM application attempts to insert a second DocumentType or Element node.
 * WRONG_DOCUMENT_ERR: Raised if newChild was created from a different document than the
 * one that created this node./*from w  w  w  .  j av a 2 s .c o  m*/
 */
protected void checkChildHierarchy(final Node newChild) throws DOMException {
    Node parentNode = this;
    while (parentNode != null) {
        if (parentNode == newChild) {
            throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, "Child node is already a parent.");
        }
        parentNode = parentNode.getParentNode();
    }
    final Document thisDocument = getOwnerDocument();
    final Document childDocument = newChild.getOwnerDocument();
    if (childDocument != thisDocument && childDocument != null) {
        throw new DOMException(DOMException.WRONG_DOCUMENT_ERR, "Child node " + newChild.getNodeName()
                + " is not in the same Document as this " + getNodeName() + ".");
    }
}

From source file:org.structr.web.entity.dom.DOMNode.java

protected void checkHierarchy(Node otherNode) throws DOMException {

    // we can only check DOMNodes
    if (otherNode instanceof DOMNode) {

        // verify that the other node is not this node
        if (isSameNode(otherNode)) {
            throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, HIERARCHY_REQUEST_ERR_MESSAGE_SAME_NODE);
        }/*from  w ww.j a v a 2 s  . co  m*/

        // verify that otherNode is not one of the
        // the ancestors of this node
        // (prevent circular relationships)
        Node _parent = getParentNode();
        while (_parent != null) {

            if (_parent.isSameNode(otherNode)) {
                throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR,
                        HIERARCHY_REQUEST_ERR_MESSAGE_ANCESTOR);
            }

            _parent = _parent.getParentNode();
        }

        // TODO: check hierarchy constraints imposed by the schema
        // validation sucessful
        return;
    }

    throw new DOMException(DOMException.NOT_SUPPORTED_ERR, NOT_SUPPORTED_ERR_MESSAGE);
}

From source file:com.gargoylesoftware.htmlunit.html.HtmlElement.java

/**
 * {@inheritDoc}/*  ww w . j  a  v  a  2s  . c o  m*/
 */
@Override
protected void checkChildHierarchy(final Node childNode) throws DOMException {
    if (!((childNode instanceof Element) || (childNode instanceof Text) || (childNode instanceof Comment)
            || (childNode instanceof ProcessingInstruction) || (childNode instanceof CDATASection)
            || (childNode instanceof EntityReference))) {
        throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR,
                "The Element may not have a child of this type: " + childNode.getNodeType());
    }
    super.checkChildHierarchy(childNode);
}

From source file:com.gargoylesoftware.htmlunit.html.HtmlPage.java

/**
 * {@inheritDoc}//from  ww  w  .j a v  a 2  s . c  o  m
 */
@Override
protected void checkChildHierarchy(final org.w3c.dom.Node newChild) throws DOMException {
    if (newChild instanceof Element) {
        if (getDocumentElement() != null) {
            throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR,
                    "The Document may only have a single child Element.");
        }
    } else if (newChild instanceof DocumentType) {
        if (getDoctype() != null) {
            throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR,
                    "The Document may only have a single child DocumentType.");
        }
    } else if (!((newChild instanceof Comment) || (newChild instanceof ProcessingInstruction))) {
        throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR,
                "The Document may not have a child of this type: " + newChild.getNodeType());
    }
    super.checkChildHierarchy(newChild);
}

From source file:org.apache.axis.message.NodeImpl.java

/**
 * Adds the node <code>newChild</code> to the end of the list of children
 * of this node. If the <code>newChild</code> is already in the tree, it
 * is first removed.//  ww w.j a va  2  s  .c  o  m
 * 
 * @param newChild The node to add.If it is a
 *                 <code>DocumentFragment</code> object, the entire contents of the
 *                 document fragment are moved into the child list of this node
 * @return The node added.
 * @throws org.w3c.dom.DOMException HIERARCHY_REQUEST_ERR: Raised if this node is of a type that does not
 *                                  allow children of the type of the <code>newChild</code> node, or if
 *                                  the node to append is one of this node's ancestors or this node
 *                                  itself.
 *                                  <br>WRONG_DOCUMENT_ERR: Raised if <code>newChild</code> was created
 *                                  from a different document than the one that created this node.
 *                                  <br>NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly or
 *                                  if the previous parent of the node being inserted is readonly.
 *
 */
public Node appendChild(Node newChild) throws DOMException {
    if (newChild == null) {
        throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, "Can't append a null node.");
    }
    initializeChildren();
    // per DOM spec - must remove from tree. If newChild.parent == null,
    // detachNode() does nothing.  So this shouldn't hurt performace of
    // serializers.
    ((NodeImpl) newChild).detachNode();
    children.add(newChild);
    ((NodeImpl) newChild).parent = this;
    setDirty();
    return newChild;
}

From source file:org.structr.web.entity.dom.Page.java

@Override
protected void checkHierarchy(Node otherNode) throws DOMException {

    // verify that this document has only one document element
    if (getDocumentElement() != null) {
        throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, HIERARCHY_REQUEST_ERR_MESSAGE_DOCUMENT);
    }/*from www.j ava2  s.  c om*/

    if (!(otherNode instanceof Html || otherNode instanceof Comment || otherNode instanceof Template)) {

        throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, HIERARCHY_REQUEST_ERR_MESSAGE_ELEMENT);
    }

    super.checkHierarchy(otherNode);
}