1 18 package org.apache.beehive.netui.script.el.tokens; 19 20 import org.apache.beehive.netui.util.internal.InternalStringBuilder; 21 22 import java.util.Map ; 23 24 import org.apache.beehive.netui.util.logging.Logger; 25 26 29 public class MapKeyToken 30 extends ExpressionToken { 31 32 private static final Logger LOGGER = Logger.getInstance(MapKeyToken.class); 33 34 private String _identifier = null; 35 private boolean _dblQuote = false; 36 37 public MapKeyToken(String identifier) { 38 this._identifier = identifier; 39 40 if(identifier.startsWith("\"")) 41 _dblQuote = true; 42 43 this._identifier = convertToEcmaScriptString(this._identifier.substring(1, identifier.length() - 1)); 46 } 47 48 52 private final String convertToEcmaScriptString(String string) { 53 CharSequence cs = string; 54 55 int len = cs.length(); 56 InternalStringBuilder buf = new InternalStringBuilder(len); 57 for(int i = 0; i < len; i++) { 58 char c = cs.charAt(i); 59 if(c == '\\' && i + 1 < len) { 64 i++; 65 66 c = cs.charAt(i); 68 69 if(c == 'b') 70 c = '\b'; 71 else if(c == 't') 72 c = '\t'; 73 else if(c == 'n') 74 c = '\n'; 75 else if(c == 'f') 77 c = '\f'; 78 else if(c == 'r') c = '\r'; 79 } 81 82 buf.append(c); 83 } 84 85 if(LOGGER.isDebugEnabled()) LOGGER.debug("new _identifier: " + buf.toString()); 86 87 return buf.toString(); 88 } 89 90 public void update(Object root, Object newValue) { 91 if(root instanceof Map ) 92 mapUpdate((Map )root, _identifier, newValue); 93 else 94 beanUpdate(root, _identifier, newValue); 95 } 96 97 public Object evaluate(Object value) { 98 if(value instanceof Map ) 99 return mapLookup((Map )value, _identifier); 100 else 101 return beanLookup(value, _identifier); 102 } 103 104 public String getTokenString() { 105 if(_dblQuote) 106 return "[\"" + _identifier + "\"]"; 107 else 108 return "['" + _identifier + "']"; 109 } 110 111 public String toString() { 112 return _identifier; 113 } 114 } 115 | Popular Tags |