1 28 29 package com.caucho.jsp.java; 30 31 import com.caucho.jsp.JspParseException; 32 import com.caucho.server.util.CauchoSystem; 33 import com.caucho.util.L10N; 34 import com.caucho.vfs.WriteStream; 35 import com.caucho.xml.QName; 36 37 import javax.servlet.jsp.HttpJspPage ; 38 import java.io.IOException ; 39 import java.util.ArrayList ; 40 41 public class JspDirectivePage extends JspNode { 42 static L10N L = new L10N(JspDirectivePage.class); 43 44 private static final QName IS_EL_IGNORED = new QName("isELIgnored"); 45 private static final QName IS_VELOCITY_ENABLED = 46 new QName("isVelocityEnabled"); 47 private static final QName INFO = new QName("info"); 48 private static final QName CONTENT_TYPE = new QName("contentType"); 49 private static final QName PAGE_ENCODING = new QName("pageEncoding"); 50 private static final QName LANGUAGE = new QName("language"); 51 private static final QName IMPORT = new QName("import"); 52 private static final QName SESSION = new QName("session"); 53 private static final QName BUFFER = new QName("buffer"); 54 private static final QName ERROR_PAGE = new QName("errorPage"); 55 private static final QName IS_ERROR_PAGE = new QName("isErrorPage"); 56 private static final QName AUTO_FLUSH = new QName("autoFlush"); 57 private static final QName IS_THREAD_SAFE = new QName("isThreadSafe"); 58 private static final QName EXTENDS = new QName("extends"); 59 private static final QName TRIM_WS = new QName("trimDirectiveWhitespaces"); 60 private static final QName DEFER = new QName("deferredSyntaxAllowedAsLiteral"); 61 62 private Boolean _isElIgnored; 63 64 70 public void addAttribute(QName name, String value) 71 throws JspParseException 72 { 73 if (IS_EL_IGNORED.equals(name)) { 74 boolean isIgnored = value.equals("true"); 75 76 if (! _parseState.setELIgnored(isIgnored)) 77 throw error(L.l("isELIgnored values conflict")); 78 79 _parseState.markELIgnoredSet(); 80 } 81 85 else if (IS_VELOCITY_ENABLED.equals(name)) 86 _parseState.setVelocityEnabled(value.equals("true")); 87 else if (INFO.equals(name)) { 88 String oldInfo = _parseState.getInfo(); 89 90 if (oldInfo != null && ! value.equals(oldInfo)) 91 throw error(L.l("info '{0}' conflicts with previous value of info '{1}'. Check the .jsp and any included .jsp files for conflicts.", value, oldInfo)); 92 93 _parseState.setInfo(value); 94 } 95 else if (CONTENT_TYPE.equals(name)) { 96 String oldContentType = _parseState.getContentType(); 97 98 if (oldContentType != null && ! value.equals(oldContentType)) 99 throw error(L.l("contentType '{0}' conflicts with previous value of contentType '{1}'. Check the .jsp and any included .jsp files for conflicts.", value, oldContentType)); 100 101 _parseState.setContentType(value); 102 String charEncoding = parseCharEncoding(value); 103 if (charEncoding != null) 104 _parseState.setCharEncoding(charEncoding); 105 } 106 else if (PAGE_ENCODING.equals(name)) { 107 String oldEncoding = _parseState.getPageEncoding(); 108 109 119 120 _parseState.setPageEncoding(value); 121 _parseState.setCharEncoding(value); 122 } 123 else if (LANGUAGE.equals(name)) { 124 if (! value.equals("java")) 125 throw error(L.l("'{0}' is not supported as a JSP scripting language.", 126 value)); 127 } 128 else if (IMPORT.equals(name)) { 129 _parseState.addImport(value); 130 } 131 else if (SESSION.equals(name)) { 132 boolean isValid = false; 133 134 if (value.equals("true")) 135 isValid = _parseState.setSession(true); 136 else if (value.equals("false")) 137 isValid = _parseState.setSession(false); 138 else 139 throw error(L.l("session expects 'true' or 'false' at '{0}'", 140 value)); 141 142 _parseState.markSessionSet(); 143 144 if (! isValid) 145 throw error(L.l("session is assigned different values.")); 146 } 147 else if (BUFFER.equals(name)) { 148 boolean isValid = _parseState.setBuffer(processBufferSize(value)); 149 150 _parseState.markBufferSet(); 151 152 if (! isValid) 153 throw error(L.l("buffer is assigned different values.")); 154 155 if (_parseState.getBuffer() == 0 && ! _parseState.isAutoFlush()) 156 throw error(L.l("buffer must be non-zero when autoFlush is false.")); 157 } 158 else if (ERROR_PAGE.equals(name)) { 159 String errorPage = _parseState.getErrorPage(); 160 161 String newErrorPage = getRelativeUrl(value); 162 163 _parseState.setErrorPage(newErrorPage); 164 165 if (errorPage != null && ! errorPage.equals(newErrorPage)) { 166 _parseState.setErrorPage(null); 167 throw error(L.l("errorPage is assigned different value '{0}'.", 168 newErrorPage)); 169 } 170 } 171 else if (IS_ERROR_PAGE.equals(name)) { 172 boolean isValid = false; 173 174 if (value.equals("true")) 175 isValid = _parseState.setErrorPage(true); 176 else if (value.equals("false")) 177 isValid = _parseState.setErrorPage(false); 178 else 179 throw error(L.l("isErrorPage expects 'true' or 'false' at '{0}'", 180 value)); 181 182 _parseState.markErrorPage(); 183 184 if (! isValid) 185 throw error(L.l("isErrorPage is assigned different values.")); 186 } 187 else if (AUTO_FLUSH.equals(name)) { 188 boolean isValid = false; 189 190 if (value.equals("true")) 191 isValid = _parseState.setAutoFlush(true); 192 else if (value.equals("false")) 193 isValid = _parseState.setAutoFlush(false); 194 else 195 throw error(L.l("autoFlush expects 'true' or 'false' at '{0}'", 196 value)); 197 198 if (! isValid) 199 throw error(L.l("autoFlush is assigned different values.")); 200 201 if (_parseState.getBuffer() == 0 && ! _parseState.isAutoFlush()) 202 throw error(L.l("buffer must be non-zero when autoFlush is false.")); 203 204 _parseState.markAutoFlushSet(); 205 } 206 else if (IS_THREAD_SAFE.equals(name)) { 207 boolean isValid = false; 208 209 if (value.equals("true")) 210 isValid = _parseState.setThreadSafe(true); 211 else if (value.equals("false")) 212 isValid = _parseState.setThreadSafe(false); 213 else 214 throw error(L.l("isThreadSafe expects 'true' or 'false' at '{0}'", 215 value)); 216 217 _parseState.markThreadSafeSet(); 218 219 if (! isValid) 220 throw error(L.l("isThreadSafe is assigned different values.")); 221 } 222 else if (EXTENDS.equals(name)) { 223 Class cl = null; 224 225 try { 226 cl = CauchoSystem.loadClass(value); 227 } catch (Exception e) { 228 throw error(e); 229 } 230 231 if (! HttpJspPage .class.isAssignableFrom(cl)) 232 throw error(L.l("'{0}' must implement HttpJspPage. The class named by jsp:directive.page extends='...' must implement HttpJspPage.", value)); 233 234 Class oldExtends = _parseState.getExtends(); 235 236 if (oldExtends != null && ! cl.equals(oldExtends)) 237 throw error(L.l("extends '{0}' conflicts with previous value of extends '{1}'. Check the .jsp and any included .jsp files for conflicts.", value, oldExtends.getName())); 238 239 _parseState.setExtends(cl); 240 } 241 else if (TRIM_WS.equals(name)) { 242 if (value.equals("true")) 243 _parseState.setTrimWhitespace(true); 244 else if (value.equals("false")) 245 _parseState.setTrimWhitespace(false); 246 else 247 throw error(L.l("trimDirectiveWhitespaces expects 'true' or 'false' at '{0}'", 248 value)); 249 } 250 else if (DEFER.equals(name)) { 251 if (value.equals("true")) 252 _parseState.setDeferredSyntaxAllowedAsLiteral(true); 253 else if (value.equals("false")) 254 _parseState.setDeferredSyntaxAllowedAsLiteral(false); 255 else 256 throw error(L.l("deferredSyntaxAllowedAsLiteral expects 'true' or 'false' at '{0}'", 257 value)); 258 } 259 else { 260 throw error(L.l("'{0}' is an unknown JSP page directive attribute. See the JSP documentation for a complete list of page directive attributes.", 261 name.getName())); 262 } 263 } 264 265 271 private int processBufferSize(String value) throws JspParseException 272 { 273 if (value.equals("none")) 274 return 0; 275 276 int i = 0; 277 int kb = 0; 278 for (; i < value.length(); i++) { 279 char ch = value.charAt(i); 280 if (ch >= '0' && ch <= '9') 281 kb = 10 * kb + ch - '0'; 282 else 283 break; 284 } 285 286 if (! value.substring(i).equals("kb")) 287 throw error(L.l("Expected buffer size at '{0}'. Buffer sizes must end in 'kb'", value)); 288 289 return 1024 * kb; 290 } 291 292 protected String getRelativeUrl(String value) 293 { 294 if (value.length() > 0 && value.charAt(0) == '/') 295 return value; 296 else 297 return _parseState.getUriPwd() + value; 298 } 299 300 304 static String parseCharEncoding(String type) 305 throws JspParseException 306 { 307 type = type.toLowerCase(); 308 int i; 309 char ch; 310 while ((i = type.indexOf(';')) >= 0 && i < type.length()) { 311 i++; 312 while (i < type.length() && ((ch = type.charAt(i)) == ' ' || ch == '\t')) 313 i++; 314 315 if (i >= type.length()) 316 return null; 317 318 type = type.substring(i); 319 i = type.indexOf('='); 320 if (i >= 0) { 321 if (! type.startsWith("charset")) 322 continue; 323 324 for (i++; 325 i < type.length() && ((ch = type.charAt(i)) == ' ' || ch == '\t'); 326 i++) { 327 } 328 329 type = type.substring(i); 330 } 331 332 for (i = 0; 333 i < type.length() && (ch = type.charAt(i)) != ';' && ch != ' '; 334 i++) { 335 } 336 337 return type.substring(0, i); 338 } 339 340 return null; 341 } 342 343 346 public void endAttributes() 347 throws JspParseException 348 { 349 if (_gen.isTag()) 350 throw error(L.l("page directives are forbidden in tags.")); 351 } 352 353 356 public boolean isStatic() 357 { 358 return true; 359 } 360 361 366 public void printXml(WriteStream os) 367 throws IOException 368 { 369 os.print("<jsp:directive.page"); 370 printJspId(os); 371 if (! _parseState.isELIgnored()) 372 os.print(" el-ignored='false'"); 373 377 if (_parseState.getContentType() != null) 378 os.print(" content-type='" + _parseState.getContentType() + "'"); 379 380 ArrayList <String > imports = _parseState.getImportList(); 381 382 if (imports != null && imports.size() != 0) { 383 os.print(" import='"); 384 for (int i = 0; i < imports.size(); i++) { 385 if (i != 0) 386 os.print(','); 387 os.print(imports.get(i)); 388 } 389 os.print("'"); 390 } 391 392 393 os.print("/>"); 394 } 395 396 401 public void generate(JspJavaWriter out) 402 throws Exception 403 { 404 } 405 } 406 | Popular Tags |