Class Attribute
The Attribute class encapsulates all information needed to preserve the exact formatting of XML attributes during round-trip processing. It maintains both the decoded attribute value and the original raw value with entities preserved, along with formatting details like quote style and whitespace.
Attribute Properties:
- Quote Style Preservation - Maintains single vs double quotes
- Whitespace Preservation - Preserves spacing before attributes
- Entity Preservation - Maintains original entity encoding
- Fluent Mutation - Mutable setters with method chaining, plus immutable-style
withX()methods - Fluent API - Creation and modification with method chaining
Usage Examples:
// Create basic attribute
Attribute attr = new Attribute("class", "important");
// Create using factory methods
Attribute simple = Attribute.of("class", "important");
Attribute withQuotes = Attribute.of("id", "main", QuoteStyle.SINGLE);
Attribute complete = Attribute.of("data-value", "test & example", QuoteStyle.DOUBLE, " ");
// Create and modify with fluent API
Attribute fluent = Attribute.of("class", "initial")
.value("updated")
.quoteStyle(QuoteStyle.SINGLE)
.precedingWhitespace(" ");
// Create immutable variations
Attribute modified = attr.withValue("critical").withQuoteStyle(QuoteStyle.SINGLE);
Attribute Formatting:
Attributes are serialized with the following format:
[whitespace][name]=[quote][value][quote]
Example: class="important" or id='main'
Entity Handling:
The class automatically handles XML entity escaping for attribute values:
&→&<→<>→>"→"(when using double quotes)'→'(when using single quotes)
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionclone()Deprecated.copy()Creates a deep copy of this attribute.booleangetSerializationValue(boolean useRaw) Provides the attribute value to use during XML serialization, preferring the original raw text when requested.inthashCode()name()static AttributeCreates an attribute from a QName and value.static AttributeCreates an attribute with the specified name and value.static Attributeof(String name, String value, QuoteStyle quoteStyle) Creates an attribute with the specified name, value, and quote style.static Attributeof(String name, String value, QuoteStyle quoteStyle, String precedingWhitespace) Creates an attribute with all formatting options.precedingWhitespace(String precedingWhitespace) quoteStyle(QuoteStyle quoteStyle) rawValue()toString()voidtoXml(StringBuilder sb, boolean useRaw) Serializes this attribute to XMLvalue()withPrecedingWhitespace(String newWhitespace) Creates a new attribute with the specified preceding whitespace, preserving other properties.withQuoteStyle(QuoteStyle newQuoteStyle) Creates a new attribute with the specified quote style, preserving other properties.Creates a new attribute with the specified value, preserving other properties.
-
Constructor Details
-
Attribute
-
Attribute
-
Attribute
public Attribute(String name, String value, QuoteStyle quoteStyle, String precedingWhitespace, String rawValue) -
Attribute
public Attribute(String name, String value, char quoteChar, String precedingWhitespace) throws DomTripException - Throws:
DomTripException
-
Attribute
public Attribute(String name, String value, char quoteChar, String precedingWhitespace, String rawValue) throws DomTripException - Throws:
DomTripException
-
-
Method Details
-
name
-
value
-
value
-
rawValue
-
rawValue
-
quoteStyle
-
quoteStyle
-
precedingWhitespace
-
precedingWhitespace
-
getSerializationValue
Provides the attribute value to use during XML serialization, preferring the original raw text when requested.- Parameters:
useRaw- if true, return the preserved raw attribute text when present; otherwise produce an escaped form- Returns:
- `rawValue` if `useRaw` is true and a raw value exists, otherwise the escaped form of `value` using the active quote character
-
toXml
Serializes this attribute to XML -
toString
-
equals
-
hashCode
-
withValue
-
withQuoteStyle
Creates a new attribute with the specified quote style, preserving other properties. -
withPrecedingWhitespace
-
copy
Creates a deep copy of this attribute.- Returns:
- a new attribute that is a copy of this attribute
- Since:
- 1.1.0
-
clone
Deprecated.Usecopy()instead.Creates a deep copy of this attribute. -
of
-
of
Creates an attribute with the specified name, value, and quote style.Factory method for creating attributes with specific formatting.
- Parameters:
name- the attribute namevalue- the attribute valuequoteStyle- the quote style to use- Returns:
- a new Attribute
-
of
Creates an attribute from a QName and value.Creates an attribute using the QName's qualified name as the attribute name. This is useful for creating namespaced attributes.
- Parameters:
qname- the QName for the attributevalue- the attribute value- Returns:
- a new Attribute
- Throws:
DomTripException- if qname is null
-
of
public static Attribute of(String name, String value, QuoteStyle quoteStyle, String precedingWhitespace) Creates an attribute with all formatting options.Factory method for creating attributes with complete control over formatting.
- Parameters:
name- the attribute namevalue- the attribute valuequoteStyle- the quote style to useprecedingWhitespace- the whitespace before the attribute- Returns:
- a new Attribute
-
copy()instead.