1 52 53 package freemarker.core; 54 55 import java.io.IOException ; 56 import freemarker.template.*; 57 58 62 63 final class ConditionalBlock extends TemplateElement { 64 65 final Expression condition; 66 private final boolean isFirst; 67 boolean isSimple; 68 69 ConditionalBlock(Expression condition, TemplateElement nestedBlock, boolean isFirst) 70 { 71 this.condition = condition; 72 this.nestedBlock = nestedBlock; 73 this.isFirst = isFirst; 74 } 75 76 void accept(Environment env) throws TemplateException, IOException { 77 if (condition == null || condition.isTrue(env)) { 78 if (nestedBlock != null) { 79 env.visit(nestedBlock); 80 } 81 } 82 } 83 84 public String getCanonicalForm() { 85 StringBuffer buf = new StringBuffer (); 86 if (condition == null) { 87 buf.append("<#else"); 88 } 89 else if (isFirst) { 90 buf.append("<#if "); 91 } 92 else { 93 buf.append("<#elseif "); 94 } 95 if (condition != null) { 96 buf.append(condition.getCanonicalForm()); 97 } 98 buf.append(">"); 99 if (nestedBlock != null) { 100 buf.append(nestedBlock.getCanonicalForm()); 101 } 102 if (isSimple) { 103 buf.append("</#if>"); 104 } 105 return buf.toString(); 106 } 107 108 public String getDescription() { 109 String s = "if "; 110 if (condition == null) { 111 s = "else "; 112 } 113 else if (!isFirst) { 114 s = "elseif "; 115 } 116 String cond = condition != null ? condition.toString() : ""; 117 return s + cond; 118 } 119 } 120 | Popular Tags |