%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
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; |
|
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: 1.15 $ |
|
39 | */ |
|
40 | 9158 | public abstract class ExpressionSupport implements Expression { |
41 | ||
42 | 42 | protected static final Iterator EMPTY_ITERATOR = Collections.EMPTY_LIST.iterator(); |
43 | ||
44 | // inherit javadoc from interface |
|
45 | public String evaluateAsString(JellyContext context) { |
|
46 | 492 | 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 | 492 | if ( value != null ) { |
53 | 490 | return value.toString(); |
54 | } |
|
55 | 2 | return null; |
56 | } |
|
57 | ||
58 | ||
59 | // inherit javadoc from interface |
|
60 | public Object evaluateRecurse(JellyContext context) { |
|
61 | 2240 | Object value = evaluate(context); |
62 | 2240 | if (value instanceof Expression) { |
63 | 0 | Expression expression = (Expression) value; |
64 | 0 | return expression.evaluateRecurse(context); |
65 | } |
|
66 | 2240 | return value; |
67 | } |
|
68 | ||
69 | // inherit javadoc from interface |
|
70 | public boolean evaluateAsBoolean(JellyContext context) { |
|
71 | 660 | Object value = evaluateRecurse(context); |
72 | 660 | if ( value instanceof Boolean ) { |
73 | 342 | Boolean b = (Boolean) value; |
74 | 342 | return b.booleanValue(); |
75 | } |
|
76 | 318 | else if ( value instanceof String ) { |
77 | // return Boolean.getBoolean( (String) value ); |
|
78 | 0 | String str = (String) value; |
79 | ||
80 | 0 | return ( str.equalsIgnoreCase( "on" ) |
81 | || |
|
82 | str.equalsIgnoreCase( "yes" ) |
|
83 | || |
|
84 | str.equals( "1" ) |
|
85 | || |
|
86 | str.equalsIgnoreCase( "true" ) ); |
|
87 | ||
88 | } |
|
89 | 318 | return false; |
90 | } |
|
91 | ||
92 | // inherit javadoc from interface |
|
93 | public Iterator evaluateAsIterator(JellyContext context) { |
|
94 | 4 | Object value = evaluateRecurse(context); |
95 | 4 | if ( value == null ) { |
96 | 0 | return EMPTY_ITERATOR; |
97 | 4 | } else if ( value instanceof Iterator ) { |
98 | 0 | return (Iterator) value; |
99 | 4 | } else if ( value instanceof List ) { |
100 | 0 | List list = (List) value; |
101 | 0 | return list.iterator(); |
102 | 4 | } else if ( value instanceof Map ) { |
103 | 0 | Map map = (Map) value; |
104 | 0 | return map.entrySet().iterator(); |
105 | 4 | } else if ( value.getClass().isArray() ) { |
106 | 2 | return new ArrayIterator( value ); |
107 | 2 | } else if ( value instanceof Enumeration ) { |
108 | 0 | return new EnumerationIterator((Enumeration ) value); |
109 | 2 | } else if ( value instanceof Collection ) { |
110 | 0 | Collection collection = (Collection) value; |
111 | 0 | return collection.iterator(); |
112 | 2 | } else if ( value instanceof String ) { |
113 | 2 | String[] array = StringUtils.split((String) value, "," ); |
114 | 2 | array = StringUtils.stripAll( array ); |
115 | 2 | return new ArrayIterator( array ); |
116 | } else { |
|
117 | // XXX: should we return single iterator? |
|
118 | 0 | return new SingletonIterator( value ); |
119 | } |
|
120 | } |
|
121 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |