📄 attribute.java
字号:
public Attribute setName(final String name) {
final String reason = Verifier.checkAttributeName(name);
if (reason != null) {
throw new IllegalNameException(name, "attribute", reason);
}
this.name = name;
return this;
}
/**
* This will retrieve the qualified name of the <code>Attribute</code>.
* For any XML attribute whose name is
* <code>[namespacePrefix]:[elementName]</code>,
* the qualified name of the attribute would be
* everything (both namespace prefix and
* element name). When the attribute has no
* namespace, the qualified name is simply the attribute's
* local name.
* <p>
* To obtain the local name of the attribute, the
* <code>{@link #getName()}</code> method should be used.
* <p>
* To obtain the namespace prefix for this attribute,
* the <code>{@link #getNamespacePrefix()}</code>
* method should be used.
*
* @return <code>String</code> - full name for this element.
*/
public String getQualifiedName() {
// Note: Any changes here should be reflected in
// XMLOutputter.printQualifiedName()
final String prefix = namespace.getPrefix();
// no prefix found
if ((prefix == null) || ("".equals(prefix))) {
return getName();
} else {
return new StringBuffer(prefix)
.append(':')
.append(getName())
.toString();
}
}
/**
* This will retrieve the namespace prefix of the
* <code>Attribute</code>. For any XML attribute
* which appears as
* <code>[namespacePrefix]:[attributeName]</code>,
* the namespace prefix of the attribute would be
* <code>[namespacePrefix]</code>. When the attribute
* has no namespace, an empty <code>String</code> is returned.
*
* @return <code>String</code> - namespace prefix of this
* attribute.
*/
public String getNamespacePrefix() {
return namespace.getPrefix();
}
/**
* This returns the URI mapped to this <code>Attribute</code>'s
* prefix. If no mapping is found, an empty <code>String</code> is
* returned.
*
* @return <code>String</code> - namespace URI for this <code>Attribute</code>.
*/
public String getNamespaceURI() {
return namespace.getURI();
}
/**
* This will return this <code>Attribute</code>'s
* <code>{@link Namespace}</code>.
*
* @return <code>Namespace</code> - Namespace object for this <code>Attribute</code>
*/
public Namespace getNamespace() {
return namespace;
}
/**
* This sets this <code>Attribute</code>'s <code>{@link Namespace}</code>.
* If the provided namespace is null, the attribute will have no namespace.
* The namespace must have a prefix.
*
* @param namespace the new namespace
* @return <code>Element</code> - the element modified.
* @throws IllegalNameException if the new namespace is the default
* namespace. Attributes cannot be in a default namespace.
*/
public Attribute setNamespace(Namespace namespace) {
if (namespace == null) {
namespace = Namespace.NO_NAMESPACE;
}
// Verify the attribute isn't trying to be in a default namespace
// Attributes can't be in a default namespace
if (namespace != Namespace.NO_NAMESPACE &&
"".equals(namespace.getPrefix())) {
throw new IllegalNameException("", "attribute namespace",
"An attribute namespace without a prefix can only be the " +
"NO_NAMESPACE namespace");
}
this.namespace = namespace;
return this;
}
/**
* This will return the actual textual value of this
* <code>Attribute</code>. This will include all text
* within the quotation marks.
*
* @return <code>String</code> - value for this attribute.
*/
public String getValue() {
return value;
}
/**
* This will set the value of the <code>Attribute</code>.
*
* @param value <code>String</code> value for the attribute.
* @return <code>Attribute</code> - this Attribute modified.
* @throws IllegalDataException if the given attribute value is
* illegal character data (as determined by
* {@link org.jdom.Verifier#checkCharacterData}).
*/
public Attribute setValue(final String value) {
final String reason = Verifier.checkCharacterData(value);
if (reason != null) {
throw new IllegalDataException(value, "attribute", reason);
}
this.value = value;
return this;
}
/**
* This will return the actual declared type of this
* <code>Attribute</code>.
*
* @return <code>int</code> - type for this attribute.
*/
public int getAttributeType() {
return type;
}
/**
* This will set the type of the <code>Attribute</code>.
*
* @param type <code>int</code> type for the attribute.
* @return <code>Attribute</code> - this Attribute modified.
* @throws IllegalDataException if the given attribute type is
* not one of the supported types.
*/
public Attribute setAttributeType(final int type) {
if ((type < UNDECLARED_TYPE) || (type > ENUMERATED_TYPE)) {
throw new IllegalDataException(String.valueOf(type),
"attribute", "Illegal attribute type");
}
this.type = type;
return this;
}
/**
* This returns a <code>String</code> representation of the
* <code>Attribute</code>, suitable for debugging.
*
* @return <code>String</code> - information about the
* <code>Attribute</code>
*/
public String toString() {
return new StringBuffer()
.append("[Attribute: ")
.append(getQualifiedName())
.append("=\"")
.append(value)
.append("\"")
.append("]")
.toString();
}
/**
* This tests for equality of this <code>Attribute</code> to the supplied
* <code>Object</code>.
*
* @param ob <code>Object</code> to compare to.
* @return <code>boolean</code> - whether the <code>Attribute</code> is
* equal to the supplied <code>Object</code>.
*/
public final boolean equals(final Object ob) {
return (ob == this);
}
/**
* This returns the hash code for this <code>Attribute</code>.
*
* @return <code>int</code> - hash code.
*/
public final int hashCode() {
return super.hashCode();
}
/**
* This will return a clone of this <code>Attribute</code>.
*
* @return <code>Object</code> - clone of this <code>Attribute</code>.
*/
public Object clone() {
Attribute attribute = null;
try {
attribute = (Attribute) super.clone();
}
catch (final CloneNotSupportedException ignore) {
// Won't happen
}
// Name, namespace, and value are references to imutable objects
// and are copied by super.clone() (aka Object.clone())
// super.clone() copies reference to set parent to null
attribute.parent = null;
return attribute;
}
/////////////////////////////////////////////////////////////////
// Convenience Methods below here
/////////////////////////////////////////////////////////////////
/**
* This gets the value of the attribute, in
* <code>int</code> form, and if no conversion
* can occur, throws a
* <code>{@link DataConversionException}</code>
*
* @return <code>int</code> value of attribute.
* @throws DataConversionException when conversion fails.
*/
public int getIntValue() throws DataConversionException {
try {
return Integer.parseInt(value.trim());
} catch (final NumberFormatException e) {
throw new DataConversionException(name, "int");
}
}
/**
* This gets the value of the attribute, in
* <code>long</code> form, and if no conversion
* can occur, throws a
* <code>{@link DataConversionException}</code>
*
* @return <code>long</code> value of attribute.
* @throws DataConversionException when conversion fails.
*/
public long getLongValue() throws DataConversionException {
try {
return Long.parseLong(value.trim());
} catch (final NumberFormatException e) {
throw new DataConversionException(name, "long");
}
}
/**
* This gets the value of the attribute, in
* <code>float</code> form, and if no conversion
* can occur, throws a
* <code>{@link DataConversionException}</code>
*
* @return <code>float</code> value of attribute.
* @throws DataConversionException when conversion fails.
*/
public float getFloatValue() throws DataConversionException {
try {
// Avoid Float.parseFloat() to support JDK 1.1
return Float.valueOf(value.trim()).floatValue();
} catch (final NumberFormatException e) {
throw new DataConversionException(name, "float");
}
}
/**
* This gets the value of the attribute, in
* <code>double</code> form, and if no conversion
* can occur, throws a
* <code>{@link DataConversionException}</code>
*
* @return <code>double</code> value of attribute.
* @throws DataConversionException when conversion fails.
*/
public double getDoubleValue() throws DataConversionException {
try {
// Avoid Double.parseDouble() to support JDK 1.1
return Double.valueOf(value.trim()).doubleValue();
} catch (final NumberFormatException e) {
// Specially handle INF and -INF that Double.valueOf doesn't do
String v = value.trim();
if ("INF".equals(v)) {
return Double.POSITIVE_INFINITY;
}
if ("-INF".equals(v)) {
return Double.NEGATIVE_INFINITY;
}
throw new DataConversionException(name, "double");
}
}
/**
* This gets the effective boolean value of the attribute, or throws a
* <code>{@link DataConversionException}</code> if a conversion can't be
* performed. True values are: "true", "on", "1", and "yes". False
* values are: "false", "off", "0", and "no". Values are trimmed before
* comparison. Values other than those listed here throw the exception.
*
* @return <code>boolean</code> value of attribute.
* @throws DataConversionException when conversion fails.
*/
public boolean getBooleanValue() throws DataConversionException {
final String valueTrim = value.trim();
if (
(valueTrim.equalsIgnoreCase("true")) ||
(valueTrim.equalsIgnoreCase("on")) ||
(valueTrim.equalsIgnoreCase("1")) ||
(valueTrim.equalsIgnoreCase("yes"))) {
return true;
} else if (
(valueTrim.equalsIgnoreCase("false")) ||
(valueTrim.equalsIgnoreCase("off")) ||
(valueTrim.equalsIgnoreCase("0")) ||
(valueTrim.equalsIgnoreCase("no"))
) {
return false;
} else {
throw new DataConversionException(name, "boolean");
}
}
// 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());
}
private void readObject(final ObjectInputStream in)
throws IOException, ClassNotFoundException {
in.defaultReadObject();
namespace = Namespace.getNamespace(
(String) in.readObject(), (String) in.readObject());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -