1 52 53 package freemarker.core; 54 55 import java.util.ArrayList ; 56 import java.io.IOException ; 57 import freemarker.template.TemplateException; 58 59 63 64 final class IfBlock extends TemplateElement { 65 66 IfBlock(ConditionalBlock block) 67 { 68 nestedElements = new ArrayList (); 69 addBlock(block); 70 } 71 72 void addBlock(ConditionalBlock block) { 73 nestedElements.add(block); 74 } 75 76 void accept(Environment env) throws TemplateException, IOException { 77 for (int i = 0; i<nestedElements.size(); i++) { 78 ConditionalBlock cblock = (ConditionalBlock) nestedElements.get(i); 79 Expression condition = cblock.condition; 80 if (condition == null || condition.isTrue(env)) { 81 if (cblock.nestedBlock != null) { 82 env.visit(cblock.nestedBlock); 83 } 84 return; 85 } 86 } 87 } 88 89 public String getCanonicalForm() { 90 StringBuffer buf = new StringBuffer (); 91 for (int i = 0; i<nestedElements.size(); i++) { 92 ConditionalBlock cblock = (ConditionalBlock) nestedElements.get(i); 93 buf.append(cblock.getCanonicalForm()); 94 } 95 buf.append("</#if>"); 96 return buf.toString(); 97 } 98 99 TemplateElement postParseCleanup(boolean stripWhitespace) 100 throws ParseException 101 { 102 if (nestedElements.size() == 1) { 103 ConditionalBlock cblock = (ConditionalBlock) nestedElements.get(0); 104 cblock.isSimple = true; 105 cblock.setLocation(getTemplate(), cblock, this); 106 return cblock.postParseCleanup(stripWhitespace); 107 } 108 else { 109 return super.postParseCleanup(stripWhitespace); 110 } 111 } 112 113 public String getDescription() { 114 return "if-else "; 115 } 116 } 117 | Popular Tags |