KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > improve > struts > taglib > layout > util > LayoutUtils


1 package fr.improve.struts.taglib.layout.util;
2
3 import java.lang.reflect.InvocationTargetException JavaDoc;
4 import java.lang.reflect.Method JavaDoc;
5 import java.net.MalformedURLException JavaDoc;
6 import java.net.URLEncoder JavaDoc;
7 import java.util.ArrayList JavaDoc;
8 import java.util.Arrays JavaDoc;
9 import java.util.Collection JavaDoc;
10 import java.util.HashMap JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.List JavaDoc;
13 import java.util.Locale JavaDoc;
14 import java.util.Map JavaDoc;
15 import java.util.Random JavaDoc;
16 import java.util.Vector JavaDoc;
17
18 import javax.servlet.ServletContext JavaDoc;
19 import javax.servlet.ServletException JavaDoc;
20 import javax.servlet.ServletRequest JavaDoc;
21 import javax.servlet.http.HttpServletRequest JavaDoc;
22 import javax.servlet.http.HttpSession JavaDoc;
23 import javax.servlet.jsp.JspException JavaDoc;
24 import javax.servlet.jsp.PageContext JavaDoc;
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 /**
40  * Useful methods.
41  *
42  * @author: Jean-Noel Ribette
43  * @version 0.1
44  */

45 public class LayoutUtils {
46     protected static String JavaDoc bundle = Globals.MESSAGES_KEY;
47     protected static String JavaDoc localeKey = Globals.LOCALE_KEY;
48     protected static MessageResources messages =
49         MessageResources.getMessageResources(Constants.Package + ".LocalStrings");
50     protected static String JavaDoc name = Constants.BEAN_KEY;
51
52     // special mode for jsp creation without forms and actions
53
protected static boolean noErrorMode = false;
54
55     // compatiblity mode with struts-layout 0.3 to avoid bad form title key if not using the form display modes.
56
protected static boolean useFormDisplayMode = true;
57         
58     // old panel nesting compatibility
59
protected static boolean panelNesting = false;
60     
61     /**
62      * Java 1.4 encode method to use instead of deprecated 1.3 version.
63      */

64     private static Method JavaDoc encode = null;
65     private static Random JavaDoc random = new Random JavaDoc();
66
67     /**
68      * Initialize the encode variable with the 1.4 method if available.
69      */

70     static {
71         try {
72             // get version of encode method with two String args
73
Class JavaDoc[] args = new Class JavaDoc[] { String JavaDoc.class, String JavaDoc.class };
74             encode = URLEncoder JavaDoc.class.getMethod("encode", args);
75         } catch (NoSuchMethodException JavaDoc e) {
76             // log.debug("Could not find Java 1.4 encode method. Using deprecated version.", e);
77
}
78     }
79
80     public static void addMenu(PageContext JavaDoc pageContext, MenuComponent menu)
81         throws JspException JavaDoc {
82         MenuRepository repository =
83             (MenuRepository) pageContext.findAttribute(MenuRepository.MENU_REPOSITORY_KEY);
84         if (repository == null)
85             throw new JspException JavaDoc("Menu repository not found");
86         repository.addMenu(menu);
87     }
88     
89     public static void addMenu(ServletContext JavaDoc servletContext, MenuComponent menu)
90         throws ServletException JavaDoc {
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     /**
102      * Add the specify menu in the session menu repository.
103      *
104      * If a previous menu with this name exists,
105      * the new menu state state
106      * is synchronized with the old menu.
107      *
108      * The menu must be added after its root nodes have been initialized.
109      */

110     public static void addMenuIntoSession(HttpServletRequest JavaDoc in_request, MenuComponent in_menu) {
111         if (in_menu==null) {
112             throw new IllegalArgumentException JavaDoc("in_menu can not be null");
113         }
114         HttpSession JavaDoc 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     /**
126      * Get the menu with the specified name from the session menu repository.
127      * The menu state is synchronized with the user selection.
128      */

129     public static MenuComponent getMenuFromSession(HttpServletRequest JavaDoc in_request, String JavaDoc in_name) {
130         HttpSession JavaDoc 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 JavaDoc dest, Object JavaDoc orig)
144         throws JspException JavaDoc {
145         try {
146             PropertyUtils.copyProperties(dest, orig);
147         } catch (InvocationTargetException JavaDoc e) {
148             Throwable JavaDoc 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 JavaDoc("LayoutUtils.copyProperties: " + t.getMessage());
154         } catch (Throwable JavaDoc t) {
155             System.err.println("LayoutUtils.copyProperties: ");
156             System.err.println(t);
157             throw new JspException JavaDoc("LayoutUtils.copyProperties: " + t.getMessage());
158         }
159     }
160     /**
161      * Get the property 'property' of the bean named Constants.BEAN_KEY in the given page context
162      * Handle classic exception
163      **/

164     public static Object JavaDoc getBeanFromPageContext(
165         PageContext JavaDoc pageContext,
166         String JavaDoc property)
167         throws JspException JavaDoc {
168         Object JavaDoc bean = pageContext.findAttribute(name);
169         if (bean == null)
170             throw new JspException JavaDoc(messages.getMessage("getter.bean", name));
171         Object JavaDoc object = bean;
172         if (property != null) {
173             try {
174                 object = PropertyUtils.getProperty(bean, property);
175             } catch (IllegalAccessException JavaDoc e) {
176                 throw new JspException JavaDoc(messages.getMessage("getter.access", property, name));
177             } catch (InvocationTargetException JavaDoc e) {
178                 Throwable JavaDoc t = e.getTargetException();
179                 throw new JspException JavaDoc(
180                     messages.getMessage("getter.result", property, t.toString()));
181             } catch (NoSuchMethodException JavaDoc e) {
182                 throw new JspException JavaDoc(messages.getMessage("getter.method", property, name));
183             }
184         }
185         return object;
186     }
187     /**
188      * Get the property 'property' of the bean named 'name' in the given pageContext.
189      * Handle classic Exception
190      **/

191     public static Object JavaDoc getBeanFromPageContext(
192         PageContext JavaDoc pageContext,
193         String JavaDoc name,
194         String JavaDoc property)
195         throws JspException JavaDoc {
196         
197         if (noErrorMode) {
198             return "Test value";
199         }
200         
201         Object JavaDoc bean = pageContext.findAttribute(name);
202         if (bean == null) {
203             throw new JspException JavaDoc(messages.getMessage("getter.bean", name));
204         }
205         Object JavaDoc object = bean;
206
207         if (property != null) {
208             try {
209                 object = PropertyUtils.getProperty(bean, property);
210             } catch (IllegalAccessException JavaDoc e) {
211                 throw new JspException JavaDoc(messages.getMessage("getter.access", property, name));
212             } catch (InvocationTargetException JavaDoc e) {
213                 Throwable JavaDoc t = e.getTargetException();
214                 throw new JspException JavaDoc(
215                     messages.getMessage("getter.result", property, t.toString()));
216             } catch (NoSuchMethodException JavaDoc e) {
217                 throw new JspException JavaDoc(messages.getMessage("getter.method", property, name));
218             }
219         }
220         return object;
221     }
222     public static Collection JavaDoc getCollection(Object JavaDoc in_bean) {
223         return getCollection(in_bean, true);
224     }
225     /**
226      * Returns a collection if the bean if a collection.<br>
227      * @param in_wrap if true and the bean is not a collection return a collection containing the bean
228      */

229     public static Collection JavaDoc getCollection(Object JavaDoc in_bean, boolean in_wrap) {
230         if (noErrorMode) {
231             // This needs to return at least a List for getDatagrid.
232
Vector JavaDoc v = new Vector JavaDoc();
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 JavaDoc[]) in_bean);
246         }
247         if (in_bean instanceof java.util.Collection JavaDoc) {
248             return (java.util.Collection JavaDoc) in_bean;
249         }
250         if (in_bean instanceof java.util.Map JavaDoc) {
251             return ((java.util.Map JavaDoc) in_bean).entrySet();
252         }
253         if (in_bean instanceof DatagridImpl) {
254             return ((DatagridImpl) in_bean).getData();
255         }
256         if (in_wrap) {
257             Collection JavaDoc lc_collection = new ArrayList JavaDoc(1);
258             lc_collection.add(in_bean);
259             return lc_collection;
260         }
261         return null;
262     }
263                 
264     /**
265      * Build an iterator
266      */

267     public static Iterator JavaDoc getIterator(Object JavaDoc collection) throws JspException JavaDoc {
268         Iterator JavaDoc iterator;
269         if (collection.getClass().isArray())
270             collection = Arrays.asList((Object JavaDoc[]) collection);
271         if (collection instanceof java.util.Collection JavaDoc)
272             iterator = ((java.util.Collection JavaDoc) collection).iterator();
273         else if (collection instanceof Iterator JavaDoc)
274             iterator = ((Iterator JavaDoc) collection);
275         else if (collection instanceof Map JavaDoc)
276             iterator = ((Map JavaDoc) collection).entrySet().iterator();
277         else if (collection instanceof DatagridImpl)
278             iterator = ((DatagridImpl) collection).getData().iterator();
279         else
280             throw new JspException JavaDoc(
281                 messages.getMessage("optionsTag.iterator", collection.toString()));
282         return iterator;
283     }
284     public static Iterator JavaDoc getIterator(
285         PageContext JavaDoc pageContext,
286         String JavaDoc name,
287         String JavaDoc property)
288         throws JspException JavaDoc {
289         Iterator JavaDoc iterator;
290         Object JavaDoc collection = null;
291
292         if (noErrorMode) {
293             Vector JavaDoc v = new Vector JavaDoc();
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     /**
308      * Get datagrid object from the pageContext.
309      */

310     public static DatagridImpl getDatagrid(PageContext JavaDoc in_pageContext, String JavaDoc in_name, String JavaDoc in_property) throws JspException JavaDoc {
311         if (noErrorMode) {
312             DatagridImpl lc_datagrid = (DatagridImpl) DatagridImpl.getInstance();
313             lc_datagrid.setData((List JavaDoc)getCollection(null));
314             return lc_datagrid;
315         } else {
316             return (DatagridImpl) LayoutUtils.getBeanFromPageContext(in_pageContext, in_name, in_property);
317         }
318     }
319     
320     /**
321      * Get double object.
322      * @deprecated
323      */

324     public static double getDouble(Object JavaDoc in_object) {
325         if (noErrorMode) {
326             // No error mode, return a random number.
327
return random.nextInt(20);
328         } else {
329             if (in_object==null) {
330                 // Object is null, return 0.
331
return 0;
332             } else if (in_object instanceof Number JavaDoc) {
333                 // Object is a number, easy.
334
return ((Number JavaDoc)in_object).doubleValue();
335             } else {
336                 // Try to parse the object.
337
return Double.parseDouble(in_object.toString());
338             }
339         }
340     }
341     
342     /**
343      * Get double object.
344      * If object is null and in_null is false, return 0.
345      */

346     public static Double JavaDoc getDouble(Object JavaDoc in_object, boolean in_null) {
347         if (noErrorMode) {
348             // No error mode, return a random number.
349
return new Double JavaDoc(random.nextInt(20));
350         } else {
351             if (in_object==null) {
352                 // Object is null, return 0.
353
if (in_null) {
354                     return null;
355                 } else {
356                     return new Double JavaDoc(0);
357                 }
358             } else if (in_object instanceof Double JavaDoc) {
359                 return (Double JavaDoc) in_object;
360             } else if (in_object instanceof Number JavaDoc) {
361                 // Object is a number, easy.
362
return new Double JavaDoc(((Number JavaDoc)in_object).doubleValue());
363             } else {
364                 // Try to parse the object.
365
return new Double JavaDoc(in_object.toString());
366             }
367         }
368     }
369     
370     public static String JavaDoc getLabel(
371         PageContext JavaDoc pageContext,
372         String JavaDoc key,
373         Object JavaDoc[] args)
374         throws JspException JavaDoc {
375         return getLabel(pageContext, bundle, key, args, false);
376     }
377     /**
378      * Get the label with the key 'key' from the pageContext messageRessources.
379      * Handle classic exception
380      **/

381     public static String JavaDoc getLabel(
382         PageContext JavaDoc pageContext,
383         String JavaDoc key,
384         Object JavaDoc[] args,
385         boolean returnNull)
386         throws JspException JavaDoc {
387         return getLabel(pageContext, bundle, key, args, returnNull);
388     }
389     /**
390      * Get the label with the key 'key' from the pageContext messageRessources.
391      * Handle classic exception
392      **/

393     public static String JavaDoc getLabel(
394         PageContext JavaDoc pageContext,
395         String JavaDoc bundle,
396         String JavaDoc key,
397         Object JavaDoc[] args,
398         boolean returnNull)
399         throws JspException JavaDoc {
400             return getLabel((HttpServletRequest JavaDoc)pageContext.getRequest(), pageContext.getServletContext(), bundle, key, args, returnNull);
401         }
402     public static String JavaDoc getLabel(
403         HttpServletRequest JavaDoc request,
404         ServletContext JavaDoc servletContext,
405         String JavaDoc bundle,
406         String JavaDoc key,
407         Object JavaDoc[] args,
408         boolean returnNull)
409         throws JspException JavaDoc {
410         // Acquire the resources object containing our messages
411
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 JavaDoc(messages.getMessage("messageTag.resources", bundle));
421         }
422
423         // Calculate the Locale we will be using
424
Locale JavaDoc locale = null;
425         try {
426             locale =
427                 (Locale JavaDoc) request.getSession(false).getAttribute(localeKey);
428         } catch (IllegalStateException JavaDoc e) { // Invalidated session
429
locale = null;
430         } catch (NullPointerException JavaDoc npe) {
431             // no session yet.
432
locale = request.getLocale();
433         }
434         if (locale == null)
435             locale = Locale.getDefault();
436
437         // Retrieve the message string we are looking for
438
String JavaDoc 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     // return the locale
451
public static Locale JavaDoc getLocale(HttpServletRequest JavaDoc in_request) {
452         // first look in the LOCALE_KEY
453
Object JavaDoc l_object = in_request.getSession().getAttribute(Globals.LOCALE_KEY);
454         if (l_object != null && l_object instanceof Locale JavaDoc)
455             return (Locale JavaDoc) l_object;
456
457         // else return the request Locale
458
return in_request.getLocale();
459     }
460     // return the locale
461
public static Locale JavaDoc getLocale(PageContext JavaDoc in_pageContext) {
462         // first look in the LOCALE_KEY
463
Object JavaDoc l_object = in_pageContext.findAttribute(Globals.LOCALE_KEY);
464         if (l_object != null && l_object instanceof Locale JavaDoc)
465             return (Locale JavaDoc) l_object;
466
467         // else return the request Locale
468
return in_pageContext.getRequest().getLocale();
469     }
470     public static MenuComponent getMenu(PageContext JavaDoc pageContext, String JavaDoc menuName)
471         throws JspException JavaDoc {
472         MenuRepository repository =
473             (MenuRepository) pageContext.findAttribute(MenuRepository.MENU_REPOSITORY_KEY);
474         if (repository == null)
475             throw new JspException JavaDoc("Menu repository not found");
476         return repository.getMenu(menuName);
477     }
478     public static boolean getNoErrorMode() {
479         return noErrorMode;
480     }
481
482     /**
483      * Get the property 'property' of the bean 'bean'
484      * Handle classic exception
485      **/

486     public static Object JavaDoc getProperty(Object JavaDoc bean, String JavaDoc property)
487         throws JspException JavaDoc {
488         Object JavaDoc 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 JavaDoc e) {
497                 throw new JspException JavaDoc("IllegalAccessException while trying to access property " + property + " of a " + bean.getClass().getName());
498             } catch (InvocationTargetException JavaDoc e) {
499                 Throwable JavaDoc t = e.getTargetException();
500                 throw new JspException JavaDoc("Invocation target exception (" + (t==null ? "" : t.getClass().getName() + " " + t.getMessage()) + ") while trying to access property " + property + " of a " + bean.getClass().getName());
501             } catch (NoSuchMethodException JavaDoc e) {
502                 throw new JspException JavaDoc("No method to get the property " + property + " of " + bean.toString() + " (" + bean.getClass().getName() + ")");
503             } catch (IllegalArgumentException JavaDoc iae) {
504                 // one of the nested property is null.
505
object = null;
506             }
507         }
508         return object;
509     }
510     public static String JavaDoc getPropertyToChoose(ServletRequest JavaDoc request) {
511         String JavaDoc 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 JavaDoc 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 JavaDoc context)
527         throws javax.servlet.UnavailableException JavaDoc {
528         if ("noerror".equals(context.getInitParameter("struts-layout-mode")))
529             noErrorMode = true;
530     
531         String JavaDoc 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         // initialize an empty menu repository
541
if (context.getInitParameter("struts-layout-menu") != null)
542             context.setAttribute(MenuRepository.MENU_REPOSITORY_KEY, new MenuRepository());
543     }
544     
545     /**
546      * Returns the errors associated with an input field.
547      * @deprecated
548      */

549     public static List JavaDoc retrieveErrors(PageContext JavaDoc pageContext, String JavaDoc property)
550         throws JspException JavaDoc {
551         return retrieveErrors(pageContext, Globals.MESSAGES_KEY, property);
552     }
553
554     /**
555      * Returns the errors associated with an input field.
556      */

557     public static List JavaDoc retrieveErrors(PageContext JavaDoc pageContext, String JavaDoc bundle, String JavaDoc property)
558         throws JspException JavaDoc {
559         ActionMessages errors =
560             (ActionMessages) pageContext.getAttribute(
561                 Globals.ERROR_KEY,
562                 PageContext.REQUEST_SCOPE);
563         List JavaDoc localizedErrors = new ArrayList JavaDoc();
564         if (errors != null && !errors.isEmpty()) {
565             Iterator JavaDoc 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         /**
576      * Set the Struts light skin to use
577      * The file in config/'skin'.css will be use.
578      **/

579     public static void setSkin(HttpSession JavaDoc session, String JavaDoc skin) {
580         if (skin != null && !skin.equals("")) {
581             String JavaDoc 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     /**
592      * Gets the useFormDisplayMode.
593      * @return Returns a boolean
594      */

595     public static boolean getUseFormDisplayMode() {
596         return useFormDisplayMode;
597     }
598
599     /**
600      * Sets the useFormDisplayMode.
601      * @param useFormDisplayMode The useFormDisplayMode to set
602      */

603     public static void setUseFormDisplayMode(boolean useFormDisplayMode) {
604         LayoutUtils.useFormDisplayMode = useFormDisplayMode;
605     }
606
607     /**
608      * Returns the panelNesting.
609      * @return boolean
610      */

611     public static boolean isPanelNesting() {
612         return panelNesting;
613     }
614
615     /**
616      * Sets the panelNesting.
617      * @param panelNesting The panelNesting to set
618      */

619     public static void setPanelNesting(boolean panelNesting) {
620         LayoutUtils.panelNesting = panelNesting;
621     }
622     
623     
624     /**
625      * @deprecated
626      */

627     public static String JavaDoc computeURL(PageContext JavaDoc in_pageContext, String JavaDoc in_forward, String JavaDoc in_url, String JavaDoc in_page, Map JavaDoc in_params, String JavaDoc in_anchor, boolean in_redirect, String JavaDoc in_target) throws JspException JavaDoc {
628         return computeURL(in_pageContext, in_forward, in_url, in_page, null, in_params, in_anchor, in_redirect, in_target);
629     }
630     
631     /**
632      * @deprecated
633      */

634     public static String JavaDoc computeURL(PageContext JavaDoc in_pageContext, String JavaDoc in_forward, String JavaDoc in_url, String JavaDoc in_page, String JavaDoc in_action, Map JavaDoc in_params, String JavaDoc in_anchor, boolean in_redirect, String JavaDoc in_target) throws JspException JavaDoc {
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     /**
639      * Compute an url, using RequestUtils.computeURL from Struts.
640      * If links should not be followed if there are unsaved form changes,
641      * includes call to the required javascript code.
642      */

643     public static String JavaDoc computeURL(PageContext JavaDoc in_pageContext, String JavaDoc in_forward, String JavaDoc in_url, String JavaDoc in_page, String JavaDoc in_action, String JavaDoc in_module, Map JavaDoc in_params, String JavaDoc in_anchor, boolean in_redirect, String JavaDoc in_target) throws JspException JavaDoc {
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     /**
648      * Compute an url, using RequestUtils.computeURL from Struts.
649      * If links should not be followed if there are unsaved form changes,
650      * includes call to the required javascript code.
651      */

652     public static String JavaDoc computeURL(PageContext JavaDoc in_pageContext, String JavaDoc in_forward, String JavaDoc in_url, String JavaDoc in_page, String JavaDoc in_action, String JavaDoc in_module, Map JavaDoc in_params, String JavaDoc in_anchor, boolean in_redirect, String JavaDoc in_target, String JavaDoc in_scheme) throws JspException JavaDoc {
653         if (noErrorMode) {
654             return "/dummyUrl.do";
655         }
656         
657         String JavaDoc lc_computedURL;
658         
659         Skin lc_skin = getSkin(in_pageContext.getSession());
660         
661         if (lc_skin.isLinkTokenRequired()) {
662             String JavaDoc lc_token = (String JavaDoc) in_pageContext.getSession().getAttribute(Globals.TRANSACTION_TOKEN_KEY);
663             if (lc_token != null) {
664                 if (in_params==null) {
665                     in_params = new HashMap JavaDoc();
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 JavaDoc e) {
674             TagUtils.saveException(in_pageContext, e);
675             throw new JspException JavaDoc(e.getMessage());
676         }
677         if (in_scheme!=null && in_scheme.length()!=0) {
678             StringBuffer JavaDoc lc_buffer = new StringBuffer JavaDoc(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 JavaDoc lc_buffer = new StringBuffer JavaDoc("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     /**
695      * Use the new URLEncoder.encode() method from java 1.4 if available, else
696      * use the old deprecated version. This method uses reflection to find the appropriate
697      * method; if the reflection operations throw exceptions, this will return the url
698      * encoded with the old URLEncoder.encode() method.
699      * @return String - the encoded url.
700      *
701      * Copy from struts.
702      */

703     public static String JavaDoc encodeURL(String JavaDoc in_url) {
704         try {
705             // encode url with new 1.4 method and UTF-8 encoding if possible.
706
if (encode != null) {
707                 return (String JavaDoc) encode.invoke(null, new Object JavaDoc[] { in_url, "UTF-8" });
708             }
709         } catch (IllegalAccessException JavaDoc e) {
710             // nothing to do.
711
} catch (InvocationTargetException JavaDoc e) {
712             // nothing to do.
713
}
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 JavaDoc computeParameters(PageContext JavaDoc in_context, String JavaDoc in_paramId, String JavaDoc in_paramName, String JavaDoc in_paramProperty, String JavaDoc in_paramScope, String JavaDoc in_name, String JavaDoc in_property, String JavaDoc in_scope, boolean in_transaction) throws JspException JavaDoc {
723         if (noErrorMode) {
724             return new HashMap JavaDoc();
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 JavaDoc[] getArrayProperty(Object JavaDoc in_bean, String JavaDoc in_property) throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc, NoSuchMethodException JavaDoc {
730         if (noErrorMode) {
731             return new String JavaDoc[]{"Value1", "Value2", "Value3"};
732         }
733         return BeanUtils.getArrayProperty(in_bean, in_property);
734     }
735 }
Popular Tags