OutputField.java |
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 18import java.util.MissingResourceException; 19import java.util.ResourceBundle; 20 21import org.tp23.antinstaller.InstallerContext; 22import org.tp23.antinstaller.ValidationException; 23/** 24 * This is the super class of all "Input types". It is called OutputField since 25 * it handles the base features of "Input types" for outputing text for 26 * the user to read. It also encapsulates some convenience methods for 27 * interpreting boolean values from the command line and in configuration files. 28 */ 29public abstract class OutputField { 30 31 // i18n support 32 private static ResourceBundle langPack = null; 33 private static int commentIdx = 0; 34 static{ 35 try { 36 langPack = ResourceBundle.getBundle("resources.LanguagePack"); 37 } catch (MissingResourceException e) { 38 // ignore, signifies no lang packs installed 39 } 40 } 41 42 /* This is redundant unless language packs are used 43 */ 44 private String name = "comment." + ++commentIdx; 45 46 protected String displayText; 47 protected String explanatoryText; 48 protected ResultContainer resultContainer; 49 50 public OutputField() { 51 52 } 53 54 /* This is redundant unless language packs are used 55 */ 56 public String getName() { 57 return name; 58 } 59 60 /* This is redundant unless language packs are used 61 */ 62 public void setName(String name) { 63 this.name = name; 64 } 65 66 public String getDisplayText() { 67 if(langPack != null){ 68 return langPack.getString(getName() + ".displayText"); 69 } 70 return displayText; 71 } 72 73 public void setDisplayText(String displayText) { 74 this.displayText = displayText; 75 } 76 77 public String getExplanatoryText() { 78 if(langPack != null){ 79 try { 80 return langPack.getString(getName() + ".explanatoryText"); 81 } catch (MissingResourceException e) { 82 // ignore and return null explanatoryText is optional 83 } 84 } 85 return explanatoryText; 86 } 87 88 public void setExplanatoryText(String explanatoryText) { 89 this.explanatoryText = explanatoryText; 90 } 91 92 public void setResultContainer(ResultContainer resultContainer) { 93 this.resultContainer = resultContainer; 94 } 95 96 /** 97 * Validate the user input (or lack of it) 98 * This method should return false if the validation fails an throw an exception 99 * if it is not possible to validate or there is an error. 00 * 01 * @param cxt InstallerContext 02 * @throws ValidationException thrown in error conditions not validation failure 03 * @return boolean 04 */ 05 public abstract boolean validate(InstallerContext cxt) throws ValidationException; 06 07 /** 08 * Used to validate the configuration, this can be run prior to distributing the 09 * installer to check that the config is valid. Will not be used at runtime. 10 * @throws ValidationException 11 * @return boolean 12 */ 13 public abstract boolean validateObject(); 14 15 //////////////////////Static convenience methods 16 17 /** true if specified and true or yes. 18 * N.B it is possible for X, isTrue(X) == isFalse(X); 19 * This occurs if the value is null. 20 */ 21 public static boolean isTrue(String value){ 22 if(value == null)return false; 23 return value.equalsIgnoreCase("true") || value.equalsIgnoreCase("yes"); 24 } 25 /** same as isTrue() but default is false if not specified */ 26 public static boolean isFalse(String value){ 27 if(value == null)return false; 28 return value.equalsIgnoreCase("false") || value.equalsIgnoreCase("no"); 29 } 30 /** 31 * Return true if the value is set to true or false, returns false if the value is null 32 * @param value String 33 * @return boolean 34 */ 35 public static boolean requiredBoolean(String value){ 36 return isTrue(value) || isFalse(value); 37 } 38 /** 39 * Return true if the value is set to true or false, returns false if the value is null 40 * @param value String 41 * @return boolean 42 */ 43 public static boolean optionalBoolean(String value){ 44 return value == null || isTrue(value) || isFalse(value); 45 } 46} 47