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 |
|
---|
15 | package org.tp23.antinstaller.input;
|
---|
16 |
|
---|
17 | import org.tp23.antinstaller.InstallerContext;
|
---|
18 | import org.tp23.antinstaller.ValidationException;
|
---|
19 | import org.tp23.antinstaller.runtime.ConfigurationException;
|
---|
20 | import org.tp23.antinstaller.runtime.logic.Expression;
|
---|
21 | import 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><hidden></code>
|
---|
26 | * to conditionally set property values.
|
---|
27 | *
|
---|
28 | * @author mwilson
|
---|
29 | * @version $Id
|
---|
30 | * @since 0.7.4 patch 7
|
---|
31 | */
|
---|
32 | public 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]);
|
---|
100 | }
|
---|
101 | } catch (ConfigurationException configExc) {
|
---|
102 | System.out.println("Invalid conditional expression: " + configExc);
|
---|
103 | }
|
---|
104 | return false;
|
---|
105 | }
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * Get the conditional expression in preparation for evaluation
|
---|
109 | *
|
---|
110 | * @throws ConfigurationException if the expression is invalid
|
---|
111 | */
|
---|
112 | public Expression getExpression() throws ConfigurationException {
|
---|
113 | if (expression == null) {
|
---|
114 | expression = ExpressionBuilder.parseLogicalExpressions(resultContainer, ifProperty);
|
---|
115 | }
|
---|
116 |
|
---|
117 | return expression;
|
---|
118 | }
|
---|
119 | }
|
---|