Class NodeModel
- All Implemented Interfaces:
freemarker.core._UnexpectedTypeErrorExplainerTemplateModel,WrapperTemplateModel,AdapterTemplateModel,TemplateHashModel,TemplateModel,TemplateNodeModel,TemplateNodeModelEx,TemplateSequenceModel
Note that DefaultObjectWrapper automatically wraps W3C DOM Node-s into this, so you may need do that
with this class manually. However, before dropping the Node-s into the data-model, you certainly want to
apply simplify(Node) on them.
This class is not guaranteed to be thread safe, so instances of this shouldn't be used as shared variable (
Configuration.setSharedVariable(String, Object)).
To represent a node sequence (such as a query result) of exactly 1 nodes, this class should be used instead of
NodeListModel, as it adds extra capabilities by utilizing that we have exactly 1 node. If you need to wrap a
node sequence of 0 or multiple nodes, you must use NodeListModel.
-
Field Summary
Fields inherited from interface freemarker.template.TemplateModel
NOTHING -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanObject[]explainTypeError(Class[] expectedClasses)get(int i)Retrieves the i-th template model in this sequence.Gets aTemplateModelfrom the hash.getAdaptedObject(Class hint)Retrieves the underlying object, or some other object semantically equivalent to its value narrowed by the class hint.static DocumentBuilderFactoryReturns the DOM parser implementation that is used when buildingNodeModelobjects from XML files or fromInputStreamwith the static convenience methods ofNodeModel.static ErrorHandlergetNode()Retrieves the original object wrapped by this model.static ClassGet the currently used freemarker.ext.dom.XPathSupport used as the XPath engine.inthashCode()static voidmergeAdjacentText(Node parent)Merges adjacent text nodes (where CDATA counts as text node too).static NodeModelSame asparse(source, true, true), but loads from aFile; don't miss the security warnings documented there.static NodeModelSame asparse(InputSource, boolean, boolean), but loads from aFile; don't miss the security warnings documented there.static NodeModelparse(InputSource is)Same asparse(is, true, true); don't miss the security warnings documented there.static NodeModelparse(InputSource is, boolean removeComments, boolean removePIs)Convenience method to create aNodeModelfrom a SAXInputSource; please see the security warning further down.static voidremoveComments(Node parent)Recursively removes all comment nodes from the subtree.static voidRecursively removes all processing instruction nodes from the subtree.static voidsetDocumentBuilderFactory(DocumentBuilderFactory docBuilderFactory)Deprecated.It's a bad practice to change static fields, as if multiple independent components do that in the same JVM, they unintentionally affect each other.static voidsetErrorHandler(ErrorHandler errorHandler)Deprecated.It's a bad practice to change static fields, as if multiple independent components do that in the same JVM, they unintentionally affect each other.static voidSet an alternative implementation of freemarker.ext.dom.XPathSupport to use as the XPath engine.static voidRemoves all comments and processing instruction, and unites adjacent text nodes (here CDATA counts as text as well).intsize()Always returns 1.static voidTells the system to use (restore) the default (initial) XPath system used by this FreeMarker version on this system.static voidConvenience method.static voidstatic voidConvenience method.static NodeModelMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface freemarker.template.TemplateHashModel
isEmptyMethods inherited from interface freemarker.template.TemplateNodeModel
getNodeName
-
Constructor Details
-
NodeModel
-
-
Method Details
-
setDocumentBuilderFactory
Deprecated.It's a bad practice to change static fields, as if multiple independent components do that in the same JVM, they unintentionally affect each other. Therefore it's recommended to leave this static value at its default.Sets the DOM parser implementation to be used when buildingNodeModelobjects from XML files or fromInputStreamwith the static convenience methods ofNodeModel. Otherwise FreeMarker itself doesn't use this.- See Also:
getDocumentBuilderFactory()
-
getDocumentBuilderFactory
Returns the DOM parser implementation that is used when buildingNodeModelobjects from XML files or fromInputStreamwith the static convenience methods ofNodeModel. Otherwise FreeMarker itself doesn't use this. -
setErrorHandler
Deprecated.It's a bad practice to change static fields, as if multiple independent components do that in the same JVM, they unintentionally affect each other. Therefore it's recommended to leave this static value at its default.Sets the error handler to use when parsing the document with the static convenience methods ofNodeModel.- See Also:
getErrorHandler()
-
getErrorHandler
- Since:
- 2.3.20
- See Also:
setErrorHandler(ErrorHandler)
-
parse
public static NodeModel parse(InputSource is, boolean removeComments, boolean removePIs) throws SAXException, IOException, ParserConfigurationExceptionConvenience method to create aNodeModelfrom a SAXInputSource; please see the security warning further down. Adjacent text nodes will be merged (and CDATA sections are considered as text nodes) as withmergeAdjacentText(Node). Further simplifications are applied depending on the parameters. If all simplifications are turned on, then it appliessimplify(Node)on the loaded DOM.Note that
parse(...)is only a convenience method, and FreeMarker itself doesn't use it (except when you call the other similar static convenience methods, as they may build on each other). In particular, if you want full control over theDocumentBuilderFactoryused, create theNodewith your ownDocumentBuilderFactory, applysimplify(Node)(or such) on it, then callwrap(Node).Security warning: If the XML to load is coming from a source that you can't fully trust, you shouldn't use this method, as the
DocumentBuilderFactoryit uses by default supports external entities, and so it doesn't prevent XML External Entity (XXE) attacks. Note that XXE attacks are not specific to FreeMarker, they affect all XML parsers in general. If that's a problem in your application, OWASP has a cheat sheet to set up aDocumentBuilderFactorythat has limited functionality but is immune to XXE attacks. Because it's just a convenience method, you can just use your ownDocumentBuilderFactoryand do a few extra steps instead (see earlier).- Parameters:
removeComments- Whether to remove all comment nodes (recursively); this is like callingremoveComments(Node)removePIs- Whether to remove all processing instruction nodes (recursively); this is like callingremovePIs(Node)- Throws:
SAXExceptionIOExceptionParserConfigurationException
-
parse
public static NodeModel parse(InputSource is) throws SAXException, IOException, ParserConfigurationExceptionSame asparse(is, true, true); don't miss the security warnings documented there. -
parse
public static NodeModel parse(File f, boolean removeComments, boolean removePIs) throws SAXException, IOException, ParserConfigurationExceptionSame asparse(InputSource, boolean, boolean), but loads from aFile; don't miss the security warnings documented there. -
parse
public static NodeModel parse(File f) throws SAXException, IOException, ParserConfigurationExceptionSame asparse(source, true, true), but loads from aFile; don't miss the security warnings documented there. -
getNode
- Returns:
- the underling W3C DOM Node object that this TemplateNodeModel is wrapping.
-
get
Description copied from interface:TemplateHashModelGets aTemplateModelfrom the hash.- Specified by:
getin interfaceTemplateHashModel- Parameters:
key- The name by which theTemplateModelis identified in the template.- Returns:
- The
TemplateModelreferred to by the key, ornullif not found. - Throws:
TemplateModelException
-
getParentNode
- Specified by:
getParentNodein interfaceTemplateNodeModel- Returns:
- the parent of this node or null, in which case this node is the root of the tree.
-
getPreviousSibling
- Specified by:
getPreviousSiblingin interfaceTemplateNodeModelEx- Returns:
- The immediate previous sibling of this node, or
nullif there's no such node. - Throws:
TemplateModelException
-
getNextSibling
- Specified by:
getNextSiblingin interfaceTemplateNodeModelEx- Returns:
- The immediate next sibling of this node, or
nullif there's no such node. - Throws:
TemplateModelException
-
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
- 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
- Throws:
TemplateModelException
-
size
public final int size()Always returns 1.- Specified by:
sizein interfaceTemplateSequenceModel- Returns:
- the number of items in the list.
-
get
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
- 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.
-
hashCode
public final int hashCode() -
equals
-
wrap
-
removeComments
Recursively removes all comment nodes from the subtree.- See Also:
simplify(org.w3c.dom.Node)
-
removePIs
Recursively removes all processing instruction nodes from the subtree.- See Also:
simplify(org.w3c.dom.Node)
-
mergeAdjacentText
Merges adjacent text nodes (where CDATA counts as text node too). Operates recursively on the entire subtree. The merged node will have the type of the first node of the adjacent merged nodes.Because XPath assumes that there are no adjacent text nodes in the tree, not doing this can have undesirable side effects. Xalan queries like
text()will only return the first of a list of matching adjacent text nodes instead of all of them, while Jaxen will return all of them as intuitively expected.- See Also:
simplify(org.w3c.dom.Node)
-
simplify
Removes all comments and processing instruction, and unites adjacent text nodes (here CDATA counts as text as well). This is similar to applyingremoveComments(Node),removePIs(Node), and finallymergeAdjacentText(Node), but it does all that somewhat faster. -
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
Convenience method. Tells the system to use Jaxen for XPath queries.- Throws:
Exception- if the Jaxen classes are not present.
-
useXalanXPathSupport
Convenience method. Tells the system to use Xalan for XPath queries.- Throws:
Exception- if the Xalan XPath classes are not present.
-
useSunInternalXPathSupport
- Throws:
Exception
-
setXPathSupportClass
Set an alternative implementation of freemarker.ext.dom.XPathSupport to use as the XPath engine.- Parameters:
cl- the class, ornullto disable XPath support.
-
getXPathSupportClass
Get the currently used freemarker.ext.dom.XPathSupport used as the XPath engine. Returnsnullif XPath support is disabled. -
getAdaptedObject
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 whenhint.isInstance(underlyingObject)holds. When called withjava.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
Description copied from interface:WrapperTemplateModelRetrieves the original object wrapped by this model.- Specified by:
getWrappedObjectin interfaceWrapperTemplateModel
-
explainTypeError
- Specified by:
explainTypeErrorin interfacefreemarker.core._UnexpectedTypeErrorExplainerTemplateModel
-