📄 element.java
字号:
/*--
$Id: Element.java,v 1.159 2007/11/14 05:02:08 jhunter Exp $
Copyright (C) 2000-2007 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 <request_AT_jdom_DOT_org>.
4. Products derived from this software may not be called "JDOM", nor
may "JDOM" appear in their name, without prior written permission
from the JDOM Project Management <request_AT_jdom_DOT_org>.
In addition, we request (but do not require) that you include in the
end-user documentation provided with the redistribution and/or in the
software itself an acknowledgement equivalent to the following:
"This product includes software developed by the
JDOM Project (http://www.jdom.org/)."
Alternatively, the acknowledgment may be graphical using the logos
available at http://www.jdom.org/images/logos.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
This software consists of voluntary contributions made by many
individuals on behalf of the JDOM Project and was originally
created by Jason Hunter <jhunter_AT_jdom_DOT_org> and
Brett McLaughlin <brett_AT_jdom_DOT_org>. For more information
on the JDOM Project, please see <http://www.jdom.org/>.
*/
package org.jdom;
import java.io.*;
import java.util.*;
import org.jdom.filter.*;
/**
* An XML element. Methods allow the user to get and manipulate its child
* elements and content, directly access the element's textual content,
* manipulate its attributes, and manage namespaces.
*
* @version $Revision: 1.159 $, $Date: 2007/11/14 05:02:08 $
* @author Brett McLaughlin
* @author Jason Hunter
* @author Lucas Gonze
* @author Kevin Regan
* @author Dan Schaffer
* @author Yusuf Goolamabbas
* @author Kent C. Johnson
* @author Jools Enticknap
* @author Alex Rosen
* @author Bradley S. Huffman
* @author Victor Toni
*/
public class Element extends Content implements Parent {
private static final String CVS_ID =
"@(#) $RCSfile: Element.java,v $ $Revision: 1.159 $ $Date: 2007/11/14 05:02:08 $ $Name: jdom_1_1 $";
private static final int INITIAL_ARRAY_SIZE = 5;
/** The local name of the element */
protected String name;
/** The namespace of the element */
protected transient Namespace namespace;
/** Additional namespace declarations to store on this element; useful
* during output */
protected transient List additionalNamespaces;
// See http://lists.denveronline.net/lists/jdom-interest/2000-September/003030.html
// for a possible memory optimization here (using a RootElement subclass)
/**
* The attributes of the element. Subclassers have to
* track attributes using their own mechanism.
*/
AttributeList attributes = new AttributeList(this);
/**
* The content of the element. Subclassers have to
* track content using their own mechanism.
*/
ContentList content = new ContentList(this);
/**
* This protected constructor is provided in order to support an Element
* subclass that wants full control over variable initialization. It
* intentionally leaves all instance variables null, allowing a lightweight
* subclass implementation. The subclass is responsible for ensuring all the
* get and set methods on Element behave as documented.
* <p>
* 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(final String name, final 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(final 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(final String name, final 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(final String name, final String prefix, final 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(final String name) {
final 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(final String prefix) {
if (prefix == null) {
return null;
}
if ("xml".equals(prefix)) {
// 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++) {
final 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 ("".equals(namespace.getPrefix())) {
return getName();
}
return new StringBuffer(namespace.getPrefix())
.append(':')
.append(name)
.toString();
}
/**
* Adds a namespace declarations to this element. This should <i>not</i> 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 additionalNamespace namespace to add
* @throws IllegalAddException if the namespace prefix collides with another
* namespace prefix on the element
*/
public void addNamespaceDeclaration(final Namespace additionalNamespace) {
// Verify the new namespace prefix doesn't collide with another
// declared namespace, an attribute prefix, or this element's prefix
final String reason = Verifier.checkNamespaceCollision(additionalNamespace, this);
if (reason != null) {
throw new IllegalAddException(this, additionalNamespace, reason);
}
if (additionalNamespaces == null) {
additionalNamespaces = new ArrayList(INITIAL_ARRAY_SIZE);
}
additionalNamespaces.add(additionalNamespace);
}
/**
* Removes an additional namespace declarations from this element. This
* should <i>not</i> 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(final 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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -