1 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 74 public class CommandExecutor { 75 76 protected FlashFile flashFile; 77 78 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 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.getMethod( name, new Class [] {Context.class} ); 114 mp = null; 115 } else { 116 Class [] cmp = new Class [parms.size()+1]; 117 cmp[0] = Context.class; 118 for( int i=1; i<cmp.length; i++ ) 119 cmp[i] = String .class; 120 m = c.getMethod( name, cmp ); 121 mp = new Object [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 e ) { 127 m = c.getMethod( name, new Class [] {Context.class, Vector.class} ); 128 mp = new Object [] {context, parms}; 129 } 130 Object res = m.invoke( this, mp ); 131 if( res == null ) return ""; 132 return res.toString(); 133 } catch( NoSuchMethodException e ) { 134 Log.logRB(Resource.INLINECMDNOTFOUND, new Object [] {name}); 135 return ""; 136 } catch( IllegalAccessException e ) { 137 Log.logRB(Resource.INLINECMDERROR, new Object [] {name}, e); 138 return ""; 139 } catch( InvocationTargetException e ) { 140 Log.logRB(Resource.INLINECMDERROR, new Object [] {name}, e); 141 return ""; 142 } 143 } 144 145 146 147 148 149 154 public String date( Context context ) { 155 return new Date().toLocaleString(); 156 } 157 158 168 public String date( Context context, String format ) { 169 SimpleDateFormat formatter = new SimpleDateFormat(format); 170 return formatter.format( new Date() ); 171 } 172 173 178 public String version( Context context ) { 179 return Util.getVersion(); 180 } 181 182 189 public String substr( Context context, String s, String from ) { 190 try { 192 return s.substring( Util.toInt(from, -1) ); 193 } catch( Exception e ) { 194 Log.logRB(e); 195 return ""; 196 } 197 } 198 199 207 public String substr( Context context, String s, String from, String to ) { 208 try { 210 return s.substring( Util.toInt(from, -1), Util.toInt(to, -1) ); 211 } catch( Exception e ) { 212 Log.logRB(e); 213 return ""; 214 } 215 } 216 217 223 public String len( Context context, String s ) { 224 return Integer.toString(s.length()); 225 } 226 227 233 public String h2d( Context context, String s ) { 234 return Integer.toString(Integer.parseInt(s, 16)); 235 } 236 237 243 public String d2h( Context context, String s ) { 244 return Integer.toHexString(Integer.parseInt(s)); 245 } 246 247 253 public String d2b( Context context, String s ) { 254 return Integer.toBinaryString(Integer.parseInt(s)); 255 } 256 257 268 public String color2web( Context context, String s ) { 269 AlphaColor c = Util.toColor(s, AlphaColor.black); 270 StringBuffer sb = new StringBuffer (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 285 public String red( Context context, String s ) { 286 AlphaColor c = Util.toColor(s, AlphaColor.black); 287 return Integer.toString(c.getRed()); 288 } 289 290 296 public String green( Context context, String s ) { 297 AlphaColor c = Util.toColor(s, AlphaColor.black); 298 return Integer.toString(c.getGreen()); 299 } 300 301 307 public String blue( Context context, String s ) { 308 AlphaColor c = Util.toColor(s, AlphaColor.black); 309 return Integer.toString(c.getBlue()); 310 } 311 312 318 public String alpha( Context context, String s ) { 319 AlphaColor c = Util.toColor(s, AlphaColor.black); 320 return Integer.toString(c.getAlpha()); 321 } 322 323 329 public String js( Context context, String fileName ) { 330 String 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 [] args = new String [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 ) e.nextElement(); 350 } 351 } 352 353 String res = Util.executeJSFile(context, fileName, args); 354 return res; 355 } 356 357 } 358 359 360 361 | Popular Tags |