1 33 34 package com.icesoft.faces.renderkit.dom_html_basic; 35 36 import javax.faces.FacesException; 37 import javax.faces.component.UIComponent; 38 import javax.faces.context.ResponseWriter; 39 import java.io.IOException ; 40 import java.util.ArrayList ; 41 import java.util.Arrays ; 42 import java.util.Iterator ; 43 import java.util.List ; 44 import java.util.Map ; 45 46 52 public class PassThruAttributeWriter { 53 54 private static List passThruAttributeNames = new ArrayList (); 55 private static List booleanPassThruAttributeNames = new ArrayList (); 56 57 static { 58 passThruAttributeNames.add("accept"); 59 passThruAttributeNames.add("accesskey"); 60 passThruAttributeNames.add("alt"); 61 passThruAttributeNames.add("bgcolor"); 62 passThruAttributeNames.add("border"); 63 passThruAttributeNames.add("cellpadding"); 64 passThruAttributeNames.add("cellspacing"); 65 passThruAttributeNames.add("charset"); 66 passThruAttributeNames.add("cols"); 67 passThruAttributeNames.add("coords"); 68 passThruAttributeNames.add("dir"); 69 passThruAttributeNames.add("enctype"); 70 passThruAttributeNames.add("frame"); 71 passThruAttributeNames.add("height"); 72 passThruAttributeNames.add("hreflang"); 73 passThruAttributeNames.add("lang"); 74 passThruAttributeNames.add("longdesc"); 75 passThruAttributeNames.add("maxlength"); 76 passThruAttributeNames.add("onblur"); 77 passThruAttributeNames.add("onchange"); 78 passThruAttributeNames.add("onclick"); 79 passThruAttributeNames.add("ondblclick"); 80 passThruAttributeNames.add("onfocus"); 81 passThruAttributeNames.add("onkeydown"); 82 passThruAttributeNames.add("onkeypress"); 83 passThruAttributeNames.add("onkeyup"); 84 passThruAttributeNames.add("onload"); 85 passThruAttributeNames.add("onmousedown"); 86 passThruAttributeNames.add("onmousemove"); 87 passThruAttributeNames.add("onmouseout"); 88 passThruAttributeNames.add("onmouseover"); 89 passThruAttributeNames.add("onmouseup"); 90 passThruAttributeNames.add("onreset"); 91 passThruAttributeNames.add("onselect"); 92 passThruAttributeNames.add("onsubmit"); 93 passThruAttributeNames.add("onunload"); 94 passThruAttributeNames.add("rel"); 95 passThruAttributeNames.add("rev"); 96 passThruAttributeNames.add("rows"); 97 passThruAttributeNames.add("rules"); 98 passThruAttributeNames.add("shape"); 99 passThruAttributeNames.add("size"); 100 passThruAttributeNames.add("style"); 101 passThruAttributeNames.add("summary"); 102 passThruAttributeNames.add("tabindex"); 103 passThruAttributeNames.add("target"); 104 passThruAttributeNames.add("title"); 105 passThruAttributeNames.add("usemap"); 106 passThruAttributeNames.add("width"); 107 passThruAttributeNames.add("width"); 108 passThruAttributeNames.add("onclickeffect"); 109 passThruAttributeNames.add("ondblclickeffect"); 110 passThruAttributeNames.add("onmousedowneffect"); 111 passThruAttributeNames.add("onmouseupeffect"); 112 passThruAttributeNames.add("onmousemoveeffect"); 113 passThruAttributeNames.add("onmouseovereffect"); 114 passThruAttributeNames.add("onmouseouteffect"); 115 passThruAttributeNames.add("onchangeeffect"); 116 passThruAttributeNames.add("onreseteffect"); 117 passThruAttributeNames.add("onsubmiteffect"); 118 passThruAttributeNames.add("onkeypresseffect"); 119 passThruAttributeNames.add("onkeydowneffect"); 120 passThruAttributeNames.add("onkeyupeffect"); 121 passThruAttributeNames.add("autocomplete"); 122 123 booleanPassThruAttributeNames.add("disabled"); 124 booleanPassThruAttributeNames.add("readonly"); 125 booleanPassThruAttributeNames.add("ismap"); 126 } 127 128 139 public static void renderAttributes(ResponseWriter writer, 140 UIComponent uiComponent, 141 String [] excludedAttributes) 142 throws IOException { 143 renderNonBooleanAttributes(writer, uiComponent, excludedAttributes); 144 renderBooleanAttributes(writer, uiComponent, excludedAttributes); 145 } 146 147 154 public static void renderOnFocus(ResponseWriter writer) 155 throws IOException { 156 writer.writeAttribute("onfocus", "setFocus(this.id);", "onfocus"); 157 } 158 159 166 public static void renderOnBlur(ResponseWriter writer) 167 throws IOException { 168 writer.writeAttribute("onfocus", "setFocus('');", "onfocus"); 169 } 170 171 private static void renderBooleanAttributes( 172 ResponseWriter writer, UIComponent uiComponent, 173 String [] excludedAttributes) 174 throws IOException { 175 176 if (writer == null) { 177 throw new FacesException("Null pointer exception"); 178 } 179 if (uiComponent == null) { 180 throw new FacesException("Null pointer exception"); 181 } 182 183 List excludedAttributesList = null; 184 if (excludedAttributes != null && excludedAttributes.length > 0) 185 excludedAttributesList = Arrays.asList(excludedAttributes); 186 187 Object nextPassThruAttributeName; 188 Object nextPassThruAttributeValue = null; 189 Iterator passThruNameIterator = 190 booleanPassThruAttributeNames.iterator(); 191 boolean primitiveAttributeValue; 192 193 while (passThruNameIterator.hasNext()) { 194 nextPassThruAttributeName = (passThruNameIterator.next()); 195 if (excludedAttributesList != null) { 196 if (excludedAttributesList.contains(nextPassThruAttributeName)) 197 continue; 198 } 199 nextPassThruAttributeValue = uiComponent.getAttributes().get( 200 nextPassThruAttributeName); 201 if (nextPassThruAttributeValue != null) { 202 if (nextPassThruAttributeValue instanceof Boolean ) { 203 primitiveAttributeValue = ((Boolean ) 204 nextPassThruAttributeValue).booleanValue(); 205 } else { 206 if (!(nextPassThruAttributeValue instanceof String )) { 207 nextPassThruAttributeValue = 208 nextPassThruAttributeValue.toString(); 209 } 210 primitiveAttributeValue = (new Boolean ((String ) 211 nextPassThruAttributeValue)).booleanValue(); 212 } 213 if (primitiveAttributeValue) { 214 writer.writeAttribute(nextPassThruAttributeName.toString(), 215 nextPassThruAttributeValue, 216 nextPassThruAttributeName.toString()); 217 } 218 219 } 220 } 221 } 222 223 private static void renderNonBooleanAttributes( 224 ResponseWriter writer, UIComponent uiComponent, 225 String [] excludedAttributes) 226 227 throws IOException { 228 if (writer == null) { 229 throw new FacesException("Null pointer exception"); 230 } 231 232 if (uiComponent == null) { 233 throw new FacesException("Component instance is null"); 234 } 235 236 List excludedAttributesList = null; 237 if (excludedAttributes != null && excludedAttributes.length > 0) 238 excludedAttributesList = Arrays.asList(excludedAttributes); 239 240 Object nextPassThruAttributeName = null; 241 Object nextPassThruAttributeValue = null; 242 Iterator passThruNameIterator = passThruAttributeNames.iterator(); 243 while (passThruNameIterator.hasNext()) { 244 nextPassThruAttributeName = (passThruNameIterator.next()); 245 if (excludedAttributesList != null) { 246 if (excludedAttributesList.contains(nextPassThruAttributeName)) 247 continue; 248 } 249 nextPassThruAttributeValue = 250 uiComponent.getAttributes().get(nextPassThruAttributeName); 251 if (nextPassThruAttributeValue != null && 256 !attributeValueIsSentinel(nextPassThruAttributeValue)) { 257 writer.writeAttribute( 258 nextPassThruAttributeName.toString(), 259 nextPassThruAttributeValue, 260 nextPassThruAttributeValue.toString()); 261 } 262 } 263 } 264 265 273 public static boolean passThruAttributeExists(UIComponent uiComponent) { 274 if (uiComponent == null) { 275 return false; 276 } 277 Map componentAttributes = uiComponent.getAttributes(); 278 if (componentAttributes.size() <= 0) { 279 return false; 280 } 281 if (componentAttributesIncludePassThruAttribute(componentAttributes, 282 passThruAttributeNames)) 283 { 284 return true; 285 } 286 if (componentAttributesIncludePassThruAttribute(componentAttributes, 287 booleanPassThruAttributeNames)) 288 { 289 return true; 290 } 291 return false; 292 } 293 294 private static boolean attributeValueIsSentinel(Object value) { 295 if (value == null) { 296 return false; 297 } 298 if (value instanceof Boolean ) { 299 if (((Boolean ) value).booleanValue() == false) { 300 return true; 301 } 302 return false; 303 } 304 if (value instanceof Number ) { 305 if (value instanceof Integer ) { 306 if (((Integer ) value).intValue() == Integer.MIN_VALUE) { 307 return true; 308 } 309 return false; 310 } 311 if (value instanceof Long ) { 312 if (((Long ) value).longValue() == Long.MIN_VALUE) { 313 return true; 314 } 315 return false; 316 } 317 if (value instanceof Short ) { 318 if (((Short ) value).shortValue() == Short.MIN_VALUE) { 319 return true; 320 } 321 return false; 322 } 323 if (value instanceof Float ) { 324 if (((Float ) value).floatValue() == Float.MIN_VALUE) { 325 return true; 326 } 327 return false; 328 } 329 if (value instanceof Double ) { 330 if (((Double ) value).doubleValue() == Double.MIN_VALUE) { 331 return true; 332 } 333 return false; 334 } 335 if (value instanceof Byte ) { 336 if (((Byte ) value).byteValue() == Byte.MIN_VALUE) { 337 return true; 338 } 339 return false; 340 } 341 } 342 if (value instanceof Character ) { 343 if (((Character ) value).charValue() == Character.MIN_VALUE) { 344 return true; 345 } 346 return false; 347 } 348 return false; 349 } 350 351 private static boolean componentAttributesIncludePassThruAttribute( 352 Map componentAttributes, List passThru) { 353 Object componentAttributeKey; 354 Object componentAttributeValue; 355 Iterator attributeKeys = componentAttributes.keySet().iterator(); 356 while (attributeKeys.hasNext()) { 357 componentAttributeKey = attributeKeys.next(); 358 if (passThru.contains(componentAttributeKey)) { 359 componentAttributeValue = 360 componentAttributes.get(componentAttributeKey); 361 if ((componentAttributeValue != null) && 362 (componentAttributeValue != "")) { 363 return true; 364 } 365 } 366 } 367 return false; 368 } 369 370 static final List getpassThruAttributeNames() { 371 return passThruAttributeNames; 372 } 373 } 374 | Popular Tags |