View Javadoc

1   /*
2   Copyright (C) 2004 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 org.apache.commons.logging.Log;
20  import org.apache.commons.logging.LogFactory;
21  
22  /**
23   * Agent filter.
24   *
25   * @author Rodrigo Ruiz
26   * @version 1.0
27   */
28  public class Filter {
29    /**
30     * Logger.
31     */
32    private static Log log = LogFactory.getLog(Filter.class);
33  
34    /**
35     * Filter data.
36     */
37    private String expr;
38  
39    /**
40     * The class to filter. Needed to be able to modify the expression.
41     */
42    private Class<?> c;
43  
44    /**
45     * Evaluation tree.
46     */
47    private EvalNode root;
48  
49    /**
50     * Creates a new instance.
51     *
52     * @param expr  The filter expression
53     * @param c     The class to filter
54     * @throws EvalException If a syntax error is found in expr
55     */
56    public Filter(String expr, Class<?> c) throws EvalException {
57      try {
58        this.expr = (expr == null) ? "" : expr.trim();
59        this.root = (this.expr.length() == 0) ? null : Parser.parse(this.expr, c);
60        this.c = c;
61      } catch (EvalException ke) {
62        log.error("Error in filter: [Expression: " + expr + "] [Class: " + c + "] -> "
63          + ke.getMessage());
64        throw ke;
65      }
66    }
67  
68    /**
69     * Evaluates the filter.
70     *
71     * @param src  The data source to filter
72     * @return  <code>true</code> if the filter allows access to the agent
73     * @throws EvalException  If a syntax error is detected
74     */
75    public synchronized boolean eval(Object src) throws EvalException {
76      if (root == null) {
77        return true;
78      } else {
79        if ((src == null) || (src.getClass().equals(c))) {
80          return root.eval(src);
81        }
82        // FTR011=The class of the object to eval ({0})
83        // is different to the class of this filter ({1}).
84        throw new EvalException("FTR011", src.getClass(), c);
85      }
86    }
87  
88    /**
89     * Gets the filter expression.
90     *
91     * @return  The filter expression
92     */
93    public String getExpr() {
94      return this.expr;
95    }
96  
97    /**
98     * Modifies this filter expression.
99     *
100    * @param expr The new expression
101    * @throws EvalException  If a syntax error is detected
102    */
103   public void setExpr(String expr) throws EvalException {
104     expr = (expr == null) ? "" : expr.trim();
105     if (!expr.equals(this.expr)) {
106       // Different expression
107       this.root = Parser.parse(expr, c);
108       this.expr = expr;
109     }
110   }
111 
112   /**
113    * {@inheritDoc}
114    */
115   @Override public String toString() {
116     StringBuffer sb = new StringBuffer();
117     sb.append("Filter { ");
118     sb.append(expr);
119     sb.append(" }");
120     return sb.toString();
121   }
122 
123   /**
124    * For DEBUG purposes.
125    * @return Formatted Expression
126    */
127   public String getFormattedExpr() {
128     if (root == null) {
129       return null;
130     } else {
131       return root.toString();
132     }
133   }
134 }