001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.netui.script.el.tokens;
020:
021: import java.util.List;
022: import java.util.Map;
023:
024: import org.apache.beehive.netui.util.logging.Logger;
025:
026: /**
027: * General representation of an identifier in an expression. For example, in the expression
028: * <code>actionForm.foo.bar</code>, both <code>foo</code> and <code>bar</code> are tokens of this
029: * type.
030: */
031: public class IdentifierToken extends ExpressionToken {
032:
033: private static final Logger LOGGER = Logger
034: .getInstance(IdentifierToken.class);
035: private static final boolean TRACE_ENABLED = LOGGER
036: .isTraceEnabled();
037:
038: private String _identifier = null;
039:
040: public IdentifierToken(String identifier) {
041: _identifier = identifier;
042: }
043:
044: /**
045: * Read the object represetned by this token from the given object.
046: * @param object the object from which to read
047: * @return the read object
048: */
049: public Object read(Object object) {
050: if (object == null) {
051: String msg = "Can not evaluate the identifier \""
052: + _identifier + "\" on a null object.";
053: LOGGER.error(msg);
054: throw new RuntimeException(msg);
055: }
056:
057: if (TRACE_ENABLED)
058: LOGGER.trace("Read property " + _identifier
059: + " on object of type "
060: + object.getClass().getName());
061:
062: if (object instanceof Map)
063: return mapLookup((Map) object, _identifier);
064: else if (object instanceof List) {
065: int i = parseIndex(_identifier);
066: return listLookup((List) object, i);
067: } else if (object.getClass().isArray()) {
068: int i = parseIndex(_identifier);
069: return arrayLookup(object, i);
070: } else
071: return beanLookup(object, _identifier);
072: }
073:
074: /**
075: * Update the value represented by this token on the given object <code>object</code> with
076: * the value <code>value</code>.
077: * @param object the object to update
078: * @param value the new value
079: */
080: public void write(Object object, Object value) {
081: if (object == null) {
082: String msg = "Can not update the identifier \""
083: + _identifier + "\" on a null value object.";
084: LOGGER.error(msg);
085: throw new RuntimeException(msg);
086: }
087:
088: if (TRACE_ENABLED)
089: LOGGER.trace("Update property named \"" + _identifier
090: + "\" on object of type: \""
091: + object.getClass().getName() + "\"");
092:
093: if (object instanceof Map)
094: mapUpdate((Map) object, _identifier, value);
095: else if (object instanceof List) {
096: int i = parseIndex(_identifier);
097: listUpdate((List) object, i, value);
098: } else if (object.getClass().isArray()) {
099: int i = parseIndex(_identifier);
100: arrayUpdate(object, i, value);
101: } else
102: beanUpdate(object, _identifier, value);
103: }
104:
105: /**
106: * Get the String representation of this token. Note, this assumes that the token was
107: * preceeded with a ".".
108: * @return the String representing this token
109: */
110: public String getTokenString() {
111: return "." + _identifier;
112: }
113:
114: public String toString() {
115: return _identifier;
116: }
117: }
|