View Javadoc

1   /*
2    * Copyright 2002,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  package org.apache.commons.jelly.expression;
17  
18  import java.util.Collections;
19  import java.util.Collection;
20  import java.util.Enumeration;
21  import java.util.Iterator;
22  import java.util.List;
23  import java.util.Map;
24  
25  import org.apache.commons.collections.iterators.ArrayIterator;
26  import org.apache.commons.collections.iterators.EnumerationIterator;
27  import org.apache.commons.collections.iterators.SingletonIterator;
28  
29  import org.apache.commons.jelly.JellyContext;
30  import org.apache.commons.lang.StringUtils;
31  
32  /*** <p><code>ExpressionSupport</code>
33    * an abstract base class for Expression implementations
34    * which provides default implementations of some of the
35    * typesafe evaluation methods.</p>
36    *
37    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
38    * @version $Revision: 155420 $
39    */
40  public abstract class ExpressionSupport implements Expression {
41  
42      protected static final Iterator EMPTY_ITERATOR = Collections.EMPTY_LIST.iterator();
43  
44      // inherit javadoc from interface
45      public String evaluateAsString(JellyContext context) {
46          Object value = evaluateRecurse(context);
47          // sometimes when Jelly is used inside Maven the value
48          // of an expression can actually be an expression.
49          // e.g. ${foo.bar} can lookup "foo.bar" in a Maven context
50          // which could actually be an expression
51  
52          if ( value != null ) {
53              return value.toString();
54          }
55          return null;
56      }
57  
58  
59      // inherit javadoc from interface
60      public Object evaluateRecurse(JellyContext context) {
61          Object value = evaluate(context);
62          if (value instanceof Expression) {
63              Expression expression = (Expression) value;
64              return expression.evaluateRecurse(context);
65          }
66          return value;
67      }
68  
69      // inherit javadoc from interface
70      public boolean evaluateAsBoolean(JellyContext context) {
71          Object value = evaluateRecurse(context);
72          if ( value instanceof Boolean ) {
73              Boolean b = (Boolean) value;
74              return b.booleanValue();
75          }
76          else if ( value instanceof String ) {
77              // return Boolean.getBoolean( (String) value );
78              String str = (String) value;
79  
80              return ( str.equalsIgnoreCase( "on" )
81                   ||
82                   str.equalsIgnoreCase( "yes" )
83                   ||
84                   str.equals( "1" )
85                   ||
86                   str.equalsIgnoreCase( "true" ) );
87  
88          }
89          return false;
90      }
91  
92      // inherit javadoc from interface
93      public Iterator evaluateAsIterator(JellyContext context) {
94          Object value = evaluateRecurse(context);
95          if ( value == null ) {
96              return EMPTY_ITERATOR;
97          } else if ( value instanceof Iterator ) {
98              return (Iterator) value;
99          } else if ( value instanceof List ) {
100             List list = (List) value;
101             return list.iterator();
102         } else if ( value instanceof Map ) {
103             Map map = (Map) value;
104             return map.entrySet().iterator();
105         } else if ( value.getClass().isArray() ) {
106             return new ArrayIterator( value );
107         } else if ( value instanceof Enumeration ) {
108             return new EnumerationIterator((Enumeration ) value);
109         } else if ( value instanceof Collection ) {
110           Collection collection = (Collection) value;
111           return collection.iterator();
112         } else if ( value instanceof String ) {
113            String[] array = StringUtils.split((String) value, "," );
114            array = StringUtils.stripAll( array );
115            return new ArrayIterator( array );
116         } else {
117             // XXX: should we return single iterator?
118             return new SingletonIterator( value );
119         }
120     }
121 }