Package com.floreysoft.jmte.sample

Source Code of com.floreysoft.jmte.sample.SampleIfEmptyFalseExpressionCompiledTemplate

package com.floreysoft.jmte.sample;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import com.floreysoft.jmte.Engine;
import com.floreysoft.jmte.TemplateContext;
import com.floreysoft.jmte.template.AbstractCompiledTemplate;
import com.floreysoft.jmte.token.IfToken;
import com.floreysoft.jmte.token.StringToken;

// ${if !bean.trueCond}${address}${else}NIX${end}
public class SampleIfEmptyFalseExpressionCompiledTemplate extends
    AbstractCompiledTemplate {

  public SampleIfEmptyFalseExpressionCompiledTemplate(Engine engine) {
    super(engine);
  }

  @Override
  public Set<String> getUsedVariables() {
    Set<String> usedVariables = new HashSet<String>();
    usedVariables.add("address");
    return usedVariables;
  }

  @Override
  protected String transformCompiled(TemplateContext context) {
    StringBuilder buffer = new StringBuilder();

    IfToken token1 = new IfToken(Arrays.asList(new String[] { "bean",
        "trueCond" }), "bean.trueCond", true);

    context.push(token1);
    try {
      if ((Boolean) token1.evaluate(context)) {
        buffer.append(new StringToken("address", "address", null, null,
            null, null, null).evaluate(context));
      } else {
        buffer.append("NIX");
      }
    } finally {
      context.pop();
    }
    return buffer.toString();
  }

}
TOP

Related Classes of com.floreysoft.jmte.sample.SampleIfEmptyFalseExpressionCompiledTemplate

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.