1 /*
2  * Licensed under the Apache License, Version 2.0 (the "License");
3  * you may not use this file except in compliance with the License.
4  * You may obtain a copy of the License at
5  *
6  * http://www.apache.org/licenses/LICENSE-2.0
7  *
8  * Unless required by applicable law or agreed to in writing, software
9  * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and
12 * limitations under the License.
13 */
14
15package org.tp23.antinstaller.runtime.logic;
16
17import org.tp23.antinstaller.input.ResultContainer;
18import org.tp23.antinstaller.runtime.ConfigurationException;
19
20/**
21 * @author mwilson
22 * @version $Id: SimpleExpression.java,v 1.1 2006/09/08 19:16:30 anothermwilson
23 *          Exp $
24 * @since 0.7.4 patch 2
25 */
26public class SimpleExpression implements Expression {
27
28    private final Value value1;
29    private final ValuesTest testCondition;
30    private final Value value2;
31    private final String literalValue1;
32    private final String literalValue2;
33
34    public SimpleExpression(
35                final ResultContainer resultContainer, 
36                final String value1, 
37                final ValuesTest test, 
38                final String value2)
39            throws ConfigurationException {
40        
41        this.literalValue1 = value1;
42        this.value1 = getValue(resultContainer, value1);
43        this.testCondition = test;
44        this.literalValue2 = value2;
45        this.value2 = getValue(resultContainer, value2);
46        
47    }
48
49    public boolean evaluate() {
50        return testCondition.getTestResult(value1, value2);
51    }
52
53    private Value getValue(ResultContainer container, String valueStr) throws ConfigurationException {
54        if ( (valueStr.length() > 0) && (valueStr.charAt(0) == '$') ) {
55            if ( valueStr.startsWith("${") && valueStr.endsWith("}") ) {
56                return new VariableValue(container, valueStr);
57            } else {
58                throw new ConfigurationException("Badly formed variable: '" + valueStr + "'");
59            }
60        } else {
61            return new LiteralValue(valueStr);
62        }
63    }
64
65    public String toString() {
66        return "[" + literalValue1 + "==" + literalValue2 + "]";
67    }
68}
69