com.logikdev.jsftools
Class StaticXmlTool

java.lang.Object
  extended by com.logikdev.jsftools.StaticXmlTool

public class StaticXmlTool
extends java.lang.Object

Tool for reading/navigating XML files. This uses dom4j under the covers to provide complete XPath support for traversing XML files.

Since:
0.1
Author:
Stéphane Moreau
See Also:
org.apache.velocity.tools.generic.XmlTool

Constructor Summary
StaticXmlTool()
           
 
Method Summary
static java.lang.String attr(org.dom4j.Node node, java.lang.Object o)
          Returns the value of the specified attribute for the specified Node, if that Node is an Element.
static java.util.Map<java.lang.String,java.lang.String> attributes(org.dom4j.Node node)
          Returns a Map of all attributes for the specified Node.
static org.dom4j.Node children(org.dom4j.Node node)
          Returns a new Node instance that wraps all the child Elements of the specified Node.
static org.dom4j.Node find(org.dom4j.Node node, java.lang.Object o)
          Converts the specified object to a String and calls find(org.dom4j.Node, java.lang.String) with that.
static org.dom4j.Node find(org.dom4j.Node node, java.lang.String xpath)
          Performs an XPath selection on the specified Node and returns a new Node instance that wraps those results.
static org.dom4j.Node get(org.dom4j.Node node, java.lang.Number n)
          Returns a Node that wraps the specified Node.
static java.lang.Object get(org.dom4j.Node node, java.lang.Object o)
          This will first attempt to find an attribute with the specified name and return its value.
static org.dom4j.Node getFirst(org.dom4j.Node node)
          Returns a Node that wraps only the first Node from the specified Node.
static org.dom4j.Node getLast(org.dom4j.Node node)
          Returns a Node that wraps only the last Node from the specified Node.
static java.lang.Object getName(org.dom4j.Node node)
          Asks get(org.dom4j.Node, java.lang.Object) for a "name" result.
static java.lang.String getNodeName(org.dom4j.Node node)
          Returns the name of the root node.
static org.dom4j.Node getParent(org.dom4j.Node node)
          Returns a new Node instance that wraps the parent Element of the specified Node.
static java.lang.String getPath(org.dom4j.Node node)
          Returns the XPath that identifies the specified Node.
static java.lang.String getText(org.dom4j.Node node)
          Returns the concatenated text content of the specified Node.
static org.dom4j.Node parents(org.dom4j.Node node)
          Returns a new Node instance that wraps the parent Elements of the specified Node.
static org.dom4j.Node parse(java.lang.Object o)
          This accepts XML in form.
static org.dom4j.Node read(java.lang.Object o)
          If valid, it will return a new Node instance with that document as the root Node.
static java.util.List<org.dom4j.Node> selectNodes(org.dom4j.Node node, java.lang.String xpath)
          Evaluates an XPath expression and returns the result as a List of Node instances or String instances depending on the XPath expression.
static org.dom4j.Node selectSingleNode(org.dom4j.Node node, java.lang.String xpath)
          Evaluates an XPath expression and returns the result as a single Node instance.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StaticXmlTool

public StaticXmlTool()
Method Detail

attr

public static final java.lang.String attr(org.dom4j.Node node,
                                          java.lang.Object o)
Returns the value of the specified attribute for the specified Node, if that Node is an Element. If it is a non-Element node type or there is no value for that attribute in this element, then this will return null.

Parameters:
node - The node
o - The attribute
Returns:
The value of the specified attribute for the specified Node
See Also:
org.apache.velocity.tools.generic.XmlTool#attr(java.lang.Object)

attributes

public static final java.util.Map<java.lang.String,java.lang.String> attributes(org.dom4j.Node node)
Returns a Map of all attributes for the specified Node. If that Node is not an Element, this will return null.

Parameters:
node - The node
Returns:
A Map of all attributes for the specified Node
See Also:
org.apache.velocity.tools.generic.XmlTool#attributes()

children

public static final org.dom4j.Node children(org.dom4j.Node node)
Returns a new Node instance that wraps all the child Elements of the specified Node.

Parameters:
node - The node
Returns:
A new Node instance that wraps all the child Elements of the specified Node
See Also:
org.apache.velocity.tools.generic.XmlTool#children()

find

public static final org.dom4j.Node find(org.dom4j.Node node,
                                        java.lang.Object o)
Converts the specified object to a String and calls find(org.dom4j.Node, java.lang.String) with that.

Parameters:
node - The node
o - The object
Returns:
A new Node instance that wraps those results
See Also:
org.apache.velocity.tools.generic.XmlTool#find(java.lang.Object)

find

public static final org.dom4j.Node find(org.dom4j.Node node,
                                        java.lang.String xpath)
Performs an XPath selection on the specified Node and returns a new Node instance that wraps those results. If the specified values are null, then this will return null. If the specified value, when converted to a string, does not contain a '/' character, then it has "//" prepended to it. This means that a call to find(node, "a") is equivalent to calling find(node, "//a"). The full range of XPath selectors is supported here.

Parameters:
node - The node
xpath - The XPath
Returns:
A new Node instance that wraps those results
See Also:
org.apache.velocity.tools.generic.XmlTool#find(java.lang.String)

get

public static final org.dom4j.Node get(org.dom4j.Node node,
                                       java.lang.Number n)
Returns a Node that wraps the specified Node.

Parameters:
node - The node
n - The number
Returns:
a Node that wraps the specified Node
See Also:
org.apache.velocity.tools.generic.XmlTool#get(java.lang.Number)

get

public static final java.lang.Object get(org.dom4j.Node node,
                                         java.lang.Object o)
This will first attempt to find an attribute with the specified name and return its value. If no such attribute exists or its value is null, this will attempt to convert the given value to a Number and get the result of get(org.dom4j.Node, java.lang.Number). If the number conversion fails, then this will convert the object to a string. If that string does not contain a '/', it appends the result of getPath(org.dom4j.Node) and a '/' to the front of it. Finally, it delegates the string to the find(org.dom4j.Node, java.lang.String) method and returns the result of that.

Parameters:
node - The node
o - The object
Returns:
The value of an attribute found with the specified name
See Also:
org.apache.velocity.tools.generic.XmlTool#get(java.lang.Object)

getFirst

public static final org.dom4j.Node getFirst(org.dom4j.Node node)
Returns a Node that wraps only the first Node from the specified Node.

Parameters:
node - The node
Returns:
A Node that wraps only the first Node from the specified Node
See Also:
org.apache.velocity.tools.generic.XmlTool#getFirst()

getLast

public static final org.dom4j.Node getLast(org.dom4j.Node node)
Returns a Node that wraps only the last Node from the specified Node.

Parameters:
node - The node
Returns:
A Node that wraps only the last Node from the specified Node
See Also:
org.apache.velocity.tools.generic.XmlTool#getLast()

getName

public static final java.lang.Object getName(org.dom4j.Node node)
Asks get(org.dom4j.Node, java.lang.Object) for a "name" result. If none, this will return the result of getNodeName(org.dom4j.Node).

Parameters:
node - The node
Returns:
The return value of get(org.dom4j.Node, java.lang.Object) for a "name" result
See Also:
org.apache.velocity.tools.generic.XmlTool#getName()

getNodeName

public static final java.lang.String getNodeName(org.dom4j.Node node)
Returns the name of the root node.

Parameters:
node - The node
Returns:
The name of the root node
See Also:
org.apache.velocity.tools.generic.XmlTool#getNodeName()

getParent

public static final org.dom4j.Node getParent(org.dom4j.Node node)
Returns a new Node instance that wraps the parent Element of the specified Node.

Parameters:
node - The node
Returns:
A new Node instance that wraps the parent Element of the specified Node
See Also:
org.apache.velocity.tools.generic.XmlTool#getParent()

getPath

public static final java.lang.String getPath(org.dom4j.Node node)
Returns the XPath that identifies the specified Node.

Parameters:
node - The node
Returns:
The XPath that identifies the specified Node
See Also:
org.apache.velocity.tools.generic.XmlTool#getPath()

getText

public static final java.lang.String getText(org.dom4j.Node node)
Returns the concatenated text content of the specified Node.

Parameters:
node - The node
Returns:
The concatenated text content of the specified Node
See Also:
org.apache.velocity.tools.generic.XmlTool#getText()

parents

public static final org.dom4j.Node parents(org.dom4j.Node node)
Returns a new Node instance that wraps the parent Elements of the specified Node. This does not return all ancestors, just the immediate parents.

Parameters:
node - The node
Returns:
A new Node instance that wraps the parent Elements of the specified Node
See Also:
org.apache.velocity.tools.generic.XmlTool#parents()

parse

public static final org.dom4j.Node parse(java.lang.Object o)
This accepts XML in form. If the XML is valid, it will return a new Node instance with the resulting XML document as the root Node. If parsing the content fails, this will return null.

Parameters:
o - The object
Returns:
A new Node instance with the resulting XML document as the root Node
See Also:
org.apache.velocity.tools.generic.XmlTool#parse(java.lang.Object)

read

public static final org.dom4j.Node read(java.lang.Object o)
If valid, it will return a new Node instance with that document as the root Node. If reading the URL or parsing its content fails, this will return null.

Parameters:
o - The object
Returns:
A new Node instance with that document as the root Node
See Also:
org.apache.velocity.tools.generic.XmlTool#read(java.lang.Object)

selectNodes

public static final java.util.List<org.dom4j.Node> selectNodes(org.dom4j.Node node,
                                                               java.lang.String xpath)
Evaluates an XPath expression and returns the result as a List of Node instances or String instances depending on the XPath expression.

Parameters:
node - The node
xpath - The XPath expression
Returns:
A List of Node instances or String instances depending on the XPath expression
See Also:
org.dom4j.Node#selectNodes(java.lang.String)

selectSingleNode

public static final org.dom4j.Node selectSingleNode(org.dom4j.Node node,
                                                    java.lang.String xpath)
Evaluates an XPath expression and returns the result as a single Node instance.

Parameters:
node - The node
xpath - The XPath expression
Returns:
The result as a single Node instance
See Also:
org.dom4j.Node#selectSingleNode(java.lang.String)


Copyright © 2009-2010 Logik Development. All Rights Reserved.