KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > improve > struts > taglib > layout > formatter > DispatchFormatter


1 /*
2  * (c) Copyright 2001 MyCorporation.
3  * All Rights Reserved.
4  */

5 package fr.improve.struts.taglib.layout.formatter;
6
7 import java.lang.reflect.InvocationTargetException JavaDoc;
8 import java.lang.reflect.Method JavaDoc;
9 import java.util.HashMap JavaDoc;
10
11 import javax.servlet.jsp.PageContext JavaDoc;
12
13 import fr.improve.struts.taglib.layout.util.LayoutUtils;
14 import fr.improve.struts.taglib.layout.util.TagUtils;
15
16 /**
17  * Implementation of a formatter class calling a method corresponding of the format specified.
18  * The called method must have the following signature:
19  *
20  * myFormat(Object in_object, PageContext in_pageontext) : String
21  *
22  * This formatter includes support for no error mode.
23  *
24  * @version 1.0
25  * @author
26  */

27 public abstract class DispatchFormatter extends AbstractFormatter {
28     static Class JavaDoc[] argv = { Object JavaDoc.class, PageContext JavaDoc.class };
29
30     protected HashMap JavaDoc methods = new HashMap JavaDoc();
31     
32     protected Method JavaDoc getMethod(String JavaDoc name) throws NoSuchMethodException JavaDoc {
33
34         synchronized (methods) {
35             Method JavaDoc method = (Method JavaDoc) methods.get(name);
36             if (method == null) {
37                 method = getClass().getMethod(name, argv);
38                 methods.put(name, method);
39             }
40             return (method);
41         }
42
43     }
44     
45     /*
46      * @see AbstractFormatter#format(Object, String, PageContext)
47      */

48     public String JavaDoc format(
49         Object JavaDoc in_value,
50         String JavaDoc in_format,
51         PageContext JavaDoc in_pageContext) throws FormatException {
52         
53         if (LayoutUtils.getNoErrorMode()) {
54             return "(" + in_format + ")";
55         }
56
57         String JavaDoc lc_value = null;
58             try {
59                 Method JavaDoc lc_method = getMethod(in_format);
60                 Object JavaDoc[] lc_argv = new Object JavaDoc[2];
61                 lc_argv[0] = in_value;
62                 lc_argv[1] = in_pageContext;
63                 lc_value = (String JavaDoc) lc_method.invoke(this, lc_argv);
64             } catch (NoSuchMethodException JavaDoc nsme) {
65                 TagUtils.saveException(in_pageContext, nsme);
66                 throw new FormatException("No method corresponds to the formatter " + in_format + " in the class " + getClass().getName());
67             } catch (IllegalAccessException JavaDoc iae) {
68                 TagUtils.saveException(in_pageContext, iae);
69                 throw new FormatException(iae.getMessage());
70             } catch (InvocationTargetException JavaDoc ite) {
71                 TagUtils.saveException(in_pageContext, ite.getTargetException());
72                 throw new FormatException("Invocation target exception: " + ite.getTargetException());
73             }
74         return lc_value;
75     }
76
77 }
78
Popular Tags