1 16 package org.apache.myfaces.renderkit; 17 18 import org.apache.myfaces.util.HashMapUtils; 19 20 import org.apache.commons.logging.Log; 21 import org.apache.commons.logging.LogFactory; 22 23 import javax.faces.FacesException; 24 import javax.faces.render.Renderer; 25 import javax.faces.component.*; 26 import javax.faces.component.html.HtmlInputText; 27 import javax.faces.context.FacesContext; 28 import javax.faces.convert.Converter; 29 import javax.faces.convert.ConverterException; 30 import javax.faces.el.ValueBinding; 31 import javax.faces.el.PropertyNotFoundException; 32 import javax.faces.model.SelectItem; 33 import java.io.IOException ; 34 import java.lang.reflect.Array ; 35 import java.util.*; 36 37 103 public class RendererUtils 104 { 105 private static final Log log = LogFactory.getLog(RendererUtils.class); 106 107 public static final String SELECT_ITEM_LIST_ATTR = RendererUtils.class.getName() + ".LIST"; 108 public static final String EMPTY_STRING = new String (); 109 110 public static String getPathToComponent(UIComponent component) 111 { 112 StringBuffer buf = new StringBuffer (); 113 114 if(component == null) 115 { 116 buf.append("{Component-Path : "); 117 buf.append("[null]}"); 118 return buf.toString(); 119 } 120 121 getPathToComponent(component,buf); 122 123 buf.insert(0,"{Component-Path : "); 124 buf.append("}"); 125 126 return buf.toString(); 127 } 128 129 private static void getPathToComponent(UIComponent component, StringBuffer buf) 130 { 131 if(component == null) 132 return; 133 134 StringBuffer intBuf = new StringBuffer (); 135 136 intBuf.append("[Class: "); 137 intBuf.append(component.getClass().getName()); 138 if(component instanceof UIViewRoot) 139 { 140 intBuf.append(",ViewId: "); 141 intBuf.append(((UIViewRoot) component).getViewId()); 142 } 143 else 144 { 145 intBuf.append(",Id: "); 146 intBuf.append(component.getId()); 147 } 148 intBuf.append("]"); 149 150 buf.insert(0,intBuf); 151 152 if(component!=null) 153 { 154 getPathToComponent(component.getParent(),buf); 155 } 156 } 157 158 public static String getConcatenatedId(FacesContext context, UIComponent container, 159 String clientId) 160 { 161 UIComponent child = container.findComponent(clientId); 162 163 if(child == null) 164 return clientId; 165 166 return getConcatenatedId(context, child); 167 } 168 169 public static String getConcatenatedId(FacesContext context, UIComponent component) 170 { 171 if (context == null) throw new NullPointerException ("context"); 172 173 StringBuffer idBuf = new StringBuffer (); 174 175 idBuf.append(component.getId()); 176 177 UIComponent parent = null; 178 179 while((parent = component.getParent())!=null) 180 { 181 if(parent instanceof NamingContainer) 182 { 183 idBuf.insert(0,NamingContainer.SEPARATOR_CHAR); 184 idBuf.insert(0,parent.getId()); 185 } 186 } 187 188 return idBuf.toString(); 189 } 190 191 public static Boolean getBooleanValue(UIComponent component) 192 { 193 if (!(component instanceof ValueHolder)) 194 { 195 throw new IllegalArgumentException ("Component : "+getPathToComponent(component) 196 +"is not a ValueHolder"); 197 } 198 199 if (component instanceof EditableValueHolder) 200 { 201 Object submittedValue = ((EditableValueHolder)component).getSubmittedValue(); 202 if (submittedValue != null) 203 { 204 if (submittedValue instanceof Boolean ) 205 { 206 return (Boolean )submittedValue; 207 } 208 else 209 { 210 throw new IllegalArgumentException ("Expected submitted value of type Boolean for component : "+ 211 getPathToComponent(component)); 212 } 213 } 214 } 215 216 Object value = ((ValueHolder)component).getValue(); 217 218 if (value==null || value instanceof Boolean ) 219 { 220 return (Boolean ) value; 221 } 222 else 223 { 224 throw new IllegalArgumentException ("Expected submitted value of type Boolean for Component : "+ 225 getPathToComponent(component)); 226 } 227 } 228 229 public static Date getDateValue(UIComponent component) 230 { 231 if (!(component instanceof ValueHolder)) 232 { 233 throw new IllegalArgumentException ("Component : "+ 234 getPathToComponent(component)+"is not a ValueHolder"); 235 } 236 237 if (component instanceof EditableValueHolder) 238 { 239 Object submittedValue = ((EditableValueHolder)component).getSubmittedValue(); 240 if (submittedValue != null) 241 { 242 if (submittedValue instanceof Date) 243 { 244 return (Date)submittedValue; 245 } 246 else 247 { 248 throw new IllegalArgumentException ( 249 "Expected submitted value of type Date for component : "+ 250 getPathToComponent(component)); 251 } 252 } 253 } 254 255 Object value = ((ValueHolder)component).getValue(); 256 257 if (value==null || value instanceof Date) 258 { 259 return (Date) value; 260 } 261 else 262 { 263 throw new IllegalArgumentException ("Expected submitted value of type Date for component : " 264 +getPathToComponent(component)); 265 } 266 } 267 268 269 public static String getStringValue(FacesContext facesContext, 270 UIComponent component) 271 { 272 try 273 { 274 if (!(component instanceof ValueHolder)) 275 { 276 throw new IllegalArgumentException ("Component : "+getPathToComponent(component)+"is not a ValueHolder"); 277 } 278 279 if (component instanceof EditableValueHolder) 280 { 281 Object submittedValue = ((EditableValueHolder)component).getSubmittedValue(); 282 if (submittedValue != null) 283 { 284 if (submittedValue instanceof String ) 285 { 286 return (String )submittedValue; 287 } 288 else 289 { 290 throw new IllegalArgumentException ("Expected submitted value of type String for component : " 291 +getPathToComponent(component)); 292 } 293 } 294 } 295 296 Object value = ((ValueHolder)component).getValue(); 297 298 Converter converter = ((ValueHolder)component).getConverter(); 299 if (converter == null && value != null) 300 { 301 if (value instanceof String ) 302 { 303 return (String ) value; 304 } 305 306 try 307 { 308 converter = facesContext.getApplication().createConverter(value.getClass()); 309 } 310 catch (FacesException e) 311 { 312 log.error("No converter for class " + value.getClass().getName() + " found (component id=" + component.getId() + ")."); 313 } 315 } 316 317 if (converter == null) 318 { 319 if (value == null) 320 { 321 return ""; 322 } 323 else 324 { 325 return value.toString(); 326 } 327 } 328 else 329 { 330 return converter.getAsString(facesContext, component, value); 331 } 332 } 333 catch(PropertyNotFoundException ex) 334 { 335 log.error("Property not found - called by component : "+getPathToComponent(component)); 336 337 throw ex; 338 } 339 } 340 341 346 public static boolean isDefaultAttributeValue(Object value) 347 { 348 if (value == null) 349 { 350 return true; 351 } 352 else if (value instanceof Boolean ) 353 { 354 return ((Boolean )value).booleanValue() == false; 355 } 356 else if (value instanceof Number ) 357 { 358 if (value instanceof Integer ) 359 { 360 return ((Number )value).intValue() == Integer.MIN_VALUE; 361 } 362 else if (value instanceof Double ) 363 { 364 return ((Number )value).doubleValue() == Double.MIN_VALUE; 365 } 366 else if (value instanceof Long ) 367 { 368 return ((Number )value).longValue() == Long.MIN_VALUE; 369 } 370 else if (value instanceof Byte ) 371 { 372 return ((Number )value).byteValue() == Byte.MIN_VALUE; 373 } 374 else if (value instanceof Float ) 375 { 376 return ((Number )value).floatValue() == Float.MIN_VALUE; 377 } 378 else if (value instanceof Short ) 379 { 380 return ((Number )value).shortValue() == Short.MIN_VALUE; 381 } 382 } 383 return false; 384 } 385 386 391 public static Converter findUIOutputConverter(FacesContext facesContext, 392 UIOutput component) 393 throws FacesException 394 { 395 return _SharedRendererUtils.findUIOutputConverter(facesContext, component); 396 } 397 398 399 405 public static Converter findUISelectManyConverter(FacesContext facesContext, 406 UISelectMany component) 407 { 408 Converter converter = component.getConverter(); 409 if (converter != null) return converter; 410 411 ValueBinding vb = component.getValueBinding("value"); 413 if (vb == null) return null; 414 415 Class valueType = vb.getType(facesContext); 416 if (valueType == null) return null; 417 418 if (List.class.isAssignableFrom(valueType)) 419 { 420 return null; 423 } 424 425 if (!valueType.isArray()) 426 { 427 throw new IllegalArgumentException ("ValueBinding for UISelectMany : "+getPathToComponent(component)+" must be of type List or Array"); 428 } 429 430 Class arrayComponentType = valueType.getComponentType(); 431 if (String .class.equals(arrayComponentType)) return null; if (Object .class.equals(arrayComponentType)) return null; 434 try 435 { 436 return facesContext.getApplication().createConverter(arrayComponentType); 437 } 438 catch (FacesException e) 439 { 440 log.error("No Converter for type " + arrayComponentType.getName() + " found", e); 441 return null; 442 } 443 } 444 445 446 public static void checkParamValidity(FacesContext facesContext, UIComponent uiComponent, Class compClass) 447 { 448 if(facesContext == null) 449 throw new NullPointerException ("facesContext may not be null"); 450 if(uiComponent == null) 451 throw new NullPointerException ("uiComponent may not be null"); 452 453 if (compClass != null && !(compClass.isInstance(uiComponent))) 456 { 457 throw new IllegalArgumentException ("uiComponent : "+getPathToComponent(uiComponent)+ 458 " is not instance of "+compClass.getName()+" as it should be"); 459 } 460 } 461 462 463 public static void renderChildren(FacesContext facesContext, UIComponent component) 464 throws IOException 465 { 466 if (component.getChildCount() > 0) 467 { 468 for (Iterator it = component.getChildren().iterator(); it.hasNext(); ) 469 { 470 UIComponent child = (UIComponent)it.next(); 471 renderChild(facesContext, child); 472 } 473 } 474 } 475 476 477 public static void renderChild(FacesContext facesContext, UIComponent child) 478 throws IOException 479 { 480 if (!child.isRendered()) 481 { 482 return; 483 } 484 485 child.encodeBegin(facesContext); 486 if (child.getRendersChildren()) 487 { 488 child.encodeChildren(facesContext); 489 } 490 else 491 { 492 renderChildren(facesContext, child); 493 } 494 child.encodeEnd(facesContext); 495 } 496 497 498 499 503 public static List getSelectItemList(UISelectOne uiSelectOne) 504 { 505 return internalGetSelectItemList(uiSelectOne); 506 } 507 508 512 public static List getSelectItemList(UISelectMany uiSelectMany) 513 { 514 return internalGetSelectItemList(uiSelectMany); 515 } 516 517 private static List internalGetSelectItemList(UIComponent uiComponent) 518 { 519 526 527 List list = new ArrayList(uiComponent.getChildCount()); 528 for (Iterator children = uiComponent.getChildren().iterator(); children.hasNext(); ) 529 { 530 UIComponent child = (UIComponent)children.next(); 531 if (child instanceof UISelectItem) 532 { 533 Object value = ((UISelectItem) child).getValue(); 534 if (value != null) 535 { 536 if (!(value instanceof SelectItem)) 538 { 539 ValueBinding binding = ((UISelectItem) child).getValueBinding("value"); 540 throw new IllegalArgumentException ("Value binding '"+(binding==null?null:binding.getExpressionString()) 541 +"' of UISelectItem : " + getPathToComponent(child) + " does not reference an Object of type SelectItem"); 542 } 543 list.add(value); 544 } 545 else 546 { 547 Object itemValue = ((UISelectItem)child).getItemValue(); 548 String label = ((UISelectItem)child).getItemLabel(); 549 String description = ((UISelectItem)child).getItemDescription(); 550 boolean disabled = ((UISelectItem)child).isItemDisabled(); 551 if (label == null) 552 { 553 list.add(new SelectItem(itemValue, itemValue.toString(), description, disabled)); 554 } 555 else 556 { 557 list.add(new SelectItem(itemValue, label, description, disabled)); 558 } 559 } 560 } 561 else if (child instanceof UISelectItems) 562 { 563 UISelectItems items = ((UISelectItems) child); 564 565 Object value = items.getValue(); 567 if (value instanceof SelectItem) 568 { 569 list.add(value); 570 } 571 else if (value instanceof SelectItem[]) 572 { 573 for (int i = 0; i < ((SelectItem[])value).length; i++) 574 { 575 list.add(((SelectItem[])value)[i]); 576 } 577 } 578 else if (value instanceof Collection) 579 { 580 for (Iterator it = ((Collection)value).iterator(); it.hasNext();) 581 { 582 Object item = it.next(); 583 if (!(item instanceof SelectItem)) 584 { 585 ValueBinding binding = items.getValueBinding("value"); 586 throw new IllegalArgumentException ("Collection referenced by UISelectItems with binding '"+ 587 binding.getExpressionString()+"' and Component-Path : " + getPathToComponent(child) + " does not contain Objects of type SelectItem"); 588 } 589 list.add(item); 590 } 591 } 592 else if (value instanceof Map) 593 { 594 for (Iterator it = ((Map)value).entrySet().iterator(); it.hasNext();) 595 { 596 Map.Entry entry = (Map.Entry)it.next(); 597 list.add(new SelectItem(entry.getValue(), entry.getKey().toString())); 598 } 599 } 600 else 601 { 602 ValueBinding binding = items.getValueBinding("value"); 603 604 throw new IllegalArgumentException ("Value binding '"+ 605 (binding==null?null:binding.getExpressionString())+"'of UISelectItems with component-path " + getPathToComponent(child) + " does not reference an Object of type SelectItem, SelectItem[], Collection or Map but of type : "+((value==null)?null:value.getClass().getName())); 606 } 607 } 608 else 609 { 610 } 613 } 614 615 return list; 616 } 617 618 619 628 public static Set getSubmittedValuesAsSet(FacesContext context, UIComponent component, Converter converter, UISelectMany uiSelectMany) 629 { 630 Object submittedValues = uiSelectMany.getSubmittedValue(); 631 if (submittedValues == null) 632 { 633 return null; 634 } 635 636 return internalSubmittedOrSelectedValuesAsSet(context, component, converter, uiSelectMany, submittedValues); 637 } 638 639 640 649 public static Set getSelectedValuesAsSet(FacesContext context, UIComponent component, Converter converter, UISelectMany uiSelectMany) 650 { 651 Object selectedValues = uiSelectMany.getValue(); 652 653 return internalSubmittedOrSelectedValuesAsSet(context, component, converter, uiSelectMany, selectedValues); 654 } 655 656 657 662 public static String getConvertedStringValue(FacesContext context, 663 UIComponent component, Converter converter, Object value) { 664 if (converter == null) { 665 if (value == null) { 666 return ""; 667 } else if (value instanceof String ) { 668 return (String ) value; 669 } else { 670 throw new IllegalArgumentException ( 671 "Value is no String and component " 672 + component.getClientId(context) 673 + " does not have a Converter"); 674 } 675 } 676 677 return converter.getAsString(context, component, value); 678 } 679 680 681 686 public static String getConvertedStringValue(FacesContext context, 687 UIComponent component, Converter converter, SelectItem selectItem) { 688 return getConvertedStringValue(context, component, converter, selectItem.getValue()); 689 } 690 691 692 private static Set internalSubmittedOrSelectedValuesAsSet(FacesContext context, 693 UIComponent component, Converter converter, UISelectMany uiSelectMany, 694 Object values) 695 { 696 if (values == null || values == EMPTY_STRING) 697 { 698 return Collections.EMPTY_SET; 699 } 700 else if (values instanceof Object []) 701 { 702 Object [] ar = (Object [])values; 704 if (ar.length == 0) 705 { 706 return Collections.EMPTY_SET; 707 } 708 709 HashSet set = new HashSet(HashMapUtils.calcCapacity(ar.length)); 710 for (int i = 0; i < ar.length; i++) 711 { 712 set.add( getConvertedStringValue(context, component, converter, ar[i]) ); 713 } 714 return set; 715 } 716 else if (values.getClass().isArray()) 717 { 718 int len = Array.getLength(values); 720 HashSet set = new HashSet(HashMapUtils.calcCapacity(len)); 721 for (int i = 0; i < len; i++) 722 { 723 set.add( getConvertedStringValue(context, component, converter, Array.get(values,i)) ); 724 } 725 return set; 726 } 727 else if (values instanceof List) 728 { 729 List lst = (List)values; 730 if (lst.size() == 0) 731 { 732 return Collections.EMPTY_SET; 733 } 734 else 735 { 736 HashSet set = new HashSet(HashMapUtils.calcCapacity(lst.size())); 737 for(Iterator i =lst.iterator(); i.hasNext(); ) 738 set.add( getConvertedStringValue(context, component, converter, i.next()) ); 739 740 return set; 741 } 742 } 743 else 744 { 745 throw new IllegalArgumentException ("Value of UISelectMany component with path : " + getPathToComponent(uiSelectMany) + " is not of type Array or List"); 746 } 747 } 748 749 750 751 public static Object getConvertedUIOutputValue(FacesContext facesContext, 752 UIOutput output, 753 Object submittedValue) 754 throws ConverterException 755 { 756 if (submittedValue!=null && !(submittedValue instanceof String )) 757 { 758 throw new IllegalArgumentException ("Submitted value of type String for component : "+ 759 getPathToComponent(output)+"expected"); 760 } 761 762 Converter converter; 763 try 764 { 765 converter = findUIOutputConverter(facesContext, output); 766 } 767 catch (FacesException e) 768 { 769 throw new ConverterException(e); 770 } 771 772 if (converter == null) 773 { 774 return submittedValue; 776 } 777 else 778 { 779 return converter.getAsObject(facesContext, output, (String )submittedValue); 781 } 782 } 783 784 785 public static Object getConvertedUISelectManyValue(FacesContext facesContext, 786 UISelectMany selectMany, 787 Object submittedValue) 788 throws ConverterException 789 { 790 if (submittedValue == null) 791 { 792 return null; 793 } 794 else 795 { 796 if (!(submittedValue instanceof String [])) 797 { 798 throw new ConverterException("Submitted value of type String[] for component : " 799 + getPathToComponent(selectMany) + "expected"); 800 } 801 } 802 return _SharedRendererUtils.getConvertedUISelectManyValue(facesContext, 803 selectMany, 804 (String [])submittedValue); 805 } 806 807 808 public static boolean getBooleanAttribute(UIComponent component, 809 String attrName, 810 boolean defaultValue) 811 { 812 Boolean b = (Boolean )component.getAttributes().get(attrName); 813 return b != null ? b.booleanValue() : defaultValue; 814 } 815 816 public static int getIntegerAttribute(UIComponent component, 817 String attrName, 818 int defaultValue) 819 { 820 Integer i = (Integer )component.getAttributes().get(attrName); 821 return i != null ? i.intValue() : defaultValue; 822 } 823 824 public static UIForm findParentForm(UIComponentBase comp) 825 { 826 UIComponent parent = comp.getParent(); 827 while (parent != null) 828 { 829 if (parent instanceof UIForm) 830 { 831 return (UIForm)parent; 832 } 833 parent = parent.getParent(); 834 } 835 return null; 836 } 837 838 public static void copyHtmlInputTextAttributes(HtmlInputText src, HtmlInputText dest) 839 { 840 dest.setId(src.getId()); 841 dest.setImmediate(src.isImmediate()); 842 dest.setTransient(src.isTransient()); 843 dest.setAccesskey(src.getAccesskey()); 844 dest.setAlt(src.getAlt()); 845 dest.setConverter(src.getConverter()); 846 dest.setDir(src.getDir()); 847 dest.setDisabled(src.isDisabled()); 848 dest.setLang(src.getLang()); 849 dest.setLocalValueSet(src.isLocalValueSet()); 850 dest.setMaxlength(src.getMaxlength()); 851 dest.setOnblur(src.getOnblur()); 852 dest.setOnchange(src.getOnchange()); 853 dest.setOnclick(src.getOnclick()); 854 dest.setOndblclick(src.getOndblclick()); 855 dest.setOnfocus(src.getOnfocus()); 856 dest.setOnkeydown(src.getOnkeydown()); 857 dest.setOnkeypress(src.getOnkeypress()); 858 dest.setOnkeyup(src.getOnkeyup()); 859 dest.setOnmousedown(src.getOnmousedown()); 860 dest.setOnmousemove(src.getOnmousemove()); 861 dest.setOnmouseout(src.getOnmouseout()); 862 dest.setOnmouseover(src.getOnmouseover()); 863 dest.setOnmouseup(src.getOnmouseup()); 864 dest.setOnselect(src.getOnselect()); 865 dest.setReadonly(src.isReadonly()); 866 dest.setRendered(src.isRendered()); 867 dest.setRequired(src.isRequired()); 868 dest.setSize(src.getSize()); 869 dest.setStyle(src.getStyle()); 870 dest.setStyleClass(src.getStyleClass()); 871 dest.setTabindex(src.getTabindex()); 872 dest.setTitle(src.getTitle()); 873 dest.setValidator(src.getValidator()); 874 } 875 } 876 | Popular Tags |