/**
   * Adds a node using its nodeName attribute. As the nodeName attribute is used to derive the name
   * which the node must be stored under, multiple nodes of certain types (those that have a
   * "special" string value) cannot be stored as the names would clash. This is seen as preferable
   * to allowing nodes to be aliased.
   *
   * @see mf.org.w3c.dom.NamedNodeMap#setNamedItem
   * @return If the new Node replaces an existing node the replaced Node is returned, otherwise null
   *     is returned.
   * @param arg A node to store in a named node map. The node will later be accessible using the
   *     value of the namespaceURI and localName attribute of the node. If a node with those
   *     namespace URI and local name is already present in the map, it is replaced by the new one.
   * @exception mf.org.w3c.dom.DOMException The exception description.
   */
  public Node setNamedItem(Node arg) throws DOMException {

    CoreDocumentImpl ownerDocument = ownerNode.ownerDocument();
    if (ownerDocument.errorChecking) {
      if (isReadOnly()) {
        String msg =
            DOMMessageFormatter.formatMessage(
                DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null);
        throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, msg);
      }
      if (arg.getOwnerDocument() != ownerDocument) {
        String msg =
            DOMMessageFormatter.formatMessage(
                DOMMessageFormatter.DOM_DOMAIN, "WRONG_DOCUMENT_ERR", null);
        throw new DOMException(DOMException.WRONG_DOCUMENT_ERR, msg);
      }
    }

    int i = findNamePoint(arg.getNodeName(), 0);
    NodeImpl previous = null;
    if (i >= 0) {
      previous = (NodeImpl) nodes.get(i);
      nodes.set(i, arg);
    } else {
      i = -1 - i; // Insert point (may be end of list)
      if (null == nodes) {
        nodes = new ArrayList(5);
      }
      nodes.add(i, arg);
    }
    return previous;
  } // setNamedItem(Node):Node
示例#2
0
  /** Return the current Node. */
  public void setCurrentNode(Node node) {
    if (node == null) {
      String msg =
          DOMMessageFormatter.formatMessage(
              DOMMessageFormatter.DOM_DOMAIN, "NOT_SUPPORTED_ERR", null);
      throw new DOMException(DOMException.NOT_SUPPORTED_ERR, msg);
    }

    fCurrentNode = node;
  }
 /*     */ public void setCurrentNode(Node node) {
   /* 102 */ if (node == null) {
     /* 103 */ String msg =
         DOMMessageFormatter.formatMessage(
             "http://www.w3.org/dom/DOMTR", "NOT_SUPPORTED_ERR", null);
     /* 104 */ throw new DOMException((short) 9, msg);
     /*     */ }
   /*     */
   /* 107 */ this.fCurrentNode = node;
   /*     */ }
  /**
   * Introduced in DOM Level 2.
   *
   * <p>Removes a node specified by local name and namespace URI.
   *
   * @param namespaceURI The namespace URI of the node to remove. When it is null or an empty
   *     string, this method behaves like removeNamedItem.
   * @param name The local name of the node to remove.
   * @return Node The node removed from the map if a node with such a local name and namespace URI
   *     exists.
   * @throws NOT_FOUND_ERR: Raised if there is no node named name in the map.
   */
  public Node removeNamedItemNS(String namespaceURI, String name) throws DOMException {

    if (isReadOnly()) {
      String msg =
          DOMMessageFormatter.formatMessage(
              DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null);
      throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, msg);
    }
    int i = findNamePoint(namespaceURI, name);
    if (i < 0) {
      String msg =
          DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NOT_FOUND_ERR", null);
      throw new DOMException(DOMException.NOT_FOUND_ERR, msg);
    }

    NodeImpl n = (NodeImpl) nodes.get(i);
    nodes.remove(i);

    return n;
  } // removeNamedItem(String):Node
 /**
  * Introduced in DOM Level 2.
  *
  * <p>Creates an XML Document object of the specified type with its document element.
  *
  * @param namespaceURI The namespace URI of the document element to create, or null.
  * @param qualifiedName The qualified name of the document element to create.
  * @param doctype The type of document to be created or null.
  *     <p>When doctype is not null, its Node.ownerDocument attribute is set to the document being
  *     created.
  * @return Document A new Document object.
  * @throws DOMException WRONG_DOCUMENT_ERR: Raised if doctype has already been used with a
  *     different document.
  * @since WD-DOM-Level-2-19990923
  */
 public Document createDocument(String namespaceURI, String qualifiedName, DocumentType doctype)
     throws DOMException {
   if (doctype != null && doctype.getOwnerDocument() != null) {
     throw new DOMException(
         DOMException.WRONG_DOCUMENT_ERR,
         DOMMessageFormatter.formatMessage(
             DOMMessageFormatter.XML_DOMAIN, "WRONG_DOCUMENT_ERR", null));
   }
   DocumentImpl doc = new PSVIDocumentImpl(doctype);
   Element e = doc.createElementNS(namespaceURI, qualifiedName);
   doc.appendChild(e);
   return doc;
 }
  /**
   * NON-DOM INTERNAL: Within DOM actions,we sometimes need to be able to control which mutation
   * events are spawned. This version of the removeChild operation allows us to do so. It is not
   * intended for use by application programs.
   */
  Node internalRemoveChild(Node oldChild, boolean replace) throws DOMException {

    CoreDocumentImpl ownerDocument = ownerDocument();
    if (ownerDocument.errorChecking) {
      if (isReadOnly()) {
        throw new DOMException(
            DOMException.NO_MODIFICATION_ALLOWED_ERR,
            DOMMessageFormatter.formatMessage(
                DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null));
      }
      if (oldChild != null && oldChild.getParentNode() != this) {
        throw new DOMException(
            DOMException.NOT_FOUND_ERR,
            DOMMessageFormatter.formatMessage(
                DOMMessageFormatter.DOM_DOMAIN, "NOT_FOUND_ERR", null));
      }
    }

    ChildNode oldInternal = (ChildNode) oldChild;

    // notify document
    ownerDocument.removingNode(this, oldInternal, replace);

    // update cached length if we have any
    if (fNodeListCache != null) {
      if (fNodeListCache.fLength != -1) {
        fNodeListCache.fLength--;
      }
      if (fNodeListCache.fChildIndex != -1) {
        // if the removed node is the cached node
        // move the cache to its (soon former) previous sibling
        if (fNodeListCache.fChild == oldInternal) {
          fNodeListCache.fChildIndex--;
          fNodeListCache.fChild = oldInternal.previousSibling();
        } else {
          // otherwise just invalidate the cache
          fNodeListCache.fChildIndex = -1;
        }
      }
    }

    // Patch linked list around oldChild
    // Note: lastChild == firstChild.previousSibling
    if (oldInternal == firstChild) {
      // removing first child
      oldInternal.isFirstChild(false);
      firstChild = oldInternal.nextSibling;
      if (firstChild != null) {
        firstChild.isFirstChild(true);
        firstChild.previousSibling = oldInternal.previousSibling;
      }
    } else {
      ChildNode prev = oldInternal.previousSibling;
      ChildNode next = oldInternal.nextSibling;
      prev.nextSibling = next;
      if (next == null) {
        // removing last child
        firstChild.previousSibling = prev;
      } else {
        // removing some other child in the middle
        next.previousSibling = prev;
      }
    }

    // Save previous sibling for normalization checking.
    ChildNode oldPreviousSibling = oldInternal.previousSibling();

    // Remove oldInternal's references to tree
    oldInternal.ownerNode = ownerDocument;
    oldInternal.isOwned(false);
    oldInternal.nextSibling = null;
    oldInternal.previousSibling = null;

    changed();

    // notify document
    ownerDocument.removedNode(this, replace);

    checkNormalizationAfterRemove(oldPreviousSibling);

    return oldInternal;
  } // internalRemoveChild(Node,boolean):Node
  /**
   * NON-DOM INTERNAL: Within DOM actions,we sometimes need to be able to control which mutation
   * events are spawned. This version of the insertBefore operation allows us to do so. It is not
   * intended for use by application programs.
   */
  Node internalInsertBefore(Node newChild, Node refChild, boolean replace) throws DOMException {

    boolean errorChecking = ownerDocument.errorChecking;

    if (newChild.getNodeType() == Node.DOCUMENT_FRAGMENT_NODE) {
      // SLOW BUT SAFE: We could insert the whole subtree without
      // juggling so many next/previous pointers. (Wipe out the
      // parent's child-list, patch the parent pointers, set the
      // ends of the list.) But we know some subclasses have special-
      // case behavior they add to insertBefore(), so we don't risk it.
      // This approch also takes fewer bytecodes.

      // NOTE: If one of the children is not a legal child of this
      // node, throw HIERARCHY_REQUEST_ERR before _any_ of the children
      // have been transferred. (Alternative behaviors would be to
      // reparent up to the first failure point or reparent all those
      // which are acceptable to the target node, neither of which is
      // as robust. PR-DOM-0818 isn't entirely clear on which it
      // recommends?????

      // No need to check kids for right-document; if they weren't,
      // they wouldn't be kids of that DocFrag.
      if (errorChecking) {
        for (Node kid = newChild.getFirstChild(); // Prescan
            kid != null;
            kid = kid.getNextSibling()) {

          if (!ownerDocument.isKidOK(this, kid)) {
            throw new DOMException(
                DOMException.HIERARCHY_REQUEST_ERR,
                DOMMessageFormatter.formatMessage(
                    DOMMessageFormatter.DOM_DOMAIN, "HIERARCHY_REQUEST_ERR", null));
          }
        }
      }

      while (newChild.hasChildNodes()) {
        insertBefore(newChild.getFirstChild(), refChild);
      }
      return newChild;
    }

    if (newChild == refChild) {
      // stupid case that must be handled as a no-op triggering events...
      refChild = refChild.getNextSibling();
      removeChild(newChild);
      insertBefore(newChild, refChild);
      return newChild;
    }

    if (needsSyncChildren()) {
      synchronizeChildren();
    }

    if (errorChecking) {
      if (isReadOnly()) {
        throw new DOMException(
            DOMException.NO_MODIFICATION_ALLOWED_ERR,
            DOMMessageFormatter.formatMessage(
                DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null));
      }
      if (newChild.getOwnerDocument() != ownerDocument && newChild != ownerDocument) {
        throw new DOMException(
            DOMException.WRONG_DOCUMENT_ERR,
            DOMMessageFormatter.formatMessage(
                DOMMessageFormatter.DOM_DOMAIN, "WRONG_DOCUMENT_ERR", null));
      }
      if (!ownerDocument.isKidOK(this, newChild)) {
        throw new DOMException(
            DOMException.HIERARCHY_REQUEST_ERR,
            DOMMessageFormatter.formatMessage(
                DOMMessageFormatter.DOM_DOMAIN, "HIERARCHY_REQUEST_ERR", null));
      }
      // refChild must be a child of this node (or null)
      if (refChild != null && refChild.getParentNode() != this) {
        throw new DOMException(
            DOMException.NOT_FOUND_ERR,
            DOMMessageFormatter.formatMessage(
                DOMMessageFormatter.DOM_DOMAIN, "NOT_FOUND_ERR", null));
      }

      // Prevent cycles in the tree
      // newChild cannot be ancestor of this Node,
      // and actually cannot be this
      if (ownerDocument.ancestorChecking) {
        boolean treeSafe = true;
        for (NodeImpl a = this; treeSafe && a != null; a = a.parentNode()) {
          treeSafe = newChild != a;
        }
        if (!treeSafe) {
          throw new DOMException(
              DOMException.HIERARCHY_REQUEST_ERR,
              DOMMessageFormatter.formatMessage(
                  DOMMessageFormatter.DOM_DOMAIN, "HIERARCHY_REQUEST_ERR", null));
        }
      }
    }

    // notify document
    ownerDocument.insertingNode(this, replace);

    // Convert to internal type, to avoid repeated casting
    ChildNode newInternal = (ChildNode) newChild;

    Node oldparent = newInternal.parentNode();
    if (oldparent != null) {
      oldparent.removeChild(newInternal);
    }

    // Convert to internal type, to avoid repeated casting
    ChildNode refInternal = (ChildNode) refChild;

    // Attach up
    newInternal.ownerNode = this;
    newInternal.isOwned(true);

    // Attach before and after
    // Note: firstChild.previousSibling == lastChild!!
    if (firstChild == null) {
      // this our first and only child
      firstChild = newInternal;
      newInternal.isFirstChild(true);
      newInternal.previousSibling = newInternal;
    } else {
      if (refInternal == null) {
        // this is an append
        ChildNode lastChild = firstChild.previousSibling;
        lastChild.nextSibling = newInternal;
        newInternal.previousSibling = lastChild;
        firstChild.previousSibling = newInternal;
      } else {
        // this is an insert
        if (refChild == firstChild) {
          // at the head of the list
          firstChild.isFirstChild(false);
          newInternal.nextSibling = firstChild;
          newInternal.previousSibling = firstChild.previousSibling;
          firstChild.previousSibling = newInternal;
          firstChild = newInternal;
          newInternal.isFirstChild(true);
        } else {
          // somewhere in the middle
          ChildNode prev = refInternal.previousSibling;
          newInternal.nextSibling = refInternal;
          prev.nextSibling = newInternal;
          refInternal.previousSibling = newInternal;
          newInternal.previousSibling = prev;
        }
      }
    }

    changed();

    // update cached length if we have any
    if (fNodeListCache != null) {
      if (fNodeListCache.fLength != -1) {
        fNodeListCache.fLength++;
      }
      if (fNodeListCache.fChildIndex != -1) {
        // if we happen to insert just before the cached node, update
        // the cache to the new node to match the cached index
        if (fNodeListCache.fChild == refInternal) {
          fNodeListCache.fChild = newInternal;
        } else {
          // otherwise just invalidate the cache
          fNodeListCache.fChildIndex = -1;
        }
      }
    }

    // notify document
    ownerDocument.insertedNode(this, newInternal, replace);

    checkNormalizationAfterInsert(newInternal);

    return newChild;
  } // internalInsertBefore(Node,Node,boolean):Node