KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > iv > flash > util > CommandExecutor


1 /*
2  * $Id: CommandExecutor.java,v 1.5 2002/04/04 08:14:25 skavish Exp $
3  *
4  * ===========================================================================
5  *
6  * The JGenerator Software License, Version 1.0
7  *
8  * Copyright (c) 2000 Dmitry Skavish (skavish@usa.net). All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions 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, if
22  * any, must include the following acknowlegement:
23  * "This product includes software developed by Dmitry Skavish
24  * (skavish@usa.net, http://www.flashgap.com/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The name "The JGenerator" must not be used to endorse or promote
29  * products derived from this software without prior written permission.
30  * For written permission, please contact skavish@usa.net.
31  *
32  * 5. Products derived from this software may not be called "The JGenerator"
33  * nor may "The JGenerator" appear in their names without prior written
34  * permission of Dmitry Skavish.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL DMITRY SKAVISH OR THE OTHER
40  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  *
49  */

50
51 package org.openlaszlo.iv.flash.util;
52
53 import org.openlaszlo.iv.flash.api.*;
54 import org.openlaszlo.iv.flash.context.*;
55
56 import java.util.*;
57 import java.text.*;
58 import java.io.*;
59 import java.lang.reflect.*;
60
61 /**
62  * Standard executor of context commands.
63  * <P>
64  * To add new command you have to add new method to this class
65  * or to one of its subclasses. Name of the command will be name of the method.
66  * If command was invoked without parameters then method without parameters will be
67  * called, if command was invoked with several parameters then method with corresponding
68  * number of parameters will be called. If none of these was succeded then method with
69  * parameter Vector will be called.
70  *
71  * @author Dmitry Skavish
72  * @see CommandContext
73  */

74 public class CommandExecutor {
75
76     protected FlashFile flashFile;
77
78     /**
79      * Create executor in environment of given flash file.
80      *
81      * @param file environment for executor
82      */

83     public CommandExecutor( FlashFile flashFile ) {
84         this.flashFile = flashFile;
85     }
86
87     public CommandExecutor() {
88     }
89
90     public void setFlashFile( FlashFile flashFile ) {
91         this.flashFile = flashFile;
92     }
93
94     public FlashFile getFlashFile() {
95         return flashFile;
96     }
97
98     /**
99      * Executes command.
100      *
101      * @param context context from which the command was called
102      * @param name name fo the command
103      * @param parms parameters or null
104      * @return result of the command execution
105      */

106     public String JavaDoc execute( Context context, String JavaDoc name, Vector parms ) {
107         try {
108             Class JavaDoc c = getClass();
109             Method m;
110             Object JavaDoc[] mp;
111             try {
112                 if( parms == null ) {
113                     m = c.getMethod( name, new Class JavaDoc[] {Context.class} );
114                     mp = null;
115                 } else {
116                     Class JavaDoc[] cmp = new Class JavaDoc[parms.size()+1];
117                     cmp[0] = Context.class;
118                     for( int i=1; i<cmp.length; i++ )
119                         cmp[i] = String JavaDoc.class;
120                     m = c.getMethod( name, cmp );
121                     mp = new Object JavaDoc[cmp.length];
122                     mp[0] = context;
123                     for( int i=1; i<cmp.length; i++ )
124                         mp[i] = parms.elementAt(i-1);
125                 }
126             } catch( NoSuchMethodException JavaDoc e ) {
127                 m = c.getMethod( name, new Class JavaDoc[] {Context.class, Vector.class} );
128                 mp = new Object JavaDoc[] {context, parms};
129             }
130             Object JavaDoc res = m.invoke( this, mp );
131             if( res == null ) return "";
132             return res.toString();
133         } catch( NoSuchMethodException JavaDoc e ) {
134             Log.logRB(Resource.INLINECMDNOTFOUND, new Object JavaDoc[] {name});
135             return "";
136         } catch( IllegalAccessException JavaDoc e ) {
137             Log.logRB(Resource.INLINECMDERROR, new Object JavaDoc[] {name}, e);
138             return "";
139         } catch( InvocationTargetException e ) {
140             Log.logRB(Resource.INLINECMDERROR, new Object JavaDoc[] {name}, e);
141             return "";
142         }
143     }
144
145     /*---------------------------------------------------------------------------------*/
146     /* C O M M A N D S */
147     /*---------------------------------------------------------------------------------*/
148
149     /**
150      * Returns current time and date
151      *
152      * @return current time and date
153      */

154     public String JavaDoc date( Context context ) {
155         return new Date().toLocaleString();
156     }
157
158     /**
159      * Returns current time and date formatted according to given template.<p>
160      * Examples:
161      * <UL>
162      * <LI>MM/dd/yyyy hh:mm:ss z
163      * </UL>
164      *
165      * @param format date format
166      * @return current formatted date
167      */

168     public String JavaDoc date( Context context, String JavaDoc format ) {
169         SimpleDateFormat formatter = new SimpleDateFormat(format);
170         return formatter.format( new Date() );
171     }
172
173     /**
174      * Return JGenerator version
175      *
176      * @return JGenerator version
177      */

178     public String JavaDoc version( Context context ) {
179         return Util.getVersion();
180     }
181
182     /**
183      * Returns substring of specified string
184      *
185      * @param s specified string
186      * @param from start index
187      * @return substring
188      */

189     public String JavaDoc substr( Context context, String JavaDoc s, String JavaDoc from ) {
190         //System.out.println( "substr('"+s+"', '"+from+"')" );
191
try {
192             return s.substring( Util.toInt(from, -1) );
193         } catch( Exception JavaDoc e ) {
194             Log.logRB(e);
195             return "";
196         }
197     }
198
199     /**
200      * Returns substring of specified string
201      *
202      * @param s specified string
203      * @param from start index
204      * @param to to index
205      * @return substring
206      */

207     public String JavaDoc substr( Context context, String JavaDoc s, String JavaDoc from, String JavaDoc to ) {
208         //System.out.println( "substr('"+s+"', '"+from+"', '"+to+"')" );
209
try {
210             return s.substring( Util.toInt(from, -1), Util.toInt(to, -1) );
211         } catch( Exception JavaDoc e ) {
212             Log.logRB(e);
213             return "";
214         }
215     }
216
217     /**
218      * Returns length of the specified string
219      *
220      * @param s specified string
221      * @return length of the specified string
222      */

223     public String JavaDoc len( Context context, String JavaDoc s ) {
224         return Integer.toString(s.length());
225     }
226
227     /**
228      * Converts hex to decimal: 20 -> 32
229      *
230      * @param s string in hex representation
231      * @return decimal representation
232      */

233     public String JavaDoc h2d( Context context, String JavaDoc s ) {
234         return Integer.toString(Integer.parseInt(s, 16));
235     }
236
237     /**
238      * Converts decimal to hex: 32 -> 20
239      *
240      * @param s string in decimal representation
241      * @return hex representation
242      */

243     public String JavaDoc d2h( Context context, String JavaDoc s ) {
244         return Integer.toHexString(Integer.parseInt(s));
245     }
246
247     /**
248      * Converts decimal to binary: 4 -> 100
249      *
250      * @param s string in decimal representation
251      * @return binary representation
252      */

253     public String JavaDoc d2b( Context context, String JavaDoc s ) {
254         return Integer.toBinaryString(Integer.parseInt(s));
255     }
256
257     /**
258      * Converts color to its web color representation
259      * <P>
260      * For example:
261      * <P>
262      * red -> #ffff0000
263      * green -> #ff00ff00
264      *
265      * @param s color specified either by name or by webcolor
266      * @return web representation of color
267      */

268     public String JavaDoc color2web( Context context, String JavaDoc s ) {
269         AlphaColor c = Util.toColor(s, AlphaColor.black);
270         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(10);
271         sb.append('#');
272         sb.append(Util.b2h(c.getAlpha()));
273         sb.append(Util.b2h(c.getRed()));
274         sb.append(Util.b2h(c.getGreen()));
275         sb.append(Util.b2h(c.getBlue()));
276         return sb.toString();
277     }
278
279     /**
280      * Returns red component of specified color in decimal
281      *
282      * @param s specified color either by its name or by web color
283      * @return red component
284      */

285     public String JavaDoc red( Context context, String JavaDoc s ) {
286         AlphaColor c = Util.toColor(s, AlphaColor.black);
287         return Integer.toString(c.getRed());
288     }
289
290     /**
291      * Returns green component of specified color in decimal
292      *
293      * @param s specified color either by its name or by web color
294      * @return green component
295      */

296     public String JavaDoc green( Context context, String JavaDoc s ) {
297         AlphaColor c = Util.toColor(s, AlphaColor.black);
298         return Integer.toString(c.getGreen());
299     }
300
301     /**
302      * Returns blue component of specified color in decimal
303      *
304      * @param s specified color either by its name or by web color
305      * @return blue component
306      */

307     public String JavaDoc blue( Context context, String JavaDoc s ) {
308         AlphaColor c = Util.toColor(s, AlphaColor.black);
309         return Integer.toString(c.getBlue());
310     }
311
312     /**
313      * Returns alpha component of specified color in decimal
314      *
315      * @param s specified color either by its name or by web color
316      * @return alpha component
317      */

318     public String JavaDoc alpha( Context context, String JavaDoc s ) {
319         AlphaColor c = Util.toColor(s, AlphaColor.black);
320         return Integer.toString(c.getAlpha());
321     }
322
323     /**
324      * Loads and executes javascript file
325      *
326      * @param fileName file with javascript
327      * @return whatever was printed during execution of javascript
328      */

329     public String JavaDoc js( Context context, String JavaDoc fileName ) {
330         String JavaDoc s = fileName;
331         int idx = s.indexOf('?');
332         if( idx != -1 ) {
333             fileName = s.substring(0, idx);
334         }
335         fileName = fileName.trim();
336
337         File file = new File(fileName);
338         if( !file.isAbsolute() )
339             file = new File(flashFile.getFileDir(), file.getPath());
340         fileName = file.getAbsolutePath();
341
342         Hashtable parms = null;
343         if( idx >= 0 ) parms = Util.parseUrlParms(s, idx);
344
345         String JavaDoc[] args = new String JavaDoc[parms!=null?parms.size():0];
346         if( parms != null ) {
347             int i=0;
348             for( Enumeration e = parms.keys(); e.hasMoreElements(); ) {
349                 args[i++] = (String JavaDoc) e.nextElement();
350             }
351         }
352
353         String JavaDoc res = Util.executeJSFile(context, fileName, args);
354         return res;
355     }
356
357 }
358
359
360
361
Popular Tags