KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > modules > actions > TemplateAction


1 package org.apache.turbine.modules.actions;
2
3 /* ====================================================================
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 2001-2003 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 import java.lang.reflect.Method JavaDoc;
58 import java.util.Enumeration JavaDoc;
59
60 import org.apache.fulcrum.parser.ParameterParser;
61 import org.apache.turbine.RunData;
62 import org.apache.turbine.TemplateContext;
63 import org.apache.turbine.modules.ActionEvent;
64
65 /**
66  * If you are using VelocitySite stuff, then your Action's should
67  * extend this class instead of extending the ActionEvent class. The
68  * difference between this class and the ActionEvent class is that
69  * this class will first attempt to execute one of your doMethod's
70  * with a constructor like this:
71  *
72  * <p>doEvent(RunData data, Context context)
73  *
74  * <p>It gets the context from the TemplateInfo.getTemplateContext()
75  * method. If it can't find a method like that, then it will try to
76  * execute the method without the Context in it.
77  *
78  * @author <a HREF="mailto:jon@latchkey.com">Jon S. Stevens</a>
79  * @author <a HREF="mailto:jvanzyl@periapt.com">Jason van Zyl</a>
80  * @version $Id: TemplateAction.java,v 1.5 2004/11/12 10:26:30 epugh Exp $
81  */

82 public abstract class TemplateAction
83     extends ActionEvent
84 {
85     
86     /**
87      * You need to implement this in your classes that extend this
88      * class.
89      *
90      * @param data A Turbine RunData object.
91      * @throws Exception a generic exception.
92      */

93     public void doPerform( RunData data )
94         throws Exception JavaDoc
95     {
96         doPerform(data, getTemplateContext(data));
97     }
98     
99     /**
100      * You can also implement this method instead to easily
101      * get the TemplateContext object.
102      *
103      * @param data A Turbine RunData object.
104      * @param context A Turbine TemplateContext object.
105      * @throws Exception a generic exception.
106      */

107     public void doPerform( RunData data, TemplateContext context )
108         throws Exception JavaDoc
109     {
110     }
111
112     /**
113      * This method is called by the parent ActionEvent
114      * to try and execute an event for this request.
115      *
116      * @param data A Turbine RunData object.
117      * @exception Exception a generic exception.
118      */

119     public void executeEvents(RunData data)
120         throws Exception JavaDoc
121     {
122         // Name of the button.
123
String JavaDoc theButton = null;
124
125         // ParameterParser.
126
ParameterParser pp = data.getParameters();
127
128         String JavaDoc button = pp.convert(BUTTON);
129
130         // Loop through and find the button.
131
for (Enumeration JavaDoc e = pp.keys() ; e.hasMoreElements() ;)
132         {
133             String JavaDoc key = (String JavaDoc) e.nextElement();
134             if (key.startsWith(button))
135             {
136                 theButton = formatString(key);
137                 break;
138             }
139         }
140
141         if (theButton == null)
142         {
143             throw new NoSuchMethodException JavaDoc(
144                 "ActionEvent: The button was null");
145         }
146
147         try
148         {
149             // The arguments to the method to find.
150
Class JavaDoc[] classes = new Class JavaDoc[2];
151             classes[0] = RunData.class;
152             classes[1] = TemplateContext.class;
153
154             // The arguments to pass to the method to execute.
155
Object JavaDoc[] args = new Object JavaDoc[2];
156
157             Method JavaDoc method = getClass().getMethod(theButton, classes);
158             args[0] = data;
159             args[1] = getTemplateContext(data);
160             method.invoke(this, args);
161         }
162         catch (NoSuchMethodException JavaDoc nsme)
163         {
164             // Attempt to execut things the old way..
165
super.executeEvents(data);
166         }
167     }
168 }
169
Popular Tags