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 | */
|
---|
16 | package org.tp23.antinstaller.input;
|
---|
17 |
|
---|
18 | import java.util.MissingResourceException;
|
---|
19 | import java.util.ResourceBundle;
|
---|
20 |
|
---|
21 | import org.tp23.antinstaller.InstallerContext;
|
---|
22 | import 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 | */
|
---|
29 | public abstract class OutputField {
|
---|
30 |
|
---|
31 | private static int commentIdx = 0;
|
---|
32 |
|
---|
33 | /* This is redundant unless language packs are used
|
---|
34 | */
|
---|
35 | private String name = "comment." + ++commentIdx;
|
---|
36 |
|
---|
37 | protected String displayText;
|
---|
38 | protected String explanatoryText;
|
---|
39 | protected ResultContainer resultContainer;
|
---|
40 |
|
---|
41 | public OutputField() {
|
---|
42 |
|
---|
43 | }
|
---|
44 |
|
---|
45 | /* This is redundant unless language packs are used
|
---|
46 | */
|
---|
47 | public String getName() {
|
---|
48 | return name;
|
---|
49 | }
|
---|
50 |
|
---|
51 | /* This is redundant unless language packs are used
|
---|
52 | */
|
---|
53 | public void setName(String name) {
|
---|
54 | this.name = name;
|
---|
55 | }
|
---|
56 |
|
---|
57 | public String getDisplayText() {
|
---|
58 | if( org.tp23.antinstaller.Installer.langPack != null ) {
|
---|
59 | return org.tp23.antinstaller.Installer.langPack.getString(getName() + ".displayText");
|
---|
60 | }
|
---|
61 | return displayText;
|
---|
62 | }
|
---|
63 |
|
---|
64 | public void setDisplayText(String displayText) {
|
---|
65 | this.displayText = displayText;
|
---|
66 | }
|
---|
67 |
|
---|
68 | public String getExplanatoryText() {
|
---|
69 | if( org.tp23.antinstaller.Installer.langPack != null ) {
|
---|
70 | try {
|
---|
71 | return org.tp23.antinstaller.Installer.langPack.getString(getName() + ".explanatoryText");
|
---|
72 | } catch (MissingResourceException e) {
|
---|
73 | // ignore and return null explanatoryText is optional
|
---|
74 | }
|
---|
75 | }
|
---|
76 | return explanatoryText;
|
---|
77 | }
|
---|
78 |
|
---|
79 | public void setExplanatoryText(String explanatoryText) {
|
---|
80 | this.explanatoryText = explanatoryText;
|
---|
81 | }
|
---|
82 |
|
---|
83 | public void setResultContainer(ResultContainer resultContainer) {
|
---|
84 | this.resultContainer = resultContainer;
|
---|
85 | }
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * Validate the user input (or lack of it)
|
---|
89 | * This method should return false if the validation fails an throw an exception
|
---|
90 | * if it is not possible to validate or there is an error.
|
---|
91 | *
|
---|
92 | * @param cxt InstallerContext
|
---|
93 | * @throws ValidationException thrown in error conditions not validation failure
|
---|
94 | * @return boolean
|
---|
95 | */
|
---|
96 | public abstract boolean validate(InstallerContext cxt) throws ValidationException;
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * Used to validate the configuration, this can be run prior to distributing the
|
---|
100 | * installer to check that the config is valid. Will not be used at runtime.
|
---|
101 | * @throws ValidationException
|
---|
102 | * @return boolean
|
---|
103 | */
|
---|
104 | public abstract boolean validateObject();
|
---|
105 |
|
---|
106 | //////////////////////Static convenience methods
|
---|
107 |
|
---|
108 | /** true if specified and true or yes.
|
---|
109 | * N.B it is possible for X, isTrue(X) == isFalse(X);
|
---|
110 | * This occurs if the value is null.
|
---|
111 | */
|
---|
112 | public static boolean isTrue(String value){
|
---|
113 | if(value == null)return false;
|
---|
114 | return value.equalsIgnoreCase("true") || value.equalsIgnoreCase("yes");
|
---|
115 | }
|
---|
116 | /** same as isTrue() but default is false if not specified */
|
---|
117 | public static boolean isFalse(String value){
|
---|
118 | if(value == null)return false;
|
---|
119 | return value.equalsIgnoreCase("false") || value.equalsIgnoreCase("no");
|
---|
120 | }
|
---|
121 | /**
|
---|
122 | * Return true if the value is set to true or false, returns false if the value is null
|
---|
123 | * @param value String
|
---|
124 | * @return boolean
|
---|
125 | */
|
---|
126 | public static boolean requiredBoolean(String value){
|
---|
127 | return isTrue(value) || isFalse(value);
|
---|
128 | }
|
---|
129 | /**
|
---|
130 | * Return true if the value is set to true or false, returns false if the value is null
|
---|
131 | * @param value String
|
---|
132 | * @return boolean
|
---|
133 | */
|
---|
134 | public static boolean optionalBoolean(String value){
|
---|
135 | return value == null || isTrue(value) || isFalse(value);
|
---|
136 | }
|
---|
137 | }
|
---|