1 52 53 package freemarker.core; 54 55 import java.io.IOException ; 56 import java.util.*; 57 import freemarker.template.TemplateException; 58 59 62 final class AssignmentInstruction extends TemplateElement { 63 64 private int scope; 65 private Expression namespaceExp; 66 67 AssignmentInstruction(int scope) { 68 this.scope = scope; 69 nestedElements = new ArrayList(1); 70 } 71 72 void addAssignment(Assignment ass) { 73 nestedElements.add(ass); 74 } 75 76 void setNamespaceExp(Expression namespaceExp) { 77 this.namespaceExp = namespaceExp; 78 for (int i=0; i<nestedElements.size();i++) { 79 ((Assignment) nestedElements.get(i)).setNamespaceExp(namespaceExp); 80 } 81 } 82 83 void accept(Environment env) throws TemplateException, IOException { 84 for (int i = 0; i<nestedElements.size(); i++) { 85 Assignment ass = (Assignment) nestedElements.get(i); 86 env.visit(ass); 87 } 88 } 89 90 public String getCanonicalForm() { 91 String tag = "<#local "; 92 if (scope == Assignment.GLOBAL) { 93 tag = "<#global "; 94 } 95 else if (scope == Assignment.NAMESPACE) { 96 tag = "<#assign "; 97 } 98 StringBuffer buf = new StringBuffer (tag); 99 for (int i = 0; i<nestedElements.size(); i++) { 100 Assignment ass = (Assignment) nestedElements.get(i); 101 buf.append(ass.getCanonicalForm()); 102 if (i < nestedElements.size() -1) { 103 buf.append(" "); 104 } 105 } 106 if (namespaceExp != null) { 107 buf.append(" in "); 108 buf.append(namespaceExp.getCanonicalForm()); 109 } 110 buf.append("/>"); 111 return buf.toString(); 112 } 113 114 public String getDescription() { 115 String tag = "local "; 116 if (scope == Assignment.GLOBAL) { 117 tag = "global "; 118 } 119 else if (scope == Assignment.NAMESPACE) { 120 tag = "assign "; 121 } 122 tag += "assignment"; 123 if (nestedElements.size() > 1) { 124 tag += "s"; 125 } 126 return tag; 127 } 128 129 public TemplateElement postParseCleanup(boolean stripWhitespace) throws ParseException { 130 super.postParseCleanup(stripWhitespace); 131 if (nestedElements.size() == 1) { 132 Assignment ass = (Assignment) nestedElements.get(0); 133 ass.setLocation(getTemplate(), this, this); 134 return ass; 135 } 136 return this; 137 } 138 } 139 | Popular Tags |