source: other-projects/trunk/realistic-books/packages/AntInstaller/src/org/tp23/antinstaller/runtime/ConfigurationLoader.java@ 19253

Last change on this file since 19253 was 19253, checked in by davidb, 15 years ago

Establishing a source code repository for Veronica's Realistic Book's software

File size: 12.2 KB
Line 
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 java.io.File;
19import java.io.IOException;
20import java.util.ArrayList;
21import java.util.HashSet;
22import java.util.Iterator;
23import java.util.Set;
24
25import org.tp23.antinstaller.InstallException;
26import org.tp23.antinstaller.Installer;
27import org.tp23.antinstaller.PropertiesFileRenderer;
28import org.tp23.antinstaller.input.ConditionalField;
29import org.tp23.antinstaller.input.InputField;
30import org.tp23.antinstaller.input.OutputField;
31import org.tp23.antinstaller.input.ResultContainer;
32import org.tp23.antinstaller.input.SelectInput;
33import org.tp23.antinstaller.input.TargetInput;
34import org.tp23.antinstaller.input.TargetSelectInput;
35import org.tp23.antinstaller.page.Page;
36import org.tp23.antinstaller.page.ProgressPage;
37import org.tp23.antinstaller.page.SimpleInputPage;
38import org.tp23.antinstaller.renderer.swing.plaf.LookAndFeelFactory;
39import org.tp23.antinstaller.runtime.exe.LoadConfigFilter;
40import org.tp23.antinstaller.runtime.exe.PropertyLoaderFilter;
41import org.tp23.antinstaller.runtime.logic.ExpressionBuilder;
42/**
43 *
44 * <p>Loads the configuration file into memory as an Installer object. </p>
45 * <p>This class also contains the main() method to check the config files for common errors </p>
46 * <p>Copyright: Copyright (c) 2004</p>
47 * <p>Company: tp23</p>
48 * @todo this should be an interface not a class
49 * @author Paul Hinds
50 * @version $Id: ConfigurationLoader.java,v 1.15 2007/01/28 08:44:43 teknopaul Exp $
51 */
52public class ConfigurationLoader extends LoadConfigFilter{
53
54 /**
55 * Command line config checker
56 * @param args String[]
57 * @throws InstallException
58 */
59 public static void main(String[] args) {
60 ConfigurationLoader configurationLoader = new ConfigurationLoader();
61 String configFile = INSTALLER_CONFIG_FILE;
62 if(args.length > 1 && args[1].endsWith(".xml")){
63 configFile = args[1];
64 }
65 int ret = 1;
66 try {
67 configurationLoader.readConfig(new File(args[0]), configFile);
68 ret = configurationLoader.validate();
69
70 if(ret > 0){
71 System.out.println("VALIDATION FAILED");
72 }
73 }
74 catch (ConfigurationException ex) {
75 ex.printStackTrace();
76 System.exit(ret);
77 }
78 catch (IOException ex) {
79 ex.printStackTrace();
80 System.exit(ret);
81 } catch (InstallException ex) {
82 // probably ifProperty syntax wrong
83 ex.printStackTrace();
84 System.exit(ret);
85 }
86 }
87
88 /**
89 * This method is not valid until super.readConfig() has been run
90 * @return
91 */
92 public Installer getInstaller(){
93 return installer;
94 }
95
96 public int validate() throws IOException, ConfigurationException, InstallException{
97 Page[] pages = installer.getPages();
98 boolean foundErrors = false;
99 Set pageNames = new HashSet();
100 Set targets = new HashSet();
101 Set propertyNames = new HashSet();
102 Set pagePropertyNames = null;
103
104 if(validateInstallerAttributes()){
105 foundErrors = true;
106 }
107
108 for (int p = 0; p < pages.length; p++) {
109 System.out.println("Checking page: " + pages[p].getName() );
110 if(pageNames.contains(pages[p].getName())){
111 System.out.println("Error: page name '"
112 + pages[p].getName()
113 + "' repeated - auto loading of configuration will fail");
114 foundErrors = true;
115 }
116 pageNames.add(pages[p].getName());
117
118 //TODO check page requirements
119 //test ifProperty syntax
120 // TODO passes validation even if nothing will evaluate
121 if (pages[p] instanceof SimpleInputPage) {
122 SimpleInputPage sPage = (SimpleInputPage)pages[p];
123 if(sPage.getIfProperty() != null){
124 try {
125 ResultContainer mock = new ResultContainer();
126 ExpressionBuilder.parseLogicalExpressions( mock,
127 sPage.getIfProperty() );
128 }
129 catch( ConfigurationException configExc ){
130 System.out.println("Error: loading ifProperty," + sPage.getIfProperty() + " ,page: " + pages[p].getName() );
131 foundErrors = true;
132 }
133 }
134 }
135
136
137 pagePropertyNames = new HashSet();
138
139 OutputField[] fields = pages[p].getOutputField();
140 for (int f = 0; f < fields.length; f++) {
141 if(!fields[f].validateObject()){
142 foundErrors = true;
143 System.out.println("Error in page:" + pages[p].getName());
144 }
145 if(fields[f] instanceof TargetInput){
146 TargetInput tgtInput = (TargetInput)fields[f];
147 targets.add(tgtInput.getTarget());
148 }
149 if(fields[f] instanceof InputField && !(fields[f] instanceof ConditionalField) ){
150 InputField genericInput = (InputField)fields[f];
151 if(genericInput.getProperty().endsWith(PropertiesFileRenderer.TARGETS_SUFFIX)){
152 System.out.println("Error: invalid property name:" + genericInput.getProperty());
153 System.out.println("InputField names must not end with -targets");
154 }
155 String propertyName = genericInput.getProperty();
156 //System.out.println("Checking page.property: " + pages[p].getName() + "," + propertyName );
157 if(propertyNames.contains(propertyName)){
158 //foundErrors = true;
159 System.out.println("Repeated property name:" + propertyName);
160 System.out.println("Loading defaults from file will probably not work:" + propertyName);
161 }
162 else{
163 propertyNames.add(propertyName);
164 }
165 // repeated properties on the same page are an error always
166 if(pagePropertyNames.contains(propertyName)){
167 foundErrors = true;
168 System.out.println("Repeated property name: page=" +
169 pages[p].getName() + ", property=" + propertyName);
170 }
171 else{
172 pagePropertyNames.add(propertyName);
173 }
174
175 }
176 }
177
178 }
179 System.out.println("Finished checking config inputs");
180 // check page structure
181 if(!(pages[pages.length-1] instanceof ProgressPage)){
182 foundErrors = true;
183 System.out.println("Last Page should be a progress page");
184 }
185 else{
186 if (pages[pages.length-1].getPostDisplayTarget() != null){
187 foundErrors = true;
188 System.out.println("Progress pages do not support postDisplayTarget");
189 }
190 }
191 // check for targets
192 int numOfPageTargets = 0;
193 for (int p = 0; p < pages.length; p++) {
194 numOfPageTargets += pages[p].getAllTargets().size();
195 }
196 if(numOfPageTargets == 0){
197 System.out.println("Warning: No Page Targets (not a problem if there are target input types)");
198 }
199
200 Iterator iter = targets.iterator();
201 while (iter.hasNext()) {
202 String tgt = (String) iter.next();
203 if(tgt.endsWith(PropertiesFileRenderer.TARGETS_SUFFIX)){
204 System.out.println("Error: invalid target name:" + tgt);
205 System.out.println("Target names must not end with -targets");
206 foundErrors = true;
207 }
208 }
209
210 //@todo check targets exist in build.xml remember OSSpecific could be tricky to validate
211
212 int numOfTargetInputs = 0;
213 // check ifTargets
214 ArrayList targetsSoFar = new ArrayList();
215 for (int p = 0; p < pages.length; p++) {
216 if(pages[p] instanceof SimpleInputPage){
217 SimpleInputPage simple = (SimpleInputPage)pages[p];
218 String ifTarget = simple.getIfTarget();
219 if(ifTarget != null && !targetsSoFar.contains(ifTarget)){
220 System.out.println("ifTarget=" + ifTarget);
221 System.out.println("ifTarget will never test true, no prior target in page:"+pages[p].getName());
222 // disabled due to bug 1412658 could be reinstated with proper test and OSSpecific handling
223 //foundErrors = true;
224 }
225 }
226 // add after to ensure testing previous pages
227 targetsSoFar.addAll(pages[p].getAllTargets());
228 OutputField[] fields = pages[p].getOutputField();
229 for (int f = 0; f < fields.length; f++) {
230 if(fields[f] instanceof TargetInput){
231 if(numOfTargetInputs == 0){
232 System.out.println("Found target input type");
233 }
234 numOfTargetInputs++;
235 TargetInput ti = (TargetInput)fields[f];
236 targetsSoFar.add(ti.getTarget());
237 }
238 if(fields[f] instanceof TargetSelectInput){
239 if(numOfTargetInputs == 0){
240 System.out.println("Found target input type");
241 }
242 numOfTargetInputs++;
243 TargetSelectInput ti = (TargetSelectInput)fields[f];
244 SelectInput.Option[] options = ti.getOptions();
245 for (int i = 0; i < options.length; i++) {
246 SelectInput.Option option = options[i];
247 targetsSoFar.add(option.value);
248 }
249 }
250 }
251 }
252 if(numOfPageTargets == 0 && numOfTargetInputs == 0){
253 System.out.println("Warning: No targets found, installer may do nothing.");
254 }
255// if(targetsSoFar.contains("default")){
256// System.out.println("Target:target can not be \"default\"");
257// foundErrors = true;
258// }
259
260
261 System.out.println("Finished checking config");
262 if(!foundErrors){
263 return 0;
264 }
265 return 1;
266 }
267
268 private boolean validateInstallerAttributes(){
269
270 System.out.println("Checking installer: " + installer.getName() );
271 boolean foundErrors = false;
272
273 String[] validBooleanValues = {"true", "false"};
274 foundErrors |= validateValue("antialiased", installer.getAntialiased(), true, validBooleanValues);
275
276 // done in DTD
277 //foundErrors |= validateValue("verbose", installer.isVerbose(), true, validBooleanValues);
278 //foundErrors |= validateValue("debug", installer.isDebug(), true, validBooleanValues);
279
280 String[] validLAFValues = {LookAndFeelFactory.DEFAULT_LAF,
281 LookAndFeelFactory.GREYMETAL_LAF,
282 LookAndFeelFactory.NATIVE_LAF,
283 LookAndFeelFactory.JGOODIES_LAF,
284 LookAndFeelFactory.NULL_LAF };
285 if( validateValue("lookAndFeel", installer.getLookAndFeel(), true, validLAFValues) ){
286 System.out.println("Warning: non standard LookAndFeel ensure the correct classes are on the classpath at runtime:" + installer.getLookAndFeel());
287 }
288
289 if (installer.getName() == null){
290 System.out.println("Error: installer element attribute does not exist: name");
291 foundErrors = true;
292 }
293
294 try {
295 String wide = installer.getWide();
296 if(wide != null){
297 installer.parseWideValue(wide);
298 }
299 } catch (Exception e) {
300 System.out.println("Error: installer element attribute incorrect format (e.g. 600:275): wide");
301 foundErrors = true;
302 }
303
304 String[] validLoadDefaultValues = {PropertyLoaderFilter.FALSE,
305 PropertyLoaderFilter.LOAD,
306 PropertyLoaderFilter.PROMPT,
307 PropertyLoaderFilter.PROMPT_AUTO};
308
309 boolean loadDefaultsNull = true;
310 if( installer.supportsAutoBuild() ){
311 loadDefaultsNull = false;
312 }
313 foundErrors |= validateValue("loadDefaults", installer.getLoadDefaults(), loadDefaultsNull, validLoadDefaultValues);
314
315 VersionHelper vHelper = new VersionHelper();
316 if( installer.supportsAutoBuild() ){
317 if( ! vHelper.isValid(installer.getVersion()) ){
318 System.out.println("Error: invalid version attribute, required for -auto builds:" + installer.getVersion());
319 foundErrors = true;
320 }
321 }
322 if(installer.getVersion() != null){
323 if( ! vHelper.isValid(installer.getVersion()) ){
324 System.out.println("Error: invalid version attribute format examples 1.2.0 , 0.2beta:" + installer.getVersion());
325 foundErrors = true;
326 }
327 }
328
329 return foundErrors;
330 }
331 /**
332 * @return foundErrors (true if there was an error)
333 */
334 private boolean validateValue(String att, String value, boolean allowsNull, String[] validValues){
335 if(value == null){
336 if(!allowsNull){
337 System.out.println("Error: installer element attribute does not exist: " + att);
338 return true;
339 }
340 return false;
341 }
342 else {
343 for (int i = 0; i < validValues.length; i++) {
344 if(validValues[i].equals(value)){
345 return false;
346 }
347 }
348 System.out.println("Error: installer element attribute not valid value: " + att);
349 return true;
350 }
351 }
352
353}
Note: See TracBrowser for help on using the repository browser.