1 50 51 package org.openlaszlo.iv.flash.context; 52 53 import org.openlaszlo.iv.flash.util.CommandExecutor; 54 import java.util.*; 55 56 62 public class CommandContext extends StandardContext { 63 64 private CommandExecutor executor; 65 66 71 public CommandContext( CommandExecutor executor ) { 72 this( null, executor ); 73 } 74 75 81 public CommandContext( Context parent, CommandExecutor executor ) { 82 setParent( parent ); 83 this.executor = executor; 84 } 85 86 public String getValue( String cmd ) { 87 if( cmd.length() != 0 && cmd.charAt(0) == '$' ) { 88 String res = executeCommand(this, cmd); 89 if( res != null ) return res; 90 } 91 92 return super.getValue( cmd ); 93 } 94 95 106 public String executeCommand( Context context, String cmd ) { 107 int idx = cmd.indexOf('('); 109 String name; 110 Vector parms = null; 111 if( idx == -1 ) { 112 name = cmd.substring(1).trim(); 113 } else { 114 name = cmd.substring(1,idx).trim(); 115 int i = idx+1; 116 parms = new Vector(); 117 int l = cmd.length(); 118 int cnt = 0; 119 int start = i; 120 while( i<l && cnt>=0 ) { 121 char ch = cmd.charAt(i); 122 switch( ch ) { 123 case '(': 124 cnt++; 125 break; 126 case ')': 127 cnt--; 128 break; 129 case ',': 130 if( cnt == 0 ) { 131 String s = cmd.substring(start,i).trim(); 132 parms.addElement(s); 133 start = i+1; 134 } 135 break; 136 } 137 i++; 138 } 139 i--; 140 String s = cmd.substring(start,i).trim(); 141 if( parms.size() != 0 || s.length() != 0 ) { 142 parms.addElement(s); 143 } else { 144 parms = null; 145 } 146 } 147 if( parms != null ) { 148 for( int i=0; i<parms.size(); i++ ) { 149 String c = (String ) parms.elementAt(i); 150 if( c.length() != 0 && c.charAt(0) == '$' ) { 151 String res = executeCommand(context, c); 152 if( res == null ) res = ""; 153 parms.setElementAt(res,i); 154 } 155 } 156 } 157 return executor.execute(context, name, parms); 158 } 159 160 } 161 162 163 | Popular Tags |