01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: *
17: * $Header:$
18: */
19: package org.apache.beehive.netui.script.el.tokens;
20:
21: import java.util.List;
22:
23: import org.apache.beehive.netui.util.logging.Logger;
24:
25: /**
26: *
27: */
28: public class ArrayIndexToken extends ExpressionToken {
29:
30: private static final Logger LOGGER = Logger
31: .getInstance(ArrayIndexToken.class);
32:
33: private int _index;
34:
35: public ArrayIndexToken(String identifier) {
36: _index = Integer.parseInt(identifier);
37: }
38:
39: /**
40: * Evaluate an array index token. This token takes the form "[1234]" where
41: * "1234" is the index into the array. The token is identified as this because
42: * an integer number appears between the square brackets.
43: *
44: * @param value the object from which to read a value at the index represented by this token
45: * @return the value of the item at this array index
46: */
47: public Object read(Object value) {
48: if (value instanceof List)
49: return listLookup((List) value, _index);
50: else if (value.getClass().isArray())
51: return arrayLookup(value, _index);
52: else {
53: String message = "The index \""
54: + _index
55: + "\" can not be used to index a property on an object that is not an Array or a List";
56: LOGGER.error(message);
57: throw new RuntimeException(message);
58: }
59: }
60:
61: public void write(Object root, Object value) {
62: if (root instanceof List)
63: listUpdate((List) root, _index, value);
64: else if (root.getClass().isArray())
65: arrayUpdate(root, _index, value);
66: else {
67: String message = "The index "
68: + _index
69: + "\" can not be used to update a property on an object that is not an Array or a List";
70: LOGGER.error(message);
71: throw new RuntimeException(message);
72: }
73: }
74:
75: public String getTokenString() {
76: return "[" + _index + "]";
77: }
78:
79: public String toString() {
80: return "" + _index;
81: }
82: }
|