1 52 53 package freemarker.core; 54 55 import java.util.Date ; 56 import freemarker.template.*; 57 58 62 class EvaluationUtil 63 { 64 private EvaluationUtil() 65 { 66 } 67 68 static String getString(TemplateScalarModel model, Expression expr, Environment env) 69 throws 70 TemplateException 71 { 72 String value = model.getAsString(); 73 if(value == null) 74 { 75 if(env.isClassicCompatible()) 76 { 77 return ""; 78 } 79 else 80 { 81 throw new TemplateException(expr + " evaluated to null string.", env); 82 } 83 } 84 return value; 85 } 86 87 static Number getNumber(Expression expr, Environment env) 88 throws 89 TemplateException 90 { 91 TemplateModel model = expr.getAsTemplateModel(env); 92 return getNumber(model, expr, env); 93 } 94 95 static Number getNumber(TemplateModel model, Expression expr, Environment env) 96 throws 97 TemplateException 98 { 99 if(model instanceof TemplateNumberModel) 100 { 101 return getNumber((TemplateNumberModel)model, expr, env); 102 } 103 else if(model == null) { 104 throw new InvalidReferenceException(expr + " is undefined.", env); 105 } 106 else 107 { 108 throw new NonNumericalException(expr + " is not a number, it is " + model.getClass().getName(), env); 109 } 110 } 111 112 static Number getNumber(TemplateNumberModel model, Expression expr, Environment env) 113 throws TemplateModelException, TemplateException 114 { 115 Number value = model.getAsNumber(); 116 if(value == null) { 117 throw new TemplateException(expr + " evaluated to null number.", env); 118 } 119 return value; 120 } 121 122 static Date getDate(TemplateDateModel model, Expression expr, Environment env) 123 throws TemplateModelException, TemplateException 124 { 125 Date value = model.getAsDate(); 126 if(value == null) { 127 throw new TemplateException(expr + " evaluated to null date.", env); 128 } 129 return value; 130 } 131 } 132 | Popular Tags |