001: /*
002: * Licensed under the Apache License, Version 2.0 (the "License");
003: * you may not use this file except in compliance with the License.
004: * You may obtain a copy of the License at
005: *
006: * http://www.apache.org/licenses/LICENSE-2.0
007: *
008: * Unless required by applicable law or agreed to in writing, software
009: * distributed under the License is distributed on an "AS IS" BASIS,
010: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011: * See the License for the specific language governing permissions and
012: * limitations under the License.
013: */
014:
015: package org.tp23.antinstaller.input;
016:
017: import org.tp23.antinstaller.InstallerContext;
018: import org.tp23.antinstaller.ValidationException;
019: import org.tp23.antinstaller.runtime.ConfigurationException;
020: import org.tp23.antinstaller.runtime.logic.Expression;
021: import org.tp23.antinstaller.runtime.logic.ExpressionBuilder;
022:
023: /**
024: * Container for other <code>OutputField</code> members that will be
025: * conditionally updated. Typical use is with <code><hidden></code>
026: * to conditionally set property values.
027: *
028: * @author mwilson
029: * @version $Id
030: * @since 0.7.4 patch 7
031: */
032: public class ConditionalField extends InputField {
033:
034: private String ifProperty;
035: private Expression expression;
036: private InputField[] fields;
037:
038: public void setIfProperty(final String expressionStr) {
039: this .ifProperty = expressionStr;
040: }
041:
042: public String getIfProperty() {
043: return ifProperty;
044: }
045:
046: public InputField[] getFields() {
047: return fields;
048: }
049:
050: public void setFields(InputField[] fields) {
051: this .fields = fields;
052: }
053:
054: /**
055: * Runtime validation of user input
056: *
057: * @param context Installer context
058: * @return <code>true</code> if user input is valid, otherwise <code>false</code>
059: * @throws ValidationException
060: */
061: public boolean validate(InstallerContext context)
062: throws ValidationException {
063: try {
064: getExpression();
065: if ((fields != null) && (fields.length > 0)) {
066: int i = 0;
067: for (; (i < fields.length)
068: && (fields[i].validate(context)); i++) {
069: }
070:
071: if (i == fields.length) {
072: return true;
073: }
074: }
075: } catch (ConfigurationException configExc) {
076: if (context.getInstaller().isVerbose()) {
077: context.log(configExc);
078: }
079: }
080: return false;
081: }
082:
083: /**
084: * Build-time validation of installer configuration file
085: *
086: * @return <code>true</code> if configuration is ok, otherwise <code>false</code>
087: */
088: public boolean validateObject() {
089: try {
090: getExpression();
091: if ((fields != null) && (fields.length > 0)) {
092: int i = 0;
093: for (; (i < fields.length)
094: && (fields[i].validateObject()); i++) {
095: }
096:
097: if (i == fields.length) {
098: return true;
099: }
100:
101: System.out.println("Invalid field:" + fields[i]);
102: }
103: } catch (ConfigurationException configExc) {
104: System.out.println("Invalid conditional expression: "
105: + configExc);
106: }
107: return false;
108: }
109:
110: /**
111: * Get the conditional expression in preparation for evaluation
112: *
113: * @throws ConfigurationException if the expression is invalid
114: */
115: public Expression getExpression() throws ConfigurationException {
116: if (expression == null) {
117: expression = ExpressionBuilder.parseLogicalExpressions(
118: resultContainer, ifProperty);
119: }
120:
121: return expression;
122: }
123: }
|