1 package fr.improve.struts.taglib.layout.util; 2 3 import java.lang.reflect.InvocationTargetException ; 4 import java.lang.reflect.Method ; 5 import java.net.MalformedURLException ; 6 import java.net.URLEncoder ; 7 import java.util.ArrayList ; 8 import java.util.Arrays ; 9 import java.util.Collection ; 10 import java.util.HashMap ; 11 import java.util.Iterator ; 12 import java.util.List ; 13 import java.util.Locale ; 14 import java.util.Map ; 15 import java.util.Random ; 16 import java.util.Vector ; 17 18 import javax.servlet.ServletContext ; 19 import javax.servlet.ServletException ; 20 import javax.servlet.ServletRequest ; 21 import javax.servlet.http.HttpServletRequest ; 22 import javax.servlet.http.HttpSession ; 23 import javax.servlet.jsp.JspException ; 24 import javax.servlet.jsp.PageContext ; 25 26 import org.apache.commons.beanutils.BeanUtils; 27 import org.apache.commons.beanutils.PropertyUtils; 28 import org.apache.struts.Globals; 29 import org.apache.struts.action.ActionMessage; 30 import org.apache.struts.action.ActionMessages; 31 import org.apache.struts.taglib.html.Constants; 32 import org.apache.struts.util.MessageResources; 33 34 import fr.improve.struts.taglib.layout.datagrid.DatagridImpl; 35 import fr.improve.struts.taglib.layout.menu.MenuComponent; 36 import fr.improve.struts.taglib.layout.menu.MenuRepository; 37 import fr.improve.struts.taglib.layout.skin.Skin; 38 import fr.improve.struts.taglib.layout.treeview.TreeViewReconciler; 39 45 public class LayoutUtils { 46 protected static String bundle = Globals.MESSAGES_KEY; 47 protected static String localeKey = Globals.LOCALE_KEY; 48 protected static MessageResources messages = 49 MessageResources.getMessageResources(Constants.Package + ".LocalStrings"); 50 protected static String name = Constants.BEAN_KEY; 51 52 protected static boolean noErrorMode = false; 54 55 protected static boolean useFormDisplayMode = true; 57 58 protected static boolean panelNesting = false; 60 61 64 private static Method encode = null; 65 private static Random random = new Random (); 66 67 70 static { 71 try { 72 Class [] args = new Class [] { String .class, String .class }; 74 encode = URLEncoder .class.getMethod("encode", args); 75 } catch (NoSuchMethodException e) { 76 } 78 } 79 80 public static void addMenu(PageContext pageContext, MenuComponent menu) 81 throws JspException { 82 MenuRepository repository = 83 (MenuRepository) pageContext.findAttribute(MenuRepository.MENU_REPOSITORY_KEY); 84 if (repository == null) 85 throw new JspException ("Menu repository not found"); 86 repository.addMenu(menu); 87 } 88 89 public static void addMenu(ServletContext servletContext, MenuComponent menu) 90 throws ServletException { 91 MenuRepository repository = 92 (MenuRepository) servletContext.getAttribute( 93 MenuRepository.MENU_REPOSITORY_KEY); 94 if (repository == null) { 95 repository = new MenuRepository(); 96 servletContext.setAttribute(MenuRepository.MENU_REPOSITORY_KEY, repository); 97 } 98 repository.addMenu(menu); 99 } 100 101 110 public static void addMenuIntoSession(HttpServletRequest in_request, MenuComponent in_menu) { 111 if (in_menu==null) { 112 throw new IllegalArgumentException ("in_menu can not be null"); 113 } 114 HttpSession lc_session = in_request.getSession(); 115 MenuRepository repository = 116 (MenuRepository) lc_session.getAttribute(MenuRepository.MENU_REPOSITORY_KEY); 117 if (repository == null) { 118 repository = new MenuRepository(); 119 lc_session.setAttribute(MenuRepository.MENU_REPOSITORY_KEY, repository); 120 } 121 repository.addMenu(in_menu); 122 TreeViewReconciler.reconceileFromCookie(in_menu, in_request); 123 } 124 125 129 public static MenuComponent getMenuFromSession(HttpServletRequest in_request, String in_name) { 130 HttpSession lc_session = in_request.getSession(); 131 MenuRepository lc_repository = (MenuRepository) lc_session.getAttribute(MenuRepository.MENU_REPOSITORY_KEY); 132 if (lc_repository==null) { 133 lc_repository = new MenuRepository(); 134 lc_session.setAttribute(MenuRepository.MENU_REPOSITORY_KEY, lc_repository); 135 } 136 MenuComponent lc_menu = lc_repository.getMenu(in_name); 137 if (lc_menu!=null) { 138 TreeViewReconciler.reconceileFromCookie(lc_menu, in_request); 139 } 140 return lc_menu; 141 } 142 143 public static void copyProperties(Object dest, Object orig) 144 throws JspException { 145 try { 146 PropertyUtils.copyProperties(dest, orig); 147 } catch (InvocationTargetException e) { 148 Throwable t = e.getTargetException(); 149 if (t == null) 150 t = e; 151 System.err.println("LayoutUtils.copyProperties: "); 152 System.err.println(t); 153 throw new JspException ("LayoutUtils.copyProperties: " + t.getMessage()); 154 } catch (Throwable t) { 155 System.err.println("LayoutUtils.copyProperties: "); 156 System.err.println(t); 157 throw new JspException ("LayoutUtils.copyProperties: " + t.getMessage()); 158 } 159 } 160 164 public static Object getBeanFromPageContext( 165 PageContext pageContext, 166 String property) 167 throws JspException { 168 Object bean = pageContext.findAttribute(name); 169 if (bean == null) 170 throw new JspException (messages.getMessage("getter.bean", name)); 171 Object object = bean; 172 if (property != null) { 173 try { 174 object = PropertyUtils.getProperty(bean, property); 175 } catch (IllegalAccessException e) { 176 throw new JspException (messages.getMessage("getter.access", property, name)); 177 } catch (InvocationTargetException e) { 178 Throwable t = e.getTargetException(); 179 throw new JspException ( 180 messages.getMessage("getter.result", property, t.toString())); 181 } catch (NoSuchMethodException e) { 182 throw new JspException (messages.getMessage("getter.method", property, name)); 183 } 184 } 185 return object; 186 } 187 191 public static Object getBeanFromPageContext( 192 PageContext pageContext, 193 String name, 194 String property) 195 throws JspException { 196 197 if (noErrorMode) { 198 return "Test value"; 199 } 200 201 Object bean = pageContext.findAttribute(name); 202 if (bean == null) { 203 throw new JspException (messages.getMessage("getter.bean", name)); 204 } 205 Object object = bean; 206 207 if (property != null) { 208 try { 209 object = PropertyUtils.getProperty(bean, property); 210 } catch (IllegalAccessException e) { 211 throw new JspException (messages.getMessage("getter.access", property, name)); 212 } catch (InvocationTargetException e) { 213 Throwable t = e.getTargetException(); 214 throw new JspException ( 215 messages.getMessage("getter.result", property, t.toString())); 216 } catch (NoSuchMethodException e) { 217 throw new JspException (messages.getMessage("getter.method", property, name)); 218 } 219 } 220 return object; 221 } 222 public static Collection getCollection(Object in_bean) { 223 return getCollection(in_bean, true); 224 } 225 229 public static Collection getCollection(Object in_bean, boolean in_wrap) { 230 if (noErrorMode) { 231 Vector v = new Vector (); 233 v.add("element 1"); 234 v.add("element 2"); 235 v.add("element 3"); 236 v.add("element 4"); 237 v.add("element 5"); 238 v.add("element 6"); 239 return v; 240 } 241 if (in_bean == null) { 242 return null; 243 } 244 if (in_bean.getClass().isArray()) { 245 return Arrays.asList((Object []) in_bean); 246 } 247 if (in_bean instanceof java.util.Collection ) { 248 return (java.util.Collection ) in_bean; 249 } 250 if (in_bean instanceof java.util.Map ) { 251 return ((java.util.Map ) in_bean).entrySet(); 252 } 253 if (in_bean instanceof DatagridImpl) { 254 return ((DatagridImpl) in_bean).getData(); 255 } 256 if (in_wrap) { 257 Collection lc_collection = new ArrayList (1); 258 lc_collection.add(in_bean); 259 return lc_collection; 260 } 261 return null; 262 } 263 264 267 public static Iterator getIterator(Object collection) throws JspException { 268 Iterator iterator; 269 if (collection.getClass().isArray()) 270 collection = Arrays.asList((Object []) collection); 271 if (collection instanceof java.util.Collection ) 272 iterator = ((java.util.Collection ) collection).iterator(); 273 else if (collection instanceof Iterator ) 274 iterator = ((Iterator ) collection); 275 else if (collection instanceof Map ) 276 iterator = ((Map ) collection).entrySet().iterator(); 277 else if (collection instanceof DatagridImpl) 278 iterator = ((DatagridImpl) collection).getData().iterator(); 279 else 280 throw new JspException ( 281 messages.getMessage("optionsTag.iterator", collection.toString())); 282 return iterator; 283 } 284 public static Iterator getIterator( 285 PageContext pageContext, 286 String name, 287 String property) 288 throws JspException { 289 Iterator iterator; 290 Object collection = null; 291 292 if (noErrorMode) { 293 Vector v = new Vector (); 294 v.add("element 1"); 295 v.add("element 2"); 296 v.add("element 3"); 297 v.add("element 4"); 298 v.add("element 5"); 299 v.add("element 6"); 300 collection = v; 301 } else { 302 collection = getBeanFromPageContext(pageContext, name, property); 303 } 304 return getIterator(collection); 305 } 306 307 310 public static DatagridImpl getDatagrid(PageContext in_pageContext, String in_name, String in_property) throws JspException { 311 if (noErrorMode) { 312 DatagridImpl lc_datagrid = (DatagridImpl) DatagridImpl.getInstance(); 313 lc_datagrid.setData((List )getCollection(null)); 314 return lc_datagrid; 315 } else { 316 return (DatagridImpl) LayoutUtils.getBeanFromPageContext(in_pageContext, in_name, in_property); 317 } 318 } 319 320 324 public static double getDouble(Object in_object) { 325 if (noErrorMode) { 326 return random.nextInt(20); 328 } else { 329 if (in_object==null) { 330 return 0; 332 } else if (in_object instanceof Number ) { 333 return ((Number )in_object).doubleValue(); 335 } else { 336 return Double.parseDouble(in_object.toString()); 338 } 339 } 340 } 341 342 346 public static Double getDouble(Object in_object, boolean in_null) { 347 if (noErrorMode) { 348 return new Double (random.nextInt(20)); 350 } else { 351 if (in_object==null) { 352 if (in_null) { 354 return null; 355 } else { 356 return new Double (0); 357 } 358 } else if (in_object instanceof Double ) { 359 return (Double ) in_object; 360 } else if (in_object instanceof Number ) { 361 return new Double (((Number )in_object).doubleValue()); 363 } else { 364 return new Double (in_object.toString()); 366 } 367 } 368 } 369 370 public static String getLabel( 371 PageContext pageContext, 372 String key, 373 Object [] args) 374 throws JspException { 375 return getLabel(pageContext, bundle, key, args, false); 376 } 377 381 public static String getLabel( 382 PageContext pageContext, 383 String key, 384 Object [] args, 385 boolean returnNull) 386 throws JspException { 387 return getLabel(pageContext, bundle, key, args, returnNull); 388 } 389 393 public static String getLabel( 394 PageContext pageContext, 395 String bundle, 396 String key, 397 Object [] args, 398 boolean returnNull) 399 throws JspException { 400 return getLabel((HttpServletRequest )pageContext.getRequest(), pageContext.getServletContext(), bundle, key, args, returnNull); 401 } 402 public static String getLabel( 403 HttpServletRequest request, 404 ServletContext servletContext, 405 String bundle, 406 String key, 407 Object [] args, 408 boolean returnNull) 409 throws JspException { 410 MessageResources resources = 412 (MessageResources) request.getAttribute(bundle); 413 if (resources == null) { 414 resources = (MessageResources) servletContext.getAttribute(bundle); 415 } 416 if (resources == null) { 417 if (noErrorMode) { 418 return key; 419 } 420 throw new JspException (messages.getMessage("messageTag.resources", bundle)); 421 } 422 423 Locale locale = null; 425 try { 426 locale = 427 (Locale ) request.getSession(false).getAttribute(localeKey); 428 } catch (IllegalStateException e) { locale = null; 430 } catch (NullPointerException npe) { 431 locale = request.getLocale(); 433 } 434 if (locale == null) 435 locale = Locale.getDefault(); 436 437 String message = null; 439 if (key != null) { 440 message = resources.getMessage(locale, key, args); 441 } 442 if (message == null) { 443 if (returnNull) 444 return null; 445 else 446 return key; 447 } 448 return message; 449 } 450 public static Locale getLocale(HttpServletRequest in_request) { 452 Object l_object = in_request.getSession().getAttribute(Globals.LOCALE_KEY); 454 if (l_object != null && l_object instanceof Locale ) 455 return (Locale ) l_object; 456 457 return in_request.getLocale(); 459 } 460 public static Locale getLocale(PageContext in_pageContext) { 462 Object l_object = in_pageContext.findAttribute(Globals.LOCALE_KEY); 464 if (l_object != null && l_object instanceof Locale ) 465 return (Locale ) l_object; 466 467 return in_pageContext.getRequest().getLocale(); 469 } 470 public static MenuComponent getMenu(PageContext pageContext, String menuName) 471 throws JspException { 472 MenuRepository repository = 473 (MenuRepository) pageContext.findAttribute(MenuRepository.MENU_REPOSITORY_KEY); 474 if (repository == null) 475 throw new JspException ("Menu repository not found"); 476 return repository.getMenu(menuName); 477 } 478 public static boolean getNoErrorMode() { 479 return noErrorMode; 480 } 481 482 486 public static Object getProperty(Object bean, String property) 487 throws JspException { 488 Object object = bean; 489 490 if (noErrorMode) 491 return object; 492 493 if (property != null) { 494 try { 495 object = PropertyUtils.getProperty(bean, property); 496 } catch (IllegalAccessException e) { 497 throw new JspException ("IllegalAccessException while trying to access property " + property + " of a " + bean.getClass().getName()); 498 } catch (InvocationTargetException e) { 499 Throwable t = e.getTargetException(); 500 throw new JspException ("Invocation target exception (" + (t==null ? "" : t.getClass().getName() + " " + t.getMessage()) + ") while trying to access property " + property + " of a " + bean.getClass().getName()); 501 } catch (NoSuchMethodException e) { 502 throw new JspException ("No method to get the property " + property + " of " + bean.toString() + " (" + bean.getClass().getName() + ")"); 503 } catch (IllegalArgumentException iae) { 504 object = null; 506 } 507 } 508 return object; 509 } 510 public static String getPropertyToChoose(ServletRequest request) { 511 String property = 512 request.getParameter("frImproveStrutsTaglibLayoutPROPERTY_TO_CHOOSE"); 513 if (property != null && property.length() > 0) 514 return property; 515 return null; 516 } 517 public static Skin getSkin(HttpSession session) { 518 if (session == null) 519 return Skin.getSkin("default",""); 520 Skin skin = (Skin) session.getAttribute("LIGHT_SKIN"); 521 if (skin == null) { 522 return Skin.getSkin("default",""); 523 } 524 return skin; 525 } 526 public static void init(ServletContext context) 527 throws javax.servlet.UnavailableException { 528 if ("noerror".equals(context.getInitParameter("struts-layout-mode"))) 529 noErrorMode = true; 530 531 String l_string = context.getInitParameter("struts-layout-form-display-mode"); 532 if ("false".equals(l_string)) { 533 setUseFormDisplayMode(false); 534 } 535 l_string = context.getInitParameter("struts-layout-old-panel-nesting"); 536 if ("false".equals(l_string)) { 537 setPanelNesting(false); 538 } 539 540 if (context.getInitParameter("struts-layout-menu") != null) 542 context.setAttribute(MenuRepository.MENU_REPOSITORY_KEY, new MenuRepository()); 543 } 544 545 549 public static List retrieveErrors(PageContext pageContext, String property) 550 throws JspException { 551 return retrieveErrors(pageContext, Globals.MESSAGES_KEY, property); 552 } 553 554 557 public static List retrieveErrors(PageContext pageContext, String bundle, String property) 558 throws JspException { 559 ActionMessages errors = 560 (ActionMessages) pageContext.getAttribute( 561 Globals.ERROR_KEY, 562 PageContext.REQUEST_SCOPE); 563 List localizedErrors = new ArrayList (); 564 if (errors != null && !errors.isEmpty()) { 565 Iterator iterator = errors.get(property); 566 while (iterator != null && iterator.hasNext()) { 567 ActionMessage report = (ActionMessage) iterator.next(); 568 localizedErrors.add( 569 LayoutUtils.getLabel(pageContext, bundle, report.getKey(), report.getValues(), false)); 570 } 571 } 572 return localizedErrors; 573 } 574 575 579 public static void setSkin(HttpSession session, String skin) { 580 if (skin != null && !skin.equals("")) { 581 String theSkin = skin; 582 if (!theSkin.endsWith(".css")) { 583 session.setAttribute("LIGHT_SKIN", Skin.getSkin(theSkin, "")); 584 } else { 585 session.setAttribute("LIGHT_SKIN", Skin.getSkin(theSkin.substring(0, theSkin.lastIndexOf('.')), null)); 586 } 587 } else 588 session.removeAttribute("LIGHT_SKIN"); 589 } 590 591 595 public static boolean getUseFormDisplayMode() { 596 return useFormDisplayMode; 597 } 598 599 603 public static void setUseFormDisplayMode(boolean useFormDisplayMode) { 604 LayoutUtils.useFormDisplayMode = useFormDisplayMode; 605 } 606 607 611 public static boolean isPanelNesting() { 612 return panelNesting; 613 } 614 615 619 public static void setPanelNesting(boolean panelNesting) { 620 LayoutUtils.panelNesting = panelNesting; 621 } 622 623 624 627 public static String computeURL(PageContext in_pageContext, String in_forward, String in_url, String in_page, Map in_params, String in_anchor, boolean in_redirect, String in_target) throws JspException { 628 return computeURL(in_pageContext, in_forward, in_url, in_page, null, in_params, in_anchor, in_redirect, in_target); 629 } 630 631 634 public static String computeURL(PageContext in_pageContext, String in_forward, String in_url, String in_page, String in_action, Map in_params, String in_anchor, boolean in_redirect, String in_target) throws JspException { 635 return computeURL(in_pageContext, in_forward, in_url, in_page, in_action, null, in_params, in_anchor, in_redirect, in_target); 636 } 637 638 643 public static String computeURL(PageContext in_pageContext, String in_forward, String in_url, String in_page, String in_action, String in_module, Map in_params, String in_anchor, boolean in_redirect, String in_target) throws JspException { 644 return computeURL(in_pageContext, in_forward, in_url, in_page, in_action, in_module, in_params, in_anchor, in_redirect, in_target, null); 645 } 646 647 652 public static String computeURL(PageContext in_pageContext, String in_forward, String in_url, String in_page, String in_action, String in_module, Map in_params, String in_anchor, boolean in_redirect, String in_target, String in_scheme) throws JspException { 653 if (noErrorMode) { 654 return "/dummyUrl.do"; 655 } 656 657 String lc_computedURL; 658 659 Skin lc_skin = getSkin(in_pageContext.getSession()); 660 661 if (lc_skin.isLinkTokenRequired()) { 662 String lc_token = (String ) in_pageContext.getSession().getAttribute(Globals.TRANSACTION_TOKEN_KEY); 663 if (lc_token != null) { 664 if (in_params==null) { 665 in_params = new HashMap (); 666 } 667 in_params.put(Constants.TOKEN_KEY, lc_token); 668 } 669 } 670 671 try { 672 lc_computedURL = TagUtils.computeURL(in_pageContext, in_forward, in_url, in_page, in_action, in_module, in_params, in_anchor, in_redirect); 673 } catch (MalformedURLException e) { 674 TagUtils.saveException(in_pageContext, e); 675 throw new JspException (e.getMessage()); 676 } 677 if (in_scheme!=null && in_scheme.length()!=0) { 678 StringBuffer lc_buffer = new StringBuffer (in_scheme.length() + 3 + lc_computedURL.length()); 679 lc_buffer.append(in_scheme).append("://").append(in_pageContext.getRequest().getServerName()).append(lc_computedURL); 680 lc_computedURL = lc_buffer.toString(); 681 } 682 if (in_target==null && !lc_skin.getFollowLinkIfFormChanged()) { 683 StringBuffer lc_buffer = new StringBuffer ("javascript:checkFormChange('"); 684 lc_buffer.append(lc_computedURL); 685 lc_buffer.append("','"); 686 lc_buffer.append(getLabel(in_pageContext, "layout.dataLost", null)); 687 lc_buffer.append("')"); 688 lc_computedURL = lc_buffer.toString(); 689 } 690 691 return lc_computedURL; 692 } 693 694 703 public static String encodeURL(String in_url) { 704 try { 705 if (encode != null) { 707 return (String ) encode.invoke(null, new Object [] { in_url, "UTF-8" }); 708 } 709 } catch (IllegalAccessException e) { 710 } catch (InvocationTargetException e) { 712 } 714 715 return URLEncoder.encode(in_url); 716 } 717 718 public static void setNoErrorMode(boolean in_value) { 719 noErrorMode = in_value; 720 } 721 722 public static Map computeParameters(PageContext in_context, String in_paramId, String in_paramName, String in_paramProperty, String in_paramScope, String in_name, String in_property, String in_scope, boolean in_transaction) throws JspException { 723 if (noErrorMode) { 724 return new HashMap (); 725 } 726 return TagUtils.computeParameters(in_context, in_paramId, in_paramName, in_paramProperty, in_paramScope, in_name, in_property, in_scope, in_transaction); 727 } 728 729 public static String [] getArrayProperty(Object in_bean, String in_property) throws IllegalAccessException , InvocationTargetException , NoSuchMethodException { 730 if (noErrorMode) { 731 return new String []{"Value1", "Value2", "Value3"}; 732 } 733 return BeanUtils.getArrayProperty(in_bean, in_property); 734 } 735 } | Popular Tags |