1 50 51 package org.openlaszlo.iv.flash.context; 52 53 import org.apache.commons.jexl.Expression; 54 import org.apache.commons.jexl.ExpressionFactory; 55 import org.apache.commons.jexl.context.HashMapContext; 56 57 import java.util.ArrayList ; 58 import java.util.Collection ; 59 import java.util.List ; 60 import java.util.ListIterator ; 61 import java.util.Map ; 62 import java.util.Set ; 63 64 74 public class BeanContext extends GraphContext implements Map 75 { 76 77 public static final String ROOT_ITEM_KEY = "root"; 78 79 80 private HashMapContext jexlContext = new HashMapContext(); 81 82 85 public BeanContext() 86 { 87 } 88 89 95 public BeanContext( Context context, Map values ) 96 { 97 setParent( context ); 98 99 if ( values != null ) 100 { 101 this.jexlContext.setVars( values ); 102 } 103 } 104 105 116 117 public String getValue( String path ) 118 { 119 121 Object o = evaluatePath( path ); 122 123 127 if ( o == null ) 128 { 129 return getValueFromParent( path ); 130 } 131 132 138 else 139 { 140 return o.toString(); 141 } 142 } 143 144 159 public List getValueList( String path ) 160 { 161 163 Object o = evaluatePath( path ); 164 165 169 if ( o == null ) 170 { 171 return getValueListFromParent( path ); 172 } 173 174 178 ArrayList contextList = new ArrayList (); 179 180 if ( o instanceof List ) 181 { 182 ListIterator iter = ( ( List ) o ).listIterator(); 183 184 while ( iter.hasNext() ) 185 { 186 addToContextList( iter.next(), contextList ); 187 } 188 } 189 else 190 { 191 addToContextList( o, contextList ); 192 } 193 194 return contextList; 195 } 196 197 203 private void addToContextList( Object o, List contextList ) 204 { 205 BeanContext newContext = new BeanContext( this, null ); 206 207 newContext.put( ROOT_ITEM_KEY, o ); 208 209 contextList.add( newContext ); 210 } 211 212 218 private Object evaluatePath( String path ) 219 { 220 try 221 { 222 224 Expression expr = ExpressionFactory.createExpression( path ); 225 226 228 Object o = expr.evaluate( jexlContext ); 229 230 return o; 231 } 232 catch ( Exception e ) 233 { 234 237 return null; 238 } 239 } 240 241 243 public int size() 244 { 245 return jexlContext.size(); 246 } 247 248 public boolean isEmpty() 249 { 250 return jexlContext.isEmpty(); 251 } 252 253 public boolean containsKey( Object key ) 254 { 255 return jexlContext.containsKey( key ); 256 } 257 258 public boolean containsValue( Object value ) 259 { 260 return jexlContext.containsValue( value ); 261 } 262 263 public Object get( Object key ) 264 { 265 return jexlContext.get( key ); 266 } 267 268 public Object put( Object key, Object value ) 269 { 270 return jexlContext.put( key, value ); 271 } 272 273 public Object remove( Object key ) 274 { 275 return jexlContext.remove( key ); 276 } 277 278 public void putAll( Map t ) 279 { 280 jexlContext.putAll( t ); 281 } 282 283 public void clear() 284 { 285 jexlContext.clear(); 286 } 287 288 public Set keySet() 289 { 290 return jexlContext.keySet(); 291 } 292 293 public Collection values() 294 { 295 return jexlContext.values(); 296 } 297 298 public Set entrySet() 299 { 300 return jexlContext.entrySet(); 301 } 302 303 public boolean equals( Object o ) 304 { 305 return jexlContext.equals( o ); 306 } 307 308 public int hashCode() 309 { 310 return jexlContext.hashCode(); 311 } 312 313 } 314 | Popular Tags |