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 privateAttributeSupport()Constructor.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidaddXMLBase(Element element, String base)Adds axml:baseattribute to the given Element.static voidaddXMLId(Element element, String id)Adds axml:idattribute to the given Element.static voidaddXMLLang(Element element, String lang)Adds axml:langattribute to the given Element.static voidaddXMLSpace(Element element, XMLSpace space)Adds axml:spaceattribute to the given Element.static voidappendAttribute(Element element, QName attributeName, String attributeValue)Adds an non-id attribute name and value to a DOM Element.static voidappendAttribute(Element element, QName attributeName, String attributeValue, boolean isIDAttribute)Adds an attribute name and value to a DOM Element.static voidappendAttribute(Element element, QName attributeName, List<String> attributeValues, boolean isIDAttribute)Adds an attribute name and value to a DOM Element.static voidappendDateTimeAttribute(Element element, QName attributeName, Instant instant)Adds an attribute to the given element.static voidappendDurationAttribute(Element element, QName attributeName, Duration duration)Adds an attribute to given element.static AttrconstructAttribute(Document document, String namespaceURI, String localName, String prefix)Constructs an attribute owned by the given document with the given name.static AttrconstructAttribute(Document owningDocument, QName attributeName)Constructs an attribute owned by the given document with the given name.static AttrgetAttribute(Element element, QName attributeName)Gets the attribute with the given name.static StringgetAttributeValue(Element element, String namespace, String attributeLocalName)Gets the value of an attribute from an element.static StringgetAttributeValue(Element element, QName attributeName)Gets the value of an attribute from an element.static BooleangetAttributeValueAsBoolean(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 QNamegetAttributeValueAsQName(Attr attribute)Constructs a QName from an attributes value.static InstantgetDateTimeAttribute(Attr attribute)Gets the value of a dateTime-type attribute as anInstant.static DurationgetDurationAttributeValue(Attr attribute)Gets the value of a duration-type attribute as aDuration.static AttrgetIdAttribute(Element element)Gets the ID attribute of a DOM element.static StringgetXMLBase(Element element)Gets thexml:baseattribute from a given Element.static StringgetXMLId(Element element)Gets thexml:idattribute from a given Element.static StringgetXMLLang(Element element)Gets thexml:langattribute from a given Element.static XMLSpacegetXMLSpace(Element element)Gets thexml:spaceattribute from a given Element.static booleanhasAttribute(Element element, QName name)Checks if the given attribute has an attribute with the given name.static booleanremoveAttribute(Element element, QName attributeName)Removes an attribute from an element.
-
-
-
Method Detail
-
addXMLBase
public static void addXMLBase(@Nonnull Element element, @Nonnull String base)Adds axml:baseattribute to the given Element.- Parameters:
element- the element to which to add the attributebase- the base value
-
addXMLId
public static void addXMLId(@Nonnull Element element, @Nonnull String id)Adds axml:idattribute to the given Element.- Parameters:
element- the element to which to add the attributeid- the Id value
-
addXMLLang
public static void addXMLLang(@Nonnull Element element, @Nonnull String lang)Adds axml:langattribute to the given Element.- Parameters:
element- the element to which to add the attributelang- the lang value
-
addXMLSpace
public static void addXMLSpace(@Nonnull Element element, @Nonnull XMLSpace space)Adds axml:spaceattribute 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
@Nullable public static Boolean getAttributeValueAsBoolean(@Nullable Attr attribute)
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
@Nonnull public static List<String> getAttributeValueAsList(@Nullable Attr attribute)
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
@Nullable public static QName getAttributeValueAsQName(@Nullable Attr attribute)
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
@Nullable public static Instant getDateTimeAttribute(@Nullable Attr attribute)
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
@Nullable public static Duration getDurationAttributeValue(@Nullable Attr attribute)
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
@Nullable public static Attr getIdAttribute(@Nullable Element element)
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
@Nullable public static String getXMLBase(@Nullable Element element)
Gets thexml:baseattribute 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
@Nullable public static String getXMLId(@Nullable Element element)
Gets thexml:idattribute 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
@Nullable public static String getXMLLang(@Nullable Element element)
Gets thexml:langattribute 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
@Nullable public static XMLSpace getXMLSpace(@Nullable Element element)
Gets thexml:spaceattribute 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
public static boolean hasAttribute(@Nullable Element element, @Nullable QName name)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
public static boolean removeAttribute(@Nullable Element element, @Nullable QName attributeName)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
-
-