📄 nodeimpl.java
字号:
* <code>null</code>.
*/
public Node getNextSibling() {
return nextSibling;
}
/**
* Returns <code>null</code>, since <code>NodeImpl</code>s
* do not belong to any <code>Document</code>.
*
* @return document owner as <code>Document</code>.
*/
public Document getOwnerDocument() {
return ownerDocument;
}
/**
* Inserts the node <code>newChild</code> before the existing
* child node <code>refChild</code>. If <code>refChild</code> is
* <code>null</code>, insert <code>newChild</code> at the end of
* the list of children.
*
* @param newChild the <code>Node</code> to insert.
* @param refChild the reference <code>Node</code>.
*
* @return the node being inserted.
*
* @exception IllegalArgumentException if <code>newChild</code> is
* <code>null</code>.
*/
public Node insertBefore(Node newChild, Node refChild) {
if (newChild == null) {
throw new IllegalArgumentException("newChild == null!");
}
checkNode(newChild);
checkNode(refChild);
NodeImpl newChildNode = (NodeImpl) newChild;
NodeImpl refChildNode = (NodeImpl) refChild;
// Siblings, can be null.
NodeImpl previous = null;
NodeImpl next = null;
if (refChild == null) {
previous = this.lastChild;
next = null;
this.lastChild = newChildNode;
} else {
previous = refChildNode.previousSibling;
next = refChildNode;
}
if (previous != null) {
previous.nextSibling = newChildNode;
}
if (next != null) {
next.previousSibling = newChildNode;
}
newChildNode.parent = this;
newChildNode.previousSibling = previous;
newChildNode.nextSibling = next;
// N.B.: O.K. if refChild == null
if (this.firstChild == refChildNode) {
this.firstChild = newChildNode;
}
++numChildren;
return newChildNode;
}
/**
* Replaces the child node <code>oldChild</code> with
* <code>newChild</code> in the list of children, and returns the
* <code>oldChild</code> node.
*
* @param newChild the <code>Node</code> to insert.
* @param oldChild the <code>Node</code> to be replaced.
*
* @return the node replaced.
*
* @exception IllegalArgumentException if <code>newChild</code> is
* <code>null</code>.
*/
public Node replaceChild(Node newChild, Node oldChild) {
if (newChild == null) {
throw new IllegalArgumentException("newChild == null!");
}
checkNode(newChild);
checkNode(oldChild);
NodeImpl newChildNode = (NodeImpl) newChild;
NodeImpl oldChildNode = (NodeImpl) oldChild;
NodeImpl previous = oldChildNode.previousSibling;
NodeImpl next = oldChildNode.nextSibling;
if (previous != null) {
previous.nextSibling = newChildNode;
}
if (next != null) {
next.previousSibling = newChildNode;
}
newChildNode.parent = this;
newChildNode.previousSibling = previous;
newChildNode.nextSibling = next;
if (firstChild == oldChildNode) {
firstChild = newChildNode;
}
if (lastChild == oldChildNode) {
lastChild = newChildNode;
}
oldChildNode.parent = null;
oldChildNode.previousSibling = null;
oldChildNode.nextSibling = null;
return oldChildNode;
}
/**
* Removes the child node indicated by <code>oldChild</code> from
* the list of children, and returns it.
*
* @param oldChild the <code>Node</code> to be removed.
*
* @return the node removed.
*
* @exception IllegalArgumentException if <code>oldChild</code> is
* <code>null</code>.
*/
public Node removeChild(Node oldChild) {
if (oldChild == null) {
throw new IllegalArgumentException("oldChild == null!");
}
checkNode(oldChild);
NodeImpl oldChildNode = (NodeImpl) oldChild;
NodeImpl previous = oldChildNode.previousSibling;
NodeImpl next = oldChildNode.nextSibling;
if (previous != null) {
previous.nextSibling = next;
}
if (next != null) {
next.previousSibling = previous;
}
if (this.firstChild == oldChildNode) {
this.firstChild = next;
}
if (this.lastChild == oldChildNode) {
this.lastChild = previous;
}
oldChildNode.parent = null;
oldChildNode.previousSibling = null;
oldChildNode.nextSibling = null;
--numChildren;
return oldChildNode;
}
/**
* Adds the node <code>newChild</code> to the end of the list of
* children of this node.
*
* @param newChild the <code>Node</code> to insert.
*
* @return the node added.
*
* @exception IllegalArgumentException if <code>newChild</code> is
* <code>null</code>.
*/
public Node appendChild(Node newChild) {
if (newChild == null) {
throw new IllegalArgumentException("newChild == null!");
}
checkNode(newChild);
// insertBefore will increment numChildren
return insertBefore(newChild, null);
}
/**
* Returns <code>true</code> if this node has child nodes.
*
* @return <code>true</code> if this node has children.
*/
public boolean hasChildNodes() {
return numChildren > 0;
}
/**
* Returns a duplicate of this node. The duplicate node has no
* parent (<code>getParentNode</code> returns <code>null</code>).
* If a shallow clone is being performed (<code>deep</code> is
* <code>false</code>), the new node will not have any children or
* siblings. If a deep clone is being performed, the new node
* will form the root of a complete cloned subtree.
*
* @param deep if <code>true</code>, recursively clone the subtree
* under the specified node; if <code>false</code>, clone only the
* node itself.
*
* @return the duplicate node.
*/
public Node cloneNode(boolean deep) {
return new NodeImpl(this, deep);
}
/**
* Does nothing, since <code>NodeImpl</code>s do not
* contain <code>Text</code> children.
*/
public void normalize() {
}
/**
* Returns <code>false</code> since DOM features are not
* supported.
*
* @return <code>false</code>.
*
* @param feature a <code>String</code>, which is ignored.
* @param version a <code>String</code>, which is ignored.
*/
public boolean isSupported(String feature, String version) {
return false;
}
/**
* @return <code>null</code>, since namespaces are not supported.
* @throws DOMException
*/
public String getNamespaceURI() throws DOMException {
return null;
}
/**
* Returns <code>null</code>, since namespaces are not supported.
*
* @return <code>null</code>.
*
* @see #setPrefix
*/
public String getPrefix() {
return null;
}
/**
* Does nothing, since namespaces are not supported.
*
* @param prefix a <code>String</code>, which is ignored.
*
* @see #getPrefix
*/
public void setPrefix(String prefix) {
}
/**
* Equivalent to <code>getNodeName</code>.
*
* @return the node name, as a <code>String</code>.
*/
public String getLocalName() {
return nodeName;
}
/**
* Returns all attribute nodes of this node.
*
* @return all attribute nodes of this node.
*/
public NamedNodeMap getAttributes() {
return null;
}
/**
* Returns <code>true</code>, if this node has attributes, otherwise
* <code>false</code>.
*
* @return <code>true</code> if node has attributes, otherwise <code>false</code>..
*/
public boolean hasAttributes() {
return false;
}
// Methods from NodeList
/**
* Returns number of child nodes.
*
* @return all number of child nodes.
*/
public int getLength() {
return numChildren;
}
/**
* Returns child node with the given index.
*
* @return child node with the given index.
* @param index represents index
*/
public Node item(int index) {
if (index < 0) {
return null;
}
Node child = getFirstChild();
while (child != null && index-- > 0) {
child = child.getNextSibling();
}
return child;
}
// String methodes
/**
* Returns <code>String</code> representation of this node.
*
* @return <code>String</code> representation of this node.
*/
public String toString() {
return toString(Indent.DEFAULT_TAB);
}
/**
* Returns <code>String</code> representation of this node.
*
* @param tab tab for node indentation.
*
* @return <code>String</code> representation of this node.
*/
public String toString(String tab) {
StringBuffer sb = new StringBuffer();
this.allToString(sb, new Indent(0,tab));
return sb.toString();
}
/**
* Method beginToString should be redefined in extended classes.
* Each type of node has its own <code>beginToString and
* <code>endToString</code>. This was added to support
* writing of the xml file. The <code>Element</code>
* type of node: it writes the beginning tag, then calls
* the child's <code>toString</code>, and then writes the ending tag.
*
* @param sb string buffer to add resulting string.
* @param indent used in formating the output.
*/
protected void beginToString(StringBuffer sb, Indent indent) {
}
/**
* Method endToString should be redefined in extended classes.
* Each type of node has its own <code>beginToString and
* <code>endToString</code>. This was added to support
* writing of the xml file. The <code>Element</code>
* type of node: it writes the beginning tag, then calls
* the child's <code>toString</code>, and then writes the ending tag.
*
* @param sb string buffer to add resulting string.
* @param indent used in formating the output.
*/
protected void endToString(StringBuffer sb, Indent indent) {
}
private void allToString(StringBuffer sb, Indent indent) {
this.beginToString(sb, indent);
Node child = getFirstChild();
while (child != null) {
((NodeImpl) child).allToString(sb, indent);
child = child.getNextSibling();
}
this.endToString(sb, indent);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -