org.jscience.ml.cml.util
Class PMRDOMUtils

java.lang.Object
  extended by org.jscience.ml.cml.util.PMRDOMUtils

public class PMRDOMUtils
extends java.lang.Object

re-usable routines to support PMR* interfaces


Field Summary
static int PRETTY
          Description of the Field
 
Constructor Summary
PMRDOMUtils()
           
 
Method Summary
static org.w3c.dom.Node cloneNode(org.w3c.dom.Node node, org.w3c.dom.Document document)
          clones node in context of new document.
static org.w3c.dom.Attr[] copyAttributes(org.w3c.dom.Element e, Picker p)
          copies attributes chosen via a Picker callback.
static java.util.Vector createVector(org.w3c.dom.NamedNodeMap nodeMap)
          converts a NamedNodeMap to a vector

the vector (but not its elements) is safely mutable.

static java.util.Vector createVector(org.w3c.dom.NodeList nodes)
          converts a NodeList to a vector

the vector (but not its elements) is safely mutable.

static org.w3c.dom.Attr[] cutAttributes(org.w3c.dom.Element e, Picker p)
          copies and removes attributes chosen via a Picker callback.
static java.lang.String debug(org.w3c.dom.Node node)
          convenience routine to output a Node as a String
static void deleteWhitespaceDescendants(org.w3c.dom.Document document)
          recursively removes all whitespace nodes
static void deleteWhitespaceDescendants(org.w3c.dom.Element element)
          recursively deletes all whitespace descendants
static java.util.Enumeration depthFirstEnumeration(org.w3c.dom.Element element)
          return all descendants in document order
static org.w3c.dom.Element getAncestor(org.w3c.dom.Element element, java.lang.String tagName)
          gets first ancestor of 'element' with tagName; [element is not its own ancestor]
static java.util.Vector getAncestors(org.w3c.dom.Element element)
          gets all ancestors of 'element' including the document Element but not Document.
static java.lang.String getAttribute(org.w3c.dom.Element element, java.lang.String attName)
          Kludge to overcome cases where getAttribute(attName) has been subverted
static java.lang.String getAttributeString(org.w3c.dom.Attr att)
          returns a string representing a single attribute in a start tag, for example foo=bar plugh"; String starts with space and delimiters are QUOT, not APOS
static java.lang.String getAttributeString(org.w3c.dom.Element element)
          returns the attributes in concatenated text form (suitable for insertion to start tags).
static org.w3c.dom.Element getChildElement(org.w3c.dom.Element element, int index)
          get the index'th Element child If none returns null Count ignores non-Element nodes
static java.util.Vector getChildElements(org.w3c.dom.Element element)
          get all Element children If none, returns empty Vector
static java.util.Vector getChildrenWithElementName(org.w3c.dom.Element element, java.lang.String name)
          gets vector of Element children with given element name (ignored if name is null or bad value of sensitivity).
static org.w3c.dom.Element getFirstChildWithElementName(org.w3c.dom.Element element, java.lang.String name)
          gets first child with given element name (ignored if name is null).
static org.w3c.dom.Text getPCDATAChildNode(org.w3c.dom.Element element)
          if this has a single PCDATA child, returns it, else null
static java.lang.String getPCDATAContent(org.w3c.dom.Element element)
          if this has a single PCDATA child, returns its string value, else null
static boolean isEmptyAttribute(java.lang.String attributeValue)
          Gets the emptyAttribute attribute of the PMRDOMUtils class
static boolean isEmptyElement(org.w3c.dom.Element element)
          Gets the emptyElement attribute of the PMRDOMUtils class
static java.lang.String normalizeString(java.lang.String s)
          normalizes all whitespace in a string to single spaces
static java.lang.String output(org.w3c.dom.Document document, java.io.Writer w)
          convenience routine
static java.lang.String output(org.w3c.dom.Node node)
          convenience routine to output a Node as a String
static java.lang.String output(org.w3c.dom.Node node, java.io.Writer w)
          convenience routine
static void outputChildContent(org.w3c.dom.Element element, java.io.Writer w, int type, int level)
          subclassed by specialist classes
static void outputEmptyTag(org.w3c.dom.Element element, java.io.Writer w, int type, int level)
          subclassed by specialist classes
static void outputEndElement(org.w3c.dom.Element element, java.io.Writer w, int level)
          for building the tree If writer is null, noop; else outputs an end tag
static void outputEndTag(org.w3c.dom.Element element, java.io.Writer w, int type, int level)
          subclassed by specialist classes
static java.lang.String outputEventStream(org.w3c.dom.Node node, java.io.Writer w, int type, int level)
          Description of the Method
static java.lang.String outputEventStream(org.w3c.dom.Node node, java.io.Writer w, int type, int level, java.lang.String encoding, boolean xmlDecl, java.lang.String dtd)
          same as above but also outputs XMLDeclaration and DOCTYPE
static void outputStartElement(org.w3c.dom.Element element, java.io.Writer w, org.w3c.dom.Element parentNode, int level)
          for building the tree If writer is null, builds a tree by adding this to parentNode, else outputs a start tag.
static void outputStartTag(org.w3c.dom.Element element, java.io.Writer w, int type, int level)
          subclassed by specialist classes
static void pasteAttributes(org.w3c.dom.Element e, org.w3c.dom.Attr[] attrs)
          adds Attributes to a given Element.
static java.util.List pickElements(org.w3c.dom.Element rootElement, Picker p)
          match decendent elements.
static void removeChildNodes(org.w3c.dom.Element element)
          remove all child nodes
static void removeNode(org.w3c.dom.Node node)
          remove a node from its parent
static void setPCDATAContent(org.w3c.dom.Element element, java.lang.String s)
          Sets a string to be the content of an existing single PCDATANode If there are no children, create a new PCDATANode.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PRETTY

public static final int PRETTY
Description of the Field

See Also:
Constant Field Values
Constructor Detail

PMRDOMUtils

public PMRDOMUtils()
Method Detail

deleteWhitespaceDescendants

public static void deleteWhitespaceDescendants(org.w3c.dom.Element element)
recursively deletes all whitespace descendants

Parameters:
element - Description of the Parameter

removeNode

public static void removeNode(org.w3c.dom.Node node)
remove a node from its parent

Parameters:
node - to be removed

removeChildNodes

public static void removeChildNodes(org.w3c.dom.Element element)
remove all child nodes

Parameters:
element - to remove child nodes from

depthFirstEnumeration

public static java.util.Enumeration depthFirstEnumeration(org.w3c.dom.Element element)
return all descendants in document order

Parameters:
element - Description of the Parameter
Returns:
Description of the Return Value

getChildElements

public static java.util.Vector getChildElements(org.w3c.dom.Element element)
get all Element children If none, returns empty Vector

Parameters:
element - Description of the Parameter
Returns:
The childElements value

getChildElement

public static org.w3c.dom.Element getChildElement(org.w3c.dom.Element element,
                                                  int index)
get the index'th Element child If none returns null Count ignores non-Element nodes

Parameters:
element - Description of the Parameter
index - Description of the Parameter
Returns:
The childElement value

getAncestors

public static java.util.Vector getAncestors(org.w3c.dom.Element element)
gets all ancestors of 'element' including the document Element but not Document. [element is not its own ancestor]

Parameters:
element - Description of the Parameter
Returns:
Element the ancestor (null = none)

getAncestor

public static org.w3c.dom.Element getAncestor(org.w3c.dom.Element element,
                                              java.lang.String tagName)
gets first ancestor of 'element' with tagName; [element is not its own ancestor]

Parameters:
element - Description of the Parameter
tagName - Description of the Parameter
Returns:
Element the ancestor (null = none)

createVector

public static java.util.Vector createVector(org.w3c.dom.NodeList nodes)
converts a NodeList to a vector

the vector (but not its elements) is safely mutable. shallowCopies the items in the node list

Parameters:
nodes - NodeList to copy
Returns:
the equiavlent vector

createVector

public static java.util.Vector createVector(org.w3c.dom.NamedNodeMap nodeMap)
converts a NamedNodeMap to a vector

the vector (but not its elements) is safely mutable. shallowCopies the items in the nodemap

Parameters:
nodeMap - NodeMap to copy
Returns:
the equiavlent vector

getAttribute

public static java.lang.String getAttribute(org.w3c.dom.Element element,
                                            java.lang.String attName)
Kludge to overcome cases where getAttribute(attName) has been subverted

Parameters:
element - Description of the Parameter
attName - Description of the Parameter
Returns:
String the attribute String; null if none

getAttributeString

public static java.lang.String getAttributeString(org.w3c.dom.Element element)
returns the attributes in concatenated text form (suitable for insertion to start tags). If none, returns "". Example:
xml:link="simple" href="http://www.some/where"

Parameters:
element - Description of the Parameter
Returns:
String the attribute String

getAttributeString

public static java.lang.String getAttributeString(org.w3c.dom.Attr att)
returns a string representing a single attribute in a start tag, for example foo=bar plugh"; String starts with space and delimiters are QUOT, not APOS

Parameters:
att - Description of the Parameter
Returns:
The attributeString value

normalizeString

public static java.lang.String normalizeString(java.lang.String s)
normalizes all whitespace in a string to single spaces

Parameters:
s - the string
Returns:
normalized string; null if s is null

getChildrenWithElementName

public static java.util.Vector getChildrenWithElementName(org.w3c.dom.Element element,
                                                          java.lang.String name)
gets vector of Element children with given element name (ignored if name is null or bad value of sensitivity).

Parameters:
element - Description of the Parameter
name - Description of the Parameter
Returns:
Vector the vector of children (empty if none)

getFirstChildWithElementName

public static org.w3c.dom.Element getFirstChildWithElementName(org.w3c.dom.Element element,
                                                               java.lang.String name)
gets first child with given element name (ignored if name is null).

Parameters:
element - Description of the Parameter
name - Description of the Parameter
Returns:
Element the first child or null

getPCDATAContent

public static java.lang.String getPCDATAContent(org.w3c.dom.Element element)
if this has a single PCDATA child, returns its string value, else null

Parameters:
element - Description of the Parameter
Returns:
String String content of single PCDATA child

getPCDATAChildNode

public static org.w3c.dom.Text getPCDATAChildNode(org.w3c.dom.Element element)
if this has a single PCDATA child, returns it, else null

Parameters:
element - Description of the Parameter
Returns:
PCDATANode single PCDATA child

setPCDATAContent

public static void setPCDATAContent(org.w3c.dom.Element element,
                                    java.lang.String s)
Sets a string to be the content of an existing single PCDATANode If there are no children, create a new PCDATANode. If there are non-PCDATA children or more than one PCDATANode child, return without action.

Parameters:
element - The new pCDATAContent value
s - The new pCDATAContent value

outputEventStream

public static java.lang.String outputEventStream(org.w3c.dom.Node node,
                                                 java.io.Writer w,
                                                 int type,
                                                 int level)
                                          throws java.io.IOException
Description of the Method

Parameters:
node - Description of the Parameter
w - Description of the Parameter
type - Description of the Parameter
level - Description of the Parameter
Returns:
Description of the Return Value
Throws:
java.io.IOException - Description of the Exception

debug

public static java.lang.String debug(org.w3c.dom.Node node)
convenience routine to output a Node as a String

Parameters:
node - Description of the Parameter
Returns:
Description of the Return Value

output

public static java.lang.String output(org.w3c.dom.Node node)
convenience routine to output a Node as a String

Parameters:
node - Description of the Parameter
Returns:
Description of the Return Value

output

public static java.lang.String output(org.w3c.dom.Document document,
                                      java.io.Writer w)
                               throws java.io.IOException
convenience routine

Parameters:
document - Description of the Parameter
w - Description of the Parameter
Returns:
Description of the Return Value
Throws:
java.io.IOException - Description of the Exception

output

public static java.lang.String output(org.w3c.dom.Node node,
                                      java.io.Writer w)
                               throws java.io.IOException
convenience routine

Parameters:
node - Description of the Parameter
w - Description of the Parameter
Returns:
Description of the Return Value
Throws:
java.io.IOException - Description of the Exception

outputEventStream

public static java.lang.String outputEventStream(org.w3c.dom.Node node,
                                                 java.io.Writer w,
                                                 int type,
                                                 int level,
                                                 java.lang.String encoding,
                                                 boolean xmlDecl,
                                                 java.lang.String dtd)
                                          throws java.io.IOException
same as above but also outputs XMLDeclaration and DOCTYPE

Parameters:
node - Description of the Parameter
w - Description of the Parameter
type - Description of the Parameter
level - Description of the Parameter
encoding - Description of the Parameter
xmlDecl - Description of the Parameter
dtd - Description of the Parameter
Returns:
Description of the Return Value
Throws:
java.io.IOException - Description of the Exception

isEmptyElement

public static boolean isEmptyElement(org.w3c.dom.Element element)
Gets the emptyElement attribute of the PMRDOMUtils class

Parameters:
element - Description of the Parameter
Returns:
The emptyElement value

outputStartTag

public static void outputStartTag(org.w3c.dom.Element element,
                                  java.io.Writer w,
                                  int type,
                                  int level)
                           throws java.io.IOException
subclassed by specialist classes

Parameters:
element - Description of the Parameter
w - Description of the Parameter
type - Description of the Parameter
level - Description of the Parameter
Throws:
java.io.IOException - Description of the Exception

outputEmptyTag

public static void outputEmptyTag(org.w3c.dom.Element element,
                                  java.io.Writer w,
                                  int type,
                                  int level)
                           throws java.io.IOException
subclassed by specialist classes

Parameters:
element - Description of the Parameter
w - Description of the Parameter
type - Description of the Parameter
level - Description of the Parameter
Throws:
java.io.IOException - Description of the Exception

outputStartElement

public static void outputStartElement(org.w3c.dom.Element element,
                                      java.io.Writer w,
                                      org.w3c.dom.Element parentNode,
                                      int level)
                               throws java.io.IOException
for building the tree If writer is null, builds a tree by adding this to parentNode, else outputs a start tag.

Parameters:
element - Description of the Parameter
w - Description of the Parameter
parentNode - Description of the Parameter
level - Description of the Parameter
Throws:
java.io.IOException - Description of the Exception

outputChildContent

public static void outputChildContent(org.w3c.dom.Element element,
                                      java.io.Writer w,
                                      int type,
                                      int level)
                               throws java.io.IOException
subclassed by specialist classes

Parameters:
element - Description of the Parameter
w - Description of the Parameter
type - Description of the Parameter
level - Description of the Parameter
Throws:
java.io.IOException - Description of the Exception

outputEndTag

public static void outputEndTag(org.w3c.dom.Element element,
                                java.io.Writer w,
                                int type,
                                int level)
                         throws java.io.IOException
subclassed by specialist classes

Parameters:
element - Description of the Parameter
w - Description of the Parameter
type - Description of the Parameter
level - Description of the Parameter
Throws:
java.io.IOException - Description of the Exception

outputEndElement

public static void outputEndElement(org.w3c.dom.Element element,
                                    java.io.Writer w,
                                    int level)
                             throws java.io.IOException
for building the tree If writer is null, noop; else outputs an end tag

Parameters:
element - Description of the Parameter
w - Description of the Parameter
level - Description of the Parameter
Throws:
java.io.IOException - Description of the Exception

deleteWhitespaceDescendants

public static void deleteWhitespaceDescendants(org.w3c.dom.Document document)
recursively removes all whitespace nodes

Parameters:
document - Description of the Parameter

isEmptyAttribute

public static boolean isEmptyAttribute(java.lang.String attributeValue)
Gets the emptyAttribute attribute of the PMRDOMUtils class

Parameters:
attributeValue - Description of the Parameter
Returns:
The emptyAttribute value

cloneNode

public static org.w3c.dom.Node cloneNode(org.w3c.dom.Node node,
                                         org.w3c.dom.Document document)
clones node in context of new document.

provides workaraound for setDocument(newDocument) which is DOM3 uses document.createFoo() does not maintain namespaces only does Element, Text, Comment, ProcessingInstruction at present creates W3C classes, not subclasses cannot clone Document node and returns null cloned node will have no parent

Parameters:
node - to clone
document - context document
Returns:
new node

pickElements

public static java.util.List pickElements(org.w3c.dom.Element rootElement,
                                          Picker p)
match decendent elements.

Finds all decendent elements of rootElement that match the criteria defined by p.

Parameters:
rootElement - the element who along with its children will be processed.
p - the Picker used to match the elements
Returns:
a List of the matching elements, or null if none matched.

copyAttributes

public static org.w3c.dom.Attr[] copyAttributes(org.w3c.dom.Element e,
                                                Picker p)
copies attributes chosen via a Picker callback.

Using the Picker, choose a set of Attributes to be copied from the Element.

Parameters:
e - the element whose Attributes are to be processed
p - the picker that determines whether to choose the attribute
Returns:
an array of the matching Attributes, or null if none matched.

cutAttributes

public static org.w3c.dom.Attr[] cutAttributes(org.w3c.dom.Element e,
                                               Picker p)
                                        throws org.w3c.dom.DOMException
copies and removes attributes chosen via a Picker callback.

Using the Picker, choose a set of Attributes to be removed from the Element. These Attributes are then returned

Parameters:
e - the element whose attributes are to be processed
p - the Picker that determines whether to choose the attribute
Returns:
an array of the matching Attributes, or null if none matched.
Throws:
org.w3c.dom.DOMException - propagated from the call to remove the Attribute node from the Element.

pasteAttributes

public static void pasteAttributes(org.w3c.dom.Element e,
                                   org.w3c.dom.Attr[] attrs)
                            throws org.w3c.dom.DOMException
adds Attributes to a given Element.

Adds a set of Attributes to an Element.

Parameters:
e - the element whose attributes are to be processed
attrs - an array of the Attributes to be added
Throws:
org.w3c.dom.DOMException - propagated from the call to add the Attribute node to the Element.