| 1 33 34 package com.icesoft.faces.renderkit.dom_html_basic; 35 36 import com.icesoft.faces.context.DOMContext; 37 import com.icesoft.faces.context.effects.CurrentStyle; 38 import com.icesoft.faces.context.effects.LocalEffectEncoder; 39 import org.w3c.dom.Element ; 40 41 import javax.faces.FacesException; 42 import javax.faces.component.UIComponent; 43 import javax.faces.component.html.HtmlSelectManyCheckbox; 44 import javax.faces.context.FacesContext; 45 import java.util.ArrayList ; 46 import java.util.Arrays ; 47 import java.util.Iterator ; 48 import java.util.List ; 49 import java.util.Map ; 50 51 54 public class PassThruAttributeRenderer { 55 56 private static List passThruAttributeNames = new ArrayList (); 57 private static List booleanPassThruAttributeNames = new ArrayList (); 58 59 static { 60 passThruAttributeNames.add("accept"); 61 passThruAttributeNames.add("accesskey"); 62 passThruAttributeNames.add("alt"); 63 passThruAttributeNames.add("bgcolor"); 64 passThruAttributeNames.add("border"); 65 passThruAttributeNames.add("cellpadding"); 66 passThruAttributeNames.add("cellspacing"); 67 passThruAttributeNames.add("charset"); 68 passThruAttributeNames.add("cols"); 69 passThruAttributeNames.add("coords"); 70 passThruAttributeNames.add("dir"); 71 passThruAttributeNames.add("enctype"); 72 passThruAttributeNames.add("frame"); 73 passThruAttributeNames.add("height"); 74 passThruAttributeNames.add("hreflang"); 75 passThruAttributeNames.add("lang"); 76 passThruAttributeNames.add("longdesc"); 77 passThruAttributeNames.add("maxlength"); 78 passThruAttributeNames.add("onblur"); 79 passThruAttributeNames.add("onchange"); 80 passThruAttributeNames.add("onclick"); 81 passThruAttributeNames.add("ondblclick"); 82 passThruAttributeNames.add("onfocus"); 83 passThruAttributeNames.add("onkeydown"); 84 passThruAttributeNames.add("onkeypress"); 85 passThruAttributeNames.add("onkeyup"); 86 passThruAttributeNames.add("onload"); 87 passThruAttributeNames.add("onmousedown"); 88 passThruAttributeNames.add("onmousemove"); 89 passThruAttributeNames.add("onmouseout"); 90 passThruAttributeNames.add("onmouseover"); 91 passThruAttributeNames.add("onmouseup"); 92 passThruAttributeNames.add("onreset"); 93 passThruAttributeNames.add("onselect"); 94 passThruAttributeNames.add("onsubmit"); 95 passThruAttributeNames.add("onunload"); 96 passThruAttributeNames.add("rel"); 97 passThruAttributeNames.add("rev"); 98 passThruAttributeNames.add("rows"); 99 passThruAttributeNames.add("rules"); 100 passThruAttributeNames.add("shape"); 101 passThruAttributeNames.add("size"); 102 passThruAttributeNames.add("style"); 103 passThruAttributeNames.add("summary"); 104 passThruAttributeNames.add("tabindex"); 105 passThruAttributeNames.add("target"); 106 passThruAttributeNames.add("title"); 107 passThruAttributeNames.add("usemap"); 108 passThruAttributeNames.add("width"); 109 passThruAttributeNames.add("width"); 110 passThruAttributeNames.add("onclickeffect"); 111 passThruAttributeNames.add("ondblclickeffect"); 112 passThruAttributeNames.add("onmousedowneffect"); 113 passThruAttributeNames.add("onmouseupeffect"); 114 passThruAttributeNames.add("onmousemoveeffect"); 115 passThruAttributeNames.add("onmouseovereffect"); 116 passThruAttributeNames.add("onmouseouteffect"); 117 passThruAttributeNames.add("onchangeeffect"); 118 passThruAttributeNames.add("onreseteffect"); 119 passThruAttributeNames.add("onsubmiteffect"); 120 passThruAttributeNames.add("onkeypresseffect"); 121 passThruAttributeNames.add("onkeydowneffect"); 122 passThruAttributeNames.add("onkeyupeffect"); 123 passThruAttributeNames.add("autocomplete"); 124 125 booleanPassThruAttributeNames.add("disabled"); 126 booleanPassThruAttributeNames.add("readonly"); 127 booleanPassThruAttributeNames.add("ismap"); 128 } 129 130 140 public static void renderAttributes(FacesContext facesContext, 141 UIComponent uiComponent, 142 String [] excludedAttributes) { 143 renderAttributes( 144 facesContext, uiComponent, null, null, excludedAttributes); 145 } 146 147 159 public static void renderAttributes(FacesContext facesContext, 160 UIComponent uiComponent, 161 Element attributeElement, 162 Element styleElement, 163 String [] excludedAttributes) { 164 renderNonBooleanAttributes( 165 facesContext, uiComponent, attributeElement, excludedAttributes); 166 renderBooleanAttributes( 167 facesContext, uiComponent, attributeElement, excludedAttributes); 168 CurrentStyle.apply(facesContext, uiComponent, styleElement, null); 169 170 if(attributeElement == null) { 171 DOMContext domContext = 172 DOMContext.getDOMContext(facesContext, uiComponent); 173 Element rootElement = (Element ) domContext.getRootNode(); 174 attributeElement = rootElement; 175 } 176 LocalEffectEncoder 177 .encodeLocalEffects(uiComponent, attributeElement, facesContext); 178 renderOnFocus(uiComponent, attributeElement); 179 renderOnBlur(attributeElement); 180 } 181 182 189 public static void renderOnFocus(UIComponent uiComponent, Element root) { 190 String nodeName = root.getNodeName(); 192 193 if (nodeName.equalsIgnoreCase(HTML.ANCHOR_ELEM) || 194 nodeName.equalsIgnoreCase(HTML.INPUT_ELEM) || 195 nodeName.equalsIgnoreCase(HTML.SELECT_ELEM)) { 196 String original = 197 (String ) uiComponent.getAttributes().get("onfocus"); 198 String onfocus = "setFocus(this.id);"; 199 200 if (original == null) { 201 original = ""; 202 } 203 root.setAttribute(HTML.ONFOCUS_ATTR, onfocus + original); 204 } 205 } 206 207 213 public static void renderOnBlur(Element root) { 214 String nodeName = root.getNodeName(); 217 218 if (nodeName.equalsIgnoreCase(HTML.ANCHOR_ELEM) || 219 nodeName.equalsIgnoreCase(HTML.INPUT_ELEM) || 220 nodeName.equalsIgnoreCase(HTML.SELECT_ELEM)) { 221 String original = root.getAttribute("onblur"); 222 String onblur = "setFocus('');"; 223 224 if (original == null) { 225 original = ""; 226 } 227 root.setAttribute(HTML.ONBLUR_ATTR, onblur + original); 228 } 229 } 230 231 private static void renderBooleanAttributes( 232 FacesContext facesContext, UIComponent uiComponent, 233 Element targetElement, 234 String [] excludedAttributes) { 235 236 if (facesContext == null) { 237 throw new FacesException("Null pointer exception"); 238 } 239 if (uiComponent == null) { 240 throw new FacesException("Null pointer exception"); 241 } 242 243 if(targetElement == null) { 244 DOMContext domContext = 245 DOMContext.getDOMContext(facesContext, uiComponent); 246 Element rootElement = (Element ) domContext.getRootNode(); 247 if (rootElement == null) { 248 throw new FacesException("DOMContext is null"); 249 } 250 targetElement = rootElement; 251 } 252 253 List excludedAttributesList = null; 254 if (excludedAttributes != null && excludedAttributes.length > 0) { 255 excludedAttributesList = Arrays.asList(excludedAttributes); 256 } 257 258 Object nextPassThruAttributeName; 259 Object nextPassThruAttributeValue = null; 260 Iterator passThruNameIterator = 261 booleanPassThruAttributeNames.iterator(); 262 boolean primitiveAttributeValue; 263 264 while (passThruNameIterator.hasNext()) { 265 nextPassThruAttributeName = (passThruNameIterator.next()); 266 if (excludedAttributesList != null) { 267 if (excludedAttributesList 268 .contains(nextPassThruAttributeName)) { 269 continue; 270 } 271 } 272 nextPassThruAttributeValue = uiComponent.getAttributes().get( 273 nextPassThruAttributeName); 274 if (nextPassThruAttributeValue != null) { 275 if (nextPassThruAttributeValue instanceof Boolean ) { 276 primitiveAttributeValue = ((Boolean ) 277 nextPassThruAttributeValue).booleanValue(); 278 } else { 279 if (!(nextPassThruAttributeValue instanceof String )) { 280 nextPassThruAttributeValue = 281 nextPassThruAttributeValue.toString(); 282 } 283 primitiveAttributeValue = (new Boolean ((String ) 284 nextPassThruAttributeValue)).booleanValue(); 285 } 286 if (primitiveAttributeValue) { 287 targetElement.setAttribute( 288 nextPassThruAttributeName.toString(), 289 nextPassThruAttributeName.toString()); 290 } else { 291 targetElement.removeAttribute( 292 nextPassThruAttributeName.toString()); 293 } 294 295 } else { 296 targetElement.removeAttribute( 297 nextPassThruAttributeName.toString()); 298 } 299 } 300 } 301 302 private static void renderNonBooleanAttributes( 303 FacesContext facesContext, UIComponent uiComponent, 304 Element targetElement, 305 String [] excludedAttributes) { 306 307 if (uiComponent == null) { 308 throw new FacesException("Component instance is null"); 309 } 310 311 if(targetElement == null) { 312 DOMContext domContext = 313 DOMContext.getDOMContext(facesContext, uiComponent); 314 315 Element rootElement = (Element ) domContext.getRootNode(); 316 if (rootElement == null) { 317 throw new FacesException("DOMContext is not initialized"); 318 } 319 targetElement = rootElement; 320 } 321 322 List excludedAttributesList = null; 323 if (excludedAttributes != null && excludedAttributes.length > 0) { 324 excludedAttributesList = Arrays.asList(excludedAttributes); 325 } 326 327 Object nextPassThruAttributeName = null; 328 Object nextPassThruAttributeValue = null; 329 Iterator passThruNameIterator = passThruAttributeNames.iterator(); 330 while (passThruNameIterator.hasNext()) { 331 nextPassThruAttributeName = (passThruNameIterator.next()); 332 if (excludedAttributesList != null) { 333 if (excludedAttributesList 334 .contains(nextPassThruAttributeName)) { 335 continue; 336 } 337 } 338 nextPassThruAttributeValue = 339 uiComponent.getAttributes().get(nextPassThruAttributeName); 340 if (nextPassThruAttributeValue != null && 345 !attributeValueIsSentinel(nextPassThruAttributeValue)) { 346 targetElement.setAttribute( 347 nextPassThruAttributeName.toString(), 348 nextPassThruAttributeValue.toString()); 349 } else { 350 targetElement.removeAttribute( 351 nextPassThruAttributeName.toString()); 352 } 353 } 354 } 355 356 364 public static boolean passThruAttributeExists(UIComponent uiComponent) { 365 if (uiComponent == null) { 366 return false; 367 } 368 Map componentAttributes = uiComponent.getAttributes(); 369 if (componentAttributes.size() <= 0) { 370 return false; 371 } 372 if (componentAttributesIncludePassThruAttribute(componentAttributes, 373 passThruAttributeNames)) { 374 return true; 375 } 376 if (componentAttributesIncludePassThruAttribute(componentAttributes, 377 booleanPassThruAttributeNames)) { 378 return true; 379 } 380 return false; 381 } 382 383 private static boolean attributeValueIsSentinel(Object value) { 384 if (value == null) { 385 return false; 386 } 387 if (value instanceof Boolean ) { 388 if (((Boolean ) value).booleanValue() == false) { 389 return true; 390 } 391 return false; 392 } 393 if (value instanceof Number ) { 394 if (value instanceof Integer ) { 395 if (((Integer ) value).intValue() == Integer.MIN_VALUE) { 396 return true; 397 } 398 return false; 399 } 400 if (value instanceof Long ) { 401 if (((Long ) value).longValue() == Long.MIN_VALUE) { 402 return true; 403 } 404 return false; 405 } 406 if (value instanceof Short ) { 407 if (((Short ) value).shortValue() == Short.MIN_VALUE) { 408 return true; 409 } 410 return false; 411 } 412 if (value instanceof Float ) { 413 if (((Float ) value).floatValue() == Float.MIN_VALUE) { 414 return true; 415 } 416 return false; 417 } 418 if (value instanceof Double ) { 419 if (((Double ) value).doubleValue() == Double.MIN_VALUE) { 420 return true; 421 } 422 return false; 423 } 424 if (value instanceof Byte ) { 425 if (((Byte ) value).byteValue() == Byte.MIN_VALUE) { 426 return true; 427 } 428 return false; 429 } 430 } 431 if (value instanceof Character ) { 432 if (((Character ) value).charValue() == Character.MIN_VALUE) { 433 return true; 434 } 435 return false; 436 } 437 return false; 438 } 439 440 private static boolean componentAttributesIncludePassThruAttribute( 441 Map componentAttributes, List passThru) { 442 Object componentAttributeKey; 443 Object componentAttributeValue; 444 Iterator attributeKeys = componentAttributes.keySet().iterator(); 445 while (attributeKeys.hasNext()) { 446 componentAttributeKey = attributeKeys.next(); 447 if (passThru.contains(componentAttributeKey)) { 448 componentAttributeValue = 449 componentAttributes.get(componentAttributeKey); 450 if ((componentAttributeValue != null) && 451 (componentAttributeValue != "")) { 452 return true; 453 } 454 } 455 } 456 return false; 457 } 458 459 static final List getpassThruAttributeNames() { 460 return passThruAttributeNames; 461 } 462 } 463 | Popular Tags |