001: /*
002: * Copyright 2005 Paul Hinds
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.tp23.antinstaller.input;
017:
018: import org.tp23.antinstaller.InstallerContext;
019: import org.tp23.antinstaller.ValidationException;
020:
021: /**
022: *
023: * <p>Input type to select targets to install </p>
024: * If the osSpecific flag is set the OS of the current system will
025: * be appended to the name of the target actually by ant run so that different
026: * Targets can be run according to the target platform.
027: * This feature goes against the principles of
028: * building cross platform installers, but is provided so that common installer
029: * tasks such as creating icons and shortcuts can be run on Windows for
030: * all those useless users who can't run a command script ;)
031: * <br>
032: * Currently there are two modes strict and not strict (lax).</p>
033: * <p>Strict target will return the target name plus the exact String in the
034: * System Property "os.name" this means you will have to provide targets for
035: * every possible OS version. See
036: * <a href="http://lopica.sourceforge.net/os.html">this page</a> for a list of possible values
037: * There are a great many but you may not want to consider some of the options.</p>
038: * <p>Lax target will return one of the following strings only
039: * <ul>
040: * <li>"[target-name]-linux" - Linux </li>
041: * <li>"[target-name]-mac" - Mac OS and Mac OS X</li>
042: * <li>"[target-name]-sun" - SunOS and Solaris</li>
043: * <li>"[target-name]-win" - Windows *</li>
044: * <li>"[target-name]-other" - any thing else</li>
045: * </ul></p> so you only have to create 5 ant targets to support all the cases.
046: * <p>Copyright: Copyright (c) 2004</p>
047: * <p>Company: tp23</p>
048: * @author Paul Hinds
049: * @version $Id: TargetInput.java,v 1.3 2006/12/07 02:42:22 teknopaul Exp $
050: */
051: public class TargetInput extends InputField implements Target {
052:
053: private String target;
054: private String force;
055: private String osSpecific;
056: private String strict;
057: //targets are ordered
058: private int idx;
059:
060: private static int globalIdx = 1;
061:
062: public TargetInput() {
063: idx = getGlobalIdx();
064: }
065:
066: public String getTarget() {
067: if (isTrue(osSpecific)) {
068: return getOSSpecificTarget();
069: } else {
070: return target;
071: }
072: }
073:
074: /**
075: * Used to fetch the target value that was set in the config file
076: * @return
077: */
078: public String getTargetName() {
079: return target;
080: }
081:
082: public void setTarget(String target) {
083: this .target = target;
084: setProperty(target);
085: }
086:
087: public String getForce() {
088: return force;
089: }
090:
091: public void setForce(String force) {
092: this .force = force;
093: }
094:
095: public String getStrict() {
096: return strict;
097: }
098:
099: public void setStrict(String strict) {
100: this .strict = strict;
101: }
102:
103: public String getOsSpecific() {
104: return osSpecific;
105: }
106:
107: public void setOsSpecific(String osSpecific) {
108: this .osSpecific = osSpecific;
109: }
110:
111: /**
112: * Called to validate the user input
113: */
114: public boolean validate(InstallerContext cxt)
115: throws ValidationException {
116: //setInputResult(target);
117: return true;
118: }
119:
120: /**
121: * Used by checkConfig to validate the configuration file.
122: * Not used at runtime.
123: * @return boolean
124: */
125: public boolean validateObject() {
126: if (getDisplayText() == null) {
127: System.out.println("Target:displayText must be set");
128: return false;
129: }
130: if (getTarget() == null) {
131: System.out.println("Target:target must be set");
132: return false;
133: }
134: // if(getTarget().equals("default")){
135: // System.out.println("Target:target can not be \"default\"");
136: // return false;
137: // }
138: if (!InputField.optionalBoolean(getForce())) {
139: System.out
140: .println("Target:force must be true or false or null");
141: return false;
142: }
143: if (!InputField.optionalBoolean(getStrict())) {
144: System.out
145: .println("Target:strict must be true or false or null");
146: return false;
147: }
148: if (!InputField.optionalBoolean(getOsSpecific())) {
149: System.out
150: .println("Target:osSpecific must be true or false or null");
151: return false;
152: }
153: if (!InputField.requiredBoolean(getDefaultValue())) {
154: System.out
155: .println("Target:defaultValue must be true or false");
156: return false;
157: }
158: return true;
159: }
160:
161: public int getIdx() {
162: return idx;
163: }
164:
165: public static int getGlobalIdx() {
166: return globalIdx++;
167: }
168:
169: public String getOSSpecificTarget() {
170: if (isTrue(strict)) {
171: return getStrictTarget();
172: } else
173: return getLaxTarget();
174: }
175:
176: private String getStrictTarget() {
177: return target + getOsSpecificSuffix();
178: }
179:
180: private String getLaxTarget() {
181: return target + getLaxOsSpecificSuffix();
182: }
183:
184: /**
185: * N.B. should have added a "-" but to late to change now and not important
186: * @return
187: */
188: public static String getOsSpecificSuffix() {
189: return System.getProperty("os.name");
190: }
191:
192: public static String getLaxOsSpecificSuffix() {
193: String osName = System.getProperty("os.name").toLowerCase();
194: if (osName.indexOf("linux") != -1) {
195: return "-linux";
196: }
197: if (osName.indexOf("mac") != -1) {
198: return "-mac";
199: }
200: if (osName.indexOf("windows") != -1) {
201: return "-win";
202: }
203: if (osName.indexOf("solaris") != -1
204: || osName.indexOf("sunos") != -1) {
205: return "-sun";
206: }
207: return "-other";
208:
209: }
210: }
|