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