1 52 53 package freemarker.core; 54 55 import java.io.*; 56 import java.util.*; 57 import freemarker.template.*; 58 59 64 final class TransformBlock extends TemplateElement { 65 66 private Expression transformExpression; 67 Map namedArgs; 68 69 72 TransformBlock(Expression transformExpression, 73 Map namedArgs, 74 TemplateElement nestedBlock) { 75 this.transformExpression = transformExpression; 76 this.namedArgs = namedArgs; 77 this.nestedBlock = nestedBlock; 78 } 79 80 void accept(Environment env) 81 throws TemplateException, IOException 82 { 83 TemplateTransformModel ttm = env.getTransform(transformExpression); 84 if (ttm != null) { 85 Map args; 86 if (namedArgs != null && !namedArgs.isEmpty()) { 87 args = new HashMap(); 88 for (Iterator it = namedArgs.entrySet().iterator(); it.hasNext();) { 89 Map.Entry entry = (Map.Entry) it.next(); 90 String key = (String ) entry.getKey(); 91 Expression valueExp = (Expression) entry.getValue(); 92 TemplateModel value = valueExp.getAsTemplateModel(env); 93 args.put(key, value); 94 } 95 } else { 96 args = EmptyMap.instance; 97 } 98 env.visit(nestedBlock, ttm, args); 99 } 100 else { 101 TemplateModel tm = transformExpression.getAsTemplateModel(env); 102 throw invalidTypeException(tm, transformExpression, env, "transform"); 103 } 104 } 105 106 public String getCanonicalForm() { 107 StringBuffer buf = new StringBuffer ("<#transform "); 108 buf.append(transformExpression); 109 if (namedArgs != null) { 110 for (Iterator it = namedArgs.entrySet().iterator(); it.hasNext();) { 111 Map.Entry entry = (Map.Entry) it.next(); 112 buf.append(' '); 113 buf.append(entry.getKey()); 114 buf.append('='); 115 Expression val = (Expression) entry.getValue(); 116 buf.append(val.getCanonicalForm()); 117 } 118 } 119 buf.append(">"); 120 if (nestedBlock != null) { 121 buf.append(nestedBlock.getCanonicalForm()); 122 } 123 buf.append("</#transform>"); 124 return buf.toString(); 125 } 126 127 public String getDescription() { 128 return "transform " + transformExpression; 129 } 130 } 131 | Popular Tags |