| 1 33 48 package com.icesoft.faces.component.util; 49 50 import com.icesoft.faces.renderkit.dom_html_basic.FormRenderer; 51 import org.apache.commons.logging.Log; 52 import org.apache.commons.logging.LogFactory; 53 54 import javax.faces.FacesException; 55 import javax.faces.component.EditableValueHolder; 56 import javax.faces.component.NamingContainer; 57 import javax.faces.component.UIComponent; 58 import javax.faces.component.UIForm; 59 import javax.faces.component.UIViewRoot; 60 import javax.faces.component.ValueHolder; 61 import javax.faces.component.html.HtmlInputText; 62 import javax.faces.context.FacesContext; 63 import javax.faces.convert.Converter; 64 import javax.faces.el.PropertyNotFoundException; 65 import java.io.IOException ; 66 import java.util.Date ; 67 import java.util.HashSet ; 68 import java.util.Iterator ; 69 import java.util.Map ; 70 import java.util.Set ; 71 import java.util.StringTokenizer ; 72 73 public class CustomComponentUtils { 74 75 private static final String HIDDEN_COMMAND_INPUTS_SET_ATTR 76 = FormRenderer.class.getName() + ".HIDDEN_COMMAND_INPUTS_SET"; 77 public static final String HIDDEN_COMMANDLINK_FIELD_NAME = "_link_hidden_"; 78 public static final String FOOTER_CLASS_ATTR = "footerClass"; 79 public static final String HEADER_CLASS_ATTR = "headerClass"; 80 public static final String [] EMPTY_STRING_ARRAY = new String [0]; 81 public static final String EMPTY_STRING = new String (); 82 private static final String HIDDEN_TREE_NAV_FIELD_NAME = "_idtn"; 83 private static final String HIDDEN_TREE_ACTION_FIELD_NAME = "_idta"; 84 private static final Log log = 85 LogFactory.getLog(CustomComponentUtils.class); 86 87 public static void restoreAncestorState(UIComponent uiComponent) { 88 uiComponent.setId(uiComponent.getId()); 89 if ((uiComponent = uiComponent.getParent()) != null) { 90 restoreAncestorState(uiComponent); 91 } 92 } 93 94 public static void restoreDescendentState(UIComponent uiComponent) { 95 uiComponent.setId(uiComponent.getId()); 96 Iterator it = uiComponent.getFacetsAndChildren(); 97 while (it.hasNext()) { 98 UIComponent next = (UIComponent) it.next(); 99 restoreDescendentState(next); 100 } 101 } 102 103 104 public static String getStringValue(FacesContext facesContext, 105 UIComponent component) { 106 try { 107 if (!(component instanceof ValueHolder)) { 108 throw new IllegalArgumentException ("Component : " + 109 getPathToComponent( 110 component) + 111 "is not a ValueHolder"); 112 } 113 114 if (component instanceof EditableValueHolder) { 115 Object submittedValue = 116 ((EditableValueHolder) component).getSubmittedValue(); 117 if (submittedValue != null) { 118 if (submittedValue instanceof String ) { 119 return (String ) submittedValue; 120 } else { 121 throw new IllegalArgumentException ( 122 "Expected submitted value of type String for component : " 123 + getPathToComponent(component)); 124 } 125 } 126 } 127 128 Object value = ((ValueHolder) component).getValue(); 129 130 Converter converter = ((ValueHolder) component).getConverter(); 131 if (converter == null && value != null) { 132 if (value instanceof String ) { 133 return (String ) value; 134 } 135 136 try { 137 converter = facesContext.getApplication() 138 .createConverter(value.getClass()); 139 } 140 catch (FacesException e) { 141 142 log.error("No converter for class " + 143 value.getClass().getName() + 144 " found (component id=" + component.getId() + 145 ").", e); 146 } 148 } 149 150 if (converter == null) { 151 if (value == null) { 152 return ""; 153 } else { 154 return value.toString(); 155 } 156 } else { 157 return converter.getAsString(facesContext, component, value); 158 } 159 } 160 catch (PropertyNotFoundException ex) { 161 log.error("Property not found - called by component : " + 162 getPathToComponent(component), ex); 163 throw ex; 164 } 165 } 166 167 168 public static String getPathToComponent(UIComponent component) { 169 StringBuffer buf = new StringBuffer (); 170 171 if (component == null) { 172 buf.append("{Component-Path : "); 173 buf.append("[null]}"); 174 return buf.toString(); 175 } 176 177 getPathToComponent(component, buf); 178 179 buf.insert(0, "{Component-Path : "); 180 buf.append("}"); 181 182 return buf.toString(); 183 } 184 185 private static void getPathToComponent(UIComponent component, 186 StringBuffer buf) { 187 if (component == null) { 188 return; 189 } 190 191 StringBuffer intBuf = new StringBuffer (); 192 193 intBuf.append("[Class: "); 194 intBuf.append(component.getClass().getName()); 195 if (component instanceof UIViewRoot) { 196 intBuf.append(",ViewId: "); 197 intBuf.append(((UIViewRoot) component).getViewId()); 198 } else { 199 intBuf.append(",Id: "); 200 intBuf.append(component.getId()); 201 } 202 intBuf.append("]"); 203 204 buf.insert(0, intBuf); 205 206 if (component != null) { 207 getPathToComponent(component.getParent(), buf); 208 } 209 } 210 211 212 public static void addHiddenCommandParameter(UIComponent form, 213 String paramName) { 214 Set set = 215 (Set ) form.getAttributes().get(HIDDEN_COMMAND_INPUTS_SET_ATTR); 216 if (set == null) { 217 set = new HashSet (); 218 form.getAttributes().put(HIDDEN_COMMAND_INPUTS_SET_ATTR, set); 219 } 220 set.add(paramName); 221 } 222 223 public static String getHiddenCommandLinkFieldName(String formName) { 224 return formName + NamingContainer.SEPARATOR_CHAR 225 + HIDDEN_COMMANDLINK_FIELD_NAME; 226 } 227 228 public static String getHiddenTreeExpandFieldName(String componentId, 229 String formName) { 230 return formName + NamingContainer.SEPARATOR_CHAR + componentId 231 + HIDDEN_TREE_NAV_FIELD_NAME; 232 } 233 234 public static String getHiddenTreeActionFieldName(String componentId, 235 String formName) { 236 return formName + NamingContainer.SEPARATOR_CHAR + componentId 237 + HIDDEN_TREE_ACTION_FIELD_NAME; 238 } 239 240 241 public static String getFormName(UIComponent component, 242 FacesContext context) { 243 UIComponent parent = component.getParent(); 245 while (parent != null && !(parent instanceof UIForm)) { 246 parent = parent.getParent(); 247 } 248 249 if (parent != null) { 250 return ((UIForm) parent).getClientId(context); 252 } 253 return "this"; 254 } 255 256 265 public static String [] splitShortString(String str, char separator) { 266 int len; 267 if (str == null || (len = str.length()) == 0) { 268 return EMPTY_STRING_ARRAY; 269 } 270 271 int lastTokenIndex = 0; 272 273 for (int pos = str.indexOf(separator); 276 pos >= 0; pos = str.indexOf(separator, pos + 1)) { 277 lastTokenIndex++; 278 } 279 280 String [] list = new String [lastTokenIndex + 1]; 282 283 int oldPos = 0; 284 285 for ( 287 int pos = str.indexOf(separator), i = 0; pos >= 0; 288 pos = str.indexOf(separator, (oldPos = (pos + 1)))) { 289 list[i++] = substring(str, oldPos, pos); 290 } 291 292 list[lastTokenIndex] = substring(str, oldPos, len); 293 294 return list; 295 } 296 297 public static String substring(String str, int begin, int end) { 298 if (begin == end) { 299 return ""; 300 } 301 302 return str.substring(begin, end); 303 } 304 305 public static String [] trim(String [] strings) { 306 if (strings == null) { 307 return null; 308 } 309 310 for (int i = 0, len = strings.length; i < len; i++) { 311 strings[i] = strings[i].trim(); 312 } 313 314 return strings; 315 } 316 317 public static void renderChildren(FacesContext facesContext, 318 UIComponent component) 319 throws IOException { 320 if (component.getChildCount() > 0) { 321 for (Iterator it = component.getChildren().iterator(); 322 it.hasNext();) { 323 UIComponent child = (UIComponent) it.next(); 324 renderChild(facesContext, child); 325 } 326 } 327 } 328 329 330 public static void renderChild(FacesContext facesContext, UIComponent child) 331 throws IOException { 332 if (!child.isRendered()) { 333 return; 334 } 335 336 child.encodeBegin(facesContext); 337 if (child.getRendersChildren()) { 338 child.encodeChildren(facesContext); 339 } else { 340 renderChildren(facesContext, child); 341 } 342 child.encodeEnd(facesContext); 343 } 344 345 353 public static boolean isVisibleOnUserRole(UIComponent component) { 354 String userRole; 355 if (component instanceof UserRoleAware) { 356 userRole = ((UserRoleAware) component).getVisibleOnUserRole(); 357 } else { 358 userRole = (String ) component.getAttributes() 359 .get(UserRoleAware.VISIBLE_ON_USER_ROLE_ATTR); 360 } 361 362 if (userRole == null) { 363 return true; 365 } 366 367 FacesContext facesContext = FacesContext.getCurrentInstance(); 368 StringTokenizer st = new StringTokenizer (userRole, ","); 369 while (st.hasMoreTokens()) { 370 if (facesContext.getExternalContext() 371 .isUserInRole(st.nextToken().trim())) { 372 return true; 373 } 374 } 375 return false; 376 } 377 378 379 387 public static boolean isEnabledOnUserRole(UIComponent component) { 388 String userRole; 389 if (component instanceof UserRoleAware) { 390 userRole = ((UserRoleAware) component).getEnabledOnUserRole(); 391 } else { 392 userRole = (String ) component.getAttributes() 393 .get(UserRoleAware.ENABLED_ON_USER_ROLE_ATTR); 394 } 395 396 if (userRole == null) { 397 return true; 399 } 400 401 FacesContext facesContext = FacesContext.getCurrentInstance(); 402 StringTokenizer st = new StringTokenizer (userRole, ","); 403 while (st.hasMoreTokens()) { 404 if (facesContext.getExternalContext() 405 .isUserInRole(st.nextToken().trim())) { 406 return true; 407 } 408 } 409 return false; 410 } 411 412 413 public static Object newInstance(Class clazz) 414 throws FacesException { 415 try { 416 return clazz.newInstance(); 417 } 418 catch (NoClassDefFoundError e) { 419 throw new FacesException(e); 420 } 421 catch (InstantiationException e) { 422 throw new FacesException(e); 423 } 424 catch (IllegalAccessException e) { 425 throw new FacesException(e); 426 } 427 } 428 429 437 public static Class simpleClassForName(String type) { 438 try { 439 return classForName(type); 440 } 441 catch (ClassNotFoundException e) { 442 throw new FacesException(e); 443 } 444 } 445 446 public static Object newInstance(String type) 447 throws FacesException { 448 if (type == null) { 449 return null; 450 } 451 return newInstance(simpleClassForName(type)); 452 } 453 454 464 public static Class classForName(String type) 465 throws ClassNotFoundException { 466 if (type == null) { 467 throw new NullPointerException ("type"); 468 } 469 try { 470 return Class.forName(type, 472 false, Thread.currentThread().getContextClassLoader()); 474 } 475 catch (ClassNotFoundException ignore) { 476 return Class.forName(type, 478 false, CustomComponentUtils.class.getClassLoader()); 480 } 481 } 482 483 public static boolean isDisabledOrReadOnly(UIComponent component) { 484 return isTrue(component.getAttributes().get("disabled")) || 485 isTrue(component.getAttributes().get("readOnly")); 486 } 487 488 private static boolean isTrue(Object obj) { 489 if (!(obj instanceof Boolean )) { 490 return false; 491 } 492 493 return ((Boolean ) obj).booleanValue(); 494 } 495 496 public static void decodeUIInput(FacesContext facesContext, 497 UIComponent component) { 498 if (!(component instanceof EditableValueHolder)) { 499 throw new IllegalArgumentException ("Component " 500 + component 501 .getClientId(facesContext) 502 + 503 " is not an EditableValueHolder"); 504 } 505 Map paramMap = facesContext.getExternalContext() 506 .getRequestParameterMap(); 507 String clientId = component.getClientId(facesContext); 508 if (paramMap.containsKey(clientId)) { 509 ((EditableValueHolder) component).setSubmittedValue(paramMap 511 .get(clientId)); 512 } else { 513 if (!isDisabledOrReadOnly(component)) { 516 ((EditableValueHolder) component) 517 .setSubmittedValue(EMPTY_STRING); 518 } 519 } 520 } 521 522 public static Date getDateValue(UIComponent component) { 523 if (!(component instanceof ValueHolder)) { 524 throw new IllegalArgumentException ("Component : " + 525 getPathToComponent(component) + 526 "is not a ValueHolder"); 527 } 528 529 if (component instanceof EditableValueHolder) { 530 Object submittedValue = 531 ((EditableValueHolder) component).getSubmittedValue(); 532 if (submittedValue != null) { 533 if (submittedValue instanceof Date ) { 534 return (Date ) submittedValue; 535 } else { 536 throw new IllegalArgumentException ( 537 "Expected submitted value of type Date for component : " + 538 getPathToComponent(component)); 539 } 540 } 541 } 542 543 Object value = ((ValueHolder) component).getValue(); 544 545 if (value == null || value instanceof Date ) { 546 return (Date ) value; 547 } else { 548 throw new IllegalArgumentException ( 549 "Expected submitted value of type Date for component : " 550 + getPathToComponent(component)); 551 } 552 } 553 554 public static void copyHtmlInputTextAttributes(HtmlInputText src, 555 HtmlInputText dest) { 556 dest.setId(src.getId()); 557 dest.setImmediate(src.isImmediate()); 558 dest.setTransient(src.isTransient()); 559 dest.setAccesskey(src.getAccesskey()); 560 dest.setAlt(src.getAlt()); 561 dest.setConverter(src.getConverter()); 562 dest.setDir(src.getDir()); 563 dest.setDisabled(src.isDisabled()); 564 dest.setLang(src.getLang()); 565 dest.setLocalValueSet(src.isLocalValueSet()); 566 dest.setMaxlength(src.getMaxlength()); 567 dest.setOnblur(src.getOnblur()); 568 dest.setOnchange(src.getOnchange()); 569 dest.setOnclick(src.getOnclick()); 570 dest.setOndblclick(src.getOndblclick()); 571 dest.setOnfocus(src.getOnfocus()); 572 dest.setOnkeydown(src.getOnkeydown()); 573 dest.setOnkeypress(src.getOnkeypress()); 574 dest.setOnkeyup(src.getOnkeyup()); 575 dest.setOnmousedown(src.getOnmousedown()); 576 dest.setOnmousemove(src.getOnmousemove()); 577 dest.setOnmouseout(src.getOnmouseout()); 578 dest.setOnmouseover(src.getOnmouseover()); 579 dest.setOnmouseup(src.getOnmouseup()); 580 dest.setOnselect(src.getOnselect()); 581 dest.setReadonly(src.isReadonly()); 582 dest.setRendered(src.isRendered()); 583 dest.setRequired(src.isRequired()); 584 dest.setSize(src.getSize()); 585 dest.setStyle(src.getStyle()); 586 dest.setStyleClass(src.getStyleClass()); 587 dest.setTabindex(src.getTabindex()); 588 dest.setTitle(src.getTitle()); 589 dest.setValidator(src.getValidator()); 590 } 591 592 } 593 | Popular Tags |