01: /*
02: * Copyright 2005 Paul Hinds
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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 org.tp23.antinstaller.runtime;
17:
18: import org.tp23.antinstaller.InstallException;
19: import org.tp23.antinstaller.InstallerContext;
20: import org.tp23.antinstaller.input.ResultContainer;
21: import org.tp23.antinstaller.page.ConditionalPage;
22: import org.tp23.antinstaller.page.Page;
23: import org.tp23.antinstaller.page.SimpleInputPage;
24: import org.tp23.antinstaller.page.TextPage;
25: import org.tp23.antinstaller.runtime.logic.Expression;
26: import org.tp23.antinstaller.runtime.logic.ExpressionBuilder;
27:
28: /**
29: * <p>Encapsulates code fo the ifProperty feature</p>
30: * N.B. It is the installer generator's responsibility to ensure that properties passed
31: * to the less than or greater than test are valid Numbers.
32: * The internal Java format used is a Double so avalid regex would be
33: * something like [0-9]+\.*[0-9]* or [0-9]+ for an Integer.
34: * The rather strange -= and += syntax is used because > and <
35: * must be escaped to &gt; and &lt; in XML attributes and the legibility
36: * of the configuration files would be impared.
37: * REF: 1145496
38: * @author Paul Hinds
39: * @version $Id: IfPropertyHelper.java,v 1.5 2007/01/19 00:24:36 teknopaul Exp $
40: */
41: public class IfPropertyHelper {
42:
43: private InstallerContext ctx = null;
44:
45: public IfPropertyHelper(InstallerContext ctx) {
46: this .ctx = ctx;
47: }
48:
49: /**
50: * @return boolean true to SHOW the page
51: */
52: public boolean ifProperty(Page next) throws InstallException {
53: // show page if ifProperty is specified and property is correct
54: if (next instanceof ConditionalPage) {
55: ConditionalPage conditionalPage = (ConditionalPage) next;
56: String ifProperty = conditionalPage.getIfProperty();
57: return ifProperty(ifProperty, ctx.getInstaller()
58: .getResultContainer());
59: }
60: return true; // show the page by default
61: }
62:
63: public boolean ifProperty(String ifProperty, ResultContainer results)
64: throws InstallException {
65: if (ifProperty != null) {
66: Expression expression;
67: try {
68: expression = ExpressionBuilder.parseLogicalExpressions(
69: results, ifProperty);
70: } catch (ConfigurationException configExc) {
71: throw new InstallException(
72: "Error parsing ifProperty condition for ifProperty "
73: + ifProperty, configExc);
74: }
75: return expression.evaluate();
76: }
77: return true; // show the page by default
78: }
79:
80: /**
81: * @return boolean true to SHOW the page
82: */
83: public boolean ifTarget(Page next, Page[] pages) {
84: // skip iftarget specified and target is missing
85: if (next instanceof ConditionalPage) {
86: ConditionalPage conditionalPage = (ConditionalPage) next;
87: String ifTarget = conditionalPage.getIfTarget();
88: if (ifTarget != null) {
89: boolean show = false;
90: for (int p = 0; p < pages.length; p++) {
91: show |= pages[p].isTarget(ifTarget);
92: }
93: return show;
94: }
95: }
96: return true;
97: }
98: }
|