001: /*
002: * $Id: CommandExecutor.java,v 1.5 2002/04/04 08:14:25 skavish Exp $
003: *
004: * ===========================================================================
005: *
006: * The JGenerator Software License, Version 1.0
007: *
008: * Copyright (c) 2000 Dmitry Skavish (skavish@usa.net). All rights reserved.
009: *
010: * Redistribution and use in source and binary forms, with or without
011: * modification, are permitted provided that the following conditions are met:
012: *
013: * 1. Redistributions of source code must retain the above copyright
014: * notice, this list of conditions and the following disclaimer.
015: *
016: * 2. Redistributions in binary form must reproduce the above copyright
017: * notice, this list of conditions and the following disclaimer in
018: * the documentation and/or other materials provided with the
019: * distribution.
020: *
021: * 3. The end-user documentation included with the redistribution, if
022: * any, must include the following acknowlegement:
023: * "This product includes software developed by Dmitry Skavish
024: * (skavish@usa.net, http://www.flashgap.com/)."
025: * Alternately, this acknowlegement may appear in the software itself,
026: * if and wherever such third-party acknowlegements normally appear.
027: *
028: * 4. The name "The JGenerator" must not be used to endorse or promote
029: * products derived from this software without prior written permission.
030: * For written permission, please contact skavish@usa.net.
031: *
032: * 5. Products derived from this software may not be called "The JGenerator"
033: * nor may "The JGenerator" appear in their names without prior written
034: * permission of Dmitry Skavish.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL DMITRY SKAVISH OR THE OTHER
040: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: *
049: */
050:
051: package org.openlaszlo.iv.flash.util;
052:
053: import org.openlaszlo.iv.flash.api.*;
054: import org.openlaszlo.iv.flash.context.*;
055:
056: import java.util.*;
057: import java.text.*;
058: import java.io.*;
059: import java.lang.reflect.*;
060:
061: /**
062: * Standard executor of context commands.
063: * <P>
064: * To add new command you have to add new method to this class
065: * or to one of its subclasses. Name of the command will be name of the method.
066: * If command was invoked without parameters then method without parameters will be
067: * called, if command was invoked with several parameters then method with corresponding
068: * number of parameters will be called. If none of these was succeded then method with
069: * parameter Vector will be called.
070: *
071: * @author Dmitry Skavish
072: * @see CommandContext
073: */
074: public class CommandExecutor {
075:
076: protected FlashFile flashFile;
077:
078: /**
079: * Create executor in environment of given flash file.
080: *
081: * @param file environment for executor
082: */
083: public CommandExecutor(FlashFile flashFile) {
084: this .flashFile = flashFile;
085: }
086:
087: public CommandExecutor() {
088: }
089:
090: public void setFlashFile(FlashFile flashFile) {
091: this .flashFile = flashFile;
092: }
093:
094: public FlashFile getFlashFile() {
095: return flashFile;
096: }
097:
098: /**
099: * 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 execute(Context context, String name, Vector parms) {
107: try {
108: Class c = getClass();
109: Method m;
110: Object[] mp;
111: try {
112: if (parms == null) {
113: m = c
114: .getMethod(name,
115: new Class[] { Context.class });
116: mp = null;
117: } else {
118: Class[] cmp = new Class[parms.size() + 1];
119: cmp[0] = Context.class;
120: for (int i = 1; i < cmp.length; i++)
121: cmp[i] = String.class;
122: m = c.getMethod(name, cmp);
123: mp = new Object[cmp.length];
124: mp[0] = context;
125: for (int i = 1; i < cmp.length; i++)
126: mp[i] = parms.elementAt(i - 1);
127: }
128: } catch (NoSuchMethodException e) {
129: m = c.getMethod(name, new Class[] { Context.class,
130: Vector.class });
131: mp = new Object[] { context, parms };
132: }
133: Object res = m.invoke(this , mp);
134: if (res == null)
135: return "";
136: return res.toString();
137: } catch (NoSuchMethodException e) {
138: Log
139: .logRB(Resource.INLINECMDNOTFOUND,
140: new Object[] { name });
141: return "";
142: } catch (IllegalAccessException e) {
143: Log
144: .logRB(Resource.INLINECMDERROR,
145: new Object[] { name }, e);
146: return "";
147: } catch (InvocationTargetException e) {
148: Log
149: .logRB(Resource.INLINECMDERROR,
150: new Object[] { name }, e);
151: return "";
152: }
153: }
154:
155: /*---------------------------------------------------------------------------------*/
156: /* C O M M A N D S */
157: /*---------------------------------------------------------------------------------*/
158:
159: /**
160: * Returns current time and date
161: *
162: * @return current time and date
163: */
164: public String date(Context context) {
165: return new Date().toLocaleString();
166: }
167:
168: /**
169: * Returns current time and date formatted according to given template.<p>
170: * Examples:
171: * <UL>
172: * <LI>MM/dd/yyyy hh:mm:ss z
173: * </UL>
174: *
175: * @param format date format
176: * @return current formatted date
177: */
178: public String date(Context context, String format) {
179: SimpleDateFormat formatter = new SimpleDateFormat(format);
180: return formatter.format(new Date());
181: }
182:
183: /**
184: * Return JGenerator version
185: *
186: * @return JGenerator version
187: */
188: public String version(Context context) {
189: return Util.getVersion();
190: }
191:
192: /**
193: * Returns substring of specified string
194: *
195: * @param s specified string
196: * @param from start index
197: * @return substring
198: */
199: public String substr(Context context, String s, String from) {
200: //System.out.println( "substr('"+s+"', '"+from+"')" );
201: try {
202: return s.substring(Util.toInt(from, -1));
203: } catch (Exception e) {
204: Log.logRB(e);
205: return "";
206: }
207: }
208:
209: /**
210: * Returns substring of specified string
211: *
212: * @param s specified string
213: * @param from start index
214: * @param to to index
215: * @return substring
216: */
217: public String substr(Context context, String s, String from,
218: String to) {
219: //System.out.println( "substr('"+s+"', '"+from+"', '"+to+"')" );
220: try {
221: return s
222: .substring(Util.toInt(from, -1), Util.toInt(to, -1));
223: } catch (Exception e) {
224: Log.logRB(e);
225: return "";
226: }
227: }
228:
229: /**
230: * Returns length of the specified string
231: *
232: * @param s specified string
233: * @return length of the specified string
234: */
235: public String len(Context context, String s) {
236: return Integer.toString(s.length());
237: }
238:
239: /**
240: * Converts hex to decimal: 20 -> 32
241: *
242: * @param s string in hex representation
243: * @return decimal representation
244: */
245: public String h2d(Context context, String s) {
246: return Integer.toString(Integer.parseInt(s, 16));
247: }
248:
249: /**
250: * Converts decimal to hex: 32 -> 20
251: *
252: * @param s string in decimal representation
253: * @return hex representation
254: */
255: public String d2h(Context context, String s) {
256: return Integer.toHexString(Integer.parseInt(s));
257: }
258:
259: /**
260: * Converts decimal to binary: 4 -> 100
261: *
262: * @param s string in decimal representation
263: * @return binary representation
264: */
265: public String d2b(Context context, String s) {
266: return Integer.toBinaryString(Integer.parseInt(s));
267: }
268:
269: /**
270: * Converts color to its web color representation
271: * <P>
272: * For example:
273: * <P>
274: * red -> #ffff0000
275: * green -> #ff00ff00
276: *
277: * @param s color specified either by name or by webcolor
278: * @return web representation of color
279: */
280: public String color2web(Context context, String s) {
281: AlphaColor c = Util.toColor(s, AlphaColor.black);
282: StringBuffer sb = new StringBuffer(10);
283: sb.append('#');
284: sb.append(Util.b2h(c.getAlpha()));
285: sb.append(Util.b2h(c.getRed()));
286: sb.append(Util.b2h(c.getGreen()));
287: sb.append(Util.b2h(c.getBlue()));
288: return sb.toString();
289: }
290:
291: /**
292: * Returns red component of specified color in decimal
293: *
294: * @param s specified color either by its name or by web color
295: * @return red component
296: */
297: public String red(Context context, String s) {
298: AlphaColor c = Util.toColor(s, AlphaColor.black);
299: return Integer.toString(c.getRed());
300: }
301:
302: /**
303: * Returns green component of specified color in decimal
304: *
305: * @param s specified color either by its name or by web color
306: * @return green component
307: */
308: public String green(Context context, String s) {
309: AlphaColor c = Util.toColor(s, AlphaColor.black);
310: return Integer.toString(c.getGreen());
311: }
312:
313: /**
314: * Returns blue component of specified color in decimal
315: *
316: * @param s specified color either by its name or by web color
317: * @return blue component
318: */
319: public String blue(Context context, String s) {
320: AlphaColor c = Util.toColor(s, AlphaColor.black);
321: return Integer.toString(c.getBlue());
322: }
323:
324: /**
325: * Returns alpha component of specified color in decimal
326: *
327: * @param s specified color either by its name or by web color
328: * @return alpha component
329: */
330: public String alpha(Context context, String s) {
331: AlphaColor c = Util.toColor(s, AlphaColor.black);
332: return Integer.toString(c.getAlpha());
333: }
334:
335: /**
336: * Loads and executes javascript file
337: *
338: * @param fileName file with javascript
339: * @return whatever was printed during execution of javascript
340: */
341: public String js(Context context, String fileName) {
342: String s = fileName;
343: int idx = s.indexOf('?');
344: if (idx != -1) {
345: fileName = s.substring(0, idx);
346: }
347: fileName = fileName.trim();
348:
349: File file = new File(fileName);
350: if (!file.isAbsolute())
351: file = new File(flashFile.getFileDir(), file.getPath());
352: fileName = file.getAbsolutePath();
353:
354: Hashtable parms = null;
355: if (idx >= 0)
356: parms = Util.parseUrlParms(s, idx);
357:
358: String[] args = new String[parms != null ? parms.size() : 0];
359: if (parms != null) {
360: int i = 0;
361: for (Enumeration e = parms.keys(); e.hasMoreElements();) {
362: args[i++] = (String) e.nextElement();
363: }
364: }
365:
366: String res = Util.executeJSFile(context, fileName, args);
367: return res;
368: }
369:
370: }
|