1 5 package fr.improve.struts.taglib.layout.formatter; 6 7 import java.lang.reflect.InvocationTargetException ; 8 import java.lang.reflect.Method ; 9 import java.util.HashMap ; 10 11 import javax.servlet.jsp.PageContext ; 12 13 import fr.improve.struts.taglib.layout.util.LayoutUtils; 14 import fr.improve.struts.taglib.layout.util.TagUtils; 15 16 27 public abstract class DispatchFormatter extends AbstractFormatter { 28 static Class [] argv = { Object .class, PageContext .class }; 29 30 protected HashMap methods = new HashMap (); 31 32 protected Method getMethod(String name) throws NoSuchMethodException { 33 34 synchronized (methods) { 35 Method method = (Method ) 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 48 public String format( 49 Object in_value, 50 String in_format, 51 PageContext in_pageContext) throws FormatException { 52 53 if (LayoutUtils.getNoErrorMode()) { 54 return "(" + in_format + ")"; 55 } 56 57 String lc_value = null; 58 try { 59 Method lc_method = getMethod(in_format); 60 Object [] lc_argv = new Object [2]; 61 lc_argv[0] = in_value; 62 lc_argv[1] = in_pageContext; 63 lc_value = (String ) lc_method.invoke(this, lc_argv); 64 } catch (NoSuchMethodException 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 iae) { 68 TagUtils.saveException(in_pageContext, iae); 69 throw new FormatException(iae.getMessage()); 70 } catch (InvocationTargetException 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 |