Class XmlTool
- java.lang.Object
-
- org.apache.velocity.tools.generic.SafeConfig
-
- org.apache.velocity.tools.generic.XmlTool
-
@DefaultKey("xml") public class XmlTool extends SafeConfig
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.- Since:
- VelocityTools 2.0
- Version:
- $Revision$ $Date: 2006-11-27 10:49:37 -0800 (Mon, 27 Nov 2006) $
- Author:
- Nathan Bubna
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classXmlTool.NodeIteratorIterator implementation that wraps a Node list iterator to return new XmlTool instances for each item in the wrapped iterator.s
-
Field Summary
Fields Modifier and Type Field Description static StringFILE_KEYprotected org.apache.velocity.runtime.log.LogLOG-
Fields inherited from class org.apache.velocity.tools.generic.SafeConfig
LOCK_CONFIG_KEY, OLD_LOCK_CONFIG_KEY, SAFE_MODE_KEY
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Stringattr(Object o)Returns the value of the specified attribute for the first/soleNodein the internal Node list for this instance, if that Node is anElement.Map<String,String>attributes()Returns aMapof all attributes for the first/soleNodeheld internally by this instance.XmlToolchildren()Returns a newXmlToolinstance that wraps all the childElements of all the current internally held nodes that areElements themselves.protected voidconfigure(ValueParser parser)Looks for the "file" parameter and automatically usesread(String)to parse the file and set the resultingDocumentas the root node for this instance.XmlToolfind(Object o)Converts the specified object to a String and callsfind(String)with that.XmlToolfind(String xpath)Performs an XPath selection on the current set ofNodes held by this instance and returns a newXmlToolinstance that wraps those results.XmlToolget(Number n)Returns anXmlToolthat wraps the specifiedNodefrom this instance's internal Node list.Objectget(Object o)This will first attempt to find an attribute with the specified name and return its value.XmlToolgetFirst()Returns anXmlToolthat wraps only the firstNodefrom this instance's internal Node list.XmlToolgetLast()Returns anXmlToolthat wraps only the lastNodefrom this instance's internal Node list.ObjectgetName()Asksget(Object)for a "name" result.StringgetNodeName()Returns the name of the root node.XmlToolgetParent()Returns a newXmlToolinstance that wraps the parentElementof the first/soleNodebeing wrapped by this instance.StringgetPath()Returns the XPath that identifies the first/soleNoderepresented by this instance.StringgetText()Returns the concatenated text content of all the internally held nodes.booleanisEmpty()Returnstrueif there are noNodes internally held by this instance.Iterator<XmlTool>iterator()org.dom4j.Nodenode()Returns the first/soleNodefrom this instance's internal Node list, if any.XmlToolparents()Returns a newXmlToolinstance that wraps the parentElements of each of theNodes being wrapped by this instance.XmlToolparse(Object o)This accepts XML in form.protected voidparse(String xml)Parses the given XML string and uses the resultingDocumentas the rootNode.XmlToolread(Object o)If safe mode is explicitly turned off for this tool, then this will accept either aURLor the string representation thereof.protected voidread(String file)protected voidread(URL url)Reads, parses and creates aDocumentfrom the givenURLand uses it as the rootNodefor this instance.protected voidsetRoot(org.dom4j.Node node)Sets a singular rootNodefor this instance.intsize()Returns the number ofNodes internally held by this instance.StringtoString()If this instance has no XMLNodes, then this returns the result ofsuper.toString().-
Methods inherited from class org.apache.velocity.tools.generic.SafeConfig
configure, isConfigLocked, isSafeMode, setLockConfig, setSafeMode
-
-
-
-
Field Detail
-
FILE_KEY
public static final String FILE_KEY
- See Also:
- Constant Field Values
-
LOG
protected org.apache.velocity.runtime.log.Log LOG
-
-
Constructor Detail
-
XmlTool
public XmlTool()
-
XmlTool
public XmlTool(org.dom4j.Node node)
-
XmlTool
public XmlTool(List<org.dom4j.Node> nodes)
-
-
Method Detail
-
configure
protected void configure(ValueParser parser)
Looks for the "file" parameter and automatically usesread(String)to parse the file and set the resultingDocumentas the root node for this instance.- Overrides:
configurein classSafeConfig
-
setRoot
protected void setRoot(org.dom4j.Node node)
Sets a singular rootNodefor this instance.
-
read
protected void read(URL url) throws Exception
Reads, parses and creates aDocumentfrom the givenURLand uses it as the rootNodefor this instance.- Throws:
Exception
-
parse
protected void parse(String xml) throws Exception
Parses the given XML string and uses the resultingDocumentas the rootNode.- Throws:
Exception
-
read
public XmlTool read(Object o)
If safe mode is explicitly turned off for this tool, then this will accept either aURLor the string representation thereof. If valid, it will return a newXmlToolinstance with that document as the rootNode. If reading the URL or parsing its content fails or if safe mode is on (the default), this will returnnull.
-
parse
public XmlTool parse(Object o)
This accepts XML in form. If the XML is valid, it will return a newXmlToolinstance with the resulting XML document as the rootNode. If parsing the content fails, this will returnnull.
-
get
public Object get(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 isnull, this will attempt to convert the given value to aNumberand get the result ofget(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 ofgetPath()and a '/' to the front of it. Finally, it delegates the string to thefind(String)method and returns the result of that.
-
getName
public Object getName()
Asksget(Object)for a "name" result. If none, this will return the result ofgetNodeName().
-
getNodeName
public String getNodeName()
Returns the name of the root node. If the internalNodelist has more than oneNode, it will only return the name of the first node in the list.
-
getPath
public String getPath()
Returns the XPath that identifies the first/soleNoderepresented by this instance.
-
attr
public String attr(Object o)
Returns the value of the specified attribute for the first/soleNodein the internal Node list for this instance, if that Node is anElement. If it is a non-Element node type or there is no value for that attribute in this element, then this will returnnull.
-
attributes
public Map<String,String> attributes()
Returns aMapof all attributes for the first/soleNodeheld internally by this instance. If that Node is not anElement, this will return null.
-
isEmpty
public boolean isEmpty()
Returnstrueif there are noNodes internally held by this instance.
-
size
public int size()
Returns the number ofNodes internally held by this instance.
-
getFirst
public XmlTool getFirst()
Returns anXmlToolthat wraps only the firstNodefrom this instance's internal Node list.
-
getLast
public XmlTool getLast()
Returns anXmlToolthat wraps only the lastNodefrom this instance's internal Node list.
-
get
public XmlTool get(Number n)
Returns anXmlToolthat wraps the specifiedNodefrom this instance's internal Node list.
-
node
public org.dom4j.Node node()
Returns the first/soleNodefrom this instance's internal Node list, if any.
-
find
public XmlTool find(Object o)
Converts the specified object to a String and callsfind(String)with that.
-
find
public XmlTool find(String xpath)
Performs an XPath selection on the current set ofNodes held by this instance and returns a newXmlToolinstance that wraps those results. If the specified value is null or this instance does not currently hold any nodes, then this will returnnull. 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.
-
getParent
public XmlTool getParent()
Returns a newXmlToolinstance that wraps the parentElementof the first/soleNodebeing wrapped by this instance.
-
parents
public XmlTool parents()
Returns a newXmlToolinstance that wraps the parentElements of each of theNodes being wrapped by this instance. This does not return all ancestors, just the immediate parents.
-
children
public XmlTool children()
Returns a newXmlToolinstance that wraps all the childElements of all the current internally held nodes that areElements themselves.
-
getText
public String getText()
Returns the concatenated text content of all the internally held nodes. Obviously, this is most useful when only one node is held.
-
-