1 52 53 package freemarker.core; 54 55 import java.io.IOException ; 56 import java.util.*; 57 import freemarker.template.*; 58 59 63 final class BodyInstruction extends TemplateElement { 64 65 66 private List bodyParameters; 67 68 69 BodyInstruction(List bodyParameters) { 70 this.bodyParameters = bodyParameters; 71 } 72 73 List getBodyParameters() { 74 return bodyParameters; 75 } 76 77 86 void accept(Environment env) throws IOException , TemplateException { 87 Context bodyContext = new Context(env); 88 env.visit(bodyContext); 89 } 90 91 public String getCanonicalForm() { 92 StringBuffer buf = new StringBuffer ("<#nested"); 93 if (bodyParameters != null) { 94 for (int i = 0; i<bodyParameters.size(); i++) { 95 buf.append(' '); 96 buf.append(bodyParameters.get(i)); 97 } 98 } 99 buf.append('>'); 100 return buf.toString(); 101 } 102 103 public String getDescription() { 104 return "nested macro content"; 105 } 106 107 116 class Context implements LocalContext { 117 Macro.Context invokingMacroContext; 118 Environment.Namespace bodyVars; 119 120 Context(Environment env) throws TemplateException { 121 invokingMacroContext = env.getCurrentMacroContext(); 122 List bodyParameterNames = invokingMacroContext.bodyParameterNames; 123 if (bodyParameters != null) { 124 for (int i=0; i<bodyParameters.size(); i++) { 125 Expression exp = (Expression) bodyParameters.get(i); 126 TemplateModel tm = exp.getAsTemplateModel(env); 127 if (bodyParameterNames != null && i < bodyParameterNames.size()) { 128 String bodyParameterName = (String ) bodyParameterNames.get(i); 129 if (bodyVars == null) { 130 bodyVars = env.new Namespace(); 131 } 132 bodyVars.put(bodyParameterName, tm); 133 } 134 } 135 } 136 } 137 138 public TemplateModel getLocalVariable(String name) throws TemplateModelException { 139 return bodyVars == null ? null : bodyVars.get(name); 140 } 141 142 public Set getLocalVariableNames() { 143 List bodyParameterNames = invokingMacroContext.bodyParameterNames; 144 if (bodyParameterNames == null) return Collections.EMPTY_SET; 145 return new HashSet(bodyParameterNames); 146 } 147 } 148 } 149 | Popular Tags |