An attribute within a tag.
Holds the name, assignment string, value and quote character.
This class was made deliberately simple. Except for
#setRawValue, the properties are completely orthogonal,
that is: each property is independant of the others. This means you have
enough rope here to hang yourself, and it's very easy to create
malformed HTML. Where it's obvious, warnings and notes have been provided
in the setters javadocs, but it is up to you -- the programmer --
to ensure that the contents of the four fields will yield valid HTML
(if that's what you want).
Be especially mindful of quotes and assignment strings. These are handled
by the constructors where it's obvious, but in general, you need to set
them explicitly when building an attribute. For example to construct
the attribute label="A multi word value."
you could use:
attribute = new Attribute ();
attribute.setName ("label");
attribute.setAssignment ("=");
attribute.setValue ("A multi word value.");
attribute.setQuote ('"');
or
attribute = new Attribute ();
attribute.setName ("label");
attribute.setAssignment ("=");
attribute.setRawValue ("A multi word value.");
or
attribute = new Attribute ("label", "A multi word value.");
Note that the assignment value and quoting need to be set separately when
building the attribute from scratch using the properties.
Valid States for Attributes.
Description |
toString() |
Name |
Assignment |
Value |
Quote |
whitespace attribute |
value |
null |
null |
"value" |
0 |
standalone attribute |
name |
"name" |
null |
null |
0 |
empty attribute |
name= |
"name" |
"=" |
null |
0 |
empty single quoted attribute |
name='' |
"name" |
"=" |
null |
' |
empty double quoted attribute |
name="" |
"name" |
"=" |
null |
" |
naked attribute |
name=value |
"name" |
"=" |
"value" |
0 |
single quoted attribute |
name='value' |
"name" |
"=" |
"value" |
' |
double quoted attribute |
name="value" |
"name" |
"=" |
"value" |
" |
In words:
If Name is null, and Assignment is null, and Quote is zero,
it's whitepace and Value has the whitespace text -- value
If Name is not null, and both Assignment and Value are null
it's a standalone attribute -- name
If Name is not null, and Assignment is an equals sign, and Quote is zero
it's an empty attribute -- name=
If Name is not null, and Assignment is an equals sign,
and Value is "" or null, and Quote is '
it's an empty single quoted attribute -- name=''
If Name is not null, and Assignment is an equals sign,
and Value is "" or null, and Quote is "
it's an empty double quoted attribute -- name=""
If Name is not null, and Assignment is an equals sign,
and Value is something, and Quote is zero
it's a naked attribute -- name=value
If Name is not null, and Assignment is an equals sign,
and Value is something, and Quote is '
it's a single quoted attribute -- name='value'
If Name is not null, and Assignment is an equals sign,
and Value is something, and Quote is "
it's a double quoted attribute -- name="value"
All other states are invalid HTML.
From the
HTML 4.01 Specification, W3C Recommendation 24 December 1999
http://www.w3.org/TR/html4/intro/sgmltut.html#h-3.2.2:
3.2.2 Attributes
Elements may have associated properties, called attributes, which may
have values (by default, or set by authors or scripts). Attribute/value
pairs appear before the final ">" of an element's start tag. Any number
of (legal) attribute value pairs, separated by spaces, may appear in an
element's start tag. They may appear in any order.
In this example, the id attribute is set for an H1 element:
By default, SGML requires that all attribute values be delimited using
either double quotation marks (ASCII decimal 34) or single quotation
marks (ASCII decimal 39). Single quote marks can be included within the
attribute value when the value is delimited by double quote marks, and
vice versa. Authors may also use numeric character references to
represent double quotes (") and single quotes (').
For doublequotes authors can also use the character entity reference
".
In certain cases, authors may specify the value of an attribute without
any quotation marks. The attribute value may only contain letters
(a-z and A-Z), digits (0-9), hyphens (ASCII decimal 45),
periods (ASCII decimal 46), underscores (ASCII decimal 95),
and colons (ASCII decimal 58). We recommend using quotation marks even
when it is possible to eliminate them.
Attribute names are always case-insensitive.
Attribute values are generally case-insensitive. The definition of each
attribute in the reference manual indicates whether its value is
case-insensitive.
All the attributes defined by this specification are listed in the
attribute
index.