|
HTML Parser Home Page | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.htmlparser.Attribute
public class Attribute
An attribute within a tag. Holds the name, assignment string, value and quote character.
This class was made deliberately simple. Except for
RawValue
, 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.
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" | " |
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:
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.
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
".
<H1 id="section1">
This is an identified heading thanks to the id attribute
</H1>
Field Summary | |
---|---|
protected String |
mAssignment
The assignment string of the attribute. |
protected String |
mName
The name of this attribute. |
protected char |
mQuote
The quote, if any, surrounding the value of the attribute, if any. |
protected String |
mValue
The value of the attribute. |
Constructor Summary | |
---|---|
Attribute()
Create an empty attribute. |
|
Attribute(String value)
Create a whitespace attribute with the value given. |
|
Attribute(String name,
String value)
Create an attribute with the name and value given. |
|
Attribute(String name,
String value,
char quote)
Create an attribute with the name, value and quote given. |
|
Attribute(String name,
String assignment,
String value)
Create an attribute with the name, assignment string and value given. |
|
Attribute(String name,
String assignment,
String value,
char quote)
Create an attribute with the name, assignment, value and quote given. |
Method Summary | |
---|---|
String |
getAssignment()
Get the assignment string of this attribute. |
void |
getAssignment(StringBuffer buffer)
Get the assignment string of this attribute. |
int |
getLength()
Get the length of the string value of this attribute. |
String |
getName()
Get the name of this attribute. |
void |
getName(StringBuffer buffer)
Get the name of this attribute. |
char |
getQuote()
Get the quote, if any, surrounding the value of the attribute, if any. |
void |
getQuote(StringBuffer buffer)
Get the quote, if any, surrounding the value of the attribute, if any. |
String |
getRawValue()
Get the raw value of the attribute. |
void |
getRawValue(StringBuffer buffer)
Get the raw value of the attribute. |
String |
getValue()
Get the value of the attribute. |
void |
getValue(StringBuffer buffer)
Get the value of the attribute. |
boolean |
isEmpty()
Predicate to determine if this attribute has an equals sign but no value. |
boolean |
isStandAlone()
Predicate to determine if this attribute has no equals sign (or value). |
boolean |
isValued()
Predicate to determine if this attribute has a value. |
boolean |
isWhitespace()
Predicate to determine if this attribute is whitespace. |
void |
setAssignment(String assignment)
Set the assignment string of this attribute. |
void |
setName(String name)
Set the name of this attribute. |
void |
setQuote(char quote)
Set the quote surrounding the value of the attribute. |
void |
setRawValue(String value)
Set the value of the attribute and the quote character. |
void |
setValue(String value)
Set the value of the attribute. |
String |
toString()
Get a text representation of this attribute. |
void |
toString(StringBuffer buffer)
Get a text representation of this attribute. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
protected String mName
null
if the attribute is whitespace.
protected String mAssignment
null
if the attribute is a
stand-alone attribute.
protected String mValue
null
if the attribute is an empty or
stand-alone attribute.
protected char mQuote
Constructor Detail |
---|
public Attribute(String name, String assignment, String value, char quote)
setRawValue(java.lang.String)
which sets the quote character to a proper value if necessary.
name
- The name of this attribute.assignment
- The assignment string of this attribute.value
- The value of this attribute.quote
- The quote around the value of this attribute.public Attribute(String name, String value, char quote)
null
, and calls setRawValue(java.lang.String)
to get the
correct quoting if quote
is zero.
name
- The name of this attribute.value
- The value of this attribute.quote
- The quote around the value of this attribute.public Attribute(String value) throws IllegalArgumentException
value
- The value of this attribute.
IllegalArgumentException
- if the value contains other than
whitespace. To set a real value use Attribute(String,String)
.public Attribute(String name, String value)
null
, and calls setRawValue(java.lang.String)
to get the
correct quoting.
name
- The name of this attribute.value
- The value of this attribute.public Attribute(String name, String assignment, String value)
setRawValue(java.lang.String)
to get the correct quoting.
name
- The name of this attribute.assignment
- The assignment string of this attribute.value
- The value of this attribute.public Attribute()
toString()
and
toString(StringBuffer)
methods.
Method Detail |
---|
public String getName()
null
if it's just a whitepace
'attribute'.setName(java.lang.String)
public void getName(StringBuffer buffer)
buffer
- The buffer to place the name in.getName()
,
setName(java.lang.String)
public void setName(String name)
null
can result in
malformed HTML if the assignment string is not null
.
name
- The new name.getName()
,
getName(StringBuffer)
public String getAssignment()
setAssignment(java.lang.String)
public void getAssignment(StringBuffer buffer)
buffer
- The buffer to place the assignment string in.getAssignment()
,
setAssignment(java.lang.String)
public void setAssignment(String assignment)
null
will result in malformed HTML. In the case of a
null
, the value
should also be set to
null
.
assignment
- The new assignment string.getAssignment()
,
getAssignment(StringBuffer)
public String getValue()
getRawValue()
.
null
if it's a stand-alone or
empty attribute, or the text if it's just a whitepace 'attribute'.setValue(java.lang.String)
public void getValue(StringBuffer buffer)
buffer
- The buffer to place the value in.getValue()
,
setValue(java.lang.String)
public void setValue(String value)
value
- The new value.getValue()
,
getValue(StringBuffer)
public char getQuote()
setQuote(char)
public void getQuote(StringBuffer buffer)
buffer
- The buffer to place the quote in.getQuote()
,
setQuote(char)
public void setQuote(char quote)
value
needs to be quoted (i.e. contains
whitespace).
quote
- The new quote value.getQuote()
,
getQuote(StringBuffer)
public String getRawValue()
null
if it's a stand-alone attribute,
or the text if it's just a whitepace 'attribute'.setRawValue(java.lang.String)
public void getRawValue(StringBuffer buffer)
buffer
- The string buffer to append the attribute value to.getRawValue()
,
setRawValue(java.lang.String)
public void setRawValue(String value)
value
as the real value.
Otherwise, examine the string to determine if quotes are needed
and an appropriate quote character if so. This may involve changing
double quotes within the string to character references.
value
- The new value.getRawValue()
,
getRawValue(StringBuffer)
public boolean isWhitespace()
true
if this attribute is whitespace,
false
if it is a real attribute.public boolean isStandAlone()
true
if this attribute is a standalone attribute.
false
if has an equals sign.public boolean isEmpty()
true
if this attribute is an empty attribute.
false
if has an equals sign and a value.public boolean isValued()
true
if this attribute has a value.
false
if it is empty or standalone.public int getLength()
public String toString()
value
name
name=
name=value
name='value'
name="value"
toString
in class Object
public void toString(StringBuffer buffer)
buffer
- The accumulator for placing the text into.toString()
|
© 2006 Derrick Oswald Sep 17, 2006
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
HTML Parser is an open source library released under Common Public License. |