![JAR search and dependency download from the Maven repository](/logo.png)
js.template.xhtml.IfOperator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of js-xhtml-template Show documentation
Show all versions of js-xhtml-template Show documentation
Reference implementation for j(s)-lib template API, declarative, natural and based on X(HT)ML language.
The newest version!
package js.template.xhtml;
import js.dom.Element;
import js.template.TemplateException;
/**
* Include DOM branch if conditional expression evaluates to true. Operand is a conditional expression. If evaluates to true, in
* our example type
value is DIRECTORY
, div
element and all its descendants are excluded
* from resulting document. See {@link ConditionalExpression} for valid syntax supported by if
operator.
*
*
* <div data-if="type=DIRECTORY">
* . . .
* </div>
*
*
* This operator can combine conditional expression and not
flag to emulate if/else
. Please note
* exclamation mark from condition expression start on else
branch.
*
*
* <div data-if="type=DIRECTORY">
* // this branch is included if type is DIRECTORY
* </div>
* <div data-if="!type=DIRECTORY">
* // this branch is included if type is not DIRECTORY
* </div>
*
*
* Similarly a switch/case
branch can be emulated like in sample code below:
*
*
* <div data-if="fruit=APPLE">
* // enable this branch if fruit is APPLE
* </div>
* <div data-if="fruit=ORANGE">
* // enable this branch if fruit is ORANGE
* </div>
* <div data-if="fruit=BANANA">
* // enable this branch if fruit is BANANA
* </div>
*
*
* @author Iulian Rotaru
*/
final class IfOperator extends Operator {
/** Dynamic content. */
private Content content;
/**
* Construct IF operator instance.
*
* @param content dynamic content.
*/
IfOperator(Content content) {
this.content = content;
}
/**
* Execute IF operator. Evaluate given expression
and return evaluation result. Returned value acts as branch
* enabled flag.
*
* @param element context element, unused,
* @param scope scope object,
* @param expression conditional expression,
* @param arguments optional arguments, not used.
* @return true if element
and all its descendants should be included in processed document.
* @throws TemplateException if content value is undefined.
*/
@Override
protected Object doExec(Element element, Object scope, String expression, Object... arguments) throws TemplateException {
ConditionalExpression conditionalExpression = new ConditionalExpression(content, scope, expression);
return conditionalExpression.value();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy