xArch/xADL 2.0 Data Binding Library

edu.uci.isr.xarch
Class DOMUtils

java.lang.Object
  extended byedu.uci.isr.xarch.DOMUtils

public class DOMUtils
extends Object

Some utility functions for working with the DOM API.

Author:
Eric M. Dashofy (edashofy@ics.uci.edu)

Field Summary
protected static boolean WRONG_DOCUMENT_ERR_WORKAROUND
          This variable is set to TRUE if we're using JDK 1.4.2 or below; this fixes an obscure DOM bug that involves parsed documents' Attrs not having the proper ownerDocument and sometimes causing WRONG_DOCUMENT_ERR
 
Constructor Summary
DOMUtils()
           
 
Method Summary
static void addXSIType(Element elt, String namespaceURI, String typeName)
          Adds the xsi:type attribute to an element.
static Element cloneAndRename(Element elt, Document doc, String newNamespaceURI, String newTagName)
          Clones a DOM element and gives the clone a new tag name and namespace URI.
static Element cloneAndRename(Element elt, String newTagName)
          Clones a DOM element and gives the clone a new tag name.
static Element cloneAndRename(Element elt, String newNamespaceURI, String newTagName)
          Clones a DOM element and gives the clone a new tag name and namespace URI.
static String getAttributeValue(Node node, String namespaceURI, String attrName)
          Given a node, a namespace URI and attribute name for an attribute, gets that attribute value, or null if the attribute does not exist.
static NodeList getChildren(Node parentNode, String namespaceURI, String childName)
          Gets all the children of a given DOM node with a specific namespace URI/name combination.
static Object getDOMLock(Node n)
          Gets the DOM lock for a particular node.
static QName getXSIType(Element elt)
          Gets the xsi:type of an element in the form of a qualified name.
static boolean hasXSIType(Element elt, String namespaceURI, String typeName)
          Determines if the element has the given xsi:type.
static String normalizeString(String s)
           
static boolean objNullEq(Object object1, Object object2)
          Determines if two objects are equal.
static void order(Element elt, SequenceOrder order)
          Given an element with children and a sequence order of tag names, does a stable sort on the children of the element.
static void removeAttribute(Node node, String namespaceURI, String attrName)
          Given a node, a namespace URI and attribute name for an attribute, remoevs that attribute.
static void removeChildren(Node parentNode, String namespaceURI, String childName)
          Removes all the children of a given DOM node with a specific namespace URI/name combination.
static void setAttribute(Node node, String namespaceURI, String attrName, String attrVal)
          Given a node, a namespace URI and attribute name for an attribute, creates that attribute.
static void setXSIType(Node node, String typeName)
          Sets the xsi:type attribute on a node.
static String stripPrefix(String name)
          Strips the namespace prefix from a tag name.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

WRONG_DOCUMENT_ERR_WORKAROUND

protected static boolean WRONG_DOCUMENT_ERR_WORKAROUND
This variable is set to TRUE if we're using JDK 1.4.2 or below; this fixes an obscure DOM bug that involves parsed documents' Attrs not having the proper ownerDocument and sometimes causing WRONG_DOCUMENT_ERR

Constructor Detail

DOMUtils

public DOMUtils()
Method Detail

getDOMLock

public static final Object getDOMLock(Node n)
Gets the DOM lock for a particular node. Since DOM is not thread-safe (and this can be a real problem), methods that access the DOM directly should synchronize on the object returned by this method.

Parameters:
n - The node
Returns:
An object suitable for synchronization.

addXSIType

public static void addXSIType(Element elt,
                              String namespaceURI,
                              String typeName)
Adds the xsi:type attribute to an element.

Parameters:
elt - Element to annotate
namespaceURI - Namespace URI for the xsi:type
typeName - Type name for the xsi:type

hasXSIType

public static boolean hasXSIType(Element elt,
                                 String namespaceURI,
                                 String typeName)
Determines if the element has the given xsi:type. If the type has already been fixed with a prefix instead of a namespace URI, this will walk up the DOM tree to find the namespace URI.

Parameters:
elt - Element to check
namespaceURI - Namespace URI for the xsi:type
typeName - Type name for the xsi:type
Returns:
true if the element has the given xsi:type, false otherwise or if the namespace URI cannot be determined.

getXSIType

public static QName getXSIType(Element elt)
Gets the xsi:type of an element in the form of a qualified name. If the type has already been fixed with a prefix instead of a namespace URI, this will walk up the DOM tree to find the namespace URI.

Parameters:
elt - Element to check
Returns:
Qualified name representing the xsi:type

stripPrefix

public static String stripPrefix(String name)
Strips the namespace prefix from a tag name.

Parameters:
name - Name to strip the prefix from.
Returns:
Name stripped of prefix.

getChildren

public static NodeList getChildren(Node parentNode,
                                   String namespaceURI,
                                   String childName)
Gets all the children of a given DOM node with a specific namespace URI/name combination.

Parameters:
parentNode - DOM node to check for children.
namespaceURI - Namespace URI to look for in the children.
childName - Name to look for in the children.
Returns:
NodeList containing all children with the given namespace URI and child name.

removeChildren

public static void removeChildren(Node parentNode,
                                  String namespaceURI,
                                  String childName)
Removes all the children of a given DOM node with a specific namespace URI/name combination.

Parameters:
parentNode - DOM node to check for children.
namespaceURI - Namespace URI to look for in the children.
childName - Name to look for in the children.

getAttributeValue

public static String getAttributeValue(Node node,
                                       String namespaceURI,
                                       String attrName)
Given a node, a namespace URI and attribute name for an attribute, gets that attribute value, or null if the attribute does not exist.

Parameters:
node - Node to check for attribute
namespaceURI - Namespace URI of attribute to check for
attrName - Name of attribute to check for
Returns:
Attribute value, or null if attribute does not exist.

removeAttribute

public static void removeAttribute(Node node,
                                   String namespaceURI,
                                   String attrName)
Given a node, a namespace URI and attribute name for an attribute, remoevs that attribute.

Parameters:
node - Node to check for attribute
namespaceURI - Namespace URI of attribute to remove
attrName - Name of attribute to remove

setAttribute

public static void setAttribute(Node node,
                                String namespaceURI,
                                String attrName,
                                String attrVal)
Given a node, a namespace URI and attribute name for an attribute, creates that attribute.

Parameters:
node - Node to add attribute to
namespaceURI - Namespace URI of attribute to add
attrName - Name of attribute to add

setXSIType

public static void setXSIType(Node node,
                              String typeName)
Sets the xsi:type attribute on a node.

Parameters:
node - Node to set type on
typeName - Value for xsi:type attribute.

order

public static void order(Element elt,
                         SequenceOrder order)
Given an element with children and a sequence order of tag names, does a stable sort on the children of the element. Children with tag names not specified in the sequence order are shifted to the end, and kept in their original order.

Parameters:
elt - Element on which to sort children.
order - Sequence order defining sort order for children.

objNullEq

public static boolean objNullEq(Object object1,
                                Object object2)
Determines if two objects are equal. Two null objects are considered to be equal. If only one of the objects is non-null, they are not equal. If both are non-null, then this returns object1.equals(object2);

Parameters:
object1 - First object to compare.
object2 - Second object to compare.
Returns:
true or false according to the above description.

cloneAndRename

public static Element cloneAndRename(Element elt,
                                     String newTagName)
Clones a DOM element and gives the clone a new tag name.

Parameters:
elt - Element to clone.
newTagName - New tag name for clone.
Returns:
cloned and renamed element.

cloneAndRename

public static Element cloneAndRename(Element elt,
                                     String newNamespaceURI,
                                     String newTagName)
Clones a DOM element and gives the clone a new tag name and namespace URI.

Parameters:
elt - Element to clone.
newNamespaceURI - New namespace URI for clone.
newTagName - New tag name for clone.
Returns:
cloned and renamed element.

cloneAndRename

public static Element cloneAndRename(Element elt,
                                     Document doc,
                                     String newNamespaceURI,
                                     String newTagName)
Clones a DOM element and gives the clone a new tag name and namespace URI. Also moves the element and all its children into a new document, if necessary.

Parameters:
elt - Element to clone.
newNamespaceURI - New namespace URI for clone.
newTagName - New tag name for clone.
Returns:
cloned and renamed element.

normalizeString

public static String normalizeString(String s)

xArch/xADL 2.0 Data Binding Library