1 50 51 package org.openlaszlo.iv.flash.context; 52 53 import java.io.*; 54 import java.util.*; 55 import java.lang.reflect.*; 56 import org.openlaszlo.iv.flash.util.*; 57 58 65 66 public abstract class Context { 67 68 private Context parent; 69 70 75 public void setParent( Context parent ) { 76 this.parent = parent; 77 } 78 79 84 public Context getParent() { 85 return parent; 86 } 87 88 94 public abstract String getValue( String name ); 95 96 103 protected String getValueFromParent( String expr ) { 104 if ( parent != null ) { 105 return parent.getValue( expr ); 106 } else { 107 return null; 108 } 109 } 110 111 116 public CommandContext getCommandContext() { 117 if( this instanceof CommandContext ) return (CommandContext) this; 118 if( parent != null ) return parent.getCommandContext(); 119 return null; 120 } 121 122 131 public String apply( String s ) { 132 if( s == null ) return s; 133 int start = s.indexOf('{'); 134 if( start == -1 ) return s; 135 136 int pos = 0; 138 int from = 0; 139 int slen = s.length(); 140 int extra = 10; 141 char[] sa = new char[slen + extra]; 142 143 for(;;) { 144 int start1 = start+1; 145 if( start1 < slen && s.charAt(start1) == '{' ) { 146 s.getChars(from, start1, sa, pos); 148 pos += start1-from; 149 from = start1+1; 150 } else { 151 int cnt = 1; 152 int end = start1; 153 while( end<slen && cnt>0 ) { 154 char ch = s.charAt(end++); 155 if( ch == '}' ) cnt--; 156 else if( ch == '{' ) cnt++; 157 } 158 end--; 159 if( end >= slen ) { 160 s.getChars(from, start, sa, pos); 161 pos += start-from; 162 break; 163 } 164 s.getChars(from, end, sa, pos); 165 pos += start-from; 166 167 String varName = new String ( sa, pos+1, end-start1 ); 169 String value = null; 170 171 int varlen = varName.length(); 172 if( varlen>0 ) { 173 char ch = varName.charAt(0); 174 if( ch == '$' ) { String cmdCall = apply(varName).trim(); 176 CommandContext cmdexec = getCommandContext(); 177 if( cmdexec == null ) { 178 value = getValue(cmdCall); 179 } else { 180 value = cmdexec.executeCommand(this, cmdCall); 181 } 182 } else if( ch == '#' && varlen > 1 ) { String js_text = varName.substring(1); 184 value = Util.executeJSString(this, js_text, null); 185 } else { 186 String varName1 = apply(varName).trim(); 187 value = getValue(varName1); 188 } 189 } 190 191 if( value != null ) { 192 int vlen = value.length(); 193 int vnlen = varName.length() + 2; 195 if( vlen > vnlen ) { 197 extra -= (vlen - vnlen); 198 if( extra < 0 ) { 199 extra = -extra; 200 char[] t = new char[sa.length + extra * 2]; 201 System.arraycopy(sa, 0, t, 0, sa.length); 202 sa = t; 203 } 204 } 205 value.getChars(0, vlen, sa, pos); 206 pos += vlen; 207 } 208 from = end+1; 209 } 210 start = s.indexOf('{', from); 211 if( start == -1 ) { 212 s.getChars(from, slen, sa, pos); 213 pos += slen-from; 214 break; 215 } 216 } 217 218 return new String (sa, 0, pos); 219 } 220 } 221 | Popular Tags |