freemarker.ext.dom
Class NodeModel

java.lang.Object
  extended by freemarker.ext.dom.NodeModel
All Implemented Interfaces:
WrapperTemplateModel, AdapterTemplateModel, TemplateHashModel, TemplateModel, TemplateNodeModel, TemplateSequenceModel

public abstract class NodeModel
extends java.lang.Object
implements TemplateNodeModel, TemplateHashModel, TemplateSequenceModel, AdapterTemplateModel, WrapperTemplateModel

A base class for wrapping a W3C DOM Node as a FreeMarker template model.

Version:
$Id: NodeModel.java,v 1.80 2005/06/22 11:33:31 ddekany Exp $
Author:
Jonathan Revusky

Field Summary
 
Fields inherited from interface freemarker.template.TemplateModel
NOTHING
 
Constructor Summary
protected NodeModel(org.w3c.dom.Node node)
           
 
Method Summary
 boolean equals(java.lang.Object other)
           
 TemplateModel exec(java.util.List args)
           
 TemplateModel get(int i)
          Retrieves the i-th template model in this sequence.
 TemplateModel get(java.lang.String key)
          Gets a TemplateModel from the hash.
 java.lang.Object getAdaptedObject(java.lang.Class hint)
          Retrieves the underlying object, or some other object semantically equivalent to its value narrowed by the class hint.
 TemplateSequenceModel getChildNodes()
           
static javax.xml.parsers.DocumentBuilderFactory getDocumentBuilderFactory()
           
 org.w3c.dom.Node getNode()
           
 java.lang.String getNodeNamespace()
           
 java.lang.String getNodeType()
           
 TemplateNodeModel getParentNode()
           
 java.lang.Object getWrappedObject()
          Retrieves the object wrapped by this model.
static java.lang.Class getXPathSupportClass()
          Get the currently used freemarker.ext.dom.XPathSupport used as the XPath engine.
 int hashCode()
           
static void mergeAdjacentText(org.w3c.dom.Node node)
          Merges adjacent text/cdata nodes, so that there are no adjacent text/cdata nodes.
static NodeModel parse(java.io.File f)
          Create a NodeModel from an XML file.
static NodeModel parse(java.io.File f, boolean removeComments, boolean removePIs)
          Create a NodeModel from an XML file.
static NodeModel parse(org.xml.sax.InputSource is)
          Create a NodeModel from an XML input source.
static NodeModel parse(org.xml.sax.InputSource is, boolean removeComments, boolean removePIs)
          Create a NodeModel from a SAX input source.
static void removeComments(org.w3c.dom.Node node)
          Recursively removes all comment nodes from the subtree.
static void removePIs(org.w3c.dom.Node node)
          Recursively removes all processing instruction nodes from the subtree.
static void setDocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory docBuilderFactory)
          Sets the DOM Parser implementation to be used when building NodeModel objects from XML files.
static void setErrorHandler(org.xml.sax.ErrorHandler errorHandler)
          sets the error handler to use when parsing the document.
static void setXPathSupportClass(java.lang.Class cl)
          Set an alternative implementation of freemarker.ext.dom.XPathSupport to use as the XPath engine.
static void simplify(org.w3c.dom.Node node)
          Removes comments and processing instruction, and then unites adjacent text nodes.
 int size()
           
static void useDefaultXPathSupport()
          Tells the system to use (restore) the default (initial) XPath system used by this FreeMarker version on this system.
static void useJaxenXPathSupport()
          Convenience method.
static void useSunInternalXPathSupport()
           
static void useXalanXPathSupport()
          Convenience method.
static NodeModel wrap(org.w3c.dom.Node node)
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface freemarker.template.TemplateNodeModel
getNodeName
 
Methods inherited from interface freemarker.template.TemplateHashModel
isEmpty
 

Constructor Detail

NodeModel

protected NodeModel(org.w3c.dom.Node node)
Method Detail

setDocumentBuilderFactory

public static void setDocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory docBuilderFactory)
Sets the DOM Parser implementation to be used when building NodeModel objects from XML files.


getDocumentBuilderFactory

public static javax.xml.parsers.DocumentBuilderFactory getDocumentBuilderFactory()
Returns:
the DOM Parser implementation that is used when building NodeModel objects from XML files.

setErrorHandler

public static void setErrorHandler(org.xml.sax.ErrorHandler errorHandler)
sets the error handler to use when parsing the document.


parse

public static NodeModel parse(org.xml.sax.InputSource is,
                              boolean removeComments,
                              boolean removePIs)
                       throws org.xml.sax.SAXException,
                              java.io.IOException,
                              javax.xml.parsers.ParserConfigurationException
Create a NodeModel from a SAX input source. Adjacent text nodes will be merged (and CDATA sections are considered as text nodes).

Parameters:
removeComments - whether to remove all comment nodes (recursively) from the tree before processing
removePIs - whether to remove all processing instruction nodes (recursively from the tree before processing
Throws:
org.xml.sax.SAXException
java.io.IOException
javax.xml.parsers.ParserConfigurationException

parse

public static NodeModel parse(org.xml.sax.InputSource is)
                       throws org.xml.sax.SAXException,
                              java.io.IOException,
                              javax.xml.parsers.ParserConfigurationException
Create a NodeModel from an XML input source. By default, all comments and processing instruction nodes are stripped from the tree.

Throws:
org.xml.sax.SAXException
java.io.IOException
javax.xml.parsers.ParserConfigurationException

parse

public static NodeModel parse(java.io.File f,
                              boolean removeComments,
                              boolean removePIs)
                       throws org.xml.sax.SAXException,
                              java.io.IOException,
                              javax.xml.parsers.ParserConfigurationException
Create a NodeModel from an XML file.

Parameters:
removeComments - whether to remove all comment nodes (recursively) from the tree before processing
removePIs - whether to remove all processing instruction nodes (recursively from the tree before processing
Throws:
org.xml.sax.SAXException
java.io.IOException
javax.xml.parsers.ParserConfigurationException

parse

public static NodeModel parse(java.io.File f)
                       throws org.xml.sax.SAXException,
                              java.io.IOException,
                              javax.xml.parsers.ParserConfigurationException
Create a NodeModel from an XML file. By default, all comments and processing instruction nodes are stripped from the tree.

Throws:
org.xml.sax.SAXException
java.io.IOException
javax.xml.parsers.ParserConfigurationException

getNode

public org.w3c.dom.Node getNode()
Returns:
the underling W3C DOM Node object that this TemplateNodeModel is wrapping.

get

public TemplateModel get(java.lang.String key)
                  throws TemplateModelException
Description copied from interface: TemplateHashModel
Gets a TemplateModel from the hash.

Specified by:
get in interface TemplateHashModel
Parameters:
key - the name by which the TemplateModel is identified in the template.
Returns:
the TemplateModel referred to by the key, or null if not found.
Throws:
TemplateModelException

getParentNode

public TemplateNodeModel getParentNode()
Specified by:
getParentNode in interface TemplateNodeModel
Returns:
the parent of this node or null, in which case this node is the root of the tree.

getChildNodes

public TemplateSequenceModel getChildNodes()
Specified by:
getChildNodes in interface TemplateNodeModel
Returns:
a sequence containing this node's children. If the returned value is null or empty, this is essentially a leaf node.

getNodeType

public final java.lang.String getNodeType()
                                   throws TemplateModelException
Specified by:
getNodeType in interface TemplateNodeModel
Returns:
a String describing the type of node this is. In the W3C DOM, this should be "element", "text", "attribute", etc. A TemplateNodeModel implementation that models other kinds of trees could return whatever it appropriate for that application. It can be null, if you don't want to use node-types.
Throws:
TemplateModelException

exec

public TemplateModel exec(java.util.List args)
                   throws TemplateModelException
Throws:
TemplateModelException

size

public final int size()
Specified by:
size in interface TemplateSequenceModel
Returns:
the number of items in the list.

get

public final TemplateModel get(int i)
Description copied from interface: TemplateSequenceModel
Retrieves the i-th template model in this sequence.

Specified by:
get in interface TemplateSequenceModel
Returns:
the item at the specified index, or null if the index is out of bounds. Note that a null value is interpreted by FreeMarker as "variable does not exist", and accessing a missing variables is usually considered as an error in the FreeMarker Template Language, so the usage of a bad index will not remain hidden.

getNodeNamespace

public java.lang.String getNodeNamespace()
Specified by:
getNodeNamespace in interface TemplateNodeModel
Returns:
the XML namespace URI with which this node is associated. If this TemplateNodeModel implementation is not XML-related, it will almost certainly be null. Even for XML nodes, this will often be null.

hashCode

public final int hashCode()
Overrides:
hashCode in class java.lang.Object

equals

public boolean equals(java.lang.Object other)
Overrides:
equals in class java.lang.Object

wrap

public static NodeModel wrap(org.w3c.dom.Node node)

removeComments

public static void removeComments(org.w3c.dom.Node node)
Recursively removes all comment nodes from the subtree.

See Also:
simplify(org.w3c.dom.Node)

removePIs

public static void removePIs(org.w3c.dom.Node node)
Recursively removes all processing instruction nodes from the subtree.

See Also:
simplify(org.w3c.dom.Node)

mergeAdjacentText

public static void mergeAdjacentText(org.w3c.dom.Node node)
Merges adjacent text/cdata nodes, so that there are no adjacent text/cdata nodes. Operates recursively on the entire subtree. You thus lose information about any CDATA sections occurring in the doc.

See Also:
simplify(org.w3c.dom.Node)

simplify

public static void simplify(org.w3c.dom.Node node)
Removes comments and processing instruction, and then unites adjacent text nodes. Note that CDATA sections count as text nodes.


useDefaultXPathSupport

public static void useDefaultXPathSupport()
Tells the system to use (restore) the default (initial) XPath system used by this FreeMarker version on this system.


useJaxenXPathSupport

public static void useJaxenXPathSupport()
                                 throws java.lang.Exception
Convenience method. Tells the system to use Jaxen for XPath queries.

Throws:
java.lang.Exception - if the Jaxen classes are not present.

useXalanXPathSupport

public static void useXalanXPathSupport()
                                 throws java.lang.Exception
Convenience method. Tells the system to use Xalan for XPath queries.

Throws:
java.lang.Exception - if the Xalan XPath classes are not present.

useSunInternalXPathSupport

public static void useSunInternalXPathSupport()
                                       throws java.lang.Exception
Throws:
java.lang.Exception

setXPathSupportClass

public static void setXPathSupportClass(java.lang.Class cl)
Set an alternative implementation of freemarker.ext.dom.XPathSupport to use as the XPath engine.

Parameters:
cl - the class, or null to disable XPath support.

getXPathSupportClass

public static java.lang.Class getXPathSupportClass()
Get the currently used freemarker.ext.dom.XPathSupport used as the XPath engine. Returns null if XPath support is disabled.


getAdaptedObject

public java.lang.Object getAdaptedObject(java.lang.Class hint)
Description copied from interface: AdapterTemplateModel
Retrieves the underlying object, or some other object semantically equivalent to its value narrowed by the class hint.

Specified by:
getAdaptedObject in interface AdapterTemplateModel
Parameters:
hint - the desired class of the returned value. An implementation should make reasonable effort to retrieve an object of the requested class, but if that is impossible, it must at least return the underlying object as-is. As a minimal requirement, an implementation must always return the exact underlying object when hint.isInstance(underlyingObject) == true holds. When called with java.lang.Object.class, it should return a generic Java object (i.e. if the model is wrapping a scripting lanugage object that is further wrapping a Java object, the deepest underlying Java object should be returned).
Returns:
the underlying object, or its value accommodated for the hint class.

getWrappedObject

public java.lang.Object getWrappedObject()
Description copied from interface: WrapperTemplateModel
Retrieves the object wrapped by this model.

Specified by:
getWrappedObject in interface WrapperTemplateModel