KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > script > el > tokens > MapKeyToken


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

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 JavaDoc;
23
24 import org.apache.beehive.netui.util.logging.Logger;
25
26 /**
27  *
28  */

29 public class MapKeyToken
30     extends ExpressionToken {
31
32     private static final Logger LOGGER = Logger.getInstance(MapKeyToken.class);
33
34     private String JavaDoc _identifier = null;
35     private boolean _dblQuote = false;
36
37     public MapKeyToken(String JavaDoc identifier) {
38         this._identifier = identifier;
39
40         if(identifier.startsWith("\""))
41             _dblQuote = true;
42
43         // convert the Java string to an EcmaScript string. Strip the quotes that exist because they're
44
// always there for this token.
45
this._identifier = convertToEcmaScriptString(this._identifier.substring(1, identifier.length() - 1));
46     }
47
48     /**
49      * Given a Java String, this value needs to be converted into a JavaScript compliant String.
50      * See JavaScript: The Definitive Guide for how to do this
51      */

52     private final String JavaDoc convertToEcmaScriptString(String JavaDoc string) {
53         CharSequence JavaDoc 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             // skip the \\ and consume the next character either appending it or turning it back into the single character
60
// that it should have been in the first place.
61
//
62
// if slash and not at the last character...
63
if(c == '\\' && i + 1 < len) {
64                 i++;
65
66                 // skip the slash
67
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 == 'v') c = '\v';
76
else if(c == 'f')
77                     c = '\f';
78                 else if(c == 'r') c = '\r';
79                 // @TODO: unicode escaping...
80
}
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 JavaDoc root, Object JavaDoc newValue) {
91         if(root instanceof Map JavaDoc)
92             mapUpdate((Map JavaDoc)root, _identifier, newValue);
93         else
94             beanUpdate(root, _identifier, newValue);
95     }
96
97     public Object JavaDoc evaluate(Object JavaDoc value) {
98         if(value instanceof Map JavaDoc)
99             return mapLookup((Map JavaDoc)value, _identifier);
100         else
101             return beanLookup(value, _identifier);
102     }
103
104     public String JavaDoc getTokenString() {
105         if(_dblQuote)
106             return "[\"" + _identifier + "\"]";
107         else
108             return "['" + _identifier + "']";
109     }
110
111     public String JavaDoc toString() {
112         return _identifier;
113     }
114 }
115
Popular Tags