📄 elementimpl.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/Element.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 ElementImpl extends NodeImpl implements Element {
/**
* A <code>HashMap</code> of <code>AttrImpl</code> nodes representing
* attributes.
*/
protected HashMap attributes = null;
/**
* Constructs an empty <code>ElementImpl</code>.
*/
public ElementImpl() {
attributes = new HashMap();
type = ELEMENT_NODE;
}
/**
* Constructs a <code>ElementImpl</code> from the given node,
* without creating entire children subtree.
*
* @param element , as a <code>ElementImpl</code>.
*/
public ElementImpl(ElementImpl element) {
super(element);
attributes = element.attributes;
type = ELEMENT_NODE;
}
/**
* Constructs an <code>ElementImpl</code> with the given
* document owner and node name.
*
* @param ownerDoc the document owner of the node, as a <code>Document</code>.
* @param name is the name of the node, as a <code>String</code>.
*/
public ElementImpl(Document ownerDoc, String name) {
super(ownerDoc,name,ELEMENT_NODE);
this.attributes = new HashMap();
}
/**
* Constructs an <code>ElementImpl</code> with the 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>.
*/
protected ElementImpl(Document ownerDoc, String nodeName, short type, String value) {
super(ownerDoc, nodeName, type, value);
}
/**
* Constructs an <code>ElementImpl</code> from a given node (creates the children subtree too),
* as a <code>Node</code>
*
* @param node , as a <code>Node</code>.
*/
public ElementImpl(Node node) {
this(node,true);
}
/**
* Constructs an <code>ElementImpl</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 ElementImpl(Node node, boolean deep) {
super(node,false);
attributes = new HashMap();
NamedNodeMap attrs = node.getAttributes();
if (attrs != null) {
for (int i = 0; i < attrs.getLength(); i++) {
Attr attr = new AttrImpl((Attr) attrs.item(i));
attributes.put(attr.getName(), attr);
}
}
if (deep)
initNodeImplChildren(node);
}
/**
* Creates new instance of <code>ElementImpl</code> from a given document
* as a <code>Document</code>.
*
* @param document document.
*
* @return new <code>Element</code> node as a root of the <code>Document</code>.
*/
public static Element newInstance(Document document) {
Node root = document.getDocumentElement();
return new ElementImpl(root);
}
/**
* 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) {
super.insertBefore(newChild,refChild);
return newChild;
}
/**
* 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) {
super.replaceChild(newChild,oldChild);
return oldChild;
}
/**
* 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) {
super.removeChild(oldChild);
return oldChild;
}
/**
* 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 ElementImpl(this,deep);
}
// Methods from Element
/**
* Returns tag name of this node.
*
* @return tag name of this node as a <code>String</code>.
*/
public String getTagName() {
return nodeName;
}
/**
* Returns all attribute nodes of this node.
*
* @return all attribute nodes of this node as a <code>NamedNodeMap</code>.
*/
public NamedNodeMap getAttributes() {
return new HashMapNamedNodeMap(attributes);
}
/**
* Returns the value of the attribute with given name.
*
* @param name name of attribute.
*
* @return value of attribute.
*/
public String getAttribute(String name) {
Attr attr = getAttributeNode(name);
if (attr == null) {
return "";
}
return attr.getValue();
}
/**
* Equivalent to <code>getAttribute(localName)</code>.
*
* @see #setAttributeNS
* @param namespaceURI is name space
* @param localName is string
* @return node
*/
public String getAttributeNS(String namespaceURI, String localName) {
return getAttribute(localName);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -