📄 element.java
字号:
* parent or if the attribute namespace prefix collides with another
* namespace prefix on the element.
*/
public Element setAttribute(final Attribute attribute) {
attributes.add(attribute);
return this;
}
/**
* <p>
* This removes the attribute with the given name and within no
* namespace. If no such attribute exists, this method does nothing.
* </p>
*
* @param name name of attribute to remove
* @return whether the attribute was removed
*/
public boolean removeAttribute(final String name) {
return removeAttribute(name, Namespace.NO_NAMESPACE);
}
/**
* <p>
* This removes the attribute with the given name and within the
* given Namespace. If no such attribute exists, this method does
* nothing.
* </p>
*
* @param name name of attribute to remove
* @param ns namespace URI of attribute to remove
* @return whether the attribute was removed
*/
public boolean removeAttribute(final String name, final 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(final 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() {
final StringBuffer stringForm = new StringBuffer(64)
.append("[Element: <")
.append(getQualifiedName());
final String nsuri = getNamespaceURI();
if (!"".equals(nsuri)) {
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
final Element 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++) {
final Attribute attribute = (Attribute) attributes.get(i);
element.attributes.add(attribute.clone());
}
}
// Cloning additional namespaces
if (additionalNamespaces != null) {
element.additionalNamespaces = new ArrayList(additionalNamespaces);
}
// Cloning content
if (content != null) {
for(int i = 0; i < content.size(); i++) {
final Content c = (Content) content.get(i);
element.content.add(c.clone());
}
}
return element;
}
// Support a custom Namespace serialization so no two namespace
// object instances may exist for the same prefix/uri pair
private void writeObject(final 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 {
final int size = additionalNamespaces.size();
out.write(size);
for (int i = 0; i < size; i++) {
final Namespace additional = (Namespace) additionalNamespaces.get(i);
out.writeObject(additional.getPrefix());
out.writeObject(additional.getURI());
}
}
}
private void readObject(final ObjectInputStream in)
throws IOException, ClassNotFoundException {
in.defaultReadObject();
namespace = Namespace.getNamespace(
(String)in.readObject(), (String)in.readObject());
final int size = in.read();
if (size != 0) {
additionalNamespaces = new ArrayList(size);
for (int i = 0; i < size; i++) {
final 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(final Filter filter) {
final Iterator iterator = new DescendantIterator(this);
return new FilterIterator(iterator, 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(final 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(final String name, final 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(final String name, final Namespace ns) {
final List elements = content.getView(new ElementFilter(name, ns));
final Iterator iter = elements.iterator();
if (iter.hasNext()) {
return (Element) iter.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(final 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(final 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(final String name, final Namespace ns) {
final Filter filter = new ElementFilter(name, ns);
final List old = content.getView(filter);
final Iterator iter = old.iterator();
if (iter.hasNext()) {
iter.next();
iter.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(final 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(final String name, final Namespace ns) {
boolean deletedSome = false;
final Filter filter = new ElementFilter(name, ns);
final List old = content.getView(filter);
final Iterator iter = old.iterator();
while (iter.hasNext()) {
iter.next();
iter.remove();
deletedSome = true;
}
return deletedSome;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -