Class AttributeSupport
java.lang.Object
net.shibboleth.utilities.java.support.xml.AttributeSupport
public final class AttributeSupport extends Object
Set of helper methods for working with DOM Attributes.
-
Constructor Summary
Constructors Modifier Constructor Description private
AttributeSupport()
Constructor. -
Method Summary
Modifier and Type Method Description static void
addXMLBase(Element element, String base)
Adds axml:base
attribute to the given Element.static void
addXMLId(Element element, String id)
Adds axml:id
attribute to the given Element.static void
addXMLLang(Element element, String lang)
Adds axml:lang
attribute to the given Element.static void
addXMLSpace(Element element, XMLSpace space)
Adds axml:space
attribute to the given Element.static void
appendAttribute(Element element, QName attributeName, String attributeValue)
Adds an non-id attribute name and value to a DOM Element.static void
appendAttribute(Element element, QName attributeName, String attributeValue, boolean isIDAttribute)
Adds an attribute name and value to a DOM Element.static void
appendAttribute(Element element, QName attributeName, List<String> attributeValues, boolean isIDAttribute)
Adds an attribute name and value to a DOM Element.static void
appendDateTimeAttribute(Element element, QName attributeName, Instant instant)
Adds an attribute to the given element.static void
appendDurationAttribute(Element element, QName attributeName, Duration duration)
Adds an attribute to given element.static Attr
constructAttribute(Document document, String namespaceURI, String localName, String prefix)
Constructs an attribute owned by the given document with the given name.static Attr
constructAttribute(Document owningDocument, QName attributeName)
Constructs an attribute owned by the given document with the given name.static Attr
getAttribute(Element element, QName attributeName)
Gets the attribute with the given name.static String
getAttributeValue(Element element, String namespace, String attributeLocalName)
Gets the value of an attribute from an element.static String
getAttributeValue(Element element, QName attributeName)
Gets the value of an attribute from an element.static Boolean
getAttributeValueAsBoolean(Attr attribute)
Parses the attribute's value.static List<String>
getAttributeValueAsList(Attr attribute)
Gets the value of a list-type attribute as a list.static QName
getAttributeValueAsQName(Attr attribute)
Constructs a QName from an attributes value.static Instant
getDateTimeAttribute(Attr attribute)
Gets the value of a dateTime-type attribute as anInstant
.static Duration
getDurationAttributeValue(Attr attribute)
Gets the value of a duration-type attribute as aDuration
.static Attr
getIdAttribute(Element element)
Gets the ID attribute of a DOM element.static String
getXMLBase(Element element)
Gets thexml:base
attribute from a given Element.static String
getXMLId(Element element)
Gets thexml:id
attribute from a given Element.static String
getXMLLang(Element element)
Gets thexml:lang
attribute from a given Element.static XMLSpace
getXMLSpace(Element element)
Gets thexml:space
attribute from a given Element.static boolean
hasAttribute(Element element, QName name)
Checks if the given attribute has an attribute with the given name.static boolean
removeAttribute(Element element, QName attributeName)
Removes an attribute from an element.
-
Constructor Details
-
AttributeSupport
private AttributeSupport()Constructor.
-
-
Method Details
-
addXMLBase
Adds axml:base
attribute to the given Element.- Parameters:
element
- the element to which to add the attributebase
- the base value
-
addXMLId
Adds axml:id
attribute to the given Element.- Parameters:
element
- the element to which to add the attributeid
- the Id value
-
addXMLLang
Adds axml:lang
attribute to the given Element.- Parameters:
element
- the element to which to add the attributelang
- the lang value
-
addXMLSpace
Adds axml:space
attribute to the given Element.- Parameters:
element
- the element to which to add the attributespace
- the space value
-
appendAttribute
public static void appendAttribute(@Nonnull Element element, @Nonnull QName attributeName, List<String> attributeValues, boolean isIDAttribute)Adds an attribute name and value to a DOM Element. This is particularly useful for attributes whose names appear in namespace-qualified form.- Parameters:
attributeName
- the attribute name in QName formattributeValues
- the attribute valueselement
- the target element to which to marshallisIDAttribute
- flag indicating whether the attribute being marshalled should be handled as an ID-typed attribute
-
appendAttribute
public static void appendAttribute(@Nonnull Element element, @Nonnull QName attributeName, @Nonnull String attributeValue)Adds an non-id attribute name and value to a DOM Element. This is particularly useful for attributes whose names appear in namespace-qualified form.- Parameters:
attributeName
- the attribute name in QName formattributeValue
- the attribute valueselement
- the target element to which to marshall
-
appendAttribute
public static void appendAttribute(@Nonnull Element element, @Nonnull QName attributeName, @Nonnull String attributeValue, boolean isIDAttribute)Adds an attribute name and value to a DOM Element. This is particularly useful for attributes whose names appear in namespace-qualified form.- Parameters:
attributeName
- the attribute name in QName formattributeValue
- the attribute valueelement
- the target element to which to marshallisIDAttribute
- flag indicating whether the attribute being marshalled should be handled as an ID-typed attribute
-
appendDateTimeAttribute
public static void appendDateTimeAttribute(@Nonnull Element element, @Nonnull QName attributeName, @Nonnull Instant instant)Adds an attribute to the given element. The value of the attribute is the given instant expressed in XML dateTime format. Note that simply usinginstant.toString()
is equivalent for many use cases, but the result will be different on a system with a higher-resolution clock, as the resulting string value may have sub-millisecond precision. This method always works to millisecond precision.- Parameters:
element
- element to which the attribute will be added, not nullattributeName
- name of the attribute, not nullinstant
- instant to set into the attribute, not null
-
appendDurationAttribute
public static void appendDurationAttribute(@Nonnull Element element, @Nonnull QName attributeName, @Nonnull Duration duration)Adds an attribute to given element. The value of the attribute is the given duration in XML duration format.- Parameters:
element
- element to which the attribute will be added, not nullattributeName
- name of the attribute, not nullduration
- duration, must be greater than 0
-
constructAttribute
@Nonnull public static Attr constructAttribute(@Nonnull Document owningDocument, @Nonnull QName attributeName)Constructs an attribute owned by the given document with the given name.- Parameters:
owningDocument
- the owning documentattributeName
- the name of that attribute- Returns:
- the constructed attribute
-
constructAttribute
@Nonnull public static Attr constructAttribute(@Nonnull Document document, @Nullable String namespaceURI, @Nonnull String localName, @Nullable String prefix)Constructs an attribute owned by the given document with the given name.- Parameters:
document
- the owning documentnamespaceURI
- the URI for the namespace the attribute is inlocalName
- the local nameprefix
- the prefix of the namespace that attribute is in- Returns:
- the constructed attribute
-
getAttribute
@Nullable public static Attr getAttribute(@Nullable Element element, @Nullable QName attributeName)Gets the attribute with the given name.- Parameters:
element
- element that may contain the attribute, may be nullattributeName
- name of the attribute, may be null- Returns:
- the attribute or null if the given element or attribute was null or the given attribute did not contain an attribute with the given name
-
getAttributeValue
@Nullable public static String getAttributeValue(@Nullable Element element, @Nullable QName attributeName)Gets the value of an attribute from an element.- Parameters:
element
- the element from which to retrieve the attribute valueattributeName
- the name of the attribute- Returns:
- the value of the attribute or null if the element does not have such an attribute
-
getAttributeValue
@Nullable public static String getAttributeValue(@Nullable Element element, @Nullable String namespace, @Nullable String attributeLocalName)Gets the value of an attribute from an element.- Parameters:
element
- the element from which to retrieve the attribute valuenamespace
- the namespace URI of the attributeattributeLocalName
- the local (unqualified) attribute name- Returns:
- the value of the attribute or null if the element does not have such an attribute
-
getAttributeValueAsBoolean
Parses the attribute's value. If the value is 0 or "false" then false is returned, if the value is 1 or "true" then true is returned, if the value is anything else then null returned.- Parameters:
attribute
- attribute whose value will be converted to a boolean- Returns:
- boolean value of the attribute or null late enough to allow property replacement.
-
getAttributeValueAsList
Gets the value of a list-type attribute as a list.- Parameters:
attribute
- attribute whose value will be turned into a list- Returns:
- list of values, never null
-
getAttributeValueAsQName
Constructs a QName from an attributes value.- Parameters:
attribute
- the attribute with a QName value- Returns:
- a QName from an attributes value, or null if the given attribute is null
-
getDateTimeAttribute
Gets the value of a dateTime-type attribute as anInstant
.- Parameters:
attribute
- attribute from which to extract the value, may be null- Returns:
- date/time as an
Instant
, or null if the attribute was null
-
getDurationAttributeValue
Gets the value of a duration-type attribute as aDuration
.- Parameters:
attribute
- attribute from which to extract the value, may be null- Returns:
- duration, or null if the attribute was null
-
getIdAttribute
Gets the ID attribute of a DOM element.- Parameters:
element
- the DOM element- Returns:
- the ID attribute or null if there isn't one
-
getXMLBase
Gets thexml:base
attribute from a given Element.- Parameters:
element
- the element from which to extract the attribute- Returns:
- the value of the xml:base attribute, or null if not present
-
getXMLId
Gets thexml:id
attribute from a given Element.- Parameters:
element
- the element from which to extract the attribute- Returns:
- the value of the xml:id attribute, or null if not present
-
getXMLLang
Gets thexml:lang
attribute from a given Element.- Parameters:
element
- the element from which to extract the attribute- Returns:
- the value of the xml:lang attribute, or null if not present
-
getXMLSpace
Gets thexml:space
attribute from a given Element.- Parameters:
element
- the element from which to extract the attribute- Returns:
- the value of the xml:space attribute, or null if not present
-
hasAttribute
Checks if the given attribute has an attribute with the given name.- Parameters:
element
- element to checkname
- name of the attribute- Returns:
- true if the element has an attribute with the given name, false otherwise
-
removeAttribute
Removes an attribute from an element.- Parameters:
element
- element from which the attribute should be removedattributeName
- name of the attribute to be removed- Returns:
- true if the element contained the attribute and it was removed, false if the element did not contain such an attribute
-