Class Attribute
This class represents an attribute such as
type="empty" or
xlink:href="http://www.example.com".
Attributes that declare namespaces such as
xmlns="http://www.w3.org/TR/1999/xhtml"
or xmlns:xlink="http://www.w3.org/TR/1999/xlink"
are stored separately on the elements where they
appear. They are never represented as Attribute
objects.
- Version:
- 1.3.0
- Author:
- Elliotte Rusty Harold
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classUses the type-safe enumeration design pattern to represent attribute types, as specified by XML DTDs. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new attribute in no namespace with the specified name and value and undeclared type.Creates a new attribute in the specified namespace with the specified name and value and undeclared type.Attribute(String name, String URI, String value, Attribute.Type type) Creates a new attribute in the specified namespace with the specified name, value, and type.Attribute(String localName, String value, Attribute.Type type) Creates a new attribute in no namespace with the specified name, value, and type.Creates a copy of the specified attribute. -
Method Summary
Modifier and TypeMethodDescriptioncopy()Creates a deep copy of this attribute that is not attached to an element.final NodegetChild(int position) ThrowsIndexOutOfBoundsExceptionbecause attributes do not have children.final intReturns 0 because attributes do not have children.final StringReturns the local name of this attribute, not including the prefix.final StringReturns the prefix of this attribute, or the empty string if this attribute is not in a namespace.final StringReturns the namespace URI of this attribute, or the empty string if this attribute is not in a namespace.final StringReturns the qualified name of this attribute, including the prefix if this attribute is in a namespace.final Attribute.TypegetType()Returns the DTD type of this attribute.final StringgetValue()Returns the attribute value.voidsetLocalName(String localName) Sets the local name of the attribute.voidsetNamespace(String prefix, String URI) Sets the attribute's namespace prefix and URI.voidsetType(Attribute.Type type) Sets the type of this attribute to one of the ten DTD types orType.UNDECLARED.voidSets the attribute's value to the specified string, replacing any previous value.final StringtoString()Returns a string representation of the attribute suitable for debugging and diagnosis.final StringtoXML()Returns a string representation of the attribute that is a well-formed XML attribute.Methods inherited from class nu.xom.Node
detach, equals, getBaseURI, getDocument, getParent, hashCode, query, query
-
Constructor Details
-
Attribute
Creates a new attribute in no namespace with the specified name and value and undeclared type.
- Parameters:
localName- the unprefixed attribute namevalue- the attribute value- Throws:
IllegalNameException- if the local name is not a namespace well-formed, non-colonized nameIllegalDataException- if the value contains characters which are not legal in XML such as vertical tab or a null. Characters such as " and & are legal, but will be automatically escaped when the attribute is serialized.
-
Attribute
Creates a new attribute in no namespace with the specified name, value, and type.
- Parameters:
localName- the unprefixed attribute namevalue- the attribute valuetype- the attribute type- Throws:
IllegalNameException- if the local name is not a namespace well-formed non-colonized nameIllegalDataException- if the value contains characters which are not legal in XML such as vertical tab or a null. Note that characters such as " and & are legal, but will be automatically escaped when the attribute is serialized.
-
Attribute
Creates a new attribute in the specified namespace with the specified name and value and undeclared type.
- Parameters:
name- the prefixed attribute nameURI- the namespace URIvalue- the attribute value- Throws:
IllegalNameException- if the name is not a namespace well-formed nameIllegalDataException- if the value contains characters which are not legal in XML such as vertical tab or a null. Note that characters such as " and & are legal, but will be automatically escaped when the attribute is serialized.MalformedURIException- ifURIis not an RFC 3986 URI referenceNamespaceConflictException- if there's no prefix, but the URI is not the empty string, or the prefix isxmland the URI is not http://www.w3.org/XML/1998/namespace
-
Attribute
Creates a new attribute in the specified namespace with the specified name, value, and type.
- Parameters:
name- the prefixed attribute nameURI- the namespace URIvalue- the attribute valuetype- the attribute type- Throws:
IllegalNameException- if the name is not a namespace well-formed prefixed nameIllegalDataException- if the value contains characters which are not legal in XML such as vertical tab or a null. Note that characters such as " and & are legal, but will be automatically escaped when the attribute is serialized.MalformedURIException- ifURIis not an RFC 3986 absolute URI reference
-
Attribute
Creates a copy of the specified attribute.
- Parameters:
attribute- the attribute to copy
-
-
Method Details
-
getType
Returns the DTD type of this attribute. If this attribute does not have a type, then
Type.UNDECLAREDis returned.- Returns:
- the DTD type of this attribute
-
setType
Sets the type of this attribute to one of the ten DTD types or
Type.UNDECLARED.- Parameters:
type- the DTD type of this attribute- Throws:
NullPointerException- iftypeis nullIllegalDataException- if this is anxml:idattribute and thetypeis not ID
-
getValue
Returns the attribute value. If the attribute was originally created by a parser, it will have been normalized according to its type. However, attributes created in memory are not normalized.
-
setValue
Sets the attribute's value to the specified string, replacing any previous value. The value is not normalized automatically.
- Parameters:
value- the value assigned to the attribute- Throws:
IllegalDataException- if the value contains characters which are not legal in XML such as vertical tab or a null. Characters such as " and & are legal, but will be automatically escaped when the attribute is serialized.
-
getLocalName
Returns the local name of this attribute, not including the prefix.
- Returns:
- the attribute's local name
-
setLocalName
Sets the local name of the attribute.
- Parameters:
localName- the new local name- Throws:
IllegalNameException- iflocalNameis not a namespace well-formed, non-colonized name
-
getQualifiedName
Returns the qualified name of this attribute, including the prefix if this attribute is in a namespace.
- Returns:
- the attribute's qualified name
-
getNamespaceURI
Returns the namespace URI of this attribute, or the empty string if this attribute is not in a namespace.
- Returns:
- the attribute's namespace URI
-
getNamespacePrefix
Returns the prefix of this attribute, or the empty string if this attribute is not in a namespace.
- Returns:
- the attribute's prefix
-
setNamespace
Sets the attribute's namespace prefix and URI. Because attributes must be prefixed in order to have a namespace URI (and vice versa) this must be done simultaneously.
- Parameters:
prefix- the new namespace prefixURI- the new namespace URI- Throws:
MalformedURIException- ifURIis not an RFC 3986 URI referenceIllegalNameException- if- The prefix is
xmlns. - The prefix is null or the empty string.
- The URI is null or the empty string.
- The prefix is
NamespaceConflictException- if- The prefix is
xmland the namespace URI is nothttp://www.w3.org/XML/1998/namespace. - The prefix conflicts with an existing declaration on the attribute's parent element.
- The prefix is
-
getChild
Throws
IndexOutOfBoundsExceptionbecause attributes do not have children.- Specified by:
getChildin classNode- Parameters:
position- the child to return- Returns:
- nothing. This method always throws an exception.
- Throws:
IndexOutOfBoundsException- because attributes do not have children
-
getChildCount
public final int getChildCount()Returns 0 because attributes do not have children.
- Specified by:
getChildCountin classNode- Returns:
- zero
-
copy
Creates a deep copy of this attribute that is not attached to an element.
-
toXML
Returns a string representation of the attribute that is a well-formed XML attribute.
-
toString
Returns a string representation of the attribute suitable for debugging and diagnosis. However, this is not necessarily a well-formed XML attribute.
-