View Javadoc

1   /*
2    * Copyright 2007-2011 smartics, Kronseder & Reiner GmbH
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 de.smartics.exceptions.ognl;
17  
18  import ognl.Ognl;
19  import ognl.OgnlContext;
20  import ognl.OgnlException;
21  
22  /**
23   * Wrapper for an OGNL expression.
24   *
25   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
26   * @version $Revision:591 $
27   */
28  public final class OgnlExpression
29  {
30    // ********************************* Fields *********************************
31  
32    // --- constants ------------------------------------------------------------
33  
34    // --- members --------------------------------------------------------------
35  
36    /**
37     * The reference to the parsed expression instance.
38     */
39    private final Object expression;
40  
41    // ****************************** Initializer *******************************
42  
43    // ****************************** Constructors ******************************
44  
45    /**
46     * Default constructor.
47     *
48     * @param expressionString the OGNL expression to parse.
49     * @throws OgnlException on any parsing problem.
50     */
51    public OgnlExpression(final String expressionString) throws OgnlException
52    {
53      expression = Ognl.parseExpression(expressionString);
54    }
55  
56    // ****************************** Inner Classes *****************************
57  
58    // ********************************* Methods ********************************
59  
60    // --- init -----------------------------------------------------------------
61  
62    // --- get&set --------------------------------------------------------------
63  
64    /**
65     * Returns the reference to the parsed expression instance.
66     *
67     * @return the reference to the parsed expression instance.
68     */
69    public Object getExpression()
70    {
71      return expression;
72    }
73  
74    // --- business -------------------------------------------------------------
75  
76    /**
77     * Returns the value for the given context on the given root object.
78     *
79     * @param context the context of the evaluation.
80     * @param rootObject the root object to run the evaluation of the OGNL
81     *          expression.
82     * @return the value of the root object referenced by the OGNL expression.
83     * @throws OgnlException on any problem encountered fetching the value.
84     */
85    public Object getValue(final OgnlContext context, final Object rootObject)
86      throws OgnlException
87    {
88      return Ognl.getValue(getExpression(), context, rootObject);
89    }
90  
91    /**
92     * Sets the value dependent on the OGNL expression.
93     *
94     * @param context the context of the evaluation.
95     * @param rootObject the root object to run the evaluation of the OGNL
96     *          expression.
97     * @param value the value to set.
98     * @throws OgnlException on any problem encountered setting the value.
99     */
100   public void setValue(final OgnlContext context, final Object rootObject,
101       final Object value) throws OgnlException
102   {
103     Ognl.setValue(getExpression(), context, rootObject, value);
104   }
105 
106   // --- object basics --------------------------------------------------------
107 
108 }