Package freemarker.ext.dom
Class NodeModel
- java.lang.Object
-
- freemarker.ext.dom.NodeModel
-
- All Implemented Interfaces:
freemarker.core._UnexpectedTypeErrorExplainerTemplateModel,WrapperTemplateModel,AdapterTemplateModel,TemplateHashModel,TemplateModel,TemplateNodeModel,TemplateSequenceModel
public abstract class NodeModel extends Object implements TemplateNodeModel, TemplateHashModel, TemplateSequenceModel, AdapterTemplateModel, WrapperTemplateModel, freemarker.core._UnexpectedTypeErrorExplainerTemplateModel
A base class for wrapping a W3C DOM Node as a FreeMarker template model.Note that
DefaultObjectWrapperautomatically wraps W3C DOMNode-s into this, so you may not need to do that with this class manually. Though, before dropping theNode-s into the data-model, you may want to applysimplify(Node)on them.
-
-
Field Summary
-
Fields inherited from interface freemarker.template.TemplateModel
NOTHING
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanequals(Object other)TemplateModelexec(List args)Object[]explainTypeError(Class[] expectedClasses)TemplateModelget(int i)Retrieves the i-th template model in this sequence.TemplateModelget(String key)Gets a TemplateModel from the hash.ObjectgetAdaptedObject(Class hint)Retrieves the underlying object, or some other object semantically equivalent to its value narrowed by the class hint.TemplateSequenceModelgetChildNodes()static DocumentBuilderFactorygetDocumentBuilderFactory()static ErrorHandlergetErrorHandler()NodegetNode()StringgetNodeNamespace()StringgetNodeType()TemplateNodeModelgetParentNode()ObjectgetWrappedObject()Retrieves the original object wrapped by this model.static ClassgetXPathSupportClass()Get the currently used freemarker.ext.dom.XPathSupport used as the XPath engine.inthashCode()static voidmergeAdjacentText(Node node)Merges adjacent text/cdata nodes, so that there are no adjacent text/cdata nodes.static NodeModelparse(File f)Create a NodeModel from an XML file.static NodeModelparse(File f, boolean removeComments, boolean removePIs)Create a NodeModel from an XML file.static NodeModelparse(InputSource is)Create a NodeModel from an XML input source.static NodeModelparse(InputSource is, boolean removeComments, boolean removePIs)Create a NodeModel from a SAX input source.static voidremoveComments(Node node)Recursively removes all comment nodes from the subtree.static voidremovePIs(Node node)Recursively removes all processing instruction nodes from the subtree.static voidsetDocumentBuilderFactory(DocumentBuilderFactory docBuilderFactory)Sets the DOM Parser implementation to be used when building NodeModel objects from XML files.static voidsetErrorHandler(ErrorHandler errorHandler)sets the error handler to use when parsing the document.static voidsetXPathSupportClass(Class cl)Set an alternative implementation of freemarker.ext.dom.XPathSupport to use as the XPath engine.static voidsimplify(Node node)Removes comments and processing instruction, and then unites adjacent text nodes.intsize()static voiduseDefaultXPathSupport()Tells the system to use (restore) the default (initial) XPath system used by this FreeMarker version on this system.static voiduseJaxenXPathSupport()Convenience method.static voiduseSunInternalXPathSupport()static voiduseXalanXPathSupport()Convenience method.static NodeModelwrap(Node node)-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface freemarker.template.TemplateHashModel
isEmpty
-
Methods inherited from interface freemarker.template.TemplateNodeModel
getNodeName
-
-
-
-
Constructor Detail
-
NodeModel
protected NodeModel(Node node)
-
-
Method Detail
-
setDocumentBuilderFactory
public static void setDocumentBuilderFactory(DocumentBuilderFactory docBuilderFactory)
Sets the DOM Parser implementation to be used when building NodeModel objects from XML files.
-
getDocumentBuilderFactory
public static DocumentBuilderFactory getDocumentBuilderFactory()
- Returns:
- the DOM Parser implementation that is used when building NodeModel objects from XML files.
-
setErrorHandler
public static void setErrorHandler(ErrorHandler errorHandler)
sets the error handler to use when parsing the document.
-
getErrorHandler
public static ErrorHandler getErrorHandler()
- Since:
- 2.3.20
-
parse
public static NodeModel parse(InputSource is, boolean removeComments, boolean removePIs) throws SAXException, IOException, 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 processingremovePIs- whether to remove all processing instruction nodes (recursively from the tree before processing- Throws:
SAXExceptionIOExceptionParserConfigurationException
-
parse
public static NodeModel parse(InputSource is) throws SAXException, IOException, ParserConfigurationException
Create a NodeModel from an XML input source. By default, all comments and processing instruction nodes are stripped from the tree.
-
parse
public static NodeModel parse(File f, boolean removeComments, boolean removePIs) throws SAXException, IOException, ParserConfigurationException
Create a NodeModel from an XML file.- Parameters:
removeComments- whether to remove all comment nodes (recursively) from the tree before processingremovePIs- whether to remove all processing instruction nodes (recursively from the tree before processing- Throws:
SAXExceptionIOExceptionParserConfigurationException
-
parse
public static NodeModel parse(File f) throws SAXException, IOException, ParserConfigurationException
Create a NodeModel from an XML file. By default, all comments and processing instruction nodes are stripped from the tree.
-
getNode
public Node getNode()
- Returns:
- the underling W3C DOM Node object that this TemplateNodeModel is wrapping.
-
get
public TemplateModel get(String key) throws TemplateModelException
Description copied from interface:TemplateHashModelGets a TemplateModel from the hash.- Specified by:
getin interfaceTemplateHashModel- 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:
getParentNodein interfaceTemplateNodeModel- 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:
getChildNodesin interfaceTemplateNodeModel- 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 String getNodeType() throws TemplateModelException
- Specified by:
getNodeTypein interfaceTemplateNodeModel- 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(List args) throws TemplateModelException
- Throws:
TemplateModelException
-
size
public final int size()
- Specified by:
sizein interfaceTemplateSequenceModel- Returns:
- the number of items in the list.
-
get
public final TemplateModel get(int i)
Description copied from interface:TemplateSequenceModelRetrieves the i-th template model in this sequence.- Specified by:
getin interfaceTemplateSequenceModel- Returns:
- the item at the specified index, or
nullif the index is out of bounds. Note that anullvalue 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, unless the default value for that case was also specified in the template.
-
getNodeNamespace
public String getNodeNamespace()
- Specified by:
getNodeNamespacein interfaceTemplateNodeModel- 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.
-
removeComments
public static void removeComments(Node node)
Recursively removes all comment nodes from the subtree.- See Also:
simplify(org.w3c.dom.Node)
-
removePIs
public static void removePIs(Node node)
Recursively removes all processing instruction nodes from the subtree.- See Also:
simplify(org.w3c.dom.Node)
-
mergeAdjacentText
public static void mergeAdjacentText(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(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 ExceptionConvenience method. Tells the system to use Jaxen for XPath queries.- Throws:
Exception- if the Jaxen classes are not present.
-
useXalanXPathSupport
public static void useXalanXPathSupport() throws ExceptionConvenience method. Tells the system to use Xalan for XPath queries.- Throws:
Exception- if the Xalan XPath classes are not present.
-
useSunInternalXPathSupport
public static void useSunInternalXPathSupport() throws Exception- Throws:
Exception
-
setXPathSupportClass
public static void setXPathSupportClass(Class cl)
Set an alternative implementation of freemarker.ext.dom.XPathSupport to use as the XPath engine.- Parameters:
cl- the class, ornullto disable XPath support.
-
getXPathSupportClass
public static Class getXPathSupportClass()
Get the currently used freemarker.ext.dom.XPathSupport used as the XPath engine. Returnsnullif XPath support is disabled.
-
getAdaptedObject
public Object getAdaptedObject(Class hint)
Description copied from interface:AdapterTemplateModelRetrieves the underlying object, or some other object semantically equivalent to its value narrowed by the class hint.- Specified by:
getAdaptedObjectin interfaceAdapterTemplateModel- 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) holds. When called with java.lang.Object.class, it should return a generic Java object (i.e. if the model is wrapping a scripting language 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 Object getWrappedObject()
Description copied from interface:WrapperTemplateModelRetrieves the original object wrapped by this model.- Specified by:
getWrappedObjectin interfaceWrapperTemplateModel
-
-