View Javadoc
1 package org.apache.turbine.modules; 2 3 /* ==================================================================== 4 * The Apache Software License, Version 1.1 5 * 6 * Copyright (c) 2001 The Apache Software Foundation. All rights 7 * reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in 18 * the documentation and/or other materials provided with the 19 * distribution. 20 * 21 * 3. The end-user documentation included with the redistribution, 22 * if any, must include the following acknowledgment: 23 * "This product includes software developed by the 24 * Apache Software Foundation (http://www.apache.org/)." 25 * Alternately, this acknowledgment may appear in the software itself, 26 * if and wherever such third-party acknowledgments normally appear. 27 * 28 * 4. The names "Apache" and "Apache Software Foundation" and 29 * "Apache Turbine" must not be used to endorse or promote products 30 * derived from this software without prior written permission. For 31 * written permission, please contact apache@apache.org. 32 * 33 * 5. Products derived from this software may not be called "Apache", 34 * "Apache Turbine", nor may "Apache" appear in their name, without 35 * prior written permission of the Apache Software Foundation. 36 * 37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 48 * SUCH DAMAGE. 49 * ==================================================================== 50 * 51 * This software consists of voluntary contributions made by many 52 * individuals on behalf of the Apache Software Foundation. For more 53 * information on the Apache Software Foundation, please see 54 * <http://www.apache.org/>;. 55 */ 56 57 // JDK 58 import java.lang.reflect.Method; 59 import java.util.Enumeration; 60 61 // Turbine 62 import org.apache.turbine.services.resources.TurbineResources; 63 import org.apache.turbine.util.ParameterParser; 64 import org.apache.turbine.util.RunData; 65 66 67 /*** 68 * <p> 69 * 70 * This is an alternative to the Action class that allows you to do 71 * event based actions. Essentially, you label all your submit buttons 72 * with the prefix of "eventSubmit_" and the suffix of "methodName". 73 * For example, "eventSubmit_doDelete". Then any class that subclasses 74 * this class will get its "doDelete(RunData data)" method executed. 75 * If for any reason, it was not able to execute the method, it will 76 * fall back to executing the doPeform() method which is required to 77 * be implemented. 78 * 79 * <p> 80 * 81 * Limitations: 82 * 83 * <p> 84 * 85 * Because ParameterParser makes all the key values lowercase, we have 86 * to do some work to format the string into a method name. For 87 * example, a button name eventSubmit_doDelete gets converted into 88 * eventsubmit_dodelete. Thus, we need to form some sort of naming 89 * convention so that dodelete can be turned into doDelete. 90 * 91 * <p> 92 * 93 * Thus, the convention is this: 94 * 95 * <ul> 96 * <li>The variable name MUST have the prefix "eventSubmit_".</li> 97 * <li>The variable name after the prefix MUST begin with the letters 98 * "do".</li> 99 * <li>The first letter after the "do" will be capitalized and the 100 * rest will be lowercase</li> 101 * </ul> 102 * 103 * If you follow these conventions, then you should be ok with your 104 * method naming in your Action class. 105 * 106 * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens </a> 107 * @version $Id: ActionEvent.java,v 1.1.1.1 2001/08/16 05:08:29 jvanzyl Exp $ 108 */ 109 public abstract class ActionEvent extends Action 110 { 111 /*** 112 * You need to implement this in your classes that extend this class. 113 * 114 * @param data Turbine information. 115 * @exception Exception a generic exception. 116 */ 117 public abstract void doPerform( RunData data ) 118 throws Exception; 119 120 /*** The name of the button to look for. */ 121 protected static final String BUTTON = "eventSubmit_"; 122 /*** The length of the button to look for. */ 123 protected static final int BUTTON_LENGTH = BUTTON.length(); 124 /*** The prefix of the method name. */ 125 protected static final String METHOD_NAME_PREFIX = "do"; 126 /*** The length of the method name. */ 127 protected static final int METHOD_NAME_LENGTH = METHOD_NAME_PREFIX.length(); 128 /*** The length of the button to look for. */ 129 protected static final int LENGTH = BUTTON.length(); 130 131 /*** 132 * This overrides the default Action.perform() to execute the 133 * doEvent() method. If that fails, then it will execute the 134 * doPerform() method instead. 135 * 136 * @param data Turbine information. 137 * @exception Exception a generic exception. 138 */ 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 } 151 152 /*** 153 * This method should be called to execute the event based system. 154 * 155 * @param data Turbine information. 156 * @exception Exception a generic exception. 157 */ 158 public void executeEvents(RunData data) 159 throws Exception 160 { 161 // Name of the button. 162 String theButton = null; 163 // Parameter parser. 164 ParameterParser pp = data.getParameters(); 165 // The arguments to pass to the method to execute. 166 Object[] args = new Object[1]; 167 // The arguments to the method to find. 168 Class[] classes = new Class[1]; 169 classes[0] = RunData.class; 170 171 String button = pp.convert(BUTTON); 172 173 // Loop through and find the button. 174 for (Enumeration e = pp.keys() ; e.hasMoreElements() ;) 175 { 176 String key = (String) e.nextElement(); 177 if (key.startsWith(button)) 178 { 179 theButton = formatString(key); 180 break; 181 } 182 } 183 184 if (theButton == null) 185 throw new NoSuchMethodException("ActionEvent: The button was null"); 186 187 Method method = getClass().getMethod(theButton, classes); 188 args[0] = data; 189 method.invoke(this, args ); 190 } 191 192 /*** 193 * This method does the conversion of the lowercase method name 194 * into the proper case. 195 * 196 * @param input The unconverted method name. 197 * @return A string with the method name in the proper case. 198 */ 199 protected final String formatString(String input) 200 { 201 String fold = 202 TurbineResources.getString(ParameterParser.URL_CASE_FOLDING, "") 203 .toLowerCase(); 204 if ((fold == null) || 205 (fold.length()==0) || 206 (! fold.equals(ParameterParser.URL_CASE_FOLDING_NONE))) 207 { 208 String tmp = input; 209 210 // Chop off suffixes (for image type) 211 if (input.endsWith(".x") || input.endsWith(".y")) 212 { 213 tmp = tmp.substring(0, input.length() - 2); 214 } 215 // Chop off the prefixes. 216 tmp = tmp.substring(BUTTON_LENGTH + METHOD_NAME_LENGTH); 217 218 return (METHOD_NAME_PREFIX + firstLetterCaps(tmp)); 219 } 220 else 221 { 222 return input.substring(BUTTON_LENGTH); 223 } 224 } 225 226 /*** 227 * Makes the first letter caps and the rest lowercase. 228 * 229 * @param data The input string. 230 * @return A string with the described case. 231 */ 232 private final String firstLetterCaps( String data ) 233 { 234 String firstLetter = data.substring(0, 1).toUpperCase(); 235 String restLetters = data.substring(1).toLowerCase(); 236 return firstLetter + restLetters; 237 } 238 }

This page was automatically generated by Maven