net.sf.xbus.base.xml
Class XDomSupport

java.lang.Object
  extended by net.sf.xbus.base.xml.XDomSupport

public abstract class XDomSupport
extends java.lang.Object

XDomSupport is a collection of operations on DOM trees. It extends the possibilities offered in the org.w3c.dom package.

Author:
Stephan Dwel

Constructor Summary
XDomSupport()
           
 
Method Summary
static void deleteWhitespaceTextInElementNodes(org.w3c.dom.Document doc)
          deleteWhitespaceTextInElementNodes deletes all text nodes containing only whitespace within xml tags.
static void deleteWhitespaceTextInElementNodesAndComments(org.w3c.dom.Document doc)
          deleteWhitespaceTextInElementNodesAndComments deletes all text nodes containing only whitespace within xml tags and all comment nodes.
static java.util.List getChildElementsByValue(org.w3c.dom.Node parent, java.lang.String value, java.lang.String nodeTag)
          getChildElementsByValue retrieves child nodes of type element selected by a certain for the value stored with them.
static java.util.List getChildNodesByAttrValue(org.w3c.dom.Node parent, java.lang.String attrName, java.lang.String attrValue, java.lang.String nodeTag)
          getChildNodesByAttrValue retrieves child nodes selected by a certain value of a certain attribute.
static int getNextNodeIndexIgnoringFormatingText(org.w3c.dom.NodeList nodes, int startPos)
          getNextNodeIndexIgnoringFormatingText retrieves the position of the next node after the position startPos within the list nodes which is not a text node only containing whitespace.
static java.lang.String getNodeText(org.w3c.dom.Node node)
           
static java.lang.String getTrimedNodeText(org.w3c.dom.Node node)
          getTrimedNodeText retrieves the value for a node representing the corresponding xml tag.
static boolean isValidTagName(java.lang.String name)
          isValidTagName checks if a string conforms to the conventions for xml tag names.
static java.lang.String makeTagNameValid(java.lang.String name)
          Removes invalid characters from tag names.
static void recursivePostOrder(org.w3c.dom.Node node, IteratedNodeOperation itop)
          recursivePostOrder is the recursive heart of DOM tree pre-order traversal.
static void traversePostOrder(org.w3c.dom.Document doc, IteratedNodeOperation itop)
          traversePostOrder traverses a DOM tree in post-order and executes the specified operation on each node.
static void traversePreOrder(org.w3c.dom.Document doc, IteratedNodeOperation itop)
          traversePreOrder traverses a DOM tree in pre-order and executes the specified operation on each node.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XDomSupport

public XDomSupport()
Method Detail

getChildNodesByAttrValue

public static java.util.List getChildNodesByAttrValue(org.w3c.dom.Node parent,
                                                      java.lang.String attrName,
                                                      java.lang.String attrValue,
                                                      java.lang.String nodeTag)
                                               throws java.lang.IllegalArgumentException
getChildNodesByAttrValue retrieves child nodes selected by a certain value of a certain attribute. Child nodes without the specified attribute are not selected.

Parameters:
parent - the node for which children are searched
attrName - the attribute to check
attrValue - the attribute's value to search for
nodeTag - the tag name for the searched child nodes, if null or empty string the tag name is not used for the selection
Returns:
the list of selected child nodes
Throws:
java.lang.IllegalArgumentException - in case that parent, attrName or attrValue are null or attrName is empty

getNodeText

public static java.lang.String getNodeText(org.w3c.dom.Node node)

getTrimedNodeText

public static java.lang.String getTrimedNodeText(org.w3c.dom.Node node)
getTrimedNodeText retrieves the value for a node representing the corresponding xml tag.

Parameters:
node - the node representing the xml tag
Returns:
the value stored within the xml tag, the string is trimed for leading and ending white space

getChildElementsByValue

public static java.util.List getChildElementsByValue(org.w3c.dom.Node parent,
                                                     java.lang.String value,
                                                     java.lang.String nodeTag)
                                              throws java.lang.IllegalArgumentException
getChildElementsByValue retrieves child nodes of type element selected by a certain for the value stored with them.

Parameters:
parent - the node for which children are searched
value - the value to search for
nodeTag - the tag name for the searched child nodes, if null or empty string the tag name is not used for the selection
Returns:
the list of selected child nodes
Throws:
java.lang.IllegalArgumentException - in case that parent or value are null

getNextNodeIndexIgnoringFormatingText

public static int getNextNodeIndexIgnoringFormatingText(org.w3c.dom.NodeList nodes,
                                                        int startPos)
getNextNodeIndexIgnoringFormatingText retrieves the position of the next node after the position startPos within the list nodes which is not a text node only containing whitespace.

Parameters:
nodes - the node list
startPos - the position to start from
Returns:
the position; -1 if no node was found

deleteWhitespaceTextInElementNodes

public static void deleteWhitespaceTextInElementNodes(org.w3c.dom.Document doc)
                                               throws XException
deleteWhitespaceTextInElementNodes deletes all text nodes containing only whitespace within xml tags. This operation is useful to get rid of formatting information in a parsed xml file.

Parameters:
doc - the DOM tree to clean
Throws:
XException - only for syntactical reasons (use of a generic traversal method)

deleteWhitespaceTextInElementNodesAndComments

public static void deleteWhitespaceTextInElementNodesAndComments(org.w3c.dom.Document doc)
                                                          throws XException
deleteWhitespaceTextInElementNodesAndComments deletes all text nodes containing only whitespace within xml tags and all comment nodes. This operation is useful to get rid of formatting and editor information in a parsed xml file.

Parameters:
doc - the DOM tree to clean
Throws:
XException - only for syntactical reasons (use of a generic traversal method)

traversePreOrder

public static void traversePreOrder(org.w3c.dom.Document doc,
                                    IteratedNodeOperation itop)
                             throws XException
traversePreOrder traverses a DOM tree in pre-order and executes the specified operation on each node. The traversal may treat dynamically deleted nodes but no extensions.

Parameters:
doc - the DOM tree to traverse
itop - the operation to execute
Throws:
XException - in case that itop throws one

traversePostOrder

public static void traversePostOrder(org.w3c.dom.Document doc,
                                     IteratedNodeOperation itop)
                              throws XException
traversePostOrder traverses a DOM tree in post-order and executes the specified operation on each node. The traversal may treat dynamically deleted nodes but no extensions.

Parameters:
doc - the DOM tree to traverse
itop - the operation to execute
Throws:
XException - in case that itop throws one

recursivePostOrder

public static void recursivePostOrder(org.w3c.dom.Node node,
                                      IteratedNodeOperation itop)
                               throws XException
recursivePostOrder is the recursive heart of DOM tree pre-order traversal. It proceeds on all children of the specified node and then executes the specified operation on the node itself. The traversal may treat dynamically deleted nodes but no extensions.

Parameters:
node - the actual node
itop - the operation to execute
Throws:
XException - in case that itop throws one

isValidTagName

public static boolean isValidTagName(java.lang.String name)
isValidTagName checks if a string conforms to the conventions for xml tag names.

Parameters:
name - the string to check
Returns:
true if the string is a well-formed tag name, otherwise false

makeTagNameValid

public static java.lang.String makeTagNameValid(java.lang.String name)
Removes invalid characters from tag names.

Parameters:
name - the name of the tag
Returns:
the name of the tag without invalid characters