KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > jelly > expression > ExpressionSupport


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 JavaDoc;
19 import java.util.Collection JavaDoc;
20 import java.util.Enumeration JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Map JavaDoc;
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 JavaDoc EMPTY_ITERATOR = Collections.EMPTY_LIST.iterator();
43
44     // inherit javadoc from interface
45
public String JavaDoc evaluateAsString(JellyContext context) {
46         Object JavaDoc 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 JavaDoc evaluateRecurse(JellyContext context) {
61         Object JavaDoc 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 JavaDoc value = evaluateRecurse(context);
72         if ( value instanceof Boolean JavaDoc ) {
73             Boolean JavaDoc b = (Boolean JavaDoc) value;
74             return b.booleanValue();
75         }
76         else if ( value instanceof String JavaDoc ) {
77             // return Boolean.getBoolean( (String) value );
78
String JavaDoc str = (String JavaDoc) 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 JavaDoc evaluateAsIterator(JellyContext context) {
94         Object JavaDoc value = evaluateRecurse(context);
95         if ( value == null ) {
96             return EMPTY_ITERATOR;
97         } else if ( value instanceof Iterator JavaDoc ) {
98             return (Iterator JavaDoc) value;
99         } else if ( value instanceof List JavaDoc ) {
100             List JavaDoc list = (List JavaDoc) value;
101             return list.iterator();
102         } else if ( value instanceof Map JavaDoc ) {
103             Map JavaDoc map = (Map JavaDoc) value;
104             return map.entrySet().iterator();
105         } else if ( value.getClass().isArray() ) {
106             return new ArrayIterator( value );
107         } else if ( value instanceof Enumeration JavaDoc ) {
108             return new EnumerationIterator((Enumeration JavaDoc ) value);
109         } else if ( value instanceof Collection JavaDoc ) {
110           Collection JavaDoc collection = (Collection JavaDoc) value;
111           return collection.iterator();
112         } else if ( value instanceof String JavaDoc ) {
113            String JavaDoc[] array = StringUtils.split((String JavaDoc) 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 }
122
Popular Tags