1 52 53 package freemarker.core; 54 55 import java.util.*; 56 import java.io.IOException ; 57 import freemarker.template.*; 58 59 62 final class SwitchBlock extends TemplateElement { 63 64 private Case defaultCase; 65 private Expression testExpression; 66 67 70 SwitchBlock(Expression testExpression) { 71 this.testExpression = testExpression; 72 nestedElements = new LinkedList(); 73 } 74 75 78 void addCase(Case cas) { 79 if (cas.isDefault) { 80 defaultCase = cas; 81 } 82 nestedElements.add(cas); 83 } 84 85 void accept(Environment env) 86 throws TemplateException, IOException 87 { 88 boolean processedCase = false; 89 Iterator iterator = nestedElements.iterator(); 90 try { 91 while (iterator.hasNext()) { 92 Case cas = (Case)iterator.next(); 93 boolean processCase = false; 94 95 if (processedCase) { 97 processCase = true; 98 } else if (!cas.isDefault) { 99 ComparisonExpression equalsOp = new ComparisonExpression(testExpression, cas.expression, "=="); 101 processCase = equalsOp.isTrue(env); 102 } 103 if (processCase) { 104 env.visit(cas); 105 processedCase = true; 106 } 107 } 108 109 if (!processedCase && defaultCase != null) { 112 env.visit(defaultCase); 113 } 114 } 115 catch (BreakInstruction.Break br) {} 116 } 117 118 public String getCanonicalForm() { 119 StringBuffer buf = new StringBuffer ("<#switch "); 120 buf.append(testExpression.getCanonicalForm()); 121 buf.append(">"); 122 for (int i = 0; i<nestedElements.size(); i++) { 123 Case cas = (Case) nestedElements.get(i); 124 buf.append(cas.getCanonicalForm()); 125 } 126 if (defaultCase != null) { 127 buf.append(defaultCase.getCanonicalForm()); 128 } 129 buf.append("</#switch>"); 130 return buf.toString(); 131 } 132 133 public String getDescription() { 134 return "switch " + testExpression; 135 } 136 } 137 | Popular Tags |