001: /*
002: * Copyright 2002,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.jelly.expression;
017:
018: import java.util.Collections;
019: import java.util.Collection;
020: import java.util.Enumeration;
021: import java.util.Iterator;
022: import java.util.List;
023: import java.util.Map;
024:
025: import org.apache.commons.collections.iterators.ArrayIterator;
026: import org.apache.commons.collections.iterators.EnumerationIterator;
027: import org.apache.commons.collections.iterators.SingletonIterator;
028:
029: import org.apache.commons.jelly.JellyContext;
030: import org.apache.commons.lang.StringUtils;
031:
032: /** <p><code>ExpressionSupport</code>
033: * an abstract base class for Expression implementations
034: * which provides default implementations of some of the
035: * typesafe evaluation methods.</p>
036: *
037: * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
038: * @version $Revision: 155420 $
039: */
040: public abstract class ExpressionSupport implements Expression {
041:
042: protected static final Iterator EMPTY_ITERATOR = Collections.EMPTY_LIST
043: .iterator();
044:
045: // inherit javadoc from interface
046: public String evaluateAsString(JellyContext context) {
047: Object value = evaluateRecurse(context);
048: // sometimes when Jelly is used inside Maven the value
049: // of an expression can actually be an expression.
050: // e.g. ${foo.bar} can lookup "foo.bar" in a Maven context
051: // which could actually be an expression
052:
053: if (value != null) {
054: return value.toString();
055: }
056: return null;
057: }
058:
059: // inherit javadoc from interface
060: public Object evaluateRecurse(JellyContext context) {
061: Object value = evaluate(context);
062: if (value instanceof Expression) {
063: Expression expression = (Expression) value;
064: return expression.evaluateRecurse(context);
065: }
066: return value;
067: }
068:
069: // inherit javadoc from interface
070: public boolean evaluateAsBoolean(JellyContext context) {
071: Object value = evaluateRecurse(context);
072: if (value instanceof Boolean) {
073: Boolean b = (Boolean) value;
074: return b.booleanValue();
075: } else if (value instanceof String) {
076: // return Boolean.getBoolean( (String) value );
077: String str = (String) value;
078:
079: return (str.equalsIgnoreCase("on")
080: || str.equalsIgnoreCase("yes") || str.equals("1") || str
081: .equalsIgnoreCase("true"));
082:
083: }
084: return false;
085: }
086:
087: // inherit javadoc from interface
088: public Iterator evaluateAsIterator(JellyContext context) {
089: Object value = evaluateRecurse(context);
090: if (value == null) {
091: return EMPTY_ITERATOR;
092: } else if (value instanceof Iterator) {
093: return (Iterator) value;
094: } else if (value instanceof List) {
095: List list = (List) value;
096: return list.iterator();
097: } else if (value instanceof Map) {
098: Map map = (Map) value;
099: return map.entrySet().iterator();
100: } else if (value.getClass().isArray()) {
101: return new ArrayIterator(value);
102: } else if (value instanceof Enumeration) {
103: return new EnumerationIterator((Enumeration) value);
104: } else if (value instanceof Collection) {
105: Collection collection = (Collection) value;
106: return collection.iterator();
107: } else if (value instanceof String) {
108: String[] array = StringUtils.split((String) value, ",");
109: array = StringUtils.stripAll(array);
110: return new ArrayIterator(array);
111: } else {
112: // XXX: should we return single iterator?
113: return new SingletonIterator(value);
114: }
115: }
116: }
|