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.input;
16
17import org.tp23.antinstaller.InstallerContext;
18import org.tp23.antinstaller.ValidationException;
19import org.tp23.antinstaller.runtime.ConfigurationException;
20import org.tp23.antinstaller.runtime.logic.Expression;
21import org.tp23.antinstaller.runtime.logic.ExpressionBuilder;
22
23/**
24 * Container for other <code>OutputField</code> members that will be
25 * conditionally updated. Typical use is with <code>&lt;hidden&gt;</code>
26 * to conditionally set property values.
27 *
28 * @author mwilson
29 * @version $Id
30 * @since 0.7.4 patch 7
31 */
32public class ConditionalField extends InputField {
33    
34    private String ifProperty;
35    private Expression expression;
36    private InputField[] fields;
37
38    public void setIfProperty(final String expressionStr) {
39        this.ifProperty = expressionStr;
40    }
41
42    public String getIfProperty() {
43        return ifProperty;
44    }
45
46    public InputField[] getFields() {
47        return fields;
48    }
49
50    public void setFields(InputField[] fields) {
51        this.fields = fields;
52    }
53
54    /**
55     * Runtime validation of user input
56     *
57     * @param context Installer context
58     * @return <code>true</code> if user input is valid, otherwise <code>false</code>
59     * @throws ValidationException
60     */
61    public boolean validate(InstallerContext context)
62            throws ValidationException {
63        try {
64            getExpression();
65            if ((fields != null) && (fields.length > 0)) {
66                int i = 0;
67                for (; (i < fields.length) && (fields[i].validate(context)); i++) {
68                }
69
70                if (i == fields.length) {
71                    return true;
72                }
73            }
74        } catch (ConfigurationException configExc) {
75            if(context.getInstaller().isVerbose()) {
76                context.log(configExc);
77            }
78        }
79        return false;
80    }
81
82    /**
83     * Build-time validation of installer configuration file
84     *
85     * @return <code>true</code> if configuration is ok, otherwise <code>false</code>
86     */
87    public boolean validateObject() {
88        try {
89            getExpression();
90            if ((fields != null) && (fields.length > 0)) {
91                int i = 0;
92                for (; (i < fields.length) && (fields[i].validateObject()); i++) {
93                }
94
95                if (i == fields.length) {
96                    return true;
97                }
98
99                System.out.println("Invalid field:" + fields[i]);
00            }
01        } catch (ConfigurationException configExc) {
02            System.out.println("Invalid conditional expression: " + configExc);
03        }
04        return false;
05    }
06
07    /**
08     * Get the conditional expression in preparation for evaluation
09     *
10     * @throws ConfigurationException if the expression is invalid
11     */
12    public Expression getExpression() throws ConfigurationException {
13        if (expression == null) {
14            expression = ExpressionBuilder.parseLogicalExpressions(resultContainer, ifProperty);
15        }
16
17        return expression;
18    }
19}
20