📄 attribute.java
字号:
/*--
$Id: Attribute.java,v 1.56 2007/11/10 05:28:58 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.*;
/**
* An XML attribute. Methods allow the user to obtain the value of the attribute
* as well as namespace and type information.
*
* @version $Revision: 1.56 $, $Date: 2007/11/10 05:28:58 $
* @author Brett McLaughlin
* @author Jason Hunter
* @author Elliotte Rusty Harold
* @author Wesley Biggs
* @author Victor Toni
*/
public class Attribute implements Serializable, Cloneable {
private static final String CVS_ID =
"@(#) $RCSfile: Attribute.java,v $ $Revision: 1.56 $ $Date: 2007/11/10 05:28:58 $ $Name: jdom_1_1 $";
/**
* Attribute type: the attribute has not been declared or type
* is unknown.
*
* @see #getAttributeType
*/
public final static int UNDECLARED_TYPE = 0;
/**
* Attribute type: the attribute value is a string.
*
* @see #getAttributeType
*/
public final static int CDATA_TYPE = 1;
/**
* Attribute type: the attribute value is a unique identifier.
*
* @see #getAttributeType
*/
public final static int ID_TYPE = 2;
/**
* Attribute type: the attribute value is a reference to a
* unique identifier.
*
* @see #getAttributeType
*/
public final static int IDREF_TYPE = 3;
/**
* Attribute type: the attribute value is a list of references to
* unique identifiers.
*
* @see #getAttributeType
*/
public final static int IDREFS_TYPE = 4;
/**
* Attribute type: the attribute value is the name of an entity.
*
* @see #getAttributeType
*/
public final static int ENTITY_TYPE = 5;
/**
* <p>
* Attribute type: the attribute value is a list of entity names.
* </p>
*
* @see #getAttributeType
*/
public final static int ENTITIES_TYPE = 6;
/**
* Attribute type: the attribute value is a name token.
* <p>
* According to SAX 2.0 specification, attributes of enumerated
* types should be reported as "NMTOKEN" by SAX parsers. But the
* major parsers (Xerces and Crimson) provide specific values
* that permit to recognize them as {@link #ENUMERATED_TYPE}.
*
* @see #getAttributeType
*/
public final static int NMTOKEN_TYPE = 7;
/**
* Attribute type: the attribute value is a list of name tokens.
*
* @see #getAttributeType
*/
public final static int NMTOKENS_TYPE = 8;
/**
* Attribute type: the attribute value is the name of a notation.
*
* @see #getAttributeType
*/
public final static int NOTATION_TYPE = 9;
/**
* Attribute type: the attribute value is a name token from an
* enumeration.
*
* @see #getAttributeType
*/
public final static int ENUMERATED_TYPE = 10;
// Keep the old constant names for one beta cycle to help migration
/** The local name of the <code>Attribute</code> */
protected String name;
/** The <code>{@link Namespace}</code> of the <code>Attribute</code> */
protected transient Namespace namespace;
/** The value of the <code>Attribute</code> */
protected String value;
/** The type of the <code>Attribute</code> */
protected int type = UNDECLARED_TYPE;
/** Parent element, or null if none */
protected Element parent;
/**
* Default, no-args constructor for implementations to use if needed.
*/
protected Attribute() {}
/**
* This will create a new <code>Attribute</code> with the
* specified (local) name and value, and in the provided
* <code>{@link Namespace}</code>.
*
* @param name <code>String</code> name of <code>Attribute</code>.
* @param value <code>String</code> value for new attribute.
* @param namespace <code>Namespace</code> namespace for new attribute.
* @throws IllegalNameException if the given name is illegal as an
* attribute name or if if the new namespace is the default
* namespace. Attributes cannot be in a default namespace.
* @throws IllegalDataException if the given attribute value is
* illegal character data (as determined by
* {@link org.jdom.Verifier#checkCharacterData}).
*/
public Attribute(final String name, final String value, final Namespace namespace) {
this(name, value, UNDECLARED_TYPE, namespace);
}
/**
* This will create a new <code>Attribute</code> with the
* specified (local) name, value, and type, and in the provided
* <code>{@link Namespace}</code>.
*
* @param name <code>String</code> name of <code>Attribute</code>.
* @param value <code>String</code> value for new attribute.
* @param type <code>int</code> type for new attribute.
* @param namespace <code>Namespace</code> namespace for new attribute.
* @throws IllegalNameException if the given name is illegal as an
* attribute name or if if the new namespace is the default
* namespace. Attributes cannot be in a default namespace.
* @throws IllegalDataException if the given attribute value is
* illegal character data (as determined by
* {@link org.jdom.Verifier#checkCharacterData}) or
* if the given attribute type is not one of the
* supported types.
*/
public Attribute(final String name, final String value, final int type, final Namespace namespace) {
setName(name);
setValue(value);
setAttributeType(type);
setNamespace(namespace);
}
/**
* This will create a new <code>Attribute</code> with the
* specified (local) name and value, and does not place
* the attribute in a <code>{@link Namespace}</code>.
* <p>
* <b>Note</b>: This actually explicitly puts the
* <code>Attribute</code> in the "empty" <code>Namespace</code>
* (<code>{@link Namespace#NO_NAMESPACE}</code>).
*
* @param name <code>String</code> name of <code>Attribute</code>.
* @param value <code>String</code> value for new attribute.
* @throws IllegalNameException if the given name is illegal as an
* attribute name.
* @throws IllegalDataException if the given attribute value is
* illegal character data (as determined by
* {@link org.jdom.Verifier#checkCharacterData}).
*/
public Attribute(final String name, final String value) {
this(name, value, UNDECLARED_TYPE, Namespace.NO_NAMESPACE);
}
/**
* This will create a new <code>Attribute</code> with the
* specified (local) name, value and type, and does not place
* the attribute in a <code>{@link Namespace}</code>.
* <p>
* <b>Note</b>: This actually explicitly puts the
* <code>Attribute</code> in the "empty" <code>Namespace</code>
* (<code>{@link Namespace#NO_NAMESPACE}</code>).
*
* @param name <code>String</code> name of <code>Attribute</code>.
* @param value <code>String</code> value for new attribute.
* @param type <code>int</code> type for new attribute.
* @throws IllegalNameException if the given name is illegal as an
* attribute name.
* @throws IllegalDataException if the given attribute value is
* illegal character data (as determined by
* {@link org.jdom.Verifier#checkCharacterData}) or
* if the given attribute type is not one of the
* supported types.
*/
public Attribute(final String name, final String value, final int type) {
this(name, value, type, Namespace.NO_NAMESPACE);
}
/**
* This will return the parent of this <code>Attribute</code>.
* If there is no parent, then this returns <code>null</code>.
*
* @return parent of this <code>Attribute</code>
*/
public Element getParent() {
return parent;
}
/**
* This retrieves the owning <code>{@link Document}</code> for
* this Attribute, or null if not a currently a member of a
* <code>{@link Document}</code>.
*
* @return <code>Document</code> owning this Attribute, or null.
*/
public Document getDocument() {
final Element parentElement = getParent();
if (parentElement != null) {
return parentElement.getDocument();
}
return null;
}
/**
* This will set the parent of this <code>Attribute</code>.
*
* @param parent <code>Element</code> to be new parent.
* @return this <code>Attribute</code> modified.
*/
protected Attribute setParent(final Element parent) {
this.parent = parent;
return this;
}
/**
* This detaches the <code>Attribute</code> from its parent, or does
* nothing if the <code>Attribute</code> has no parent.
*
* @return <code>Attribute</code> - this <code>Attribute</code> modified.
*/
public Attribute detach() {
final Element parentElement = getParent();
if (parentElement != null) {
parentElement.removeAttribute(getName(),getNamespace());
}
return this;
}
/**
* This will retrieve the local name of the
* <code>Attribute</code>. For any XML attribute
* which appears as
* <code>[namespacePrefix]:[attributeName]</code>,
* the local name of the attribute would be
* <code>[attributeName]</code>. When the attribute
* has no namespace, the local name is simply the attribute
* name.
* <p>
* To obtain the namespace prefix for this
* attribute, the
* <code>{@link #getNamespacePrefix()}</code>
* method should be used.
*
* @return <code>String</code> - name of this attribute,
* without any namespace prefix.
*/
public String getName() {
return name;
}
/**
* This sets the local name of the <code>Attribute</code>.
*
* @param name the new local name to set
* @return <code>Attribute</code> - the attribute modified.
* @throws IllegalNameException if the given name is illegal as an
* attribute name.
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -