📄 element.java
字号:
* @return whether the attribute was removed
*/
public boolean removeAttribute(String name, Namespace ns) {
return attributes.remove(name, ns);
}
/**
* <p>
* This removes the supplied Attribute should it exist.
* </p>
*
* @param attribute Reference to the attribute to be removed.
* @return whether the attribute was removed
*/
public boolean removeAttribute(Attribute attribute) {
return attributes.remove(attribute);
}
/**
* <p>
* This returns a <code>String</code> representation of the
* <code>Element</code>, suitable for debugging. If the XML
* representation of the <code>Element</code> is desired,
* {@link org.jdom.output.XMLOutputter#outputString(Element)}
* should be used.
* </p>
*
* @return <code>String</code> - information about the
* <code>Element</code>
*/
public String toString() {
StringBuffer stringForm = new StringBuffer(64)
.append("[Element: <")
.append(getQualifiedName());
String nsuri = getNamespaceURI();
if (!nsuri.equals("")) {
stringForm
.append(" [Namespace: ")
.append(nsuri)
.append("]");
}
stringForm.append("/>]");
return stringForm.toString();
}
/**
* <p>
* This returns a deep clone of this element.
* The new element is detached from its parent, and getParent()
* on the clone will return null.
* </p>
*
* @return the clone of this element
*/
public Object clone() {
// Ken Rune Helland <kenh@csc.no> is our local clone() guru
Element element = null;
element = (Element) super.clone();
// name and namespace are references to immutable objects
// so super.clone() handles them ok
// Reference to parent is copied by super.clone()
// (Object.clone()) so we have to remove it
// Actually, super is a Content, which has already detached in the clone().
// element.parent = null;
// Reference to content list and attribute lists are copyed by
// super.clone() so we set it new lists if the original had lists
element.content = new ContentList(element);
element.attributes = new AttributeList(element);
// Cloning attributes
if (attributes != null) {
for (int i = 0; i < attributes.size(); i++) {
Object obj = attributes.get(i);
Attribute attribute = (Attribute)((Attribute)obj).clone();
element.attributes.add(attribute);
}
}
// Cloning additional namespaces
if (additionalNamespaces != null) {
int additionalSize = additionalNamespaces.size();
element.additionalNamespaces = new ArrayList(additionalSize);
for (int i = 0; i < additionalSize; i++) {
Object additional = additionalNamespaces.get(i);
element.additionalNamespaces.add(additional);
}
}
// Cloning content
if (content != null) {
for (int i = 0; i < content.size(); i++) {
Object obj = content.get(i);
if (obj instanceof Element) {
Element elt = (Element)((Element)obj).clone();
element.content.add(elt);
} else if (obj instanceof CDATA) {
CDATA cdata = (CDATA)((CDATA)obj).clone();
element.content.add(cdata);
} else if (obj instanceof Text) {
Text text = (Text)((Text)obj).clone();
element.content.add(text);
} else if (obj instanceof Comment) {
Comment comment = (Comment)((Comment)obj).clone();
element.content.add(comment);
} else if (obj instanceof ProcessingInstruction) {
ProcessingInstruction pi = (ProcessingInstruction)
((ProcessingInstruction)obj).clone();
element.content.add(pi);
} else if (obj instanceof EntityRef) {
EntityRef entity = (EntityRef)((EntityRef)obj).clone();
element.content.add(entity);
}
}
}
// Handle additional namespaces
if (additionalNamespaces != null) {
// Avoid additionalNamespaces.clone() because List isn't Cloneable
element.additionalNamespaces = new ArrayList();
element.additionalNamespaces.addAll(additionalNamespaces);
}
return element;
}
// Support a custom Namespace serialization so no two namespace
// object instances may exist for the same prefix/uri pair
private void writeObject(ObjectOutputStream out) throws IOException {
out.defaultWriteObject();
// We use writeObject() and not writeUTF() to minimize space
// This allows for writing pointers to already written strings
out.writeObject(namespace.getPrefix());
out.writeObject(namespace.getURI());
if (additionalNamespaces == null) {
out.write(0);
}
else {
int size = additionalNamespaces.size();
out.write(size);
for (int i = 0; i < size; i++) {
Namespace additional = (Namespace) additionalNamespaces.get(i);
out.writeObject(additional.getPrefix());
out.writeObject(additional.getURI());
}
}
}
private void readObject(ObjectInputStream in)
throws IOException, ClassNotFoundException {
in.defaultReadObject();
namespace = Namespace.getNamespace(
(String)in.readObject(), (String)in.readObject());
int size = in.read();
if (size != 0) {
additionalNamespaces = new ArrayList(size);
for (int i = 0; i < size; i++) {
Namespace additional = Namespace.getNamespace(
(String)in.readObject(), (String)in.readObject());
additionalNamespaces.add(additional);
}
}
}
/**
* Returns an iterator that walks over all descendants in document order.
*
* @return an iterator to walk descendants
*/
public Iterator getDescendants() {
return new DescendantIterator(this);
}
/**
* Returns an iterator that walks over all descendants in document order
* applying the Filter to return only elements that match the filter rule.
* With filters you can match only Elements, only Comments, Elements or
* Comments, only Elements with a given name and/or prefix, and so on.
*
* @param filter filter to select which descendants to see
* @return an iterator to walk descendants within a filter
*/
public Iterator getDescendants(Filter filter) {
return new FilterIterator(new DescendantIterator(this), filter);
}
/**
* This returns a <code>List</code> of all the child elements
* nested directly (one level deep) within this element, as
* <code>Element</code> objects. If this target element has no nested
* elements, an empty List is returned. The returned list is "live"
* in document order and changes to it affect the element's actual
* contents.
*
* <p>
* Sequential traversal through the List is best done with a Iterator
* since the underlying implement of List.size() may not be the most
* efficient.
* </p>
*
* <p>
* No recursion is performed, so elements nested two levels deep
* would have to be obtained with:
* <pre>
* <code>
* Iterator itr = (currentElement.getChildren()).iterator();
* while(itr.hasNext()) {
* Element oneLevelDeep = (Element)itr.next();
* List twoLevelsDeep = oneLevelDeep.getChildren();
* // Do something with these children
* }
* </code>
* </pre>
* </p>
*
* @return list of child <code>Element</code> objects for this element
*/
public List getChildren() {
return content.getView(new ElementFilter());
}
/**
* This returns a <code>List</code> of all the child elements
* nested directly (one level deep) within this element with the given
* local name and belonging to no namespace, returned as
* <code>Element</code> objects. If this target element has no nested
* elements with the given name outside a namespace, an empty List
* is returned. The returned list is "live" in document order
* and changes to it affect the element's actual contents.
* <p>
* Please see the notes for <code>{@link #getChildren}</code>
* for a code example.
* </p>
*
* @param name local name for the children to match
* @return all matching child elements
*/
public List getChildren(String name) {
return getChildren(name, Namespace.NO_NAMESPACE);
}
/**
* This returns a <code>List</code> of all the child elements
* nested directly (one level deep) within this element with the given
* local name and belonging to the given Namespace, returned as
* <code>Element</code> objects. If this target element has no nested
* elements with the given name in the given Namespace, an empty List
* is returned. The returned list is "live" in document order
* and changes to it affect the element's actual contents.
* <p>
* Please see the notes for <code>{@link #getChildren}</code>
* for a code example.
* </p>
*
* @param name local name for the children to match
* @param ns <code>Namespace</code> to search within
* @return all matching child elements
*/
public List getChildren(String name, Namespace ns) {
return content.getView(new ElementFilter(name, ns));
}
/**
* This returns the first child element within this element with the
* given local name and belonging to the given namespace.
* If no elements exist for the specified name and namespace, null is
* returned.
*
* @param name local name of child element to match
* @param ns <code>Namespace</code> to search within
* @return the first matching child element, or null if not found
*/
public Element getChild(String name, Namespace ns) {
List elements = content.getView(new ElementFilter(name, ns));
Iterator i = elements.iterator();
if (i.hasNext()) {
return (Element) i.next();
}
return null;
}
/**
* This returns the first child element within this element with the
* given local name and belonging to no namespace.
* If no elements exist for the specified name and namespace, null is
* returned.
*
* @param name local name of child element to match
* @return the first matching child element, or null if not found
*/
public Element getChild(String name) {
return getChild(name, Namespace.NO_NAMESPACE);
}
/**
* <p>
* This removes the first child element (one level deep) with the
* given local name and belonging to no namespace.
* Returns true if a child was removed.
* </p>
*
* @param name the name of child elements to remove
* @return whether deletion occurred
*/
public boolean removeChild(String name) {
return removeChild(name, Namespace.NO_NAMESPACE);
}
/**
* <p>
* This removes the first child element (one level deep) with the
* given local name and belonging to the given namespace.
* Returns true if a child was removed.
* </p>
*
* @param name the name of child element to remove
* @param ns <code>Namespace</code> to search within
* @return whether deletion occurred
*/
public boolean removeChild(String name, Namespace ns) {
List old = content.getView(new ElementFilter(name, ns));
Iterator i = old.iterator();
if (i.hasNext()) {
i.next();
i.remove();
return true;
}
return false;
}
/**
* <p>
* This removes all child elements (one level deep) with the
* given local name and belonging to no namespace.
* Returns true if any were removed.
* </p>
*
* @param name the name of child elements to remove
* @return whether deletion occurred
*/
public boolean removeChildren(String name) {
return removeChildren(name, Namespace.NO_NAMESPACE);
}
/**
* <p>
* This removes all child elements (one level deep) with the
* given local name and belonging to the given namespace.
* Returns true if any were removed.
* </p>
*
* @param name the name of child elements to remove
* @param ns <code>Namespace</code> to search within
* @return whether deletion occurred
*/
public boolean removeChildren(String name, Namespace ns) {
boolean deletedSome = false;
List old = content.getView(new ElementFilter(name, ns));
Iterator i = old.iterator();
while (i.hasNext()) {
i.next();
i.remove();
deletedSome = true;
}
return deletedSome;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -