|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.apache.velocity.tools.generic.SafeConfig org.apache.velocity.tools.generic.XmlTool
@DefaultKey(value="xml") public class XmlTool
Tool for reading/navigating XML files. This uses dom4j under the covers to provide complete XPath support for traversing XML files.
Here's a short example:
XML file: <foo><bar>woogie</bar><a name="test"/></foo> Template: $foo.bar.text $foo.find('a') $foo.a.name Output: woogie <a name="test"/> test Configuration: <tools> <toolbox scope="application"> <tool class="org.apache.velocity.tools.generic.XmlTool" key="foo" file="doc.xml"/> </toolbox> </tools>
Note that this tool is included in the default GenericTools configuration
under the key "xml", but unless you set safeMode="false" for it, you will
only be able to parse XML strings. Safe mode is on by default and blocks
access to the read(Object)
method.
Nested Class Summary | |
---|---|
static class |
XmlTool.NodeIterator
Iterator implementation that wraps a Node list iterator to return new XmlTool instances for each item in the wrapped iterator.s |
Field Summary | |
---|---|
static java.lang.String |
FILE_KEY
|
protected org.apache.velocity.runtime.log.Log |
LOG
|
private java.util.List<org.dom4j.Node> |
nodes
|
Fields inherited from class org.apache.velocity.tools.generic.SafeConfig |
---|
LOCK_CONFIG_KEY, OLD_LOCK_CONFIG_KEY, SAFE_MODE_KEY |
Constructor Summary | |
---|---|
XmlTool()
|
|
XmlTool(java.util.List<org.dom4j.Node> nodes)
|
|
XmlTool(org.dom4j.Node node)
|
Method Summary | |
---|---|
java.lang.String |
attr(java.lang.Object o)
Returns the value of the specified attribute for the first/sole Node in the internal Node list for this instance, if that
Node is an Element . |
java.util.Map<java.lang.String,java.lang.String> |
attributes()
Returns a Map of all attributes for the first/sole
Node held internally by this instance. |
XmlTool |
children()
Returns a new XmlTool instance that wraps all the
child Element s of all the current internally held nodes
that are Element s themselves. |
protected void |
configure(ValueParser parser)
Looks for the "file" parameter and automatically uses read(String) to parse the file and set the
resulting Document as the root node for this
instance. |
XmlTool |
find(java.lang.Object o)
Converts the specified object to a String and calls find(String) with that. |
XmlTool |
find(java.lang.String xpath)
Performs an XPath selection on the current set of Node s held by this instance and returns a new
XmlTool instance that wraps those results. |
XmlTool |
get(java.lang.Number n)
Returns an XmlTool that wraps the specified
Node from this instance's internal Node list. |
java.lang.Object |
get(java.lang.Object o)
This will first attempt to find an attribute with the specified name and return its value. |
XmlTool |
getFirst()
Returns an XmlTool that wraps only the
first Node from this instance's internal Node list. |
XmlTool |
getLast()
Returns an XmlTool that wraps only the
last Node from this instance's internal Node list. |
java.lang.Object |
getName()
Asks get(Object) for a "name" result. |
java.lang.String |
getNodeName()
Returns the name of the root node. |
XmlTool |
getParent()
Returns a new XmlTool instance that wraps
the parent Element of the first/sole Node
being wrapped by this instance. |
java.lang.String |
getPath()
Returns the XPath that identifies the first/sole Node
represented by this instance. |
java.lang.String |
getText()
Returns the concatenated text content of all the internally held nodes. |
boolean |
isEmpty()
Returns true if there are no Node s internally held
by this instance. |
java.util.Iterator<XmlTool> |
iterator()
Returns an XmlTool.NodeIterator that returns new XmlTool
instances for each Node held internally by this instance. |
private void |
log(java.lang.Object o,
java.lang.Throwable t)
|
org.dom4j.Node |
node()
Returns the first/sole Node from this
instance's internal Node list, if any. |
XmlTool |
parents()
Returns a new XmlTool instance that wraps
the parent Element s of each of the Node s
being wrapped by this instance. |
XmlTool |
parse(java.lang.Object o)
This accepts XML in form. |
protected void |
parse(java.lang.String xml)
Parses the given XML string and uses the resulting Document
as the root Node . |
XmlTool |
read(java.lang.Object o)
If safe mode is explicitly turned off for this tool, then this will accept either a URL or the string representation
thereof. |
protected void |
read(java.lang.String file)
Creates a URL from the string and passes it to read(URL) . |
protected void |
read(java.net.URL url)
Reads, parses and creates a Document from the
given URL and uses it as the root Node for this instance. |
protected void |
setRoot(org.dom4j.Node node)
Sets a singular root Node for this instance. |
int |
size()
Returns the number of Node s internally held by this instance. |
java.lang.String |
toString()
If this instance has no XML Node s, then this
returns the result of super.toString() . |
Methods inherited from class org.apache.velocity.tools.generic.SafeConfig |
---|
configure, isConfigLocked, isSafeMode, setLockConfig, setSafeMode |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
public static final java.lang.String FILE_KEY
protected org.apache.velocity.runtime.log.Log LOG
private java.util.List<org.dom4j.Node> nodes
Constructor Detail |
---|
public XmlTool()
public XmlTool(org.dom4j.Node node)
public XmlTool(java.util.List<org.dom4j.Node> nodes)
Method Detail |
---|
protected void configure(ValueParser parser)
read(String)
to parse the file and set the
resulting Document
as the root node for this
instance.
configure
in class SafeConfig
protected void setRoot(org.dom4j.Node node)
Node
for this instance.
private void log(java.lang.Object o, java.lang.Throwable t)
protected void read(java.lang.String file) throws java.lang.Exception
URL
from the string and passes it to read(URL)
.
java.lang.Exception
protected void read(java.net.URL url) throws java.lang.Exception
Document
from the
given URL
and uses it as the root Node
for this instance.
java.lang.Exception
protected void parse(java.lang.String xml) throws java.lang.Exception
Document
as the root Node
.
java.lang.Exception
public XmlTool read(java.lang.Object o)
URL
or the string representation
thereof. If valid, it will return a new XmlTool
instance
with that document as the root Node
. If reading the URL
or parsing its content fails or if safe mode is on (the default),
this will return null
.
public XmlTool parse(java.lang.Object o)
XmlTool
instance with the resulting XML document
as the root Node
. If parsing the content fails,
this will return null
.
public java.lang.Object get(java.lang.Object o)
null
, this will attempt to convert
the given value to a Number
and get the result of
get(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()
and a '/' to the front of it. Finally, it delegates the string to the
find(String)
method and returns the result of that.
public java.lang.Object getName()
get(Object)
for a "name" result.
If none, this will return the result of getNodeName()
.
public java.lang.String getNodeName()
Node
list has more than one Node
, it will only return the name
of the first node in the list.
public java.lang.String getPath()
Node
represented by this instance.
public java.lang.String attr(java.lang.Object o)
Node
in the internal Node list for this instance, 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
.
public java.util.Map<java.lang.String,java.lang.String> attributes()
Map
of all attributes for the first/sole
Node
held internally by this instance. If that Node is
not an Element
, this will return null.
public boolean isEmpty()
true
if there are no Node
s internally held
by this instance.
public int size()
Node
s internally held by this instance.
public java.util.Iterator<XmlTool> iterator()
XmlTool.NodeIterator
that returns new XmlTool
instances for each Node
held internally by this instance.
public XmlTool getFirst()
XmlTool
that wraps only the
first Node
from this instance's internal Node list.
public XmlTool getLast()
XmlTool
that wraps only the
last Node
from this instance's internal Node list.
public XmlTool get(java.lang.Number n)
XmlTool
that wraps the specified
Node
from this instance's internal Node list.
public org.dom4j.Node node()
Node
from this
instance's internal Node list, if any.
public XmlTool find(java.lang.Object o)
find(String)
with that.
public XmlTool find(java.lang.String xpath)
Node
s held by this instance and returns a new
XmlTool
instance that wraps those results.
If the specified value is null or this instance does
not currently hold any nodes, 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
$xml.find("a")
is equivalent to calling
$xml.find("//a")
. The full range of XPath
selectors is supported here.
public XmlTool getParent()
XmlTool
instance that wraps
the parent Element
of the first/sole Node
being wrapped by this instance.
public XmlTool parents()
XmlTool
instance that wraps
the parent Element
s of each of the Node
s
being wrapped by this instance. This does not return
all ancestors, just the immediate parents.
public XmlTool children()
XmlTool
instance that wraps all the
child Element
s of all the current internally held nodes
that are Element
s themselves.
public java.lang.String getText()
public java.lang.String toString()
Node
s, then this
returns the result of super.toString()
. Otherwise, it
returns the XML (as a string) of all the internally held nodes
that are not Attribute
s. For attributes, only the value
is used.
toString
in class java.lang.Object
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |