1   /***
2    * TableOperator.java
3    *  
4    * $Author: mballesteros $ 
5    * $Date: 2003/11/28 19:18:03 $ 
6    * $Revision: 1.1 $
7    */
8   package net.sf.jec.namedop;
9   
10  import java.util.ArrayList;
11  import java.util.Collection;
12  import java.util.HashMap;
13  import java.util.Iterator;
14  import java.util.List;
15  import java.util.Map;
16  
17  import net.sf.jec.EvaluationException;
18  import net.sf.jec.Operator;
19  
20  
21  /*** TableOperator operates over an object collection and returns a table, result
22   * of applying operators for each column. There are always an odd number of
23   * argument operators. The first of the pair evaluates the column name, and the
24   * second evaluates the column value. Thus, is common to have a constant operator
25   * as the first in the pair, as in this example:
26   * <code>book.pages.table('Text', text, 'Count', wordCount)</code>
27   *
28   * @author  mballesteros
29   */
30  public class TableOperator extends Operator {
31  
32      /*** Creates a new SumOperator
33       */
34      public TableOperator() { }
35      
36      /*** Returns a function representation String for this operator
37       */
38      public String toFunctionString() {
39          return "";
40      }
41      
42      /*** Returns an expression String representation for this operator
43       */
44      public String toExpressionString() {
45          return "";
46      }    
47  
48      
49      /*** Returns the associated value to the given context and root context
50       * @param rootCtx The root context, needed for operators that require
51       * expression evaluation from root point. Example: Indexer operators
52       * @param ctx The current context where the operator will work over
53       */
54      protected Object directMap(Object rootCtx, Object ctx) throws EvaluationException {
55          if (ctx instanceof Collection) {
56              List out = new ArrayList();
57              Iterator it = ((Collection)ctx).iterator();
58              while (it.hasNext()) out.add(calculateRow(rootCtx, it.next()));
59              return out;
60          } else {
61              return null;
62          }
63      }
64      
65      /*** Calculates a table row (a Map containing colum names and column values)
66       */
67      private Map calculateRow(Object rootCtx, Object obj) throws EvaluationException {
68          Map out = new HashMap();
69          for (int i=0; i < argOps.length; i+=2) {
70              out.put(    argOps[i].apply(rootCtx, obj),
71                          argOps[i+1].apply(rootCtx, obj) );
72          }
73          return out;
74      }
75  }
This page was automatically generated by Maven