View Javadoc

1   /*
2   Copyright (C) 2003 Grid Systems, S.A.
3   
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU General Public License, version 2, as
6   published by the Free Software Foundation.
7   
8   This program is distributed in the hope that it will be useful,
9   but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12  
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */
17  package com.gridsystems.beanfilter;
18  
19  import java.util.HashSet;
20  import java.util.Iterator;
21  import com.gridsystems.beanfilter.ArrayValue.ArrayType;
22  
23  /**
24   * EvalValue to process select-where command.
25   *
26   * @author Xmas
27   * @version 1.0
28   */
29  public final class SelectWhereValue extends EvalValue implements FilterParserConstants {
30  
31    /**
32     * Select operand.
33     */
34    private EvalValue select;
35  
36    /**
37     * Where operand.
38     */
39    private EvalNode where;
40  
41    /**
42     * Select Class.
43     */
44    private Class<?> selectClass;
45  
46    /**
47     * Where Token.
48     */
49    private Token whereToken;
50  
51    /**
52     * Creates a new instance.
53     *
54     * @param selectToken  The select token
55     * @param select  The select value
56     * @param whereToken  The select token
57     * @param where The where evalNode
58     * @param parentClass The parent Class
59     * @throws EvalException If an error occurs
60     */
61    public SelectWhereValue(Token selectToken, EvalValue select,
62      Token whereToken, EvalNode where, Class<?> parentClass) throws EvalException {
63      super(selectToken.beginLine, selectToken.beginColumn);
64      this.select = select;
65      this.whereToken = whereToken;
66      this.where = where;
67      this.selectClass = select.getClassValue(parentClass);
68  
69      checkWhereSyntax();
70    }
71  
72    /**
73     * {@inheritDoc}
74     */
75    @SuppressWarnings("unchecked")
76    @Override public Object getValue(Object src) throws EvalException {
77      Object selectValue = select.getValue(src);
78  
79      HashSet<Object> result = new HashSet<Object>();
80      String expr = where.toString();
81  
82      HashSet<Object> theList;
83  
84      if (selectValue instanceof HashSet) {
85        theList = (HashSet<Object>)selectValue;
86      } else {
87        if (selectValue.getClass().isArray()) {
88          theList = ArrayValue.arrayToHash(selectValue, ArrayType.ALL_ITEMS);
89        } else {
90          theList = new HashSet<Object>();
91          theList.add(selectValue);
92        }
93      }
94  
95      Iterator<Object> iter = theList.iterator();
96      while (iter.hasNext()) {
97        Object obj = iter.next();
98        Class<?> theClass;
99        if (obj == null) {
100         theClass = this.selectClass;
101       } else {
102         theClass = obj.getClass();
103       }
104       Filter filter = new Filter(expr, theClass);
105       boolean eval = filter.eval(obj);
106       if (eval) {
107         result.add(obj);
108       }
109     }
110     return result;
111   }
112 
113   /**
114    * {@inheritDoc}
115    */
116   @Override public String toString() {
117     return "select " + select.toString() + " where " + where.toString();
118   }
119 
120   /**
121    * {@inheritDoc}
122    */
123   @Override
124   public Class<?> getClassValue(Class<?> parentClass) throws EvalException {
125     return select.getClassValue(parentClass);
126   }
127 
128   /**
129    * Checks if where Evaluation corresponds with select value.
130    *
131    * @throws EvalException If error.
132    */
133   private void checkWhereSyntax() throws EvalException {
134     // Check
135     try {
136       new Filter(where.toString().trim(), selectClass);
137     } catch (EvalException e) {
138       if (e.getCode().equals("FTR005")) {
139         Object[] params = e.getParams();
140         for (int i = 0; i < params.length; i++) {
141           try {
142             if ((i == 0) || (i == 1)) {
143               int tmp = Integer.parseInt(params[i].toString());
144               if (i == 0) {
145                 // Line
146                 tmp = tmp + whereToken.beginLine - 1;
147               } else {
148                 // Position in line
149                 int whereLen = whereToken.beginLine + tokenImage[WHERE].length() - 2;
150                 tmp = tmp +  whereToken.beginColumn + whereLen - 1;
151               }
152               params[i] = String.valueOf(tmp);
153             }
154           } catch (Exception ex) {
155           }
156         }
157         // FTR005=Unknown field ''{2}'' at position [{0}, {1}])
158         throw new EvalException("FTR005", params);
159       }
160       throw e;
161     }
162   }
163 
164 }