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