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;
17
18import java.io.File;
19import java.io.FileOutputStream;
20import java.util.Iterator;
21import java.util.List;
22import java.util.Properties;
23
24import org.tp23.antinstaller.input.ConditionalField;
25import org.tp23.antinstaller.input.InputField;
26import org.tp23.antinstaller.input.OutputField;
27import org.tp23.antinstaller.input.SecretPropertyField;
28import org.tp23.antinstaller.page.Page;
29
30
31
32/**
33 *
34 * <p>Outputs the completed Pages as a java Properties file. </p>
35 * @author Paul Hinds
36 * @version $Id: DefaultPropertiesFileRenderer.java,v 1.8 2007/01/09 22:41:41 teknopaul Exp $
37 */
38public class DefaultPropertiesFileRenderer
39    implements PropertiesFileRenderer {
40
41    public DefaultPropertiesFileRenderer() {
42    }
43
44    public void renderProperties(InstallerContext ctx, File baseDir){
45        Installer installer = ctx.getInstaller();
46        Page[] completedPages = installer.getPages();
47        Properties props = new Properties();
48        props.put(FILE_ROOT_PROPERTY, baseDir.getAbsolutePath());
49        props.setProperty(INSTALLER_VERSION_PROPERTY,
50                ctx.getInstaller().getVersion());
51
52
53        for (int i = 0; i < completedPages.length; i++) {
54            OutputField[] fields = completedPages[i].getOutputField();
55
56            retrieveProperties( fields, props );
57            
58            // print targets selected
59            List targets = completedPages[i].getTargets(ctx);
60            if(targets.size() > 0){
61                Iterator iterator = targets.iterator();
62                StringBuffer targetProperty = new StringBuffer();
63                while (iterator.hasNext()) {
64                    String target = (String) iterator.next();
65                    targetProperty.append(target).append(",");
66                }
67                props.put(completedPages[i].getName() + TARGETS_SUFFIX, targetProperty.toString());             
68            }
69
70        }
71        try {
72            File antInstallProperties = new File(baseDir.getAbsolutePath(), PROPERTIES_FILE_NAME);
73            FileOutputStream fos = new FileOutputStream(antInstallProperties);
74            props.store(fos,
75                        "Ant Installer - AutoGenerated properties");
76            fos.close();
77        }
78        catch (Throwable ex) {
79            if(ctx.getInstaller().isVerbose()) {
80                ctx.log(ex);
81            }
82            //swallow Exceptions as in contract for this method
83        }
84    }
85
86    private void retrieveProperties( OutputField[] fields, Properties props ) {
87        for (int f = 0; f < fields.length; f++) {
88            if (fields[f] instanceof SecretPropertyField) {
89                //InputField field = (InputField)fields[f];
90                //props.put(field.getProperty(), "XXXXXXXX");
91            }
92            else if( fields[f] instanceof ConditionalField ) {
93                ConditionalField confField = (ConditionalField) fields[f];
94                retrieveProperties( confField.getFields(), props );
95            }
96            else if (fields[f] instanceof InputField) {
97                InputField field = (InputField)fields[f];
98
99                String result = field.getInputResult();
00                //Temporary fix for PR 1353906
01                if( result == null ) {
02                    result = "";
03                }
04                props.put(field.getProperty(), result);
05            }
06        }
07    }
08}
09