public class TemplateDocument extends DocumentDelegate
The constructor receives a template Document, and this object contains the resulting
Document. Each call to refresh()
or setData(java.util.Properties)
re-parses the
template Document. When you call data()
.setProperty
directly, you need to call refresh() yourself.
Template language
Templates are valid XML (which is required because the template itself is
parsed into a Document). There are two extra attributes, if
for
conditionals, and c
for replacement.
Replacement
When the attribute c
is present on any Node, the contents of
that node is replaced by the value of the attribute. The value can be XHTML
itself.
Example:
<p>Please look at <a href="${url}" c="the url ${url}"/> for details.</p>
This will make a link to the URL specified by property url
with
the description "the url ..." (with the dots replaced by the actual url).
It is also possible extract a portion of a string by square bracket notation. For example,
when property os.name
is equal to Windows NT
, one can retrieve
the first three characters by referencing the variable with ${os.name[0:3]}
.
If the first number is omitted, zero is used; if the last number is omitted, the length
of the string is used.
Conditionals
When the attribute if
is present on any Node, that node is
only retained when the expression evaluates to true, according to:
true
evaluates to true
false
evaluates to false
!expression
for negation
expression and expression
for the boolean and operation
expression or expression
for the boolean or operation
(subexpression)
to evaluate subexpressions, as usual
string1 == string2
to check if two strings are equal (whitespace is trimmed)string1 != string2
to check if two strings are unequal (whitespace is trimmed)<p if="${url} and ${desc}">Visit <a href="${url}" c="${desc}"/></p>
Replacement with conditional
It is also possible to use a conditional with a replacement by using
${(<expression>)}
as a variable in an attribute. This can be
used, for example, to change the class of an element based on whether a variable
is set or not: <p class="has-${(${foo})}">yeah</p>
, which
can be styled by css class has-true
when foo
is set, or
has-false
when foo
is unset.
Other remarks
When the property something.lock is defined and an element with the
attribute name="something"
is present, its readonly attribute
will be set automatically. Its use is explained in TemplatePanel
.
When the attribute
When a property cannot be found, it is looked up with selected="true"
is present, it is transformed to
selected="selected"
, as some html parsers only accept the latter. The
same happens for checked="true"
, which is transformed to
checked="checked". This allows one to use conditionals as property
value, like:
<input type="radio" name="what" checked="${(foo.what)}" />
.
System.getProperty(java.lang.String)
. If
that fails, null
is assumed as its value.
ATTRIBUTE_NODE, CDATA_SECTION_NODE, COMMENT_NODE, DOCUMENT_FRAGMENT_NODE, DOCUMENT_NODE, DOCUMENT_POSITION_CONTAINED_BY, DOCUMENT_POSITION_CONTAINS, DOCUMENT_POSITION_DISCONNECTED, DOCUMENT_POSITION_FOLLOWING, DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC, DOCUMENT_POSITION_PRECEDING, DOCUMENT_TYPE_NODE, ELEMENT_NODE, ENTITY_NODE, ENTITY_REFERENCE_NODE, NOTATION_NODE, PROCESSING_INSTRUCTION_NODE, TEXT_NODE
Constructor | Description |
---|---|
TemplateDocument(Document template) |
Create a new document from a template with empty data.
|
TemplateDocument(Document template,
Properties data) |
Create a new document from a template and directly parse from properties.
|
Modifier and Type | Method | Description |
---|---|---|
Properties |
data() |
Return the properties.
|
protected void |
doTemplate(Node node) |
Processes an XML Node to apply the template recursively.
|
protected boolean |
parseConditional(String expr) |
returns the result of a boolean expression
|
protected String |
parseExpression(String expr) |
evaluates an expression by replacing variables
|
void |
refresh() |
Rebuild the document from its template and properties
|
void |
setData(Properties p) |
Set the properties to use for the template.
|
adoptNode, appendChild, cloneNode, compareDocumentPosition, createAttribute, createAttributeNS, createCDATASection, createComment, createDocumentFragment, createElement, createElementNS, createEntityReference, createProcessingInstruction, createTextNode, getAttributes, getBaseURI, getChildNodes, getDoctype, getDocumentElement, getDocumentURI, getDomConfig, getElementById, getElementsByTagName, getElementsByTagNameNS, getFeature, getFirstChild, getImplementation, getInputEncoding, getLastChild, getLocalName, getNamespaceURI, getNextSibling, getNodeName, getNodeType, getNodeValue, getOwnerDocument, getParentNode, getPrefix, getPreviousSibling, getStrictErrorChecking, getTextContent, getUserData, getXmlEncoding, getXmlStandalone, getXmlVersion, hasAttributes, hasChildNodes, importNode, insertBefore, isDefaultNamespace, isEqualNode, isSameNode, isSupported, isValid, lookupNamespaceURI, lookupPrefix, normalize, normalizeDocument, removeChild, renameNode, replaceChild, setDocument, setDocumentURI, setNodeValue, setPrefix, setStrictErrorChecking, setTextContent, setUserData, setXmlStandalone, setXmlVersion
public TemplateDocument(Document template)
public TemplateDocument(Document template, Properties data)
public void setData(Properties p)
p
- New properties to setpublic Properties data()
public void refresh()
protected void doTemplate(Node node)
protected boolean parseConditional(String expr)
Copyright © 2010-2018 Nikhef / Stichting FOM. All Rights Reserved.