1 52 53 package freemarker.core; 54 55 import freemarker.template.*; 56 57 60 final class BuiltinVariable extends Expression { 61 62 static final String NAMESPACE = "namespace"; 63 static final String MAIN = "main"; 64 static final String GLOBALS = "globals"; 65 static final String LOCALS = "locals"; 66 static final String DATA_MODEL = "data_model"; 67 static final String LANG = "lang"; 68 static final String LOCALE = "locale"; 69 static final String CURRENT_NODE = "current_node"; 70 static final String NODE = "node"; 71 static final String PASS = "pass"; 72 static final String VARS = "vars"; 73 static final String VERSION = "version"; 74 static final String ERROR = "error"; 75 static final String OUTPUT_ENCODING = "output_encoding"; 76 static final String URL_ESCAPING_CHARSET = "url_escaping_charset"; 77 78 private final String name; 79 80 BuiltinVariable(String name) throws ParseException { 81 name = name.intern(); 82 this.name = name; 83 if (name != NAMESPACE 84 && name != MAIN 85 && name != GLOBALS 86 && name != LOCALS 87 && name != LANG 88 && name != LOCALE 89 && name != DATA_MODEL 90 && name != CURRENT_NODE 91 && name != NODE 92 && name != PASS 93 && name != VARS 94 && name != VERSION 95 && name != OUTPUT_ENCODING 96 && name != URL_ESCAPING_CHARSET 97 && name != ERROR) 98 { 99 throw new ParseException("Unknown built-in variable: " + name, this); 100 } 101 } 102 103 TemplateModel _getAsTemplateModel(Environment env) throws TemplateException { 104 if (name == NAMESPACE) { 105 return env.getCurrentNamespace(); 106 } 107 if (name == MAIN) { 108 return env.getMainNamespace(); 109 } 110 if (name == GLOBALS) { 111 return env.getGlobalVariables(); 112 } 113 if (name == LOCALS) { 114 return env.getCurrentMacroContext().getLocals(); 115 } 116 if (name == DATA_MODEL) { 117 return env.getDataModel(); 118 } 119 if (name == VARS) { 120 return new VarsHash(env); 121 } 122 if (name == LOCALE) { 123 return new SimpleScalar(env.getLocale().toString()); 124 } 125 if (name == LANG) { 126 return new SimpleScalar(env.getLocale().getLanguage()); 127 } 128 if (name == CURRENT_NODE || name == NODE) { 129 return env.getCurrentVisitorNode(); 130 } 131 if (name == PASS) { 132 return Macro.DO_NOTHING_MACRO; 133 } 134 if (name == VERSION) { 135 return new SimpleScalar(Configuration.getVersionNumber()); 136 } 137 if (name == OUTPUT_ENCODING) { 138 String s = env.getOutputEncoding(); 139 return s != null ? new SimpleScalar(s) : null; 140 } 141 if (name == URL_ESCAPING_CHARSET) { 142 String s = env.getURLEscapingCharset(); 143 return s != null ? new SimpleScalar(s) : null; 144 } 145 if (name == ERROR) { 146 return new SimpleScalar(env.getCurrentRecoveredErrorMesssage()); 147 } 148 throw new TemplateException("Invalid built-in variable: " + this, env); 149 } 150 151 public String toString() { 152 return "." + name; 153 } 154 155 public String getCanonicalForm() { 156 return "." + name; 157 } 158 159 boolean isLiteral() { 160 return false; 161 } 162 163 Expression _deepClone(String name, Expression subst) { 164 return this; 165 } 166 167 class VarsHash implements TemplateHashModel { 168 169 Environment env; 170 171 VarsHash(Environment env) { 172 this.env = env; 173 } 174 175 public TemplateModel get(String key) throws TemplateModelException { 176 return env.getVariable(key); 177 } 178 179 public boolean isEmpty() { 180 return false; 181 } 182 } 183 } 184 | Popular Tags |