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

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

public final class NamespaceSupport
extends Object

Set of helper methods for working with DOM namespaces.


Constructor Summary
private NamespaceSupport()
          Constructor.
 
Method Summary
static void appendNamespaceDeclaration(Element element, String namespaceURI, String prefix)
          Adds a namespace declaration (xmlns:) attribute to the given element.
static String lookupNamespaceURI(Element startingElement, Element stoppingElement, String prefix)
          Looks up the namespace URI associated with the given prefix starting at the given element.
static String lookupPrefix(Element startingElement, Element stopingElement, String namespaceURI)
          Looks up the namespace prefix associated with the given URI starting at the given element.
static void rootNamespaces(Element domElement)
          Ensures that all the visibly used namespaces referenced by the given Element or its descendants are declared by the given Element or one of its descendants.
private static void rootNamespaces(Element domElement, Element upperNamespaceSearchBound)
          Recursively called function that ensures all the visibly used namespaces referenced by the given Element or its descendants are declared if they don't appear in the list of already resolved namespaces.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

NamespaceSupport

private NamespaceSupport()
Constructor.

Method Detail

appendNamespaceDeclaration

public static void appendNamespaceDeclaration(@Nonnull
                                              Element element,
                                              @Nonnull
                                              String namespaceURI,
                                              @Nullable
                                              String prefix)
Adds a namespace declaration (xmlns:) attribute to the given element.

Parameters:
element - the element to add the attribute to
namespaceURI - the URI of the namespace. Cannot be null or empty (semantics would be undefined)
prefix - the prefix for the namespace. If this is null this is the default namespace being added.

lookupNamespaceURI

@Nullable
public static String lookupNamespaceURI(@Nonnull
                                                 Element startingElement,
                                                 @Nullable
                                                 Element stoppingElement,
                                                 @Nonnull
                                                 String prefix)
Looks up the namespace URI associated with the given prefix starting at the given element. This method differs from the Node.lookupNamespaceURI(java.lang.String) in that it only returns those namespaces declared by an xmlns attribute are inspected. The Node method also checks the namespace a particular node was created in by way of a call like Document.createElementNS(java.lang.String, java.lang.String) even if the resulting element doesn't have an namespace declaration attribute.

Parameters:
startingElement - the starting element
stoppingElement - the ancestor of the starting element that serves as the upper-bound, inclusive, for the search
prefix - the prefix to look up. If null then the default namespace is returned.
Returns:
the namespace URI for the given prefer or null

lookupPrefix

@Nullable
public static String lookupPrefix(@Nonnull
                                           Element startingElement,
                                           @Nullable
                                           Element stopingElement,
                                           @Nullable
                                           String namespaceURI)
Looks up the namespace prefix associated with the given URI starting at the given element. This method differs from the Node.lookupPrefix(java.lang.String) in that it only those namespaces declared by an xmlns attribute are inspected. The Node method also checks the namespace a particular node was created in by way of a call like Document.createElementNS(java.lang.String, java.lang.String) even if the resulting element doesn't have an namespace delcaration attribute.

Note:The prefix returned may not necessarily refer to the corresponding namespace within this element (if, for instance the prefix is associated with a namespaces at different points of the hierarchy.

Parameters:
startingElement - the starting element
stopingElement - the ancestor of the starting element that serves as the upper-bound, inclusive, for the search
namespaceURI - the uri to look up
Returns:
the prefix for the given namespace URI or null if non exists or the the URI is for the default namespace.

rootNamespaces

public static void rootNamespaces(@Nullable
                                  Element domElement)
Ensures that all the visibly used namespaces referenced by the given Element or its descendants are declared by the given Element or one of its descendants. NOTE: This is a very costly operation.

Parameters:
domElement - the element to act as the root of the namespace declarations

rootNamespaces

private static void rootNamespaces(@Nullable
                                   Element domElement,
                                   @Nullable
                                   Element upperNamespaceSearchBound)
Recursively called function that ensures all the visibly used namespaces referenced by the given Element or its descendants are declared if they don't appear in the list of already resolved namespaces.

Parameters:
domElement - the Element
upperNamespaceSearchBound - the "root" element of the fragment where namespaces may be rooted


Copyright © 1999-2012. All Rights Reserved.