nu.xom
Class Attribute

java.lang.Object
  extended bynu.xom.Node
      extended bynu.xom.Attribute

public class Attribute
extends Node

Represents an attribute such as class="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.0d23
Author:
Elliotte Rusty Harold

Nested Class Summary
static class Attribute.Type
           Uses the type-safe enumeration design pattern to represent attribute types, as used in XML DTDs.
 
Constructor Summary
Attribute(Attribute attribute)
           Creates a copy of the specified attribute.
Attribute(java.lang.String localName, java.lang.String value)
           Creates a new attribute in no namespace with the specified name and value and undeclared type.
Attribute(java.lang.String localName, java.lang.String value, Attribute.Type type)
           Creates a new attribute in no namespace with the specified name, value, and type.
Attribute(java.lang.String name, java.lang.String URI, java.lang.String value)
           Creates a new attribute in the specified namespace with the specified name and value and undeclared type.
Attribute(java.lang.String name, java.lang.String URI, java.lang.String value, Attribute.Type type)
           Creates a new attribute in the specified namespace with the specified name and value and undeclared type.
 
Method Summary
protected  void checkLocalName(java.lang.String localName)
           This method is called before the specified local name is assigned to this attribute.
protected  void checkNamespace(java.lang.String prefix, java.lang.String URI)
           This method is called before the specified local name is assigned to this attribute.
protected  void checkType(Attribute.Type type)
           This method is called before the specified type is assigned to this attribute.
protected  void checkValue(java.lang.String value)
           Subclasses can override this method to check the value for an attribute before it's created or its value is changed.
 Node copy()
           Creates a deep copy of this attribute that is not attached to an element.
 Node getChild(int position)
           Throws IndexOutOfBoundsExceptions because attribute do not have children.
 int getChildCount()
           Returns 0 because attributes do not have children.
 java.lang.String getLocalName()
           Returns the local name of this attribute, not including the prefix.
 java.lang.String getNamespacePrefix()
           Returns the prefix of this attribute, or the empty string if this attribute is not in a namespace.
 java.lang.String getNamespaceURI()
           Returns the namespace URI of this attribute, or the empty string if this attribute is not in a namespace.
 java.lang.String getQualifiedName()
           Returns the qualified name of this attribute, including the prefix if this attribute is in a namespace.
 Attribute.Type getType()
           Returns the DTD type of this attribute.
 java.lang.String getValue()
           Returns the attribute value.
 boolean hasChildren()
           Returns false because attributes do not have children.
 void setLocalName(java.lang.String localName)
           Sets the local name of the attribute.
 void setNamespace(java.lang.String prefix, java.lang.String URI)
           Sets the attribute's namespace URI and prefix.
 void setType(Attribute.Type type)
           Sets the type of this attribute to one of the ten DTD types or Type.UNDECLARED.
 void setValue(java.lang.String value)
           Sets the attribute's value to the specified string, replacing any previous value.
 java.lang.String toString()
           Returns a string representation of the attribute suitable for debugging and diagnosis.
 java.lang.String toXML()
           Returns a string representation of the attribute that is a well-formed XML attribute.
 
Methods inherited from class nu.xom.Node
checkDetach, detach, equals, getBaseURI, getDocument, getParent, hashCode
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Attribute

public Attribute(java.lang.String localName,
                 java.lang.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(java.lang.String localName,
                 java.lang.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(java.lang.String name,
                 java.lang.String URI,
                 java.lang.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 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 RFC2396 URI reference

Attribute

public Attribute(java.lang.String name,
                 java.lang.String URI,
                 java.lang.String value,
                 Attribute.Type type)

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
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 RFC2396 absolute URI reference

Attribute

public Attribute(Attribute attribute)

Creates a copy of the specified attribute.

Parameters:
attribute - the attribute to copy
Method Detail

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

setType

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

checkType

protected void checkType(Attribute.Type type)

This method is called before the specified type is assigned to this attribute. By default it does nothing. However, subclasses can override it and prevent certain types from being assigned to the attribute by throwing an XMLException.

Parameters:
type - the DTD type of this attribute
Throws:
XMLException - if the subclass rejects the requested type.

getValue

public final java.lang.String getValue()

Returns the attribute value. If the attribute was originally created by a parser then, it will have been normalized according to its type. However, attributes created in memory are not normalized.

Specified by:
getValue in class Node
Returns:
the value of the attribute
See Also:
Node.getValue()

setValue

public final void setValue(java.lang.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

checkValue

protected void checkValue(java.lang.String value)

Subclasses can override this method to check the value for an attribute before it's created or its value is changed.

Parameters:
value - the proposed attribute value
Throws:
XMLException - if value does not satisfy the local constraints

getLocalName

public final java.lang.String getLocalName()

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

Returns:
the attribute's qualified name

setLocalName

public final void setLocalName(java.lang.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

getQualifiedName

public final java.lang.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

getNamespaceURI

public final java.lang.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

getNamespacePrefix

public final java.lang.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

checkLocalName

protected void checkLocalName(java.lang.String localName)

This method is called before the specified local name is assigned to this attribute. By default it does nothing. However, subclasses can override it and prevent certain local names from being assigned to the attribute by throwing an XMLException.

Parameters:
localName - the proposed local name of the attribute
Throws:
XMLException - if the subclass rejects the name.

setNamespace

public final void setNamespace(java.lang.String prefix,
                               java.lang.String URI)

Sets the attribute's namespace URI and prefix. 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 RFC2396 URI reference
IllegalNameException - if
  • The prefix is xmlns
  • The prefix is null or the empty string.
  • The URI is null or the empty string.
NamespaceConflictException - if
  • The prefix is xml and the namespace URI is not http://www.w3.org/XML/1998/namespace
  • The prefix conflicts with an existing declaration on the attribute's parent element.

checkNamespace

protected void checkNamespace(java.lang.String prefix,
                              java.lang.String URI)

This method is called before the specified local name is assigned to this attribute. By default it does nothing. However, subclasses can override it and prevent certain namespace URIs from being assigned to the attribute by throwing an XMLException.

Parameters:
prefix - the proposed namespace prefix for this attribute
URI - the proposed namespace URI for this attribute
Throws:
XMLException - if the subclass rejects the name.

hasChildren

public final boolean hasChildren()

Returns false because attributes do not have children.

Specified by:
hasChildren in class Node
Returns:
false
See Also:
Node.hasChildren()

getChild

public final Node getChild(int position)

Throws IndexOutOfBoundsExceptions because attribute do not have children.

Specified by:
getChild in class Node
Parameters:
position - the child to return
Returns:
nothing. This method always throws an exception.
Throws:
java.lang.IndexOutOfBoundsException - because attributes do not have children.

getChildCount

public final int getChildCount()

Returns 0 because attributes do not have children.

Specified by:
getChildCount in class Node
Returns:
zero
See Also:
Node.getChildCount()

copy

public Node copy()

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

Specified by:
copy in class Node
Returns:
a copy of this Attribute

toXML

public final java.lang.String toXML()

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

Specified by:
toXML in class Node
Returns:
a string containing the XML form of this attribute
See Also:
Node.toXML()

toString

public final java.lang.String toString()

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

Specified by:
toString in class Node
Returns:
a non-XML string representation of this attribute
See Also:
Object.toString()


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