KickJava   Java API By Example, From Geeks To Geeks.

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


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

28 public class IdentifierToken
29     extends ExpressionToken {
30
31     private static final Logger LOGGER = Logger.getInstance(IdentifierToken.class);
32     private static final boolean DEBUG_ENABLED = LOGGER.isDebugEnabled();
33
34     private String JavaDoc _identifier = null;
35
36     public IdentifierToken(String JavaDoc identifier) {
37         _identifier = identifier;
38     }
39
40     public Object JavaDoc evaluate(Object JavaDoc value) {
41         /* todo: error handling */
42         if(value == null) {
43             String JavaDoc msg = "Can not evaluate the identifier \"" + _identifier + "\" on a null value object.";
44             if(LOGGER.isErrorEnabled())
45                 LOGGER.error(msg);
46             throw new RuntimeException JavaDoc(msg);
47         }
48
49         if(DEBUG_ENABLED) {
50             LOGGER.debug("evaluate: " + _identifier);
51             LOGGER.debug("value type: " + value.getClass().getName());
52         }
53
54         if(value instanceof Map JavaDoc) {
55             return mapLookup((Map JavaDoc)value, _identifier);
56         } else if(value instanceof List JavaDoc) {
57             int i = parseIndex(_identifier);
58             return listLookup((List JavaDoc)value, i);
59         } else if(value.getClass().isArray()) {
60             int i = parseIndex(_identifier);
61             return arrayLookup(value, i);
62         } else
63             return beanLookup(value, _identifier);
64     }
65
66     public void update(Object JavaDoc root, Object JavaDoc newValue) {
67         /* todo: error handling */
68         if(root == null) {
69             String JavaDoc msg = "Can not update the identifier \"" + _identifier + "\" on a null value object.";
70             if(LOGGER.isErrorEnabled())
71                 LOGGER.error(msg);
72             throw new RuntimeException JavaDoc(msg);
73         }
74
75         if(DEBUG_ENABLED)
76             LOGGER.debug("Update _identifier \"" + _identifier + "\" on object of type: \"" + root.getClass().getName() + "\"");
77
78         if(root instanceof Map JavaDoc)
79             mapUpdate((Map JavaDoc)root, _identifier, newValue);
80         else if(root instanceof List JavaDoc) {
81             int i = parseIndex(_identifier);
82             listUpdate((List JavaDoc)root, i, newValue);
83         } else if(root.getClass().isArray()) {
84             int i = parseIndex(_identifier);
85             arrayUpdate(root, i, newValue);
86         } else
87             beanUpdate(root, _identifier, newValue);
88     }
89
90     public String JavaDoc getTokenString() {
91         return "." + _identifier;
92     }
93
94     public String JavaDoc toString() {
95         return _identifier;
96     }
97 }
98
Popular Tags