1 52 53 package freemarker.core; 54 55 import freemarker.template.*; 56 import freemarker.ext.beans.BeanModel; 57 58 62 abstract public class Expression extends TemplateObject { 63 64 abstract TemplateModel _getAsTemplateModel(Environment env) throws TemplateException; 65 abstract boolean isLiteral(); 66 67 70 TemplateModel constantValue; 71 72 74 void setLocation(Template template, int beginColumn, int beginLine, int endColumn, int endLine) 75 throws 76 ParseException 77 { 78 super.setLocation(template, beginColumn, beginLine, endColumn, endLine); 79 if (isLiteral()) { 80 try { 81 constantValue = _getAsTemplateModel(null); 82 } catch (Exception e) { 83 } 85 } 86 } 87 88 public final TemplateModel getAsTemplateModel(Environment env) throws TemplateException { 89 return constantValue != null ? constantValue : _getAsTemplateModel(env); 90 } 91 92 String getStringValue(Environment env) throws TemplateException { 93 return getStringValue(getAsTemplateModel(env), this, env); 94 } 95 96 static String getStringValue(TemplateModel referentModel, Expression exp, Environment env) 97 throws 98 TemplateException 99 { 100 if (referentModel instanceof TemplateNumberModel) { 101 return env.formatNumber(EvaluationUtil.getNumber((TemplateNumberModel) referentModel, exp, env)); 102 } 103 if (referentModel instanceof TemplateDateModel) { 104 TemplateDateModel dm = (TemplateDateModel) referentModel; 105 return env.formatDate(EvaluationUtil.getDate(dm, exp, env), dm.getDateType()); 106 } 107 if (referentModel instanceof TemplateScalarModel) { 108 return EvaluationUtil.getString((TemplateScalarModel) referentModel, exp, env); 109 } 110 if(env.isClassicCompatible()) { 111 if (referentModel instanceof TemplateBooleanModel) { 112 return ((TemplateBooleanModel)referentModel).getAsBoolean() ? "true" : ""; 113 } 114 if (referentModel == null) { 115 return ""; 116 } 117 } 118 assertNonNull(referentModel, exp, env); 119 120 String msg = "Error " + exp.getStartLocation() 121 +"\nExpecting a string, " 122 + (env.isClassicCompatible() ? "boolean, " : "" ) 123 + "date or number here, Expression " + exp 124 + " is instead a " 125 + referentModel.getClass().getName(); 126 throw new NonStringException(msg, env); 127 } 128 129 Expression deepClone(String name, Expression subst) { 130 Expression clone = _deepClone(name, subst); 131 clone.copyLocationFrom(this); 132 return clone; 133 } 134 135 abstract Expression _deepClone(String name, Expression subst); 136 137 boolean isTrue(Environment env) throws TemplateException { 138 TemplateModel referent = getAsTemplateModel(env); 139 if (referent instanceof TemplateBooleanModel) { 140 return ((TemplateBooleanModel) referent).getAsBoolean(); 141 } 142 if (env.isClassicCompatible()) { 143 return referent != null && !isEmpty(referent); 144 } 145 assertNonNull(referent, this, env); 146 String msg = "Error " + getStartLocation() 147 + "\nExpecting a boolean (true/false) expression here" 148 + "\nExpression " + this + " does not evaluate to true/false " 149 + "\nit is an instance of " + referent.getClass().getName(); 150 throw new NonBooleanException(msg, env); 151 } 152 153 154 static boolean isEmpty(TemplateModel model) throws TemplateModelException 155 { 156 if (model instanceof BeanModel) { 157 return ((BeanModel) model).isEmpty(); 158 } else if (model instanceof TemplateSequenceModel) { 159 return ((TemplateSequenceModel) model).size() == 0; 160 } else if (model instanceof TemplateScalarModel) { 161 String s = ((TemplateScalarModel) model).getAsString(); 162 return (s == null || s.length() == 0); 163 } else if (model instanceof TemplateCollectionModel) { 164 return !((TemplateCollectionModel) model).iterator().hasNext(); 165 } else if (model instanceof TemplateHashModel) { 166 return ((TemplateHashModel) model).isEmpty(); 167 } else { 168 return true; 169 } 170 } 171 } 172 | Popular Tags |