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.input;
17
18
19import org.tp23.antinstaller.InstallerContext;
20import org.tp23.antinstaller.ValidationException;
21
22
23/**
24 *
25 * <p>Input type to select targets to install </p>
26 * If the osSpecific flag is set the OS of the current system will
27 * be appended to the name of the target actually by ant run so that different
28 * Targets can be run according to the target platform.
29 * This feature goes against the principles of
30 * building cross platform installers, but is provided so that common installer
31 * tasks such as creating icons and shortcuts can be run on Windows for
32 * all those useless users who can't run a command script ;)
33 * <br>
34 * Currently there are two modes strict and not strict (lax).</p>
35 * <p>Strict target will return the target name plus the exact String in the
36 * System Property "os.name" this means you will have to provide targets for
37 * every possible OS version. See
38 * <a href="http://lopica.sourceforge.net/os.html">this page</a> for a list of possible values
39 * There are a great many but you may not want to consider some of the options.</p>
40 * <p>Lax target will return one of the following strings only
41 * <ul>
42 * <li>"[target-name]-linux" - Linux </li>
43 * <li>"[target-name]-mac" - Mac OS and Mac OS X</li>
44 * <li>"[target-name]-sun" - SunOS and Solaris</li>
45 * <li>"[target-name]-win" - Windows *</li>
46 * <li>"[target-name]-other" - any thing else</li>
47 * </ul></p> so you only have to create 5 ant targets to support all the cases.
48 * <p>Copyright: Copyright (c) 2004</p>
49 * <p>Company: tp23</p>
50 * @author Paul Hinds
51 * @version $Id: TargetInput.java,v 1.3 2006/12/07 02:42:22 teknopaul Exp $
52 */
53public class TargetInput
54    extends InputField
55    implements Target{
56
57
58    private String target;
59    private String force;
60    private String osSpecific;
61    private String strict;
62    //targets are ordered
63    private int idx;
64
65    private static int globalIdx = 1;
66
67    public TargetInput() {
68        idx = getGlobalIdx();
69    }
70
71    public String getTarget() {
72        if(isTrue(osSpecific)){
73            return getOSSpecificTarget();
74        } else {
75            return target;
76        }
77    }
78
79    /**
80     * Used to fetch the target value that was set in the config file
81     * @return
82     */
83    public String getTargetName() {
84        return target;
85    }
86
87    public void setTarget(String target) {
88        this.target = target;
89        setProperty(target);
90    }
91
92    public String getForce() {
93        return force;
94    }
95
96    public void setForce(String force) {
97        this.force = force;
98    }
99
00
01    public String getStrict() {
02        return strict;
03    }
04
05    public void setStrict(String strict) {
06        this.strict = strict;
07    }
08
09    public String getOsSpecific() {
10        return osSpecific;
11    }
12
13    public void setOsSpecific(String osSpecific) {
14        this.osSpecific = osSpecific;
15    }
16
17    /**
18     * Called to validate the user input
19     */
20    public boolean validate(InstallerContext cxt) throws ValidationException {
21        //setInputResult(target);
22        return true;
23    }
24
25
26
27    /**
28     * Used by checkConfig to validate the configuration file.
29     * Not used at runtime.
30     * @return boolean
31     */
32    public boolean validateObject() {
33        if(getDisplayText()==null){
34            System.out.println("Target:displayText must be set");
35            return false;
36        }
37        if(getTarget()==null){
38            System.out.println("Target:target must be set");
39            return false;
40        }
41//      if(getTarget().equals("default")){
42//          System.out.println("Target:target can not be \"default\"");
43//          return false;
44//      }
45        if(!InputField.optionalBoolean(getForce())){
46            System.out.println("Target:force must be true or false or null");
47            return false;
48        }
49        if(!InputField.optionalBoolean(getStrict())){
50            System.out.println("Target:strict must be true or false or null");
51            return false;
52        }
53        if(!InputField.optionalBoolean(getOsSpecific())){
54            System.out.println("Target:osSpecific must be true or false or null");
55            return false;
56        }
57        if(!InputField.requiredBoolean(getDefaultValue())){
58            System.out.println("Target:defaultValue must be true or false");
59            return false;
60        }
61        return true;
62    }
63    public int getIdx() {
64        return idx;
65    }
66    public static int getGlobalIdx() {
67        return globalIdx++;
68    }
69
70    public String getOSSpecificTarget() {
71        if(isTrue(strict)){
72            return getStrictTarget();
73        }
74        else return getLaxTarget();
75    }
76
77    private String getStrictTarget(){
78        return target + getOsSpecificSuffix();
79    }
80
81    private String getLaxTarget(){
82        return target + getLaxOsSpecificSuffix();
83    }
84
85    /**
86     * N.B.  should have added a "-" but to late to change now and not important
87     * @return
88     */
89    public static String getOsSpecificSuffix(){
90        return System.getProperty("os.name");
91    }
92    public static String getLaxOsSpecificSuffix(){
93        String osName = System.getProperty("os.name").toLowerCase();
94        if(osName.indexOf("linux") != -1){
95            return "-linux";
96        }
97        if(osName.indexOf("mac") != -1){
98            return "-mac";
99        }
00        if(osName.indexOf("windows") != -1){
01            return "-win";
02        }
03        if(osName.indexOf("solaris") != -1 || osName.indexOf("sunos") != -1){
04            return "-sun";
05        }
06        return "-other";
07        
08    }
09}
10