1 52 53 package freemarker.core; 54 55 import java.util.ArrayList ; 56 import java.util.Collections ; 57 import java.util.Iterator ; 58 import java.util.List ; 59 import java.util.ListIterator ; 60 import freemarker.template.*; 61 import freemarker.template.utility.Collections12; 62 import java.io.IOException ; 63 64 final class ListLiteral extends Expression { 65 66 final ArrayList values; 67 68 ListLiteral(ArrayList values) { 69 this.values = values; 70 values.trimToSize(); 71 } 72 73 TemplateModel _getAsTemplateModel(Environment env) throws TemplateException { 74 SimpleSequence list = new SimpleSequence(values.size()); 75 for (Iterator it = values.iterator(); it.hasNext();) { 76 Expression exp = (Expression) it.next(); 77 TemplateModel tm = exp.getAsTemplateModel(env); 78 assertNonNull(tm, exp, env); 79 list.add(tm); 80 } 81 return list; 82 } 83 84 88 List getValueList(Environment env) throws TemplateException { 89 int size = values.size(); 90 switch(size) { 91 case 0: { 92 return Collections.EMPTY_LIST; 93 } 94 case 1: { 95 return Collections12.singletonList(((Expression)values.get(0)).getStringValue(env)); 96 } 97 default: { 98 List result = new ArrayList (values.size()); 99 for (ListIterator iterator = values.listIterator(); iterator.hasNext();) { 100 Expression exp = (Expression)iterator.next(); 101 result.add(exp.getStringValue(env)); 102 } 103 return result; 104 } 105 } 106 } 107 108 112 List getModelList(Environment env) throws TemplateException { 113 int size = values.size(); 114 switch(size) { 115 case 0: { 116 return Collections.EMPTY_LIST; 117 } 118 case 1: { 119 return Collections12.singletonList(((Expression)values.get(0)).getAsTemplateModel(env)); 120 } 121 default: { 122 List result = new ArrayList (values.size()); 123 for (ListIterator iterator = values.listIterator(); iterator.hasNext();) { 124 Expression exp = (Expression)iterator.next(); 125 result.add(exp.getAsTemplateModel(env)); 126 } 127 return result; 128 } 129 } 130 } 131 132 public String getCanonicalForm() { 133 StringBuffer buf = new StringBuffer ("["); 134 int size = values.size(); 135 for (int i = 0; i<size; i++) { 136 Expression value = (Expression) values.get(i); 137 buf.append(value.getCanonicalForm()); 138 if (i != size-1) { 139 buf.append(","); 140 } 141 } 142 buf.append("]"); 143 return buf.toString(); 144 } 145 146 boolean isLiteral() { 147 if (constantValue != null) { 148 return true; 149 } 150 for (int i = 0; i<values.size(); i++) { 151 Expression exp = (Expression) values.get(i); 152 if (!exp.isLiteral()) { 153 return false; 154 } 155 } 156 return true; 157 } 158 159 161 TemplateSequenceModel evaluateStringsToNamespaces(Environment env) throws TemplateException { 162 TemplateSequenceModel val = (TemplateSequenceModel) getAsTemplateModel(env); 163 SimpleSequence result = new SimpleSequence(val.size()); 164 for (int i=0; i<values.size(); i++) { 165 if (values.get(i) instanceof StringLiteral) { 166 String s = ((StringLiteral) values.get(i)).getAsString(); 167 try { 168 Environment.Namespace ns = env.importLib(s, null); 169 result.add(ns); 170 } 171 catch (IOException ioe) { 172 throw new TemplateException("Could not import library '" + s + "', " + ioe.getMessage(), env); 173 } 174 } 175 else { 176 result.add(val.get(i)); 177 } 178 } 179 return result; 180 } 181 182 Expression _deepClone(String name, Expression subst) { 183 ArrayList clonedValues = (ArrayList )values.clone(); 184 for (ListIterator iter = clonedValues.listIterator(); iter.hasNext();) { 185 iter.set(((Expression)iter.next()).deepClone(name, subst)); 186 } 187 return new ListLiteral(clonedValues); 188 } 189 190 } 191 | Popular Tags |