📄 nodeimpl.java
字号:
/*
Copyright (C) 2003 Together
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.enhydra.xml;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.HashMap;
import java.util.HashMap;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.DOMException;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
* @author Tweety
*
* A class representing a node in a meta-data tree, which implements
* the <a href="../../../../api/org/w3c/dom/Node.html">
*
* <p> Namespaces are ignored in this implementation. The terms "tag
* name" and "node name" are always considered to be synonymous.
*
* @version 1.0
*/
public class NodeImpl implements Node, NodeList {
/**
* Owner document.
*/
protected Document ownerDocument;
/**
* The name (tag) of the node as a <code>String</code>.
*/
protected String nodeName = null;
/**
* The value of the node as a <code>String</code>.
*/
protected String nodeValue = null;
/**
* The type of the node as a <code>short</code>.
*/
protected short type = ELEMENT_NODE;
/**
* The parent node of this node, or <code>null</code> if this node
* forms the root of its own tree.
*/
protected NodeImpl parent = null;
/**
* The number of child nodes.
*/
protected int numChildren = 0;
/**
* The first (leftmost) child node of this node, or
* <code>null</code> if this node is a leaf node.
*/
protected NodeImpl firstChild = null;
/**
* The last (rightmost) child node of this node, or
* <code>null</code> if this node is a leaf node.
*/
protected NodeImpl lastChild = null;
/**
* The next (right) sibling node of this node, or
* <code>null</code> if this node is its parent's last child node.
*/
protected NodeImpl nextSibling = null;
/**
* The previous (left) sibling node of this node, or
* <code>null</code> if this node is its parent's first child node.
*/
protected NodeImpl previousSibling = null;
/**
* Constructs an empty <code>NodeImpl</code>.
*/
public NodeImpl() {
}
/**
* Constructs a <code>NodeImpl</code> from the given node,
* without creating entire children subtree.
*
* @param node , as a <code>NodeImpl</code>.
*/
public NodeImpl(NodeImpl node) {
ownerDocument = node.ownerDocument;
nodeName = node.nodeName;
nodeValue = node.nodeValue;
type = node.type;
parent = node.parent;
numChildren = node.numChildren;
firstChild = node.firstChild;
lastChild = node.lastChild;
nextSibling = node.nextSibling;
previousSibling = node.previousSibling;
}
/**
* Constructs an <code>NodeImpl</code> from a given node (creates the children subtree too),
* as a <code>Node</code>
*
* @param node , as a <code>Node</code>.
*/
public NodeImpl(Node node) {
this(node, true);
}
/**
* Constructs an <code>NodeImpl</code> from a given node, as a <code>Node</code>,
* and deep as <code>boolean</code>.
*
* @param node , as a <code>Node</code>.
* @param deep if <code>true</code>, recursively clone the subtree
* under the specified node; if <code>false</code>, clone only the
* node itself.
*/
public NodeImpl(Node node, boolean deep) {
this.ownerDocument = node.getOwnerDocument();
this.nodeName = node.getNodeName();
this.type = node.getNodeType();
this.nodeValue = node.getNodeValue();
if (deep)
this.initNodeImplChildren(node);
}
/**
* Constructs a <code>NodeImpl</code> from the given document owner and node name.
*
* @param ownerDoc the document owner of the node, as a <code>Document</code>.
* @param name the name of the node, as a <code>String</code>.
*/
public NodeImpl(Document ownerDoc, String name) {
this.ownerDocument = ownerDoc;
this.nodeName = nodeName;
}
/**
* Constructs an <code>NodeImpl</code> from a given document owner,
* node name and node type.
*
* @param ownerDoc the document owner of the node, as a <code>Document</code>.
* @param nodeName the name of the node, as a <code>String</code>.
* @param type the type of the node, as a <code>short</code>.
*/
public NodeImpl(Document ownerDoc, String nodeName, short type) {
this.ownerDocument = ownerDoc;
this.nodeName = nodeName;
this.type = type;
}
/**
* Constructs an <code>NodeImpl</code> from a given document owner,
* node name, node type and node value.
*
* @param ownerDoc the document owner of the node, as a <code>Document</code>.
* @param nodeName the name of the node, as a <code>String</code>.
* @param type the type of the node, as a <code>short</code>.
* @param value the value of the node, as a <code>String</code>.
*/
public NodeImpl(Document ownerDoc, String nodeName, short type, String value) {
this.ownerDocument = ownerDoc;
this.nodeName = nodeName;
this.type = type;
this.nodeValue = value;
}
/**
* Creates the children subtree and adds to this node.
* (this part had to be splited from the constructor)
*
* @param node a <code>Node</code>.
*/
protected void initNodeImplChildren(Node node) {
// add children
Node child = node.getFirstChild();
while (child != null) {
switch (child.getNodeType()) {
case Node.ELEMENT_NODE : {
this.appendChild(newElementInstance(child));
}; break;
case Node.TEXT_NODE : {
this.appendChild(newTextInstance(child));
}; break;
case Node.COMMENT_NODE : {
this.appendChild(newCommentInstance(child));
}; break;
default : {
this.appendChild(newDefaultInstance(child));
};
}
child = child.getNextSibling();
}
}
/**
* Creates new instance of the ElementImpl class.
*
* @param node , as a <code>Node</code>.
* @return Node new instance of the ElementImpl class.
*/
protected Node newElementInstance(Node node) {
return new ElementImpl(node);
}
/**
* Creates new instance of the TextImpl class.
*
* @param node , as a <code>Node</code>.
* @return Node new instance of the TextImpl class.
*/
protected Node newTextInstance(Node node) {
return new TextImpl(node);
}
/**
* Creates new instance of the CommentImpl class.
*
* @param node , as a <code>Node</code>.
* @return Node new instance of the CommentImpl class.
*/
protected Node newCommentInstance(Node node) {
return new CommentImpl(node);
}
/**
* Creates new instance of the NodeImpl class.
*
* @param node , as a <code>Node</code>.
* @return Node new instance of the NodeImpl class.
*/
protected Node newDefaultInstance(Node node) {
return new NodeImpl(node);
}
/**
* Check that the node is either <code>null</code> or an
* <code>NodeImpl</code>.
*
* @param node , as a <code>Node</code>.
* @exception DOMException if node is not an instance of <code>NodeImpl</code>.
*/
protected void checkNode(Node node) throws DOMException {
if (node == null) {
return;
}
if (!(node instanceof NodeImpl))
throw new NodeDOMException(DOMException.WRONG_DOCUMENT_ERR, "Node is not an instance of NodeImpl!");
}
// Methods from Node
/**
* Returns the name associated with this node.
*
* @return the name, as a <code>String</code>.
*/
public String getNodeName() {
return nodeName;
}
/**
* Returns the value associated with this node.
*
* @return the node value, as a <code>String</code>.
*/
public String getNodeValue() {
return nodeValue;
}
/**
* Sets the node value of this node.
*
* @param nodeValue new node value, as a <code>String</code>.
*/
public void setNodeValue(String nodeValue) {
this.nodeValue = nodeValue;
}
/**
* Returns the node type.
*
* @return the <code>short</code> value node type.
*/
public short getNodeType() {
return type;
}
/**
* Returns the parent of this node. A <code>null</code> value
* indicates that the node is the root of its own tree. To add a
* node to an existing tree, use one of the
* <code>insertBefore</code>, <code>replaceChild</code>, or
* <code>appendChild</code> methods.
*
* @return the parent, as a <code>Node</code>.
*
* @see #insertBefore
* @see #replaceChild
* @see #appendChild
*/
public Node getParentNode() {
return parent;
}
/**
* Returns all child nodes of this node, or <code>null</code> if
* the node has no children.
*
* @return all child nodes of this node, as a <code>Node</code>, or
* <code>null</code>.
*/
public NodeList getChildNodes() {
return this;
}
/**
* Returns the first child of this node, or <code>null</code> if
* the node has no children.
*
* @return the first child, as a <code>Node</code>, or
* <code>null</code>
*/
public Node getFirstChild() {
return firstChild;
}
/**
* Returns the last child of this node, or <code>null</code> if
* the node has no children.
*
* @return the last child, as a <code>Node</code>, or
* <code>null</code>.
*/
public Node getLastChild() {
return lastChild;
}
/**
* Returns the previous sibling of this node, or <code>null</code>
* if this node has no previous sibling.
*
* @return the previous sibling, as a <code>Node</code>, or
* <code>null</code>.
*/
public Node getPreviousSibling() {
return previousSibling;
}
/**
* Returns the next sibling of this node, or <code>null</code> if
* the node has no next sibling.
*
* @return the next sibling, as a <code>Node</code>, or
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -