/*--
$Id: Element.java,v 1.2 2013-09-20 15:11:33 fbusquets Exp $
Copyright (C) 2000-2004 Jason Hunter & Brett McLaughlin.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions, and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions, and the disclaimer that follows
these conditions in the documentation and/or other materials
provided with the distribution.
3. The name "JDOM" must not be used to endorse or promote products
derived from this software without prior written permission. For
written permission, please contact
* When implementing an Element subclass which doesn't require full control
* over variable initialization, be aware that simply calling super() (or
* letting the compiler add the implicit super() call) will not initialize
* the instance variables which will cause many of the methods to throw a
* NullPointerException. Therefore, the constructor for these subclasses
* should call one of the public constructors so variable initialization is
* handled automatically.
*/
protected Element() { }
/**
* Creates a new element with the supplied (local) name and namespace. If
* the provided namespace is null, the element will have no namespace.
*
* @param name local name of the element
* @param namespace namespace for the element
* @throws IllegalNameException if the given name is illegal as an element
* name
*/
public Element(String name, Namespace namespace) {
setName(name);
setNamespace(namespace);
}
/**
* Create a new element with the supplied (local) name and no namespace.
*
* @param name local name of the element
* @throws IllegalNameException if the given name is illegal as an element
* name.
*/
public Element(String name) {
this(name, (Namespace) null);
}
/**
* Creates a new element with the supplied (local) name and a namespace
* given by a URI. The element will be put into the unprefixed (default)
* namespace.
*
* @param name name of the element
* @param uri namespace URI for the element
* @throws IllegalNameException if the given name is illegal as an element
* name or the given URI is illegal as a
* namespace URI
*/
public Element(String name, String uri) {
this(name, Namespace.getNamespace("", uri));
}
/**
* Creates a new element with the supplied (local) name and a namespace
* given by the supplied prefix and URI combination.
*
* @param name local name of the element
* @param prefix namespace prefix
* @param uri namespace URI for the element
* @throws IllegalNameException if the given name is illegal as an element
* name, the given prefix is illegal as a
* namespace prefix, or the given URI is
* illegal as a namespace URI
*/
public Element(String name, String prefix, String uri) {
this(name, Namespace.getNamespace(prefix, uri));
}
/**
* Returns the (local) name of the element (without any namespace prefix).
*
* @return local element name
*/
public String getName() {
return name;
}
/**
* Sets the (local) name of the element.
*
* @param name the new (local) name of the element
* @return the target element
* @throws IllegalNameException if the given name is illegal as an Element
* name
*/
public Element setName(String name) {
String reason = Verifier.checkElementName(name);
if (reason != null) {
throw new IllegalNameException(name, "element", reason);
}
this.name = name;
return this;
}
/**
* Returns the element's {@link Namespace}.
*
* @return the element's namespace
*/
public Namespace getNamespace() {
return namespace;
}
/**
* Sets the element's {@link Namespace}. If the provided namespace is null,
* the element will have no namespace.
*
* @param namespace the new namespace
* @return the target element
*/
public Element setNamespace(Namespace namespace) {
if (namespace == null) {
namespace = Namespace.NO_NAMESPACE;
}
this.namespace = namespace;
return this;
}
/**
* Returns the namespace prefix of the element or an empty string if none
* exists.
*
* @return the namespace prefix
*/
public String getNamespacePrefix() {
return namespace.getPrefix();
}
/**
* Returns the namespace URI mapped to this element's prefix (or the
* in-scope default namespace URI if no prefix). If no mapping is found, an
* empty string is returned.
*
* @return the namespace URI for this element
*/
public String getNamespaceURI() {
return namespace.getURI();
}
/**
* Returns the {@link Namespace} corresponding to the given prefix in scope
* for this element. This involves searching up the tree, so the results
* depend on the current location of the element. Returns null if there is
* no namespace in scope with the given prefix at this point in the
* document.
*
* @param prefix namespace prefix to look up
* @return the Namespace for this prefix at this
* location, or null if none
*/
public Namespace getNamespace(String prefix) {
if (prefix == null) {
return null;
}
if (prefix.equals("xml")) {
// Namespace "xml" is always bound.
return Namespace.XML_NAMESPACE;
}
// Check if the prefix is the prefix for this element
if (prefix.equals(getNamespacePrefix())) {
return getNamespace();
}
// Scan the additional namespaces
if (additionalNamespaces != null) {
for (int i = 0; i < additionalNamespaces.size(); i++) {
Namespace ns = (Namespace) additionalNamespaces.get(i);
if (prefix.equals(ns.getPrefix())) {
return ns;
}
}
}
// If we still don't have a match, ask the parent
if (parent instanceof Element) {
return ((Element)parent).getNamespace(prefix);
}
return null;
}
/**
* Returns the full name of the element, in the form
* [namespacePrefix]:[localName]. If the element does not have a namespace
* prefix, then the local name is returned.
*
* @return qualified name of the element (including
* namespace prefix)
*/
public String getQualifiedName() {
// Note: Any changes here should be reflected in
// XMLOutputter.printQualifiedName()
if (namespace.getPrefix().equals("")) {
return getName();
}
return new StringBuilder(namespace.getPrefix())
.append(':')
.append(name)
// JCLIC modification
//.toString();
.substring(0);
}
/**
* Adds a namespace declarations to this element. This should not be
* used to add the declaration for this element itself; that should be
* assigned in the construction of the element. Instead, this is for adding
* namespace declarations on the element not relating directly to itself.
* It's used during output to for stylistic reasons move namespace
* declarations higher in the tree than they would have to be.
*
* @param additional namespace to add
* @throws IllegalAddException if the namespace prefix collides with another
* namespace prefix on the element
*/
public void addNamespaceDeclaration(Namespace additional) {
// Verify the new namespace prefix doesn't collide with another
// declared namespace, an attribute prefix, or this element's prefix
String reason = Verifier.checkNamespaceCollision(additional, this);
if (reason != null) {
throw new IllegalAddException(this, additional, reason);
}
if (additionalNamespaces == null) {
additionalNamespaces = new ArrayList(INITIAL_ARRAY_SIZE);
}
additionalNamespaces.add(additional);
}
/**
* Removes an additional namespace declarations from this element. This
* should not be used to remove the declaration for this element
* itself; that should be handled in the construction of the element.
* Instead, this is for removing namespace declarations on the element not
* relating directly to itself. If the declaration is not present, this
* method does nothing.
*
* @param additionalNamespace namespace to remove
*/
public void removeNamespaceDeclaration(Namespace additionalNamespace) {
if (additionalNamespaces == null) {
return;
}
additionalNamespaces.remove(additionalNamespace);
}
/**
* Returns a list of the additional namespace declarations on this element.
* This includes only additional namespace, not the namespace of the element
* itself, which can be obtained through {@link #getNamespace()}. If there
* are no additional declarations, this returns an empty list. Note, the
* returned list is unmodifiable.
*
* @return a List of the additional namespace
* declarations
*/
public List getAdditionalNamespaces() {
// Not having the returned list be live allows us to avoid creating a
// new list object when XMLOutputter calls this method on an element
// with an empty list.
if (additionalNamespaces == null) {
return Collections.EMPTY_LIST;
}
return Collections.unmodifiableList(additionalNamespaces);
}
/**
* Returns the XPath 1.0 string value of this element, which is the
* complete, ordered content of all text node descendants of this element
* (i.e. the text that's left after all references are resolved
* and all other markup is stripped out.)
*
* @return a concatentation of all text node descendants
*/
public String getValue() {
StringBuilder buffer = new StringBuilder();
Iterator itr = getContent().iterator();
while (itr.hasNext()) {
Content child = (Content) itr.next();
if (child instanceof Element || child instanceof Text) {
buffer.append(child.getValue());
}
}
return buffer
// JCLIC modification
//.toString();
.substring(0);
}
/**
* Returns whether this element is a root element. This can be used in
* tandem with {@link #getParent} to determine if an element has any
* "attachments" to a parent element or document.
*
* @return whether this is a root element
*/
public boolean isRootElement() {
return parent instanceof Document;
}
public int getContentSize() {
return content.size();
}
public int indexOf(Content child) {
return content.indexOf(child);
}
// private int indexOf(int start, Filter filter) {
// int size = getContentSize();
// for (int i = start; i < size; i++) {
// if (filter.matches(getContent(i))) {
// return i;
// }
// }
// return -1;
// }
/**
* Returns the textual content directly held under this element as a string.
* This includes all text within this single element, including whitespace
* and CDATA sections if they exist. It's essentially the concatenation of
* all {@link Text} and {@link CDATA} nodes returned by {@link #getContent}.
* The call does not recurse into child elements. If no textual value exists
* for the element, an empty string is returned.
*
* @return text content for this element, or empty
* string if none
*/
public String getText() {
if (content.size() == 0) {
return "";
}
// If we hold only a Text or CDATA, return it directly
if (content.size() == 1) {
Object obj = content.get(0);
if (obj instanceof Text) {
return ((Text) obj).getText();
} else {
return "";
}
}
// Else build String up
StringBuilder textContent = new StringBuilder();
boolean hasText = false;
for (int i = 0; i < content.size(); i++) {
Object obj = content.get(i);
if (obj instanceof Text) {
textContent.append(((Text) obj).getText());
hasText = true;
}
}
if (!hasText) {
return "";
}
else {
return textContent
// JCLIC modification
//.toString();
.substring(0);
}
}
/**
* Returns the textual content of this element with all surrounding
* whitespace removed. If no textual value exists for the element, or if
* only whitespace exists, the empty string is returned.
*
* @return trimmed text content for this element, or
* empty string if none
*/
public String getTextTrim() {
return getText().trim();
}
/**
* Returns the textual content of this element with all surrounding
* whitespace removed and internal whitespace normalized to a single space.
* If no textual value exists for the element, or if only whitespace exists,
* the empty string is returned.
*
* @return normalized text content for this element, or
* empty string if none
*/
public String getTextNormalize() {
return Text.normalizeString(getText());
}
/**
* Returns the textual content of the named child element, or null if
* there's no such child. This method is a convenience because calling
*
* Sequential traversal through the List is best done with an Iterator
* since the underlying implement of List.size() may require walking the
* entire list.
*
* Sequential traversal through the List is best done with a Iterator
* since the underlying implement of List.size() may require walking the
* entire list.
*
* When all objects in the supplied List are legal and before the new
* content is added, all objects in the old content will have their
* parentage set to null (no parent) and the old content list will be
* cleared. This has the effect that any active list (previously obtained
* with a call to {@link #getContent} or {@link #getChildren}) will also
* change to reflect the new content. In addition, all objects in the
* supplied List will have their parentage set to this element, but the
* List itself will not be "live" and further removals and additions will
* have no effect on this elements content. If the user wants to continue
* working with a "live" list, then a call to setContent should be
* followed by a call to {@link #getContent} or {@link #getChildren} to
* obtain a "live" version of the content.
*
* Passing a null or empty List clears the existing content.
*
* In event of an exception the original content will be unchanged and
* the objects in the supplied content will be unaltered.
*
* In event of an exception the original content will be unchanged and
* the supplied child will be unaltered.
*
* In event of an exception the original content will be unchanged and
* the content in the supplied collection will be unaltered.
*
* If the supplied child is legal content for this parent and before
* it is added, all content in the current content list will
* be cleared and all current children will have their parentage set to
* null.
*
* This has the effect that any active list (previously obtained with
* a call to one of the {@link #getContent} methods will also change
* to reflect the new content. In addition, all content in the supplied
* collection will have their parentage set to this parent. If the user
* wants to continue working with a "live" list of this parent's
* child, then a call to setContent should be followed by a call to one
* of the {@link #getContent} methods to obtain a "live"
* version of the children.
*
* Passing a null child clears the existing content.
*
* In event of an exception the original content will be unchanged and
* the supplied child will be unaltered.
*
* @param child new content to replace existing content
* @return the parent on which the method was called
* @throws IllegalAddException if the supplied child is already attached
* or not legal content for an Element
*/
public Element setContent(Content child) {
content.clear();
content.add(child);
return this;
}
/**
* Determines if this element is the ancestor of another element.
*
* @param element
* This returns the complete set of attributes for this element, as a
*
* This returns the attribute for this element with the given name
* and within no namespace, or null if no such attribute exists.
*
* This returns the attribute for this element with the given name
* and within the given Namespace, or null if no such attribute exists.
*
* This returns the attribute value for the attribute with the given name
* and within no namespace, null if there is no such attribute, and the
* empty string if the attribute value is empty.
*
* This returns the attribute value for the attribute with the given name
* and within no namespace, or the passed-in default if there is no
* such attribute.
*
* This returns the attribute value for the attribute with the given name
* and within the given Namespace, null if there is no such attribute, and
* the empty string if the attribute value is empty.
*
* This returns the attribute value for the attribute with the given name
* and within the given Namespace, or the passed-in default if there is no
* such attribute.
*
* This sets the attributes of the element. The supplied List should
* contain only objects of type
* When all objects in the supplied List are legal and before the new
* attributes are added, all old attributes will have their
* parentage set to null (no parent) and the old attribute list will be
* cleared. This has the effect that any active attribute list (previously
* obtained with a call to {@link #getAttributes}) will also change to
* reflect the new attributes. In addition, all attributes in the supplied
* List will have their parentage set to this element, but the List itself
* will not be "live" and further removals and additions will have no
* effect on this elements attributes. If the user wants to continue
* working with a "live" attribute list, then a call to setAttributes
* should be followed by a call to {@link #getAttributes} to obtain a
* "live" version of the attributes.
*
* Passing a null or empty List clears the existing attributes.
*
* In cases where the List contains duplicate attributes, only the last
* one will be retained. This has the same effect as calling
* {@link #setAttribute(Attribute)} sequentially.
*
* In event of an exception the original attributes will be unchanged and
* the attributes in the supplied attributes will be unaltered.
*
* This sets an attribute value for this element. Any existing attribute
* with the same name and namespace URI is removed.
*
* This sets an attribute value for this element. Any existing attribute
* with the same name and namespace URI is removed.
*
* This sets an attribute value for this element. Any existing attribute
* with the same name and namespace URI is removed.
*
* This removes the attribute with the given name and within no
* namespace. If no such attribute exists, this method does nothing.
*
* This removes the attribute with the given name and within the
* given Namespace. If no such attribute exists, this method does
* nothing.
*
* This removes the supplied Attribute should it exist.
*
* This returns a
* This returns a deep clone of this element.
* The new element is detached from its parent, and getParent()
* on the clone will return null.
*
* Sequential traversal through the List is best done with a Iterator
* since the underlying implement of List.size() may not be the most
* efficient.
*
* No recursion is performed, so elements nested two levels deep
* would have to be obtained with:
* getChild().getText()
can throw a NullPointerException.
*
* @param name the name of the child
* @return text content for the named child, or null if
* no such child
*/
public String getChildText(String name) {
Element child = getChild(name);
if (child == null) {
return null;
}
return child.getText();
}
/**
* Returns the trimmed textual content of the named child element, or null
* if there's no such child. See {@link #getTextTrim()}
for
* details of text trimming.
*
* @param name the name of the child
* @return trimmed text content for the named child, or
* null if no such child
*/
public String getChildTextTrim(String name) {
Element child = getChild(name);
if (child == null) {
return null;
}
return child.getTextTrim();
}
/**
* Returns the normalized textual content of the named child element, or
* null if there's no such child. See {@link
* #getTextNormalize()}
for details of text normalizing.
*
* @param name the name of the child
* @return normalized text content for the named child,
* or null if no such child
*/
public String getChildTextNormalize(String name) {
Element child = getChild(name);
if (child == null) {
return null;
}
return child.getTextNormalize();
}
/**
* Returns the textual content of the named child element, or null if
* there's no such child.
*
* @param name the name of the child
* @param ns the namespace of the child
* @return text content for the named child, or null if
* no such child
*/
public String getChildText(String name, Namespace ns) {
Element child = getChild(name, ns);
if (child == null) {
return null;
}
return child.getText();
}
/**
* Returns the trimmed textual content of the named child element, or null
* if there's no such child.
*
* @param name the name of the child
* @param ns the namespace of the child
* @return trimmed text content for the named child, or
* null if no such child
*/
public String getChildTextTrim(String name, Namespace ns) {
Element child = getChild(name, ns);
if (child == null) {
return null;
}
return child.getTextTrim();
}
/**
* Returns the normalized textual content of the named child element, or
* null if there's no such child.
*
* @param name the name of the child
* @param ns the namespace of the child
* @return normalized text content for the named child,
* or null if no such child
*/
public String getChildTextNormalize(String name, Namespace ns) {
Element child = getChild(name, ns);
if (child == null) {
return null;
}
return child.getTextNormalize();
}
/**
* Sets the content of the element to be the text given. All existing text
* content and non-text context is removed. If this element should have both
* textual content and nested elements, use {@link #setContent}
* instead. Setting a null text value is equivalent to setting an empty
* string value.
*
* @param text new text content for the element
* @return the target element
* @throws IllegalDataException if the assigned text contains an illegal
* character such as a vertical tab (as
* determined by {@link
* org.jdom.Verifier#checkCharacterData})
*/
public Element setText(String text) {
content.clear();
if (text != null) {
addContent(new Text(text));
}
return this;
}
/**
* This returns the full content of the element as a List which
* may contain objects of type Text
, Element
,
* Comment
, ProcessingInstruction
,
* CDATA
, and EntityRef
.
* The List returned is "live" in document order and modifications
* to it affect the element's actual contents. Whitespace content is
* returned in its entirety.
*
* List
containing the mixed content of the
* element: may contain Text
,
* {@link Element}
, {@link Comment}
,
* {@link ProcessingInstruction}
,
* {@link CDATA}
, and
* {@link EntityRef}
objects.
*/
public List getContent() {
return content;
}
/**
* Return a filter view of this Element
's content.
*
* Filter
to apply
* @return List
- filtered Element content
*/
public List getContent(Filter filter) {
return content.getView(filter);
}
/**
* Removes all child content from this parent.
*
* @return list of the old children detached from this parent
*/
public List removeContent() {
List old = new ArrayList(content);
content.clear();
return old;
}
/**
* Remove all child content from this parent matching the supplied filter.
*
* @param filter filter to select which content to remove
* @return list of the old children detached from this parent
*/
public List removeContent(Filter filter) {
List old = new ArrayList();
Iterator itr = content.getView(filter).iterator();
while (itr.hasNext()) {
Content child = (Content) itr.next();
old.add(child);
itr.remove();
}
return old;
}
/**
* This sets the content of the element. The supplied List should
* contain only objects of type Element
, Text
,
* CDATA
, Comment
,
* ProcessingInstruction
, and EntityRef
.
*
* List
of content to set
* @return this element modified
* @throws IllegalAddException if the List contains objects of
* illegal types or with existing parentage.
*/
public Element setContent(Collection newContent) {
content.clearAndSet(newContent);
return this;
}
/**
* Replace the current child the given index with the supplied child.
* setText()
.
*
* @param str String
to add
* @return this element modified
* @throws IllegalDataException if str
contains an
* illegal character such as a vertical tab (as determined
* by {@link org.jdom.Verifier#checkCharacterData})
*/
public Element addContent(String str) {
return addContent(new Text(str));
}
/**
* Appends the child to the end of the element's content list.
*
* @param child child to append to end of content list
* @return the element on which the method was called
* @throws IllegalAddException if the given child already has a parent. */
public Element addContent(Content child) {
content.add(child);
return this;
}
/**
* Appends all children in the given collection to the end of
* the content list. In event of an exception during add the
* original content will be unchanged and the objects in the supplied
* collection will be unaltered.
*
* @param collection collection to append
* @return the element on which the method was called
* @throws IllegalAddException if any item in the collection
* already has a parent or is of an inappropriate type.
*/
public Element addContent(Collection collection) {
content.addAll(collection);
return this;
}
/**
* Inserts the child into the content list at the given index.
*
* @param index location for adding the collection
* @param child child to insert
* @return the parent on which the method was called
* @throws IndexOutOfBoundsException if index is negative or beyond
* the current number of children
* @throws IllegalAddException if the given child already has a parent.
*/
public Element addContent(int index, Content child) {
content.add(index, child);
return this;
}
/**
* Inserts the content in a collection into the content list
* at the given index. In event of an exception the original content
* will be unchanged and the objects in the supplied collection will be
* unaltered.
*
* @param index location for adding the collection
* @param c collection to insert
* @return the parent on which the method was called
* @throws IndexOutOfBoundsException if index is negative or beyond
* the current number of children
* @throws IllegalAddException if any item in the collection
* already has a parent or is of an inappropriate type.
*/
public Element addContent(int index, Collection c) {
content.addAll(index, c);
return this;
}
public List cloneContent() {
int size = getContentSize();
List list = new ArrayList(size);
for (int i = 0; i < size; i++) {
Content child = getContent(i);
list.add(child.clone());
}
return list;
}
public Content getContent(int index) {
return (Content) content.get(index);
}
// public Content getChild(Filter filter) {
// int i = indexOf(0, filter);
// return (i < 0) ? null : getContent(i);
// }
public boolean removeContent(Content child) {
return content.remove(child);
}
public Content removeContent(int index) {
return (Content) content.remove(index);
}
/**
* Set this element's content to be the supplied child.
* Element
to check against
* @return true
if this element is the ancestor of the
* supplied element
*/
public boolean isAncestor(Element element) {
Object p = element.getParent();
while (p instanceof Element) {
if (p == this) {
return true;
}
p = ((Element) p).getParent();
}
return false;
}
/**
* List
of Attribute
objects in no particular
* order, or an empty list if there are none.
* The returned list is "live" and changes to it affect the
* element's actual attributes.
* Namespace
to search within
* @return attribute for the element
*/
public Attribute getAttribute(String name, Namespace ns) {
return (Attribute) attributes.get(name, ns);
}
/**
* Namespace
to search within
* @return the named attribute's value, or null if no such attribute
*/
public String getAttributeValue(String name, Namespace ns) {
return getAttributeValue(name, ns, null);
}
/**
* Namespace
to search within
* @param def a default value to return if the attribute does not exist
* @return the named attribute's value, or the default if no such attribute
*/
public String getAttributeValue(String name, Namespace ns, String def) {
Attribute attribute = (Attribute) attributes.get(name, ns);
return (attribute == null) ? def : attribute.getValue();
}
/**
* Attribute
.
* List
of attributes to set
* @return this element modified
* @throws IllegalAddException if the List contains objects
* that are not instances of Attribute
,
* or if any of the Attribute
objects have
* conflicting namespace prefixes.
*/
public Element setAttributes(List newAttributes) {
attributes.clearAndSet(newAttributes);
return this;
}
/**
* Attribute
to set
* @return this element modified
* @throws IllegalAddException if the attribute being added already has a
* parent or if the attribute namespace prefix collides with another
* namespace prefix on the element.
*/
public Element setAttribute(Attribute attribute) {
attributes.add(attribute);
return this;
}
/**
* String
representation of the
* Element
, suitable for debugging. If the XML
* representation of the Element
is desired,
* {@link org.jdom.output.XMLOutputter#outputString(Element)}
* should be used.
* String
- information about the
* Element
*/
public String toString() {
StringBuilder stringForm = new StringBuilder(64)
.append("[Element: <")
.append(getQualifiedName());
String nsuri = getNamespaceURI();
if (!nsuri.equals("")) {
stringForm
.append(" [Namespace: ")
.append(nsuri)
.append("]");
}
stringForm.append("/>]");
return stringForm
// JCLIC modification
//.toString();
.substring(0);
}
/**
* List
of all the child elements
* nested directly (one level deep) within this element, as
* Element
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.
*
*
*
*
* Iterator itr = (currentElement.getChildren()).iterator();
* while(itr.hasNext()) {
* Element oneLevelDeep = (Element)itr.next();
* List twoLevelsDeep = oneLevelDeep.getChildren();
* // Do something with these children
* }
*
*
Element
objects for this element
*/
public List getChildren() {
return content.getView(new ElementFilter());
}
/**
* This returns a List
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
* Element
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.
*
* Please see the notes for {@link #getChildren}
* for a code example.
*
List
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
* Element
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.
*
* Please see the notes for {@link #getChildren}
* for a code example.
*
Namespace
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 Namespace
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);
}
/**
* * 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. *
* * @param name the name of child elements to remove * @return whether deletion occurred */ public boolean removeChild(String name) { return removeChild(name, Namespace.NO_NAMESPACE); } /** ** 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. *
* * @param name the name of child element to remove * @param nsNamespace
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;
}
/**
* * This removes all child elements (one level deep) with the * given local name and belonging to no namespace. * Returns true if any were removed. *
* * @param name the name of child elements to remove * @return whether deletion occurred */ public boolean removeChildren(String name) { return removeChildren(name, Namespace.NO_NAMESPACE); } /** ** 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. *
* * @param name the name of child elements to remove * @param nsNamespace
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;
}
}