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;
017:
018: import java.io.File;
019: import java.io.FileOutputStream;
020: import java.util.Iterator;
021: import java.util.List;
022: import java.util.Properties;
023:
024: import org.tp23.antinstaller.input.ConditionalField;
025: import org.tp23.antinstaller.input.InputField;
026: import org.tp23.antinstaller.input.OutputField;
027: import org.tp23.antinstaller.input.SecretPropertyField;
028: import org.tp23.antinstaller.page.Page;
029:
030: /**
031: *
032: * <p>Outputs the completed Pages as a java Properties file. </p>
033: * @author Paul Hinds
034: * @version $Id: DefaultPropertiesFileRenderer.java,v 1.8 2007/01/09 22:41:41 teknopaul Exp $
035: */
036: public class DefaultPropertiesFileRenderer implements
037: PropertiesFileRenderer {
038:
039: public DefaultPropertiesFileRenderer() {
040: }
041:
042: public void renderProperties(InstallerContext ctx, File baseDir) {
043: Installer installer = ctx.getInstaller();
044: Page[] completedPages = installer.getPages();
045: Properties props = new Properties();
046: props.put(FILE_ROOT_PROPERTY, baseDir.getAbsolutePath());
047: props.setProperty(INSTALLER_VERSION_PROPERTY, ctx
048: .getInstaller().getVersion());
049:
050: for (int i = 0; i < completedPages.length; i++) {
051: OutputField[] fields = completedPages[i].getOutputField();
052:
053: retrieveProperties(fields, props);
054:
055: // print targets selected
056: List targets = completedPages[i].getTargets(ctx);
057: if (targets.size() > 0) {
058: Iterator iterator = targets.iterator();
059: StringBuffer targetProperty = new StringBuffer();
060: while (iterator.hasNext()) {
061: String target = (String) iterator.next();
062: targetProperty.append(target).append(",");
063: }
064: props.put(completedPages[i].getName() + TARGETS_SUFFIX,
065: targetProperty.toString());
066: }
067:
068: }
069: try {
070: File antInstallProperties = new File(baseDir
071: .getAbsolutePath(), PROPERTIES_FILE_NAME);
072: FileOutputStream fos = new FileOutputStream(
073: antInstallProperties);
074: props
075: .store(fos,
076: "Ant Installer - AutoGenerated properties");
077: fos.close();
078: } catch (Throwable ex) {
079: if (ctx.getInstaller().isVerbose()) {
080: ctx.log(ex);
081: }
082: //swallow Exceptions as in contract for this method
083: }
084: }
085:
086: private void retrieveProperties(OutputField[] fields,
087: Properties props) {
088: for (int f = 0; f < fields.length; f++) {
089: if (fields[f] instanceof SecretPropertyField) {
090: //InputField field = (InputField)fields[f];
091: //props.put(field.getProperty(), "XXXXXXXX");
092: } else if (fields[f] instanceof ConditionalField) {
093: ConditionalField confField = (ConditionalField) fields[f];
094: retrieveProperties(confField.getFields(), props);
095: } else if (fields[f] instanceof InputField) {
096: InputField field = (InputField) fields[f];
097:
098: String result = field.getInputResult();
099: //Temporary fix for PR 1353906
100: if (result == null) {
101: result = "";
102: }
103: props.put(field.getProperty(), result);
104: }
105: }
106: }
107: }
|