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.JavascriptContext; 39 import com.icesoft.faces.util.Debug; 40 import org.w3c.dom.Element ; 41 42 import javax.faces.FactoryFinder; 43 import javax.faces.application.Application; 44 import javax.faces.application.ApplicationFactory; 45 import javax.faces.application.FacesMessage; 46 import javax.faces.application.FacesMessage.Severity; 47 import javax.faces.component.NamingContainer; 48 import javax.faces.component.UICommand; 49 import javax.faces.component.UIComponent; 50 import javax.faces.component.UIForm; 51 import javax.faces.component.UIInput; 52 import javax.faces.component.UIMessage; 53 import javax.faces.component.UIParameter; 54 import javax.faces.component.ValueHolder; 55 import javax.faces.component.html.HtmlMessage; 56 import javax.faces.component.html.HtmlMessages; 57 import javax.faces.context.FacesContext; 58 import javax.faces.convert.Converter; 59 import javax.faces.convert.ConverterException; 60 import javax.faces.render.Renderer; 61 import java.beans.Beans ; 62 import java.io.IOException ; 63 import java.util.HashMap ; 64 import java.util.Iterator ; 65 import java.util.Map ; 66 import java.util.Set ; 67 68 public abstract class DomBasicRenderer extends Renderer { 69 70 71 public final String ICESUBMITPARTIAL = 73 "iceSubmitPartial(form, this, event);"; 74 public final String ICESUBMIT = "iceSubmit(form,this,event);"; 76 77 public static final String WEB_UIFORM = "com.sun.rave.web.ui.Form"; 79 public static final String UIFORM = "javax.faces.form"; 80 public static final String WEB_UIJSFFORM = "com.sun.webui.jsf.Form"; 81 82 public void decode(FacesContext facesContext, UIComponent uiComponent) { 83 CurrentStyle.decode(facesContext, uiComponent); 84 validateParameters(facesContext, uiComponent, null); 85 if (!(uiComponent instanceof UIInput)) { 87 return; 88 } 89 if (isStatic(uiComponent)) { 91 return; 92 } 93 String clientId = uiComponent.getClientId(facesContext); 95 Debug.assertTrue(clientId != null, 96 "Client id is not defined for decoding"); 97 Map requestMap = 98 facesContext.getExternalContext().getRequestParameterMap(); 99 if (requestMap.containsKey(clientId)) { 100 String decodedValue = (String ) requestMap.get(clientId); 101 setSubmittedValue(uiComponent, decodedValue); 102 } 103 } 104 105 112 public void setSubmittedValue(UIComponent uiComponent, Object value) { 113 } 114 115 122 public void encodeEnd(FacesContext facesContext, UIComponent uiComponent) 123 throws IOException { 124 validateParameters(facesContext, uiComponent, null); 125 renderEnd(facesContext, uiComponent, 126 getValue(facesContext, uiComponent)); 127 DOMContext domContext = 128 DOMContext.attachDOMContext(facesContext, uiComponent); 129 130 domContext.stepOver(); 131 JavascriptContext.fireEffect(uiComponent, facesContext); 132 } 133 134 145 String getValue(FacesContext facesContext, UIComponent uiComponent) { 146 if (uiComponent instanceof UIInput) { 148 Object submittedValue = ((UIInput) uiComponent).getSubmittedValue(); 149 if (submittedValue != null && submittedValue instanceof String ) { 150 return (String ) submittedValue; 151 } 152 } 153 return formatComponentValue(facesContext, uiComponent, 154 getValue(uiComponent)); 155 } 156 157 Object getValue(UIComponent uiComponent) { 158 return null; 159 } 160 161 162 173 protected void renderEnd(FacesContext facesContext, 174 UIComponent uiComponent, String currentValue) 175 throws IOException { 176 } 177 178 189 String formatComponentValue(FacesContext facesContext, 190 UIComponent uiComponent, Object currentValue) 191 throws ConverterException { 192 193 if (currentValue == null) { 194 return null; 195 } 196 197 if (!(uiComponent instanceof ValueHolder)) { 198 return currentValue.toString(); 199 } 200 201 Converter converter = ((ValueHolder) uiComponent).getConverter(); 203 204 if (converter == null) { 207 if (currentValue instanceof String ) { 208 return (String ) currentValue; 209 } 210 converter = getConverterForClass(currentValue.getClass()); 211 } 212 213 if (converter == null) { 214 return currentValue.toString(); 215 } else { 216 if (currentValue instanceof String ) { 219 return (String ) currentValue; 220 } 221 return converter 222 .getAsString(facesContext, uiComponent, currentValue); 223 } 224 } 225 226 235 public static UIComponent findForComponent(FacesContext facesContext, 236 UIComponent uiComponent) { 237 238 String forComponentId = null; 239 if (uiComponent instanceof UIMessage) { 240 forComponentId = ((UIMessage) uiComponent).getFor(); 241 } else { 242 forComponentId = 243 (String ) uiComponent.getAttributes().get(HTML.FOR_ATTR); 244 } 245 246 if (forComponentId == null) { 247 return null; 248 } 249 if (forComponentId.length() == 0) { 250 return null; 251 } 252 253 UIComponent forComponent = uiComponent.findComponent(forComponentId); 257 258 if (forComponent == null) { 262 UIComponent nextParent = uiComponent; 263 while (true) { 264 nextParent = nextParent.getParent(); 265 while (nextParent != null && 269 !(nextParent instanceof NamingContainer)) { 270 nextParent = nextParent.getParent(); 271 } 272 if (nextParent == null) { 273 break; 274 } else { 275 forComponent = nextParent.findComponent(forComponentId); 276 } 277 if (forComponent != null) { 278 break; 279 } 280 } 281 } 282 283 if (forComponent == null) { 288 forComponent = searchDownwardsForChildComponentWithId( 289 facesContext.getViewRoot(), 290 forComponentId); 291 } 292 return forComponent; 293 } 294 295 302 public static String [] getExcludesArray(Set excludes) { 303 String [] excludesArray = new String [excludes.size()]; 304 excludes.toArray(excludesArray); 305 return excludesArray; 306 } 307 308 private static UIComponent searchDownwardsForChildComponentWithId( 309 UIComponent parent, 310 String searchChildId) { 311 UIComponent foundChild = null; 312 Iterator children = parent.getChildren().iterator(); 313 UIComponent nextChild = null; 314 while (children.hasNext()) { 315 nextChild = (UIComponent) children.next(); 316 if (nextChild instanceof NamingContainer) { 317 foundChild = nextChild.findComponent(searchChildId); 318 } 319 if (foundChild == null) { 320 searchDownwardsForChildComponentWithId(nextChild, 321 searchChildId); 322 } 323 if (foundChild != null) { 324 break; 325 } 326 } 327 return foundChild; 328 } 329 330 337 public static void encodeParentAndChildren(FacesContext facesContext, 338 UIComponent parent) 339 throws IOException { 340 parent.encodeBegin(facesContext); 341 if (parent.getRendersChildren()) { 342 parent.encodeChildren(facesContext); 343 } else { 344 Iterator children = parent.getChildren().iterator(); 345 while (children.hasNext()) { 346 UIComponent nextChild = (UIComponent) children.next(); 347 if (nextChild.isRendered()) { 348 encodeParentAndChildren(facesContext, nextChild); 349 } 350 } 351 } 352 parent.encodeEnd(facesContext); 353 } 354 355 356 protected static UIComponent getFacetByName(UIComponent uiComponent, 357 String name) { 358 UIComponent facet = uiComponent.getFacet(name); 359 if (facet == null) { 360 return null; 361 } 362 if (!facet.isRendered()) { 363 return null; 364 } 365 return facet; 366 } 367 368 static boolean idNotNull(UIComponent uiComponent) { 369 return (uiComponent.getId() != null); 370 } 371 372 380 public static void setRootElementId(FacesContext facesContext, 381 Element rootElement, 382 UIComponent uiComponent) { 383 if (idNotNull(uiComponent)) { 384 rootElement 385 .setAttribute("id", uiComponent.getClientId(facesContext)); 386 } 387 } 388 389 401 public static void renderAttribute(UIComponent uiComponent, 402 Element targetElement, 403 String attrNameInComponent, 404 String attrNameInDom) { 405 Object attrValue = uiComponent.getAttributes().get(attrNameInComponent); 406 if (attrValue != null && !attrValue.equals("")) { 407 if (attrValue.toString().equalsIgnoreCase("true") || 408 attrValue.toString().equalsIgnoreCase("false")) { 409 boolean trueValue = 410 new Boolean (attrValue.toString()).booleanValue(); 411 if (!trueValue) { 412 targetElement.removeAttribute(attrNameInDom.toString()); 413 return; 414 } 415 } 416 targetElement.setAttribute(attrNameInDom.toString(), 417 attrValue.toString()); 418 } 419 } 420 421 422 430 static Map getParameterMap(UIComponent uiComponent) { 431 Map parameterMap = new HashMap (); 432 Iterator children = uiComponent.getChildren().iterator(); 433 while (children.hasNext()) { 434 UIComponent nextChild = (UIComponent) children.next(); 435 if (nextChild instanceof UIParameter) { 436 UIParameter uiParam = (UIParameter) nextChild; 437 parameterMap.put(uiParam.getName(), uiParam.getValue()); 438 } 439 } 440 return parameterMap; 441 } 442 443 462 public void validateParameters(FacesContext facesContext, 463 UIComponent uiComponent, 464 Class validComponentType) { 465 466 if (facesContext == null) { 467 throw new NullPointerException ( 468 "Invalid Parameter - FacesContext instance must not be null"); 469 } 470 if (uiComponent == null) { 471 throw new NullPointerException ( 472 "Invalid Parameter - UIComponent instance must not be null"); 473 } 474 if (!Beans.isDesignTime() && validComponentType != null && 475 !(validComponentType.isInstance(uiComponent))) { 476 throw new IllegalArgumentException ( 477 "Invalid Parameter - UIComponent class should be [" 478 + validComponentType + 479 "] but it is an instance of [" 480 + uiComponent.getClass() + "]"); 481 } 482 483 if ((uiComponent instanceof UIInput) || (uiComponent instanceof UICommand)) { 484 if (findForm(uiComponent) == null) { 485 throw new NullPointerException ("Missing Form - the UIComponent of type [" 486 + uiComponent.getClass() + "] requires a containing form." ); 487 } 488 } 489 } 490 491 497 public static boolean isStatic(UIComponent uiComponent) { 498 boolean isStatic = false; 501 Object disabled = uiComponent.getAttributes().get("disabled"); 502 Object readonly = uiComponent.getAttributes().get("readonly"); 503 if (disabled != null) { 504 if (disabled instanceof Boolean ) { 505 isStatic = ((Boolean ) disabled).booleanValue(); 506 } else if (disabled instanceof String ) { 507 isStatic = ((String ) disabled).equalsIgnoreCase("true"); 508 } 509 } 510 if (isStatic) { 511 return isStatic; 512 } 513 if (readonly != null) { 514 if (readonly instanceof Boolean ) { 515 return ((Boolean ) readonly).booleanValue(); 516 } 517 if (readonly instanceof String ) { 518 return ((String ) readonly).equalsIgnoreCase("true"); 519 } 520 } 521 return isStatic; 522 } 523 524 532 public static UIComponent findNamingContainer(UIComponent uiComponent) { 533 UIComponent parent = uiComponent.getParent(); 534 while (parent != null) { 535 if (parent instanceof NamingContainer) { 536 break; 537 } 538 parent = parent.getParent(); 539 } 540 return parent; 541 } 542 543 551 public static UIComponent findForm(UIComponent uiComponent) { 552 UIComponent parent = uiComponent.getParent(); 553 while (parent != null && !(parent instanceof UIForm)) { 554 parent = findNamingContainer(parent); 555 } 556 UIComponent form = null; 557 if (parent != null && 559 (parent.getFamily().equalsIgnoreCase(WEB_UIFORM) || 560 parent.getFamily().equalsIgnoreCase(UIFORM) || 561 parent.getFamily().equalsIgnoreCase(WEB_UIJSFFORM))) { 562 form = (UIComponent) parent; 563 } 564 565 if (form == null && Beans.isDesignTime()) { 566 form = uiComponent.getParent(); 567 } 568 return form; 569 } 570 571 586 String fabricateClientId(UIComponent uiComponent, 587 FacesContext facesContext, String uiComponentId) { 588 UIComponent parentNamingContainer = findNamingContainer(uiComponent); 589 String parentNamingContainerClientId = null; 590 if (parentNamingContainer == null) { 591 return uiComponentId; 592 } else { 593 parentNamingContainerClientId = 594 parentNamingContainer.getClientId(facesContext); 595 } 596 return parentNamingContainerClientId 597 + NamingContainer.SEPARATOR_CHAR 598 + uiComponentId; 599 } 600 601 protected String [] getColumnStyleClasses(UIComponent uiComponent) { 602 return getStyleClasses(uiComponent, "columnClasses"); 603 } 604 605 614 public String [] getRowStyleClasses(UIComponent uiComponent) { 615 return getStyleClasses(uiComponent, "rowClasses"); 616 } 617 618 public String [] getStyleClasses(UIComponent uiComponent, 619 String styleClassAttributeName) { 620 String allStyleClasses = (String ) uiComponent.getAttributes() 621 .get(styleClassAttributeName); 622 if (allStyleClasses == null) { 623 return (new String [0]); 624 } 625 626 String separator = ","; 627 if (allStyleClasses.indexOf(separator) <= 0) { 628 separator = " "; 629 } 630 String [] styleClassesArray = allStyleClasses.trim().split(separator); 631 int numberOfStyles = styleClassesArray.length; 632 for (int i = 0; i < numberOfStyles; i++) { 633 styleClassesArray[i] = styleClassesArray[i].trim(); 634 } 635 return styleClassesArray; 636 } 637 638 646 static String [] getStyleAndStyleClass(UIComponent uiComponent, 647 FacesMessage facesMessage) { 648 String severityStyle = null; 650 String severityStyleClass = null; 651 Severity messageSeverity = facesMessage.getSeverity(); 652 if (messageSeverity == FacesMessage.SEVERITY_INFO) { 653 severityStyle = 654 (String ) uiComponent.getAttributes().get("infoStyle"); 655 if (uiComponent instanceof HtmlMessage) { 656 severityStyleClass = ((HtmlMessage) uiComponent).getInfoClass(); 657 } else if (uiComponent instanceof HtmlMessages) { 658 severityStyleClass = 659 ((HtmlMessages) uiComponent).getInfoClass(); 660 } 661 } else if (messageSeverity == FacesMessage.SEVERITY_WARN) { 662 severityStyle = 663 (String ) uiComponent.getAttributes().get("warnStyle"); 664 if (uiComponent instanceof HtmlMessage) { 665 severityStyleClass = ((HtmlMessage) uiComponent).getWarnClass(); 666 } else if (uiComponent instanceof HtmlMessages) { 667 severityStyleClass = 668 ((HtmlMessages) uiComponent).getWarnClass(); 669 } 670 } else if (messageSeverity == FacesMessage.SEVERITY_ERROR) { 671 severityStyle = 672 (String ) uiComponent.getAttributes().get("errorStyle"); 673 if (uiComponent instanceof HtmlMessage) { 674 severityStyleClass = 675 ((HtmlMessage) uiComponent).getErrorClass(); 676 } else if (uiComponent instanceof HtmlMessages) { 677 severityStyleClass = 678 ((HtmlMessages) uiComponent).getErrorClass(); 679 } 680 } else if (messageSeverity == FacesMessage.SEVERITY_FATAL) { 681 severityStyle = 682 (String ) uiComponent.getAttributes().get("fatalStyle"); 683 if (uiComponent instanceof HtmlMessage) { 684 severityStyleClass = 685 ((HtmlMessage) uiComponent).getFatalClass(); 686 } else if (uiComponent instanceof HtmlMessages) { 687 severityStyleClass = 688 ((HtmlMessages) uiComponent).getFatalClass(); 689 } 690 } 691 692 String style = null; 693 if (severityStyle != null) { 694 style = severityStyle; 695 } else { 696 style = (String ) uiComponent.getAttributes().get("style"); 697 } 698 String styleClass = null; 699 if (severityStyleClass != null) { 700 styleClass = severityStyleClass; 701 } else { 702 if (uiComponent instanceof HtmlMessage) { 703 styleClass = ((HtmlMessage) uiComponent).getStyleClass(); 704 } else if (uiComponent instanceof HtmlMessages) { 705 styleClass = ((HtmlMessages) uiComponent).getStyleClass(); 706 } 707 } 708 return new String []{style, styleClass}; 709 } 710 711 715 String [] getSummaryAndDetail(FacesMessage facesMessage) { 716 String summary = facesMessage.getSummary(); 717 if (summary == null) { 718 summary = ""; 719 } 720 String detail = facesMessage.getDetail(); 721 if (detail == null) { 722 detail = ""; 723 } 724 return new String []{summary, detail}; 725 } 726 727 731 boolean getToolTipAttribute(UIComponent uiComponent) { 732 boolean tooltip = false; 733 Object tooltipAttribute = uiComponent.getAttributes().get("tooltip"); 734 if (tooltipAttribute instanceof Boolean 735 && ((Boolean ) tooltipAttribute).booleanValue()) { 736 tooltip = true; 737 } 738 return tooltip; 739 } 740 741 745 Converter getConverterForClass(Class converterClass) { 746 if (converterClass == null) { 747 return null; 748 } 749 try { 750 ApplicationFactory aFactory = 751 (ApplicationFactory) FactoryFinder.getFactory( 752 FactoryFinder.APPLICATION_FACTORY); 753 Application application = aFactory.getApplication(); 754 return (application.createConverter(converterClass)); 755 } catch (Exception e) { 756 return (null); 757 } 758 } 759 760 public static String getResourceURL(FacesContext context, String path) { 761 return context.getApplication().getViewHandler() 762 .getResourceURL(context, path); 763 } 764 } 765 | Popular Tags |