nu.xom

Class Attribute

public class Attribute extends Node

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.2d1

Author: Elliotte Rusty Harold

Nested Class Summary
static classAttribute.Type

Uses the type-safe enumeration design pattern to represent attribute types, as specified by XML DTDs.

Constructor Summary
Attribute(String localName, String value)

Creates a new attribute in no namespace with the specified name and value and undeclared type.

Attribute(String localName, String value, Attribute.Type type)

Creates a new attribute in no namespace with the specified name, value, and type.

Attribute(String name, String URI, String value)

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(Attribute attribute)

Creates a copy of the specified attribute.

Method Summary
Nodecopy()

Creates a deep copy of this attribute that is not attached to an element.

NodegetChild(int position)

Throws IndexOutOfBoundsException because attributes do not have children.

intgetChildCount()

Returns 0 because attributes do not have children.

StringgetLocalName()

Returns the local name of this attribute, not including the prefix.

StringgetNamespacePrefix()

Returns the prefix of this attribute, or the empty string if this attribute is not in a namespace.

StringgetNamespaceURI()

Returns the namespace URI of this attribute, or the empty string if this attribute is not in a namespace.

StringgetQualifiedName()

Returns the qualified name of this attribute, including the prefix if this attribute is in a namespace.

Attribute.TypegetType()

Returns the DTD type of this attribute.

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 or Type.UNDECLARED.

voidsetValue(String value)

Sets the attribute's value to the specified string, replacing any previous value.

StringtoString()

Returns a string representation of the attribute suitable for debugging and diagnosis.

StringtoXML()

Returns a string representation of the attribute that is a well-formed XML attribute.

Constructor Detail

Attribute

public Attribute(String localName, String value)

Creates a new attribute in no namespace with the specified name and value and undeclared type.

Parameters: localName the unprefixed attribute name value the attribute value

Throws: IllegalNameException if the local name is not a namespace well-formed, non-colonized name 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.

Attribute

public Attribute(String localName, String value, Attribute.Type type)

Creates a new attribute in no namespace with the specified name, value, and type.

Parameters: localName the unprefixed attribute name value the attribute value type the attribute type

Throws: IllegalNameException if the local name is not a namespace well-formed non-colonized name IllegalDataException 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

public Attribute(String name, String URI, String value)

Creates a new attribute in the specified namespace with the specified name and value and undeclared type.

Parameters: name the prefixed attribute name URI the namespace URI value the attribute value

Throws: IllegalNameException if the name is not a namespace well-formed name IllegalDataException 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 if URI is not an RFC 3986 URI reference NamespaceConflictException if there's no prefix, but the URI is not the empty string, or the prefix is xml and the URI is not http://www.w3.org/XML/1998/namespace

Attribute

public 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.

Parameters: name the prefixed attribute name URI the namespace URI value the attribute value type the attribute type

Throws: IllegalNameException if the name is not a namespace well-formed prefixed name IllegalDataException 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 if URI is not an RFC 3986 absolute URI reference

Attribute

public Attribute(Attribute attribute)

Creates a copy of the specified attribute.

Parameters: attribute the attribute to copy

Method Detail

copy

public Node copy()

Creates a deep copy of this attribute that is not attached to an element.

Returns: a copy of this attribute

getChild

public final Node getChild(int position)

Throws IndexOutOfBoundsException because attributes do not have children.

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.

Returns: zero

getLocalName

public final String getLocalName()

Returns the local name of this attribute, not including the prefix.

Returns: the attribute's local name

getNamespacePrefix

public final String getNamespacePrefix()

Returns the prefix of this attribute, or the empty string if this attribute is not in a namespace.

Returns: the attribute's prefix

getNamespaceURI

public final String 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

getQualifiedName

public final String getQualifiedName()

Returns the qualified name of this attribute, including the prefix if this attribute is in a namespace.

Returns: the attribute's qualified name

getType

public final Attribute.Type getType()

Returns the DTD type of this attribute. If this attribute does not have a type, then Type.UNDECLARED is returned.

Returns: the DTD type of this attribute

getValue

public final String 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.

Returns: the value of the attribute

setLocalName

public void setLocalName(String localName)

Sets the local name of the attribute.

Parameters: localName the new local name

Throws: IllegalNameException if localName is not a namespace well-formed, non-colonized name

setNamespace

public void setNamespace(String prefix, String URI)

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 prefix URI the new namespace URI

Throws: MalformedURIException if URI is not an RFC 3986 URI reference IllegalNameException if

NamespaceConflictException if

setType

public void setType(Attribute.Type type)

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 if type is null IllegalDataException if this is an xml:id attribute and the type is not ID

setValue

public void setValue(String value)

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. MalformedURIException if this is an xml:base attribute, and the value is not a legal IRI

toString

public final String toString()

Returns a string representation of the attribute suitable for debugging and diagnosis. However, this is not necessarily a well-formed XML attribute.

Returns: a non-XML string representation of this attribute

See Also: java.lang.Object#toString()

toXML

public final String toXML()

Returns a string representation of the attribute that is a well-formed XML attribute.

Returns: a string containing the XML form of this attribute

Copyright 2002-2006 Elliotte Rusty Harold
elharo@metalab.unc.edu