freemarker.ext.jdom
Class NodeListModel

java.lang.Object
  extended by freemarker.ext.jdom.NodeListModel
All Implemented Interfaces:
TemplateHashModel, TemplateIndexedModel, TemplateListModel2, TemplateMethodModel, TemplateModel, TemplateObjectModel, TemplateScalarModel, java.io.Serializable

public class NodeListModel
extends java.lang.Object
implements TemplateHashModel, TemplateMethodModel, TemplateIndexedModel, TemplateListModel2, TemplateScalarModel, TemplateObjectModel, java.io.Serializable

Provides a template for wrapping JDOM objects. It is capable of storing not only a single JDOM node, but a list of JDOM nodes at once (hence the name). Each node is an instance of any of the core JDOM node classes (except namespaces, which are not supported at the moment), or String for representing text.

See individual method documentation for exact details on how the class works. In short:

Finally, TemplateObjectModel is implemented so that operations on the node list can be performed directly.

Version:
$Id: NodeListModel.java 1159 2013-04-20 05:53:37Z run2000 $
Author:
Attila Szegedi, szegedia@users.sourceforge.net
See Also:
Serialized Form

Field Summary
static TemplateScalarModel EMPTY_SCALAR
          A singleton empty scalar.
 
Constructor Summary
NodeListModel(org.jdom.Document document)
          Creates a node set template that holds a single Document node.
NodeListModel(org.jdom.Element element)
          Creates a node set template that holds a single Element node.
NodeListModel(java.util.List<?> nodes)
          Creates a node set template that holds a list of nodes.
NodeListModel(java.util.List<?> nodes, boolean copy)
          Creates a node set template that holds a list of nodes.
 
Method Summary
static NodeListModel createNodeListModel(java.util.List<?> list, boolean copy)
          Factory method for creating a NodeListModel that best suits the passed list.
 TemplateModel exec(java.util.List<java.lang.String> arguments)
          Applies an XPath expression to the node set and returns the resulting node set.
 TemplateModel get(java.lang.String key)
          Provides node set traversal as well as special functions: filtering by name, filtering by node type, shallow-copying, and duplicate removal.
 java.lang.Object getAsObject()
          Return the underlying object to the reflection mechanism in the freemarker.ext.beans package.
 java.lang.String getAsString()
          This method returns the string resulting from concatenation of string representations of its nodes.
 TemplateModel getAtIndex(long i)
          Retrieves the i-th element of the node list.
 boolean isEmpty()
          Returns true if this model contains no nodes.
static void main(java.lang.String[] args)
          Loads a template from a file passed as the first argument, loads an XML document from either the second argument or standard input, passes it to the template and writes the result of template processing to standard output.
 void registerNamespace(java.lang.String prefix, java.lang.String uri)
          Registers an XML namespace with this node list.
 void releaseIterator(TemplateIteratorModel iterator)
          Returns the used iterator to the list model.
 TemplateIteratorModel templateIterator()
          Retrieves an iterator to iterate over this list.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EMPTY_SCALAR

public static final TemplateScalarModel EMPTY_SCALAR
A singleton empty scalar. It is an instance of EmptyScalar.

Constructor Detail

NodeListModel

public NodeListModel(org.jdom.Document document)
Creates a node set template that holds a single Document node.


NodeListModel

public NodeListModel(org.jdom.Element element)
Creates a node set template that holds a single Element node.


NodeListModel

public NodeListModel(java.util.List<?> nodes)
Creates a node set template that holds a list of nodes.

Parameters:
nodes - the list of nodes this template should hold. The created template will copy the passed nodes list, so changes to the passed list will not affect the model.

NodeListModel

public NodeListModel(java.util.List<?> nodes,
                     boolean copy)
Creates a node set template that holds a list of nodes.

Parameters:
nodes - the list of nodes this template should hold.
copy - if true, the created template will copy the passed nodes list, so changes to the passed list will not affect the model. If false, the model will reference the passed list and will sense changes in it, altough no operations on the list will be synchronized.
Method Detail

createNodeListModel

public static NodeListModel createNodeListModel(java.util.List<?> list,
                                                boolean copy)
Factory method for creating a NodeListModel that best suits the passed list.

Returns:
the singleton empty model for null or empty input list, a model backed by a singleton collection for a single-element input list, or a generic model backed by the passed list, or a copy of it for multiple-element input list.

isEmpty

public boolean isEmpty()
Returns true if this model contains no nodes.

Specified by:
isEmpty in interface TemplateModel
Returns:
true if this object is empty, otherwise false

getAsString

public java.lang.String getAsString()
                             throws TemplateModelException
This method returns the string resulting from concatenation of string representations of its nodes. Each node is rendered using its XML serialization format, while text (String) is rendered as itself. This greatly simplifies creating XML-transformation templates, as to output a node contained in variable x as XML fragment, you simply write ${x} in the template.

Specified by:
getAsString in interface TemplateScalarModel
Returns:
the String value of this scalar.
Throws:
TemplateModelException

get

public TemplateModel get(java.lang.String key)
                  throws TemplateModelException
Provides node set traversal as well as special functions: filtering by name, filtering by node type, shallow-copying, and duplicate removal. While not as powerful as the full XPath support built into the exec(List) method, it does not require the external Jaxen library to be present at run time. Below are listed the recognized keys. In key descriptions, "applicable to this-and-that node type" means that if a key is applied to a node set that contains a node of non-applicable type a TemplateMethodModel will be thrown. However, you can use _ftype key to explicitly filter out undesired node types prior to applying the restricted-applicability key. Also "current nodes" means nodes contained in this set. The order of nodes in the resulting set is the order of evaluation of the key on each node in this set from left to right. Evaluation of the key on a single node always yields the results in "natural" order (that of the document preorder traversal), even for uptree traversals. As a consequence, if this node set's nodes are listed in natural order, applying any of the keys will produce a node set that is also naturally ordered. As a special case, all node sets that are directly or indirectly generated from a single Document or Element node through repeated invocations of this method will be naturally ordered.

Specified by:
get in interface TemplateHashModel
Parameters:
key - a key that identifies a required set of nodes
Returns:
a new NodeListModel that represents the requested set of nodes.
Throws:
TemplateModelException - there was a problem getting the value for the given key

getAtIndex

public TemplateModel getAtIndex(long i)
                         throws TemplateModelException
Retrieves the i-th element of the node list.

Specified by:
getAtIndex in interface TemplateIndexedModel
Parameters:
i - the index of the underlying value we're interested in
Returns:
a TemplateModel representing the value for the given index
Throws:
TemplateModelException - the value could not be determined, possibly due to an index out-of-bounds, or an otherwise undefined value

templateIterator

public TemplateIteratorModel templateIterator()
                                       throws TemplateModelException
Retrieves an iterator to iterate over this list.

Specified by:
templateIterator in interface TemplateListModel2
Returns:
an iterator to iterate over the current list.
Throws:
TemplateModelException - the next item in the list can't be retrieved, or no next item exists.

releaseIterator

public void releaseIterator(TemplateIteratorModel iterator)
Returns the used iterator to the list model.

Specified by:
releaseIterator in interface TemplateListModel2
Parameters:
iterator - the iterator to be returned to the object pool, if any

exec

public TemplateModel exec(java.util.List<java.lang.String> arguments)
                   throws TemplateModelException
Applies an XPath expression to the node set and returns the resulting node set. In order for this method to work, your application must have access Jaxen library classes. The implementation does cache the parsed format of XPath expressions in a weak hash map, keyed by the string representation of the XPath expression. As the string object passed as the argument is usually kept in the parsed FM-Classic template, this ensures that each XPath expression is parsed only once during the lifetime of the FM-Classic template that contains it.

Specified by:
exec in interface TemplateMethodModel
Parameters:
arguments - the list of arguments. Must contain exactly one string that is the XPath expression you wish to apply.
Returns:
a NodeListModel representing the nodes that are the result of application of the XPath to the current node set.
Throws:
TemplateModelException

registerNamespace

public void registerNamespace(java.lang.String prefix,
                              java.lang.String uri)
Registers an XML namespace with this node list. Once registered, you can refer to the registered namespace using its prefix in the get(String) method from this node list and all other node lists that are derived from this node list. Use the nodelist["prefix:localname"] or the nodelist["@prefix:localname"] syntax to reach elements and attributes whose names are namespace scoped. Note that the namespace prefix need not match the actual prefix used by the XML document itself since namespaces are compared solely by their URI. You can also register namespaces during template evaluation using the nodelist._registerNamespace(prefix, uri) syntax in the template. This mechanism is completely independent from the namespace declarations in the XML document itself; its purpose is to give you an easy way to refer to namespace-scoped elements in get(String).


getAsObject

public java.lang.Object getAsObject()
                             throws TemplateModelException
Return the underlying object to the reflection mechanism in the freemarker.ext.beans package. Any variables, methods or properties can be called directly via reflection.

Specified by:
getAsObject in interface TemplateObjectModel
Returns:
the underlying object for this template model
Throws:
TemplateModelException - the object could not be returned

main

public static void main(java.lang.String[] args)
                 throws java.lang.Exception

Loads a template from a file passed as the first argument, loads an XML document from either the second argument or standard input, passes it to the template and writes the result of template processing to standard output.

Special variables are used within template processing to provide access to the XML document and related functions. These are:

Parameters:
args - command-line arguments. The first argument should be the template to be processed, the second argument should be the XML document
Throws:
java.lang.Exception