public abstract class NodeModel extends java.lang.Object implements TemplateNodeModelEx, TemplateHashModel, TemplateSequenceModel, AdapterTemplateModel, WrapperTemplateModel, freemarker.core._UnexpectedTypeErrorExplainerTemplateModel
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
.
NOTHING
Modifier | Constructor and Description |
---|---|
protected |
NodeModel(org.w3c.dom.Node node) |
Modifier and Type | Method and Description |
---|---|
boolean |
equals(java.lang.Object other) |
TemplateModel |
exec(java.util.List args) |
java.lang.Object[] |
explainTypeError(java.lang.Class[] expectedClasses) |
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()
|
static org.xml.sax.ErrorHandler |
getErrorHandler() |
TemplateNodeModelEx |
getNextSibling() |
org.w3c.dom.Node |
getNode() |
java.lang.String |
getNodeNamespace() |
java.lang.String |
getNodeType() |
TemplateNodeModel |
getParentNode() |
TemplateNodeModelEx |
getPreviousSibling() |
java.lang.Object |
getWrappedObject()
Retrieves the original 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 parent)
Merges adjacent text nodes (where CDATA counts as text node too).
|
static NodeModel |
parse(java.io.File f)
Same as
parse(source, true, true) , but loads from a File ;
don't miss the security warnings documented there. |
static NodeModel |
parse(java.io.File f,
boolean removeComments,
boolean removePIs)
Same as
parse(InputSource, boolean, boolean) , but loads from a File ; don't miss the security
warnings documented there. |
static NodeModel |
parse(org.xml.sax.InputSource is)
Same as
parse(is, true, true) ; don't miss the security warnings
documented there. |
static NodeModel |
parse(org.xml.sax.InputSource is,
boolean removeComments,
boolean removePIs)
Convenience method to create a
NodeModel from a SAX InputSource ; please see the security warning
further down. |
static void |
removeComments(org.w3c.dom.Node parent)
Recursively removes all comment nodes from the subtree.
|
static void |
removePIs(org.w3c.dom.Node parent)
Recursively removes all processing instruction nodes from the subtree.
|
static void |
setDocumentBuilderFactory(javax.xml.parsers.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. Therefore it's recommended to leave this static
value at its default.
|
static void |
setErrorHandler(org.xml.sax.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. Therefore it's recommended to leave this static
value at its default.
|
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 parent)
Removes all comments and processing instruction, and unites adjacent text nodes (here CDATA counts as text as
well).
|
int |
size()
Always returns 1.
|
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) |
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
getNodeName
isEmpty
@Deprecated public static void setDocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory docBuilderFactory)
NodeModel
objects from XML files or from
InputStream
with the static convenience methods of NodeModel
. Otherwise FreeMarker itself doesn't
use this.getDocumentBuilderFactory()
public static javax.xml.parsers.DocumentBuilderFactory getDocumentBuilderFactory()
@Deprecated public static void setErrorHandler(org.xml.sax.ErrorHandler errorHandler)
NodeModel
.getErrorHandler()
public static org.xml.sax.ErrorHandler getErrorHandler()
setErrorHandler(ErrorHandler)
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
NodeModel
from a SAX InputSource
; please see the security warning
further down. Adjacent text nodes will be merged (and CDATA sections are considered as text nodes) as with
mergeAdjacentText(Node)
. Further simplifications are applied depending on the parameters. If all
simplifications are turned on, then it applies simplify(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 the DocumentBuilderFactory
used, create the Node
with your own
DocumentBuilderFactory
, apply simplify(Node)
(or such) on it, then call
wrap(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 DocumentBuilderFactory
it 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 a
DocumentBuilderFactory
that has limited functionality but is immune to XXE attacks. Because it's just a
convenience method, you can just use your own DocumentBuilderFactory
and do a few extra steps instead
(see earlier).
removeComments
- Whether to remove all comment nodes (recursively); this is like calling removeComments(Node)
removePIs
- Whether to remove all processing instruction nodes (recursively); this is like calling
removePIs(Node)
org.xml.sax.SAXException
java.io.IOException
javax.xml.parsers.ParserConfigurationException
public static NodeModel parse(org.xml.sax.InputSource is) throws org.xml.sax.SAXException, java.io.IOException, javax.xml.parsers.ParserConfigurationException
parse(is, true, true)
; don't miss the security warnings
documented there.org.xml.sax.SAXException
java.io.IOException
javax.xml.parsers.ParserConfigurationException
public static NodeModel parse(java.io.File f, boolean removeComments, boolean removePIs) throws org.xml.sax.SAXException, java.io.IOException, javax.xml.parsers.ParserConfigurationException
parse(InputSource, boolean, boolean)
, but loads from a File
; don't miss the security
warnings documented there.org.xml.sax.SAXException
java.io.IOException
javax.xml.parsers.ParserConfigurationException
public static NodeModel parse(java.io.File f) throws org.xml.sax.SAXException, java.io.IOException, javax.xml.parsers.ParserConfigurationException
parse(source, true, true)
, but loads from a File
;
don't miss the security warnings documented there.org.xml.sax.SAXException
java.io.IOException
javax.xml.parsers.ParserConfigurationException
public org.w3c.dom.Node getNode()
public TemplateModel get(java.lang.String key) throws TemplateModelException
TemplateHashModel
get
in interface TemplateHashModel
key
- the name by which the TemplateModel
is identified in the template.TemplateModelException
public TemplateNodeModel getParentNode()
getParentNode
in interface TemplateNodeModel
public TemplateNodeModelEx getPreviousSibling() throws TemplateModelException
getPreviousSibling
in interface TemplateNodeModelEx
null
if there's no such node.TemplateModelException
public TemplateNodeModelEx getNextSibling() throws TemplateModelException
getNextSibling
in interface TemplateNodeModelEx
null
if there's no such node.TemplateModelException
public TemplateSequenceModel getChildNodes()
getChildNodes
in interface TemplateNodeModel
public final java.lang.String getNodeType() throws TemplateModelException
getNodeType
in interface TemplateNodeModel
TemplateModelException
public TemplateModel exec(java.util.List args) throws TemplateModelException
TemplateModelException
public final int size()
size
in interface TemplateSequenceModel
public final TemplateModel get(int i)
TemplateSequenceModel
get
in interface TemplateSequenceModel
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, unless the default value for that case was also specified in the
template.public java.lang.String getNodeNamespace()
getNodeNamespace
in interface TemplateNodeModel
public final int hashCode()
hashCode
in class java.lang.Object
public boolean equals(java.lang.Object other)
equals
in class java.lang.Object
public static NodeModel wrap(org.w3c.dom.Node node)
public static void removeComments(org.w3c.dom.Node parent)
simplify(org.w3c.dom.Node)
public static void removePIs(org.w3c.dom.Node parent)
simplify(org.w3c.dom.Node)
public static void mergeAdjacentText(org.w3c.dom.Node parent)
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.
simplify(org.w3c.dom.Node)
public static void simplify(org.w3c.dom.Node parent)
removeComments(Node)
, removePIs(Node)
, and finally
mergeAdjacentText(Node)
, but it does all that somewhat faster.public static void useDefaultXPathSupport()
public static void useJaxenXPathSupport() throws java.lang.Exception
java.lang.Exception
- if the Jaxen classes are not present.public static void useXalanXPathSupport() throws java.lang.Exception
java.lang.Exception
- if the Xalan XPath classes are not present.public static void useSunInternalXPathSupport() throws java.lang.Exception
java.lang.Exception
public static void setXPathSupportClass(java.lang.Class cl)
cl
- the class, or null
to disable XPath support.public static java.lang.Class getXPathSupportClass()
null
if XPath support is disabled.public java.lang.Object getAdaptedObject(java.lang.Class hint)
AdapterTemplateModel
getAdaptedObject
in interface AdapterTemplateModel
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).public java.lang.Object getWrappedObject()
WrapperTemplateModel
getWrappedObject
in interface WrapperTemplateModel
public java.lang.Object[] explainTypeError(java.lang.Class[] expectedClasses)
explainTypeError
in interface freemarker.core._UnexpectedTypeErrorExplainerTemplateModel