1 package org.apache.turbine.modules; 2 3 56 57 import java.lang.reflect.InvocationTargetException ; 58 import java.lang.reflect.Method ; 59 import java.util.Enumeration ; 60 61 import org.apache.fulcrum.parser.ParameterParser; 62 import org.apache.turbine.RunData; 63 import org.apache.turbine.Turbine; 64 import org.apache.turbine.TurbineException; 65 66 108 public abstract class ActionEvent 109 extends Action 110 { 111 117 public abstract void doPerform( RunData data ) 118 throws Exception ; 119 120 121 protected static final String BUTTON = "eventSubmit_"; 122 123 protected static final int BUTTON_LENGTH = BUTTON.length(); 124 125 protected static final String METHOD_NAME_PREFIX = "do"; 126 127 protected static final int METHOD_NAME_LENGTH = METHOD_NAME_PREFIX.length(); 128 129 protected static final int LENGTH = BUTTON.length(); 130 131 139 protected void perform( RunData data ) 140 throws Exception 141 { 142 try 143 { 144 executeEvents(data); 145 } 146 catch (NoSuchMethodException e) 147 { 148 doPerform( data ); 149 } 150 catch (InvocationTargetException ite) 151 { 152 Throwable t = ite.getTargetException(); 159 if (t instanceof Exception ) 160 { 161 throw (Exception )t; 162 } 163 else if (t instanceof java.lang.Error ) 164 { 165 throw (java.lang.Error )t; 166 } 167 else 168 { 169 throw new TurbineException(t); 172 } 173 } 174 } 175 176 public void execute(RunData data) 177 throws Exception 178 { 179 perform(data); 180 } 181 182 188 public void executeEvents(RunData data) 189 throws Exception 190 { 191 String theButton = null; 193 ParameterParser pp = data.getParameters(); 195 Object [] args = new Object [1]; 197 Class [] classes = new Class [1]; 199 classes[0] = RunData.class; 200 201 String button = pp.convert(BUTTON); 202 203 for (Enumeration e = pp.keys() ; e.hasMoreElements() ;) 205 { 206 String key = (String ) e.nextElement(); 207 if (key.startsWith(button)) 208 { 209 theButton = formatString(key); 210 break; 211 } 212 } 213 214 if (theButton == null) 215 { 216 throw new NoSuchMethodException ( 217 "ActionEvent: The button was null"); 218 } 219 220 Method method = getClass().getMethod(theButton, classes); 221 args[0] = data; 222 method.invoke(this, args ); 223 } 224 225 232 protected final String formatString(String input) 233 { 234 String methodName = input; 235 236 if (input.endsWith(".x") || input.endsWith(".y")) 238 { 239 methodName = methodName.substring(0, input.length() - 2); 240 } 241 242 String fold = 243 Turbine.getConfiguration().getString( 244 Turbine.PP_URL_CASE_FOLDING, 245 ParameterParser.URL_CASE_FOLDING_LOWER).toLowerCase(); 246 247 if (! fold.equals(ParameterParser.URL_CASE_FOLDING_NONE)) 248 { 249 methodName = 250 methodName.substring(BUTTON_LENGTH + METHOD_NAME_LENGTH); 251 252 return (METHOD_NAME_PREFIX + firstLetterCaps(methodName)); 253 } 254 else 255 { 256 return methodName.substring(BUTTON_LENGTH); 257 } 258 } 259 260 266 private final String firstLetterCaps( String data ) 267 { 268 String firstLetter = data.substring(0, 1).toUpperCase(); 269 String restLetters = data.substring(1).toLowerCase(); 270 return firstLetter + restLetters; 271 } 272 } 273 | Popular Tags |