1 16 package org.apache.myfaces.renderkit.html.util; 17 18 import org.apache.myfaces.config.MyfacesConfig; 19 import org.apache.myfaces.renderkit.html.HTML; 20 21 import org.apache.commons.logging.Log; 22 import org.apache.commons.logging.LogFactory; 23 24 import javax.faces.context.ExternalContext; 25 import javax.faces.context.FacesContext; 26 import javax.faces.context.ResponseWriter; 27 import java.io.IOException ; 28 import java.io.UnsupportedEncodingException ; 29 import java.util.Arrays ; 30 import java.util.HashSet ; 31 import java.util.Set ; 32 33 75 public final class JavascriptUtils 76 { 77 private static final Log log = LogFactory.getLog(JavascriptUtils.class); 78 79 public static final String JAVASCRIPT_DETECTED = JavascriptUtils.class.getName() + ".JAVASCRIPT_DETECTED"; 80 81 private static final String AUTO_SCROLL_PARAM = "autoScroll"; 82 private static final String AUTO_SCROLL_FUNCTION = "getScrolling()"; 83 84 private static final String OLD_VIEW_ID = JavascriptUtils.class + ".OLD_VIEW_ID"; 85 86 87 private JavascriptUtils() 88 { 89 } 91 92 private static final Set RESERVED_WORDS = 93 new HashSet (Arrays.asList(new String []{ 94 "abstract", 95 "boolean", 96 "break", 97 "byte", 98 "case", 99 "catch", 100 "char", 101 "class", 102 "const", 103 "continue", 104 "default", 105 "delete", 106 "do", 107 "double", 108 "else", 109 "export", 110 "extends", 111 "false", 112 "final", 113 "finally", 114 "float", 115 "for", 116 "function", 117 "goto", 118 "if", 119 "implements", 120 "in", 121 "instanceof", 122 "int", 123 "long", 124 "native", 125 "new", 126 "null", 127 "package", 128 "private", 129 "protected", 130 "public", 131 "return", 132 "short", 133 "static", 134 "super", 135 "switch", 136 "synchronized", 137 "this", 138 "throw", 139 "throws", 140 "transient", 141 "true", 142 "try", 143 "typeof", 144 "var", 145 "void", 146 "while", 147 "with" 148 })); 149 150 public static String getValidJavascriptName(String s, boolean checkForReservedWord) 151 { 152 if (checkForReservedWord && RESERVED_WORDS.contains(s)) 153 { 154 return s + "_"; 155 } 156 157 StringBuffer buf = null; 158 for (int i = 0, len = s.length(); i < len; i++) 159 { 160 char c = s.charAt(i); 161 162 if (Character.isLetterOrDigit(c)) 163 { 164 if (buf != null) buf.append(c); 166 } 167 else 168 { 169 if (buf == null) 170 { 171 buf = new StringBuffer (s.length() + 10); 172 buf.append(s.substring(0, i)); 173 } 174 175 buf.append('_'); 176 if (c < 16) 177 { 178 buf.append('0'); 180 } 181 182 if (c < 128) 183 { 184 buf.append(Integer.toHexString(c).toUpperCase()); 186 } 187 else 188 { 189 byte[] bytes; 190 try 191 { 192 bytes = Character.toString(c).getBytes("UTF-8"); 193 } 194 catch (UnsupportedEncodingException e) 195 { 196 throw new RuntimeException (e); 197 } 198 199 for (int j = 0; j < bytes.length; j++) 200 { 201 int intVal = bytes[j]; 202 if (intVal < 0) 203 { 204 intVal = 256 + intVal; 206 } 207 else if (intVal < 16) 208 { 209 buf.append('0'); 211 } 212 buf.append(Integer.toHexString(intVal).toUpperCase()); 213 } 214 } 215 } 216 217 } 218 219 return buf == null ? s : buf.toString(); 220 } 221 222 223 public static String encodeString(String string) 224 { 225 if (string == null) 226 { 227 return ""; 228 } 229 StringBuffer sb = null; String app; 231 char c; 232 for (int i = 0; i < string.length (); ++i) 233 { 234 app = null; 235 c = string.charAt(i); 236 switch (c) 237 { 238 case '\\' : app = "\\"; break; 239 case '"' : app = "\\\""; break; 240 case '\'' : app = "\\'"; break; 241 case '\n' : app = "\\n"; break; 242 case '\r' : app = "\\r"; break; 243 } 244 if (app != null) 245 { 246 if (sb == null) 247 { 248 sb = new StringBuffer (string.substring(0, i)); 249 } 250 sb.append(app); 251 } else { 252 if (sb != null) 253 { 254 sb.append(c); 255 } 256 } 257 } 258 259 if (sb == null) 260 { 261 return string; 262 } 263 else 264 { 265 return sb.toString(); 266 } 267 } 268 269 270 public static boolean isJavascriptAllowed(ExternalContext externalContext) 271 { 272 MyfacesConfig myfacesConfig = MyfacesConfig.getCurrentInstance(externalContext); 273 if (myfacesConfig.isAllowJavascript()) 274 { 275 if (myfacesConfig.isDetectJavascript()) 276 { 277 return isJavascriptDetected(externalContext); 278 } 279 else 280 { 281 return true; 282 } 283 } 284 else 285 { 286 return false; 287 } 288 } 289 290 291 public static void setJavascriptDetected(ExternalContext externalContext, boolean value) 292 { 293 externalContext.getSessionMap().put(JAVASCRIPT_DETECTED, Boolean.valueOf(value)); 294 } 295 296 public static boolean isJavascriptDetected(ExternalContext externalContext) 297 { 298 Boolean sessionValue = (Boolean )externalContext.getSessionMap().get(JAVASCRIPT_DETECTED); 300 return sessionValue == null ? false : sessionValue.booleanValue(); 301 } 302 303 304 308 public static void appendAutoScrollAssignment(StringBuffer onClickValue, String formName) 309 { 310 onClickValue.append("document.forms['").append(formName).append("']"); 311 onClickValue.append(".elements['").append(AUTO_SCROLL_PARAM).append("']"); 312 onClickValue.append(".value=").append(AUTO_SCROLL_FUNCTION).append(";"); 313 } 314 315 318 public static void renderAutoScrollHiddenInput(ResponseWriter writer) throws IOException 319 { 320 writer.startElement(HTML.INPUT_ELEM, null); 321 writer.writeAttribute(HTML.TYPE_ATTR, "hidden", null); 322 writer.writeAttribute(HTML.NAME_ATTR, AUTO_SCROLL_PARAM, null); 323 writer.endElement(HTML.INPUT_ELEM); 324 } 325 326 329 public static void renderAutoScrollFunction(FacesContext facesContext, 330 ResponseWriter writer) throws IOException 331 { 332 writer.write("\n<script type=\"text/javascript\">\n" + 333 "<!--\n" + 334 "function " + AUTO_SCROLL_FUNCTION + " {\n" + 335 " var x = 0; var y = 0;\n" + 336 " if (document.body && document.body.scrollLeft && !isNaN(document.body.scrollLeft)) {\n" + 337 " x = document.body.scrollLeft;\n" + 338 " } else if (window.pageXOffset && !isNaN(window.pageXOffset)) {\n" + 339 " x = window.pageXOffset;\n" + 340 " }\n" + 341 " if (document.body && document.body.scrollTop && !isNaN(document.body.scrollTop)) {\n" + 342 " y = document.body.scrollTop;\n" + 343 " } else if (window.pageYOffset && !isNaN(window.pageYOffset)) {\n" + 344 " y = window.pageYOffset;\n" + 345 " }\n" + 346 " return x + \",\" + y;\n" + 347 "}\n"); 348 ExternalContext externalContext = facesContext.getExternalContext(); 349 String oldViewId = getOldViewId(externalContext); 350 if (oldViewId != null && oldViewId.equals(facesContext.getViewRoot().getViewId())) 351 { 352 String scrolling = (String )externalContext.getRequestParameterMap().get(AUTO_SCROLL_PARAM); 354 if (scrolling != null && scrolling.length() > 0) 355 { 356 String x = "0"; 357 String y = "0"; 358 int comma = scrolling.indexOf(','); 359 if (comma == -1) 360 { 361 log.warn("Illegal autoscroll request parameter: " + scrolling); 362 } 363 else 364 { 365 x = scrolling.substring(0, comma); 366 if (x.equals("undefined")) x = "0"; 367 y = scrolling.substring(comma + 1); 368 if (y.equals("undefined")) y = "0"; 369 } 370 writer.write("window.scrollTo(" + x + "," + y + ");\n"); 371 } 372 } 373 writer.write("//-->\n" + 374 "</script>\n"); 375 } 376 377 378 public static void setOldViewId(ExternalContext externalContext, String viewId) 379 { 380 externalContext.getRequestMap().put(OLD_VIEW_ID, viewId); 381 } 382 383 public static String getOldViewId(ExternalContext externalContext) 384 { 385 return (String )externalContext.getRequestMap().get(OLD_VIEW_ID); 386 } 387 } 388 | Popular Tags |