net.shibboleth.utilities.java.support.xml
Class AttributeSupport

java.lang.Object
  extended by net.shibboleth.utilities.java.support.xml.AttributeSupport

public final class AttributeSupport
extends Object

Set of helper methods for working with DOM Attributes.


Constructor Summary
private AttributeSupport()
          Constructor.
 
Method Summary
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, List<String> attributeValues, boolean isIDAttribute)
          Adds an attribute name and value to a DOM 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 appendDateTimeAttribute(Element element, QName attributeName, long duration)
          Adds an attribute to the given element.
static void appendDurationAttribute(Element element, QName attributeName, long duration)
          Adds an attribute to given element.
static Attr constructAttribute(Document owningDocument, QName attributeName)
          Constructs an attribute owned by the given document with the given name.
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 getAttribute(Element element, QName attributeName)
          Gets the attribute with the given name.
static String getAttributeValue(Element element, QName attributeName)
          Gets the value of an attribute from an element.
static String getAttributeValue(Element element, String namespace, String attributeLocalName)
          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 Long getDateTimeAttributeAsLong(Attr attribute)
          Gets the value of a dateTime-type attribute in milliseconds since the epoch.
static Long getDurationAttributeValueAsLong(Attr attribute)
          Gets the value of a duration-type attribute in milliseconds.
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 Locale getXMLLangAsLocale(Element element)
          Gets the locale currently active for the 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 Detail

AttributeSupport

private AttributeSupport()
Constructor.

Method Detail

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,
                                   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,
                                           long duration)
Adds an attribute to the given element. The value of the attribute is the instant now + the given duration expressed in XML dateTime format.

Parameters:
element - element to which the attribute will be added, not null
attributeName - name of the attribute, not null
duration - duration, in milliseconds, must be greater than 0

appendDurationAttribute

public static void appendDurationAttribute(@Nonnull
                                           Element element,
                                           @Nonnull
                                           QName attributeName,
                                           long 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, in milliseconds, 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

@Nonnull
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 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 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 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(@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 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

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

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

getDateTimeAttributeAsLong

@Nullable
public static Long getDateTimeAttributeAsLong(@Nullable
                                                       Attr attribute)
Gets the value of a dateTime-type attribute in milliseconds since the epoch.

Parameters:
attribute - attribute from which to extract the value, may be null
Returns:
date/time in millisecond since the epoch, or null if the attribute was null

getDurationAttributeValueAsLong

@Nullable
public static Long getDurationAttributeValueAsLong(@Nullable
                                                            Attr attribute)
Gets the value of a duration-type attribute in milliseconds.

Parameters:
attribute - attribute from which to extract the value, may be null
Returns:
duration, in millisecond, 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 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(@Nullable
                                       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(@Nullable
                                         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

getXMLLangAsLocale

@Nullable
public static Locale getXMLLangAsLocale(@Nullable
                                                 Element element)
Gets the locale currently active for the element. This is done by looking for an xml:lang attribute and parsing its content. If no xml:lang attribute is present the default locale is returned. This method only uses the language primary tag, as defined by RFC3066.

Parameters:
element - element to retrieve local information for
Returns:
the active local of the element

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(@Nullable
                                   Element element,
                                   @Nullable
                                   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(@Nullable
                                      Element element,
                                      @Nullable
                                      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


Copyright © 1999-2012. All Rights Reserved.