fr.tireme.shared.xml.dom
Class DOMUtils

java.lang.Object
  extended by fr.tireme.shared.xml.dom.DOMUtils

public class DOMUtils
extends java.lang.Object

Set of utilities enabling to work better with DOM trees.

Author:
pierre

Method Summary
 java.lang.String attrToString(org.w3c.dom.Element element)
          attrToString : serialize element attributes
 org.w3c.dom.Element elemFromStr(java.lang.String xmlStr)
          Description of the Method
 org.w3c.dom.Element elemFromStr(java.lang.String xmlStr, boolean validate)
           
 org.w3c.dom.Node elemFromStr(java.lang.String xmlStr, org.w3c.dom.Document doc)
          Description of the Method
 java.lang.String getAttrValue(org.w3c.dom.Node node, java.lang.String value)
          Gets the attr attribute of the DOMUtils class
 org.w3c.dom.Element getChildByName(org.w3c.dom.Element element, java.lang.String name)
          getChildByName
 java.lang.String getChildContent(org.w3c.dom.Element element, java.lang.String name)
          getChildContent
 org.w3c.dom.Element getFirstAncestorElement(org.w3c.dom.Element element, java.lang.String elemName)
          getParentElement
 org.w3c.dom.Element getFirstAncestorElement(org.w3c.dom.Element element, java.lang.String elemName, java.lang.String attName)
          getParentElement having a specific node
 org.w3c.dom.Element getFirstElemChild(org.w3c.dom.Element element)
           
static DOMUtils getInstance()
           
 org.w3c.dom.Element getNextSiblingElement(org.w3c.dom.Element element)
           
 org.w3c.dom.Element getParentElement(org.w3c.dom.Element element)
          getParentElement
 org.w3c.dom.Element getSiblingByAttr(org.w3c.dom.Element element, java.lang.String name, java.lang.String value)
          getSiblingByAttr
 boolean hasAttr(org.w3c.dom.Node node, java.lang.String value)
          Test if a node has a specific attribute
 org.w3c.dom.Node importNode(org.w3c.dom.Document doc, org.w3c.dom.Element element)
          Description of the Method
 org.w3c.dom.Node importNode(org.w3c.dom.Document doc, org.w3c.dom.Node child, boolean doappend)
           
 org.w3c.dom.Node importNode(org.w3c.dom.Node parent, org.w3c.dom.Node child)
          Description of the Method
 org.w3c.dom.Node importNode(org.w3c.dom.Node parent, org.w3c.dom.Node child, boolean doappend)
          Description of the Method
 void importNodeInPlace(org.w3c.dom.Node parent, org.w3c.dom.Node child, boolean doappend, org.w3c.dom.Node nodeToReplace)
          Node remplacement using an imported node from an other Document
 org.w3c.dom.Document loadXML(java.lang.String source, boolean validate)
          Routine using DOM3ErrorHandler LS (Load and Save)
 java.lang.String normalize(java.lang.String s)
          Normalizes and prints the given string.
 java.lang.String recContent(org.w3c.dom.Node node)
           Get recursively all the text content of a DOM node tree Improvements needed : - take in account other things than soles PCDATA and elements such as CDATA sections and marked sections.
 void removeChildren(org.w3c.dom.Element element)
          removeChildren
 void replaceNode(org.w3c.dom.Node oldNode, org.w3c.dom.Node newNode)
          Node remplacement in the same Document
 java.lang.String serialize(org.w3c.dom.Node n)
          Serialize a dom document within a string
 java.lang.String serialize(org.w3c.dom.Node n, boolean wantXMLDeclaration)
          Serialize a dom document within a string enabling to say if an xml declaration is needed
 void serialize(org.w3c.dom.Node doc, java.io.File outFile)
          DOM node serialization using a JAVAX dummy transformer
 void serialize(org.w3c.dom.Node doc, java.lang.String outFile)
          DOM node serialization using a JAVAX dummy transformer
 java.lang.String serialize(org.w3c.dom.Node n, java.lang.String doctypePublic, java.lang.String doctypeSystem)
          Description of the Method
 java.lang.String serialize(org.w3c.dom.Node n, java.lang.String serializationCoding, java.lang.String doctypePublic, java.lang.String doctypeSystem)
          Description of the Method
 void serialize(org.w3c.dom.Node n, java.lang.String serializationCoding, java.lang.String doctypePublic, java.lang.String doctypeSystem, java.io.File outputFile, boolean wantXMLDeclaration)
          Serialize to a file
 org.w3c.dom.Attr[] sortAttributes(org.w3c.dom.NamedNodeMap attrs)
          Returns a sorted list of attributes.
 java.lang.String toString(org.w3c.dom.Element element)
          dump a element
 java.lang.String toString(org.w3c.dom.Node node)
          dump a node
 void validateXML(java.lang.String source)
          Validate as DOM document from file system This vlidation is using JavaX interface (full compliant Xalan) In error case, error is thrown using exceptions otherwise, nothing is returned, the file is valid.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getInstance

public static DOMUtils getInstance()
Returns:
The unique instance of this class.

attrToString

public java.lang.String attrToString(org.w3c.dom.Element element)
attrToString : serialize element attributes

Parameters:
element - dom element object
Returns:
string for attributes

elemFromStr

public org.w3c.dom.Element elemFromStr(java.lang.String xmlStr)
                                throws XmlException
Description of the Method

Parameters:
xmlStr - Description of the Parameter
Returns:
Description of the Return Value
Throws:
XmlException

elemFromStr

public org.w3c.dom.Element elemFromStr(java.lang.String xmlStr,
                                       boolean validate)
                                throws XmlException
Throws:
XmlException

elemFromStr

public org.w3c.dom.Node elemFromStr(java.lang.String xmlStr,
                                    org.w3c.dom.Document doc)
                             throws XmlException
Description of the Method

Parameters:
xmlStr - Description of the Parameter
doc - Description of the Parameter
Returns:
Description of the Return Value
Throws:
XmlException

getChildByName

public org.w3c.dom.Element getChildByName(org.w3c.dom.Element element,
                                          java.lang.String name)
getChildByName

Parameters:
element - dom element
name - name of a researched child element
Returns:
a DOM Element or null

getChildContent

public java.lang.String getChildContent(org.w3c.dom.Element element,
                                        java.lang.String name)
                                 throws StandardException
getChildContent

Parameters:
element - dom element
name - name of a researched child element
Returns:
child content
Throws:
StandardException

getFirstAncestorElement

public org.w3c.dom.Element getFirstAncestorElement(org.w3c.dom.Element element,
                                                   java.lang.String elemName)
getParentElement

Parameters:
element - dom element
elemName - String
Returns:
parent element or null

getFirstAncestorElement

public org.w3c.dom.Element getFirstAncestorElement(org.w3c.dom.Element element,
                                                   java.lang.String elemName,
                                                   java.lang.String attName)
getParentElement having a specific node

Parameters:
element - dom element
elemName - String
attName - String
Returns:
parent element or null

getFirstElemChild

public org.w3c.dom.Element getFirstElemChild(org.w3c.dom.Element element)
Parameters:
element - Description of the Parameter
Returns:
The firstElemChild value

getNextSiblingElement

public org.w3c.dom.Element getNextSiblingElement(org.w3c.dom.Element element)
Parameters:
element - Description of the Parameter
Returns:
The nextSiblingElement value

getParentElement

public org.w3c.dom.Element getParentElement(org.w3c.dom.Element element)
getParentElement

Parameters:
element - dom element
Returns:
parent element or null

getSiblingByAttr

public org.w3c.dom.Element getSiblingByAttr(org.w3c.dom.Element element,
                                            java.lang.String name,
                                            java.lang.String value)
getSiblingByAttr

Parameters:
element - dom element
name - name of attribute
value - value of attribute
Returns:
a DOM Element or null

importNode

public org.w3c.dom.Node importNode(org.w3c.dom.Document doc,
                                   org.w3c.dom.Element element)
                            throws XmlException,
                                   StandardException
Description of the Method

Parameters:
doc - Description of the Parameter
element - Description of the Parameter
Returns:
Description of the Return Value
Throws:
XmlException
StandardException

normalize

public java.lang.String normalize(java.lang.String s)
Normalizes and prints the given string.

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

recContent

public java.lang.String recContent(org.w3c.dom.Node node)
                            throws StandardException

Get recursively all the text content of a DOM node tree

Parameters:
node - The node to be explored
Returns:
A text string containing all characters
Throws:
StandardException

removeChildren

public void removeChildren(org.w3c.dom.Element element)
removeChildren

Parameters:
element - dom element

serialize

public java.lang.String serialize(org.w3c.dom.Node n)
                           throws StandardException
Serialize a dom document within a string

Parameters:
n - node
Returns:
serialized string with an xml declaration in front
Throws:
StandardException

serialize

public java.lang.String serialize(org.w3c.dom.Node n,
                                  boolean wantXMLDeclaration)
                           throws StandardException
Serialize a dom document within a string enabling to say if an xml declaration is needed

Parameters:
n - node
wantXMLDeclaration - boolean
Returns:
serialized string
Throws:
StandardException

serialize

public void serialize(org.w3c.dom.Node doc,
                      java.io.File outFile)
               throws StandardException
DOM node serialization using a JAVAX dummy transformer

Parameters:
doc - document to serialize
outFile - output valid filename
Throws:
StandardException

serialize

public void serialize(org.w3c.dom.Node doc,
                      java.lang.String outFile)
               throws StandardException
DOM node serialization using a JAVAX dummy transformer

Parameters:
doc - document to serialize
outFile - output valid filename
Throws:
StandardException

serialize

public java.lang.String serialize(org.w3c.dom.Node n,
                                  java.lang.String doctypePublic,
                                  java.lang.String doctypeSystem)
                           throws StandardException
Description of the Method

Parameters:
n - node
doctypePublic - Description of Parameter
doctypeSystem - Description of Parameter
Returns:
String
Throws:
StandardException

serialize

public java.lang.String serialize(org.w3c.dom.Node n,
                                  java.lang.String serializationCoding,
                                  java.lang.String doctypePublic,
                                  java.lang.String doctypeSystem)
                           throws StandardException
Description of the Method

Parameters:
n - node
serializationCoding - Description of Parameter
doctypePublic - Description of Parameter
doctypeSystem - Description of Parameter
Returns:
serialized string
Throws:
StandardException

serialize

public void serialize(org.w3c.dom.Node n,
                      java.lang.String serializationCoding,
                      java.lang.String doctypePublic,
                      java.lang.String doctypeSystem,
                      java.io.File outputFile,
                      boolean wantXMLDeclaration)
               throws StandardException
Serialize to a file

Parameters:
n - Node
serializationCoding - String
doctypePublic - String
doctypeSystem - String
outputFile - File
wantXMLDeclaration - boolean
Throws:
StandardException

sortAttributes

public org.w3c.dom.Attr[] sortAttributes(org.w3c.dom.NamedNodeMap attrs)
Returns a sorted list of attributes.

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

toString

public java.lang.String toString(org.w3c.dom.Element element)
                          throws StandardException
dump a element

Parameters:
element - dom element object
Returns:
string for attributes
Throws:
StandardException

toString

public java.lang.String toString(org.w3c.dom.Node node)
                          throws StandardException
dump a node

Parameters:
node - dom node object
Returns:
string for attributes
Throws:
StandardException

validateXML

public void validateXML(java.lang.String source)
                 throws java.io.IOException,
                        org.xml.sax.SAXException,
                        java.lang.Exception
Validate as DOM document from file system

This vlidation is using JavaX interface (full compliant Xalan)

In error case, error is thrown using exceptions otherwise, nothing is returned, the file is valid.

Parameters:
source - XML file Path
Throws:
java.io.IOException
org.xml.sax.SAXException
java.lang.Exception

loadXML

public org.w3c.dom.Document loadXML(java.lang.String source,
                                    boolean validate)
                             throws StandardException
Routine using DOM3ErrorHandler LS (Load and Save)

Parameters:
source - source template file
validate - boolean
Returns:
Document
Throws:
StandardException

getAttrValue

public java.lang.String getAttrValue(org.w3c.dom.Node node,
                                     java.lang.String value)
Gets the attr attribute of the DOMUtils class

Parameters:
node - Node
value - attribute name where to get value
Returns:
The attr value or null

hasAttr

public boolean hasAttr(org.w3c.dom.Node node,
                       java.lang.String value)
Test if a node has a specific attribute

Parameters:
node - Node
value - The attribute to check
Returns:
yes or no

importNode

public org.w3c.dom.Node importNode(org.w3c.dom.Document doc,
                                   org.w3c.dom.Node child,
                                   boolean doappend)

importNode

public org.w3c.dom.Node importNode(org.w3c.dom.Node parent,
                                   org.w3c.dom.Node child)
Description of the Method

Parameters:
parent - Description of Parameter
child - Description of Parameter
Returns:
Description of the Returned Value

importNode

public org.w3c.dom.Node importNode(org.w3c.dom.Node parent,
                                   org.w3c.dom.Node child,
                                   boolean doappend)
Description of the Method

Parameters:
parent - Description of Parameter
child - Description of Parameter
doappend - Description of Parameter
Returns:
Description of the Returned Value

importNodeInPlace

public void importNodeInPlace(org.w3c.dom.Node parent,
                              org.w3c.dom.Node child,
                              boolean doappend,
                              org.w3c.dom.Node nodeToReplace)
Node remplacement using an imported node from an other Document

Parameters:
parent - Node
child - Node
doappend - boolean
nodeToReplace - Node

replaceNode

public void replaceNode(org.w3c.dom.Node oldNode,
                        org.w3c.dom.Node newNode)
Node remplacement in the same Document

Parameters:
oldNode - Node
newNode - Node