|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.apache.cocoon.xml.dom.DOMUtil
This class is a utility class for miscellaneous DOM functions, like getting and setting values of nodes.
Constructor Summary | |
DOMUtil()
|
Method Summary | |
static String[] |
buildPathArray(String xpath)
Build the input for the get...FromPath methods. |
static boolean |
compareAttributes(Element first,
Element second)
Compare all attributes of two elements. |
static Document |
createDocument()
Create a new empty DOM document. |
static SourceParameters |
createParameters(Node fragment,
SourceParameters source)
Create a parameter object from xml. |
static String |
createText(DocumentFragment fragment)
Create a string from a DOM document fragment. |
static DocumentFragment |
getDocumentFragment(SAXParser parser,
Reader stream)
Get a document fragment from a Reader .
|
static Node |
getFirstNodeFromPath(Node contextNode,
String[] path,
boolean create)
Use a path to select the first occurence of a node. |
static NodeList |
getNodeListFromPath(Node contextNode,
String[] path)
Use a path to select all occurences of a node. |
static Document |
getOwnerDocument(Node node)
Get the owner of the DOM document belonging to the node. |
static Node |
getSingleNode(Node contextNode,
String str,
XPathProcessor processor)
Use an XPath string to select a single node. |
static boolean |
getValueAsBooleanOf(Node root,
String path,
boolean defaultValue,
XPathProcessor processor)
Get the boolean value of the node specified by the XPath. |
static boolean |
getValueAsBooleanOf(Node root,
String path,
XPathProcessor processor)
Get the boolean value of the node specified by the XPath. |
static String |
getValueOf(Node root,
String path,
String defaultValue,
XPathProcessor processor)
Get the value of the node specified by the XPath. |
static String |
getValueOf(Node root,
String path,
XPathProcessor processor)
Get the value of the node specified by the XPath. |
static String |
getValueOfNode(Node node)
Get the value of the DOM node. |
static String |
getValueOfNode(Node node,
String defaultValue)
Get the value of the node. |
static String |
getValueOfNode(XPathProcessor processor,
Node root,
String path)
Get the value of the node specified by the XPath. |
static String |
getValueOfNode(XPathProcessor processor,
Node root,
String path,
String defaultValue)
Get the value of the node specified by the XPath. |
static boolean |
getValueOfNodeAsBoolean(XPathProcessor processor,
Node root,
String path)
Get the boolean value of the node specified by the XPath. |
static boolean |
getValueOfNodeAsBoolean(XPathProcessor processor,
Node root,
String path,
boolean defaultValue)
Get the boolean value of the node specified by the XPath. |
static NodeList |
selectNodeList(Node contextNode,
String str,
XPathProcessor processor)
Use an XPath string to select a nodelist. |
static Node |
selectSingleNode(Node rootNode,
String path,
XPathProcessor processor)
Return the Node from the DOM Node rootNode
using the XPath expression path .
|
static void |
setValueOfNode(Node node,
String value)
Set the value of the DOM node. |
static void |
valueOf(Node parent,
Collection v)
Implementation for java.util.Collection :
outputs the value by calling valueOf(Node, Object) on each element of the
collection. |
static void |
valueOf(Node parent,
Map v)
Implementation for java.util.Map :
For each entry an element is created with the childs key and value
Outputs the value and the key by calling valueOf(Node, Object)
on each value and key of the Map. |
static void |
valueOf(Node parent,
Node v)
Implementation for org.w3c.dom.Node :
converts the Node to a SAX event stream. |
static void |
valueOf(Node parent,
Object v)
Implementation for Object depending on its class :
if it's an array, call valueOf(Node, Object) on all its elements,
if it's class has a specific valueOf(Node, Object) implementation, use it,
else, output it's string representation. |
static void |
valueOf(Node parent,
String text)
Implementation for String :
outputs characters representing the value. |
static void |
valueOf(Node parent,
XMLizable v)
Implementation for XMLizable :
outputs the value by calling v.toSax(contentHandler) . |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
public DOMUtil()
Method Detail |
public static Document getOwnerDocument(Node node)
node
- The node.
public static String getValueOfNode(XPathProcessor processor, Node root, String path) throws ProcessingException
null
is returned.
root
- The node to start the search.path
- XPath search expression.
null
ProcessingException
public static String getValueOfNode(XPathProcessor processor, Node root, String path, String defaultValue) throws ProcessingException
defaultValue
is returned.
root
- The node to start the search.path
- XPath search expression.defaultValue
- The default value if the node does not exist.
defaultValue
ProcessingException
public static boolean getValueOfNodeAsBoolean(XPathProcessor processor, Node root, String path) throws ProcessingException
root
- The node to start the search.path
- XPath search expression.
ProcessingException
- If the node is not found.public static boolean getValueOfNodeAsBoolean(XPathProcessor processor, Node root, String path, boolean defaultValue) throws ProcessingException
defaultValue
is returned.
root
- The node to start the search.path
- XPath search expression.defaultValue
- Default boolean value.
defaultValue
ProcessingException
public static String getValueOfNode(Node node)
null
is returned.
public static String getValueOfNode(Node node, String defaultValue)
defaultValue
is
returned.
public static void setValueOfNode(Node node, String value)
public static DocumentFragment getDocumentFragment(SAXParser parser, Reader stream) throws ProcessingException
Reader
.
The reader must provide valid XML, but it is allowed that the XML
has more than one root node. This xml is parsed by the
specified parser instance and a DOM DocumentFragment is created.
ProcessingException
public static SourceParameters createParameters(Node fragment, SourceParameters source)
source
is not specified a new paramter object is created
otherwise the parameters are added to source.
public static String createText(DocumentFragment fragment)
public static boolean compareAttributes(Element first, Element second)
public static void valueOf(Node parent, String text) throws ProcessingException
String
:
outputs characters representing the value.
parent
- The node getting the valuetext
- the value
ProcessingException
public static void valueOf(Node parent, XMLizable v) throws ProcessingException
XMLizable
:
outputs the value by calling v.toSax(contentHandler)
.
parent
- The node getting the valuev
- the XML fragment
ProcessingException
public static void valueOf(Node parent, Node v) throws ProcessingException
org.w3c.dom.Node
:
converts the Node to a SAX event stream.
parent
- The node getting the valuev
- the value
ProcessingException
public static void valueOf(Node parent, Collection v) throws ProcessingException
java.util.Collection
:
outputs the value by calling valueOf(Node, Object)
on each element of the
collection.
parent
- The node getting the valuev
- the XML fragment
ProcessingException
public static void valueOf(Node parent, Map v) throws ProcessingException
java.util.Map
:
For each entry an element is created with the childs key and value
Outputs the value and the key by calling valueOf(Node, Object)
on each value and key of the Map.
parent
- The node getting the valuev
- the Map
ProcessingException
public static void valueOf(Node parent, Object v) throws ProcessingException
Object
depending on its class :
valueOf(Node, Object)
on all its elements,valueOf(Node, Object)
implementation, use it,
parent
- The node getting the valuev
- the value
ProcessingException
public static Node getSingleNode(Node contextNode, String str, XPathProcessor processor) throws TransformerException
contextNode
- The node to start searching from.str
- A valid XPath string.processor
- The XPath processor to use
TransformerException
public static Node selectSingleNode(Node rootNode, String path, XPathProcessor processor) throws ProcessingException
Node
from the DOM Node rootNode
using the XPath expression path
.
If the node does not exist, it is created and then returned.
This is a very simple method for creating new nodes. If the
XPath contains selectors ([,,,]) or "*" it is of course not
possible to create the new node. So if you use such XPaths
the node must exist beforehand.
An simple exception is if the expression contains attribute
test to values (e.g. [@id = 'du' and @number = 'you'],
the attributes with the given values are added. The attributes
must be separated with 'and'.
Another problem are namespaces: XPath requires sometimes selectors for
namespaces, e.g. : /*[namespace-uri()="uri" and local-name()="name"]
Creating such a node with a namespace is not possible right now as we use
a very simple XPath parser which is not able to parse all kinds of selectors
correctly.
rootNode
- The node to start the search.path
- XPath expression for searching the node.processor
- The XPath processor to use
ProcessingException
- If no path is specified or the XPath engine fails.public static String getValueOf(Node root, String path, XPathProcessor processor) throws ProcessingException
null
is returned.
root
- The node to start the search.path
- XPath search expression.processor
- The XPath processor to use
null
ProcessingException
public static String getValueOf(Node root, String path, String defaultValue, XPathProcessor processor) throws ProcessingException
defaultValue
is returned.
root
- The node to start the search.path
- XPath search expression.defaultValue
- The default value if the node does not exist.processor
- The XPath Processor
defaultValue
ProcessingException
public static boolean getValueAsBooleanOf(Node root, String path, XPathProcessor processor) throws ProcessingException
root
- The node to start the search.path
- XPath search expression.processor
- The XPath Processor
ProcessingException
- If the node is not found.public static boolean getValueAsBooleanOf(Node root, String path, boolean defaultValue, XPathProcessor processor) throws ProcessingException
defaultValue
is returned.
root
- The node to start the search.path
- XPath search expression.defaultValue
- Default boolean value.processor
- The XPath Processor
defaultValue
ProcessingException
public static Document createDocument() throws ProcessingException
ProcessingException
public static NodeList selectNodeList(Node contextNode, String str, XPathProcessor processor) throws TransformerException
contextNode
- The node to start searching from.str
- A valid XPath string.processor
- The XPath Processor
TransformerException
public static String[] buildPathArray(String xpath)
null
is returned.
public static Node getFirstNodeFromPath(Node contextNode, String[] path, boolean create)
contextNode
- The node starting the search.path
- The path to search the node. The
contextNode is searched for a child named path[0],
this node is searched for a child named path[1]...create
- If a child with the corresponding name is not found
and create is set, this node will be created.public static NodeList getNodeListFromPath(Node contextNode, String[] path)
contextNode
- The node starting the search.path
- The path to search the node. The
contextNode is searched for a child named path[0],
this node is searched for a child named path[1]...
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |