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.BufferedWriter;
019: import java.io.File;
020: import java.io.FileWriter;
021: import java.util.Date;
022: import java.util.Iterator;
023: import java.util.List;
024:
025: import org.tp23.antinstaller.input.ConditionalField;
026: import org.tp23.antinstaller.input.InputField;
027: import org.tp23.antinstaller.input.OutputField;
028: import org.tp23.antinstaller.input.SecretPropertyField;
029: import org.tp23.antinstaller.page.Page;
030:
031: /**
032: * <p>Outputs the completed Pages as a Java Properties file. The file produced is compatible
033: * with java.util.Properties. </p>
034: * <p>Output is commented as it is printed to aid debugging</p>
035: * @see http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html#100850
036: * @author Paul Hinds
037: * @version $Id: ExplicitPropertiesFileRenderer.java,v 1.9 2007/01/09 22:41:41 teknopaul Exp $
038: */
039: public class ExplicitPropertiesFileRenderer implements
040: PropertiesFileRenderer {
041:
042: private static String newLine = System
043: .getProperty("line.separator");
044: private static final char[] hexidecimals = { '0', '1', '2', '3',
045: '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
046:
047: public ExplicitPropertiesFileRenderer() {
048: }
049:
050: public void renderProperties(InstallerContext ctx, File baseDir) {
051: Installer installer = ctx.getInstaller();
052: Page[] completedPages = installer.getPages();
053:
054: StringBuffer propertiesData = new StringBuffer();
055: propertiesData
056: .append("### Ant Installer - properties auto generated on ");
057: propertiesData.append(convert(new Date().toString(), true));
058: propertiesData.append(newLine);
059: propertiesData.append(newLine);
060:
061: propertiesData.append(FILE_ROOT_PROPERTY);
062: propertiesData.append(" = ");
063: propertiesData.append(convert(baseDir.getAbsolutePath(), true));
064: propertiesData.append(newLine);
065:
066: propertiesData.append(INSTALLER_VERSION_PROPERTY);
067: propertiesData.append(" = ");
068: propertiesData.append(convert(ctx.getInstaller().getVersion(),
069: true));
070: propertiesData.append(newLine);
071:
072: propertiesData.append(newLine);
073: String property = null;
074: String value = null;
075:
076: for (int i = 0; i < completedPages.length; i++) {
077: OutputField[] fields = completedPages[i].getOutputField();
078:
079: propertiesData.append(newLine);
080: propertiesData.append("## Properties from Page:"
081: + completedPages[i].getName());
082: propertiesData.append(newLine);
083:
084: retrievePropertiesData(fields, propertiesData);
085:
086: // print targets selected
087: List targets = completedPages[i].getTargets(ctx);
088: if (targets.size() > 0) {
089: Iterator iterator = targets.iterator();
090: StringBuffer targetProperty = new StringBuffer();
091: while (iterator.hasNext()) {
092: String target = (String) iterator.next();
093: targetProperty.append(target).append(",");
094: }
095: propertiesData.append("# Targets selected for page");
096: propertiesData.append(newLine);
097: property = convert(completedPages[i].getName()
098: + TARGETS_SUFFIX, true);
099: value = convert(targetProperty.toString(), true);
100: propertiesData.append(property + " = " + value);
101: propertiesData.append(newLine);
102:
103: }
104: }
105: try {
106: File antInstallProperties = new File(baseDir
107: .getAbsolutePath(), PROPERTIES_FILE_NAME);
108: FileWriter fos = new FileWriter(antInstallProperties);
109: BufferedWriter writer = new BufferedWriter(fos);
110: writer.write(propertiesData.toString());
111: writer.flush();
112: fos.close();
113: } catch (Throwable ex) {
114: if (ctx.getInstaller().isVerbose()) {
115: ctx.log(ex);
116: }
117: //swallow Exceptions as in the contract for this method
118: }
119: }
120:
121: private void retrievePropertiesData(OutputField[] fields,
122: StringBuffer propertiesData) {
123: String property = null;
124: String value = null;
125:
126: for (int f = 0; f < fields.length; f++) {
127: if (fields[f] instanceof SecretPropertyField) {
128: InputField field = (InputField) fields[f];
129: //String result = field.getInputResult();
130: propertiesData.append("# Property hidden "
131: + printClass(fields[f].getClass()));
132: propertiesData.append(newLine);
133: property = convert(field.getProperty(), true);
134: propertiesData.append("#" + property + "=XXXXXXXX");
135: propertiesData.append(newLine);
136: } else if (fields[f] instanceof ConditionalField) {
137: ConditionalField confField = (ConditionalField) fields[f];
138: retrievePropertiesData(confField.getFields(),
139: propertiesData);
140: } else if (fields[f] instanceof InputField) {
141: InputField field = (InputField) fields[f];
142: String result = field.getInputResult();
143: propertiesData.append("# "
144: + printClass(fields[f].getClass()));
145: propertiesData.append(newLine);
146:
147: property = convert(field.getProperty(), true);
148: value = convert(result, false);
149: propertiesData.append(property + " = " + value);
150: propertiesData.append(newLine);
151: }
152: }
153: }
154:
155: private String printClass(Class clazz) {
156: String name = clazz.getName();
157: int lastDot = name.lastIndexOf('.');
158: return name.substring(lastDot, name.length());
159: }
160:
161: private String convert(String input, boolean doSpaces) {
162: if (input == null) {
163: // this happens when a page is skipped in text mode
164: return "";
165: }
166: int num = input.length();
167: StringBuffer sb = new StringBuffer(num);
168:
169: for (int i = 0; i < num; i++) {
170: char c = input.charAt(i);
171: switch (c) {
172: case ' ':
173: if (i == 0 || doSpaces) {
174: sb.append('\\');
175: }
176: sb.append(' ');
177: break;
178: case '\n':
179: sb.append("\\n");
180: break;
181: case '\r':
182: sb.append("\\r");
183: break;
184: case '\\':
185: sb.append("\\\\");
186: break;
187: case '\t':
188: sb.append("\\t");
189: break;
190: case '\f':
191: sb.append("\\f");
192: break;
193: case '=':
194: sb.append("\\=");
195: break;
196: case ':':
197: sb.append("\\:");
198: break;
199: case '#':
200: sb.append("\\#");
201: break;
202: case '!':
203: sb.append("\\!");
204: break;
205:
206: default:
207: if ((c < 0x0020) || (c > 0x007e)) {
208: sb.append("\\u").append(hex((c >> 12) & 0xF))
209: .append(hex((c >> 8) & 0xF)).append(
210: hex((c >> 4) & 0xF)).append(
211: hex(c & 0xF));
212: } else {
213: sb.append(c);
214: }
215: }
216: }
217: return sb.toString();
218: }
219:
220: private char hex(int val) {
221: return hexidecimals[(val & 0xF)];
222: }
223:
224: }
|