1 16 17 package org.apache.struts.faces.renderer; 18 19 20 import java.io.IOException ; 21 import java.util.Iterator ; 22 import java.util.Map ; 23 24 import javax.faces.application.FacesMessage; 25 import javax.faces.component.EditableValueHolder; 26 import javax.faces.component.UIComponent; 27 import javax.faces.component.ValueHolder; 28 import javax.faces.context.FacesContext; 29 import javax.faces.context.ResponseWriter; 30 import javax.faces.convert.Converter; 31 import javax.faces.convert.ConverterException; 32 import javax.faces.el.ValueBinding; 33 import javax.faces.render.Renderer; 34 35 import org.apache.commons.logging.Log; 36 import org.apache.commons.logging.LogFactory; 37 38 39 46 47 public abstract class AbstractRenderer extends Renderer { 48 49 50 52 53 private static final Log log = 54 LogFactory.getLog(AbstractRenderer.class); 55 56 57 59 60 75 public void decode(FacesContext context, UIComponent component) { 76 77 if ((context == null) || (component == null)) { 79 throw new NullPointerException (); 80 } 81 82 if (isDisabled(component) || isReadOnly(component)) { 84 return; 85 } 86 87 if (component instanceof EditableValueHolder) { 89 setSubmittedValue(context, component); 90 } 91 92 } 93 94 95 111 public void encodeBegin(FacesContext context, UIComponent component) 112 throws IOException { 113 114 if ((context == null) || (component == null)) { 116 throw new NullPointerException (); 117 } 118 119 if (log.isTraceEnabled()) { 120 log.trace("encodeBegin(id=" + component.getId() + 121 ", family=" + component.getFamily() + 122 ", rendererType=" + component.getRendererType() + ")"); 123 } 124 125 ResponseWriter writer = context.getResponseWriter(); 127 renderStart(context, component, writer); 128 renderAttributes(context, component, writer); 129 130 } 131 132 133 149 public void encodeChildren(FacesContext context, UIComponent component) 150 throws IOException { 151 152 if (context == null || component == null) { 153 throw new NullPointerException (); 154 } 155 156 if (log.isTraceEnabled()) { 157 log.trace("encodeChildren(id=" + component.getId() + 158 ", family=" + component.getFamily() + 159 ", rendererType=" + component.getRendererType() + ")"); 160 } 161 Iterator kids = component.getChildren().iterator(); 162 while (kids.hasNext()) { 163 UIComponent kid = (UIComponent) kids.next(); 164 kid.encodeBegin(context); 165 if (kid.getRendersChildren()) { 166 kid.encodeChildren(context); 167 } 168 kid.encodeEnd(context); 169 } 170 if (log.isTraceEnabled()) { 171 log.trace("encodeChildren(id=" + component.getId() + ") end"); 172 } 173 174 } 175 176 177 192 public void encodeEnd(FacesContext context, UIComponent component) 193 throws IOException { 194 195 if ((context == null) || (component == null)) { 197 throw new NullPointerException (); 198 } 199 200 if (log.isTraceEnabled()) { 201 log.trace("encodeEnd(id=" + component.getId() + 202 ", family=" + component.getFamily() + 203 ", rendererType=" + component.getRendererType() + ")"); 204 } 205 206 ResponseWriter writer = context.getResponseWriter(); 208 renderEnd(context, component, writer); 209 210 } 211 212 213 215 216 218 219 224 protected void encodeRecursive(FacesContext context, UIComponent component) 225 throws IOException { 226 227 if (!component.isRendered()) { 230 return; 231 } 232 233 if (log.isTraceEnabled()) { 235 log.trace("encodeRecursive(id=" + component.getId() + 236 ", family=" + component.getFamily() + 237 ", rendererType=" + component.getRendererType() + 238 ") encodeBegin"); 239 } 240 component.encodeBegin(context); 241 if (component.getRendersChildren()) { 242 if (log.isTraceEnabled()) { 243 log.trace("encodeRecursive(id=" + component.getId() + 244 ") delegating"); 245 } 246 component.encodeChildren(context); 247 } else { 248 if (log.isTraceEnabled()) { 249 log.trace("encodeRecursive(id=" + component.getId() + 250 ") recursing"); 251 } 252 Iterator kids = component.getChildren().iterator(); 253 while (kids.hasNext()) { 254 UIComponent kid = (UIComponent) kids.next(); 255 encodeRecursive(context, kid); 256 } 257 } 258 if (log.isTraceEnabled()) { 259 log.trace("encodeRecursive(id=" + component.getId() + ") encodeEnd"); 260 } 261 component.encodeEnd(context); 262 263 } 264 265 266 271 protected boolean isDisabled(UIComponent component) { 272 273 Object disabled = component.getAttributes().get("disabled"); 274 if (disabled == null) { 275 return (false); 276 } 277 if (disabled instanceof String ) { 278 return (Boolean.valueOf((String ) disabled).booleanValue()); 279 } else { 280 return (disabled.equals(Boolean.TRUE)); 281 } 282 283 } 284 285 286 291 protected boolean isReadOnly(UIComponent component) { 292 293 Object readonly = component.getAttributes().get("readonly"); 294 if (readonly == null) { 295 return (false); 296 } 297 if (readonly instanceof String ) { 298 return (Boolean.valueOf((String ) readonly).booleanValue()); 299 } else { 300 return (readonly.equals(Boolean.TRUE)); 301 } 302 303 } 304 305 306 323 protected void renderAttributes(FacesContext context, UIComponent component, 324 ResponseWriter writer) throws IOException { 325 326 } 327 328 329 346 protected void renderEnd(FacesContext context, UIComponent component, 347 ResponseWriter writer) throws IOException { 348 349 } 350 351 352 366 protected void renderBoolean(FacesContext context, 367 UIComponent component, 368 ResponseWriter writer, 369 String names[]) throws IOException { 370 371 if (names == null) { 372 return; 373 } 374 Map attributes = component.getAttributes(); 375 boolean flag; 376 Object value; 377 for (int i = 0; i < names.length; i++) { 378 value = attributes.get(names[i]); 379 if (value != null) { 380 if (value instanceof String ) { 381 flag = Boolean.valueOf((String ) value).booleanValue(); 382 } else { 383 flag = Boolean.valueOf(value.toString()).booleanValue(); 384 } 385 if (flag) { 386 writer.writeAttribute(names[i], names[i], names[i]); 387 flag = false; 388 } 389 } 390 } 391 392 } 393 394 395 411 protected void renderPassThrough(FacesContext context, 412 UIComponent component, 413 ResponseWriter writer, 414 String names[]) throws IOException { 415 416 if (names == null) { 417 return; 418 } 419 Map attributes = component.getAttributes(); 420 Object value; 421 for (int i = 0; i < names.length; i++) { 422 value = attributes.get(names[i]); 423 if (value != null) { 424 if (value instanceof String ) { 425 writer.writeAttribute(names[i], value, names[i]); 426 } else { 427 writer.writeAttribute(names[i], value.toString(), names[i]); 428 } 429 } 430 } 431 432 } 433 434 435 452 protected void renderStart(FacesContext context, UIComponent component, 453 ResponseWriter writer) throws IOException { 454 455 } 456 457 458 473 protected void setSubmittedValue 474 (FacesContext context, UIComponent component) { 475 476 if (!(component instanceof EditableValueHolder)) { 477 return; 478 } 479 String clientId = component.getClientId(context); 480 Map parameters = context.getExternalContext().getRequestParameterMap(); 481 if (parameters.containsKey(clientId)) { 482 if (log.isTraceEnabled()) { 483 log.trace("setSubmittedValue(" + clientId + "," + 484 (String ) parameters.get(clientId)); 485 } 486 component.getAttributes().put("submittedValue", 487 parameters.get(clientId)); 488 } 489 490 } 491 492 493 495 496 507 549 550 551 553 554 556 557 565 578 579 580 593 622 623 624 637 protected String getAsString(FacesContext context, UIComponent component, 638 Object value) throws ConverterException { 639 640 ValueBinding vb = component.getValueBinding("value"); 642 Converter converter = null; 643 if (component instanceof ValueHolder) { 644 converter = ((ValueHolder) component).getConverter(); 646 } 647 if ((converter == null) && (vb != null)) { 648 Class type = vb.getType(context); 650 if (type != null) { 651 converter = context.getApplication().createConverter(type); 652 } 653 } 654 655 if (converter != null) { 657 return (converter.getAsString(context, component, value)); 658 } else if (value == null) { 659 return (""); 660 } else if (value instanceof String ) { 661 return ((String ) value); 662 } else { 663 return (value.toString()); 664 } 665 666 } 667 668 669 } 670 | Popular Tags |