KickJava   Java API By Example, From Geeks To Geeks.

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


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

27 public class ArrayIndexToken
28     extends ExpressionToken {
29
30     private static final Logger LOGGER = Logger.getInstance(ArrayIndexToken.class);
31
32     private int _index;
33
34     public ArrayIndexToken(String JavaDoc identifier) {
35         _index = Integer.parseInt(identifier);
36     }
37
38     public void update(Object JavaDoc root, Object JavaDoc newValue) {
39         if(root instanceof List JavaDoc)
40             listUpdate((List JavaDoc)root, _index, newValue);
41         else if(root.getClass().isArray())
42             arrayUpdate(root, _index, newValue);
43         else {
44             RuntimeException JavaDoc re = new RuntimeException JavaDoc("The _index \"" + _index + "\" can not be used to look-up the type of a property" +
45                 " on an object that is not an array or list.");
46             LOGGER.error("", re);
47             throw re;
48         }
49     }
50
51     public Object JavaDoc evaluate(Object JavaDoc value) {
52         if(value instanceof List JavaDoc)
53             return listLookup((List JavaDoc)value, _index);
54         else if(value.getClass().isArray())
55             return arrayLookup(value, _index);
56         else {
57             RuntimeException JavaDoc re = new RuntimeException JavaDoc("The _index \"" + _index + "\" can not be used to look-up a property on an object that is not an array or list.");
58             LOGGER.error("", re);
59             throw re;
60         }
61     }
62
63     public String JavaDoc getTokenString() {
64         return "[" + _index + "]";
65     }
66
67     public String JavaDoc toString() {
68         return "" + _index;
69     }
70 }
71
Popular Tags