Package net.shibboleth.shared.xml

# Class AttributeSupport

java.lang.Object
net.shibboleth.shared.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 a `xml:base` attribute to the given Element.
`static void`
`addXMLId(Element element, String id)`
Adds a `xml:id` attribute to the given Element.
`static void`
`addXMLLang(Element element, String lang)`
Adds a `xml:lang` attribute to the given Element.
`static void`
`addXMLSpace(Element element, XMLSpace space)`
Adds a `xml: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 String`
`ensureAttributeValue(Element element, String namespace, String attributeLocalName)`
Gets the trimmed value of an attribute from an element, ensuring it is not null or empty.
`static String`
`ensureAttributeValue(Element element, QName attributeName)`
Gets the trimmed value of an attribute from an element, ensuring it is not null or empty.
`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 an `Instant`.
`static Duration`
`getDurationAttributeValue(Attr attribute)`
Gets the value of a duration-type attribute as a `Duration`.
`static Attr`
`getIdAttribute(Element element)`
Gets the ID attribute of a DOM element.
`static String`
`getXMLBase(Element element)`
Gets the `xml:base` attribute from a given Element.
`static String`
`getXMLId(Element element)`
Gets the `xml:id` attribute from a given Element.
`static String`
`getXMLLang(Element element)`
Gets the `xml:lang` attribute from a given Element.
`static XMLSpace`
`getXMLSpace(Element element)`
Gets the `xml: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.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Constructor Details

### AttributeSupport

private AttributeSupport()
Constructor.

## Method Details

### addXMLBase

public static void addXMLBase(@Nonnull Element element, @Nonnull String base)
Adds a `xml:base` attribute to the given Element.

Parameters:
  `element` - the element to which to add the attribute
  `base` - the base value

### addXMLId

public static void addXMLId(@Nonnull Element element, @Nonnull String id)
Adds a `xml:id` attribute to the given Element.

Parameters:
  `element` - the element to which to add the attribute
  `id` - the Id value

### addXMLLang

public static void addXMLLang(@Nonnull Element element, @Nonnull String lang)
Adds a `xml:lang` attribute to the given Element.

Parameters:
  `element` - the element to which to add the attribute
  `lang` - the lang value

### addXMLSpace

public static void addXMLSpace(@Nonnull Element element, @Nonnull XMLSpace space)
Adds a `xml:space` attribute to the given Element.

Parameters:
  `element` - the element to which to add the attribute
  `space` - the space value

### appendAttribute

public static void appendAttribute(@Nonnull Element element, @Nonnull QName attributeName, @Nonnull 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 form
  `attributeValues` - the attribute values
  `element` - the target element to which to marshall
  `isIDAttribute` - 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 form
  `attributeValue` - the attribute values
  `element` - 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 form
  `attributeValue` - the attribute value
  `element` - the target element to which to marshall
  `isIDAttribute` - 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 using `instant.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 null
  `attributeName` - name of the attribute, not null
  `instant` - 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 null
  `attributeName` - name of the attribute, not null
  `duration` - 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 document
  `attributeName` - 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 document
  `namespaceURI` - the URI for the namespace the attribute is in
  `localName` - the local name
  `prefix` - the prefix of the namespace that attribute is in
Returns:
  the constructed attribute

### getAttribute

@Nullable public static Attr getAttribute(@Nonnull Element element, @Nonnull QName attributeName)
Gets the attribute with the given name.

Parameters:
  `element` - element that may contain the attribute, may be null
  `attributeName` - name of the attribute, may be null
Returns:
  the attribute or null if the given element or attribute was null or the given element did not contain an attribute with the given name

### getAttributeValue

@Nullable public static String getAttributeValue(@Nonnull Element element, @Nonnull QName attributeName)
Gets the value of an attribute from an element.

Parameters:
  `element` - the element from which to retrieve the attribute value
  `attributeName` - 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(@Nonnull Element element, @Nullable String namespace, @Nonnull @NotEmpty String attributeLocalName)
Gets the value of an attribute from an element.

Parameters:
  `element` - the element from which to retrieve the attribute value
  `namespace` - the namespace URI of the attribute
  `attributeLocalName` - the local (unqualified) attribute name
Returns:
  the value of the attribute or null if the element does not have such an attribute

### ensureAttributeValue

@Nonnull @NotEmpty public static String ensureAttributeValue(@Nonnull Element element, @Nullable String namespace, @Nonnull @NotEmpty String attributeLocalName)
Gets the trimmed value of an attribute from an element, ensuring it is not null or empty.

Parameters:
  `element` - the element from which to retrieve the attribute value
  `namespace` - the namespace URI of the attribute
  `attributeLocalName` - the local (unqualified) attribute name
Returns:
  the trimmed value of the attribute
Since:
  9.0.0

### ensureAttributeValue

@Nonnull @NotEmpty public static String ensureAttributeValue(@Nonnull Element element, @Nonnull QName attributeName)
Gets the trimmed value of an attribute from an element, ensuring it is not null or empty.

Parameters:
  `element` - the element from which to retrieve the attribute value
  `attributeName` - the name of the attribute
Returns:
  the trimmed value of the attribute
Since:
  9.0.0

### getAttributeValueAsBoolean

@Nullable public static Boolean getAttributeValueAsBoolean(@Nonnull 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.

Note that in a Spring context this is not usually useful since you are looking at values prior to property replacement. Injecting a string into a boolean setter will usually "do the right thing".

Parameters:
  `attribute` - attribute whose value will be converted to a boolean
Returns:
  boolean value of the attribute or null

### getAttributeValueAsList

@Nonnull @Unmodifiable @NotLive public static List<String> getAttributeValueAsList(@Nonnull 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(@Nonnull 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(@Nonnull Attr attribute)
Gets the value of a dateTime-type attribute as an `Instant`.

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(@Nonnull Attr attribute)
Gets the value of a duration-type attribute as a `Duration`.

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(@Nonnull 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(@Nonnull Element element)
Gets the `xml: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

@Nullable public static String getXMLId(@Nonnull Element element)
Gets the `xml: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

@Nullable public static String getXMLLang(@Nonnull Element element)
Gets the `xml: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

@Nullable public static XMLSpace getXMLSpace(@Nullable Element element)
Gets the `xml: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

public static boolean hasAttribute(@Nonnull Element element, @Nonnull QName name)
Checks if the given attribute has an attribute with the given name.

Parameters:
  `element` - element to check
  `name` - name of the attribute
Returns:
  true if the element has an attribute with the given name, false otherwise

### removeAttribute

public static boolean removeAttribute(@Nonnull Element element, @Nonnull QName attributeName)
Removes an attribute from an element.

Parameters:
  `element` - element from which the attribute should be removed
  `attributeName` - 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