View Javadoc

1   /*
2    * Copyright 2001-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  
17  package org.apache.commons.betwixt.expression;
18  
19  import java.util.Collection;
20  import java.util.Collections;
21  import java.util.Enumeration;
22  import java.util.Iterator;
23  import java.util.Map;
24  
25  import org.apache.commons.collections.iterators.ArrayIterator;
26  import org.apache.commons.collections.iterators.EnumerationIterator;
27  
28  /*** <p><code>IteratorExpression</code> returns an iterator over the current context.</p>
29    *
30    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
31    * @version $Revision: 1.8 $
32    */
33  public class IteratorExpression implements Expression {
34      
35      /*** Use this <code>Expression</code> to perform initial evaluation*/
36      private Expression expression;
37      
38      /*** 
39       * Construct <code>IteratorExpression</code> using given expression for initial evaluation.
40       * @param expression this expression will be evaluated and the result converted to an 
41       *        iterator.
42       */
43      public IteratorExpression(Expression expression) {
44          this.expression = expression;
45      }
46      
47      /*** 
48       * Returns an interator over the current context 
49       * @see org.apache.commons.betwixt.expression.Expression
50       */
51      public Object evaluate(Context context) {        
52          // evaluate wrapped expression against context
53          Object value = expression.evaluate( context );
54          
55          // based on the class of the result,
56          // return an appropriate iterator
57          if ( value instanceof Iterator ) {
58              // if the value is an iterator, we're done
59              return (Iterator) value;
60              
61          } else if ( value instanceof Collection ) {
62              // if it's a collection, return an iterator for that collection
63              Collection collection = (Collection) value;
64              return collection.iterator();
65              
66          } else if ( value instanceof Map ) {
67              // if it's a map, return an iterator for the map entries
68              Map map = (Map) value;
69              return map.entrySet().iterator();
70              
71          } else if ( value instanceof Enumeration ) {
72              // if it's an enumeration, wrap it in an EnumerationIterator
73              return new EnumerationIterator( (Enumeration) value );
74              
75          } else if ( value != null ) {
76              // if we have an array return an ArrayIterator
77              Class type = value.getClass();
78              if ( type.isArray() ) {
79                  return new ArrayIterator( value );
80              }
81          }
82          
83          // we've got something we can't deal with
84          // so return an empty iterator
85          return Collections.EMPTY_LIST.iterator();
86      }
87  
88      /*** 
89       * Do nothing
90       * @see org.apache.commons.betwixt.expression.Expression
91       */
92      public void update(Context context, String newValue) {
93          // do nothing
94      }
95      
96      /***
97       * Returns something useful for logging
98       * @return string useful for logging
99       */
100     public String toString() {
101         return "IteratorExpression [expression=" + expression + "]";
102     }
103 }