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;
|
---|
17 |
|
---|
18 | import java.io.BufferedWriter;
|
---|
19 | import java.io.File;
|
---|
20 | import java.io.FileWriter;
|
---|
21 | import java.util.Date;
|
---|
22 | import java.util.Iterator;
|
---|
23 | import java.util.List;
|
---|
24 |
|
---|
25 | import org.tp23.antinstaller.input.ConditionalField;
|
---|
26 | import org.tp23.antinstaller.input.InputField;
|
---|
27 | import org.tp23.antinstaller.input.OutputField;
|
---|
28 | import org.tp23.antinstaller.input.SecretPropertyField;
|
---|
29 | import org.tp23.antinstaller.page.Page;
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * <p>Outputs the completed Pages as a Java Properties file. The file produced is compatible
|
---|
33 | * with java.util.Properties. </p>
|
---|
34 | * <p>Output is commented as it is printed to aid debugging</p>
|
---|
35 | * @see http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html#100850
|
---|
36 | * @author Paul Hinds
|
---|
37 | * @version $Id: ExplicitPropertiesFileRenderer.java,v 1.9 2007/01/09 22:41:41 teknopaul Exp $
|
---|
38 | */
|
---|
39 | public class ExplicitPropertiesFileRenderer
|
---|
40 | implements PropertiesFileRenderer {
|
---|
41 |
|
---|
42 | private static String newLine = System.getProperty("line.separator");
|
---|
43 | private static final char[] hexidecimals = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
|
---|
44 |
|
---|
45 | public ExplicitPropertiesFileRenderer() {
|
---|
46 | }
|
---|
47 |
|
---|
48 | public void renderProperties(InstallerContext ctx, File baseDir){
|
---|
49 | Installer installer = ctx.getInstaller();
|
---|
50 | Page[] completedPages = installer.getPages();
|
---|
51 |
|
---|
52 | StringBuffer propertiesData = new StringBuffer();
|
---|
53 | propertiesData.append("### Ant Installer - properties auto generated on ");
|
---|
54 | propertiesData.append( convert(new Date().toString(), true) );
|
---|
55 | propertiesData.append(newLine);
|
---|
56 | propertiesData.append(newLine);
|
---|
57 |
|
---|
58 | propertiesData.append(FILE_ROOT_PROPERTY);
|
---|
59 | propertiesData.append(" = ");
|
---|
60 | propertiesData.append( convert(baseDir.getAbsolutePath(), true) );
|
---|
61 | propertiesData.append(newLine);
|
---|
62 |
|
---|
63 | propertiesData.append(INSTALLER_VERSION_PROPERTY);
|
---|
64 | propertiesData.append(" = ");
|
---|
65 | propertiesData.append( convert(ctx.getInstaller().getVersion(), true) );
|
---|
66 | propertiesData.append(newLine);
|
---|
67 |
|
---|
68 | propertiesData.append(newLine);
|
---|
69 | String property = null;
|
---|
70 | String value = null;
|
---|
71 |
|
---|
72 |
|
---|
73 | for (int i = 0; i < completedPages.length; i++) {
|
---|
74 | OutputField[] fields = completedPages[i].getOutputField();
|
---|
75 |
|
---|
76 | propertiesData.append(newLine);
|
---|
77 | propertiesData.append("## Properties from Page:" + completedPages[i].getName());
|
---|
78 | propertiesData.append(newLine);
|
---|
79 |
|
---|
80 | retrievePropertiesData( fields, propertiesData );
|
---|
81 |
|
---|
82 | // print targets selected
|
---|
83 | List targets = completedPages[i].getTargets(ctx);
|
---|
84 | if(targets.size() > 0){
|
---|
85 | Iterator iterator = targets.iterator();
|
---|
86 | StringBuffer targetProperty = new StringBuffer();
|
---|
87 | while (iterator.hasNext()) {
|
---|
88 | String target = (String) iterator.next();
|
---|
89 | targetProperty.append(target).append(",");
|
---|
90 | }
|
---|
91 | propertiesData.append("# Targets selected for page");
|
---|
92 | propertiesData.append(newLine);
|
---|
93 | property = convert(completedPages[i].getName() + TARGETS_SUFFIX, true);
|
---|
94 | value = convert(targetProperty.toString(), true);
|
---|
95 | propertiesData.append(property + " = " + value);
|
---|
96 | propertiesData.append(newLine);
|
---|
97 |
|
---|
98 | }
|
---|
99 | }
|
---|
100 | try {
|
---|
101 | File antInstallProperties = new File(baseDir.getAbsolutePath(), PROPERTIES_FILE_NAME);
|
---|
102 | FileWriter fos = new FileWriter(antInstallProperties);
|
---|
103 | BufferedWriter writer = new BufferedWriter(fos);
|
---|
104 | writer.write(propertiesData.toString());
|
---|
105 | writer.flush();
|
---|
106 | fos.close();
|
---|
107 | }
|
---|
108 | catch (Throwable ex) {
|
---|
109 | if(ctx.getInstaller().isVerbose()) {
|
---|
110 | ctx.log(ex);
|
---|
111 | }
|
---|
112 | //swallow Exceptions as in the contract for this method
|
---|
113 | }
|
---|
114 | }
|
---|
115 | private void retrievePropertiesData( OutputField[] fields, StringBuffer propertiesData ) {
|
---|
116 | String property = null;
|
---|
117 | String value = null;
|
---|
118 |
|
---|
119 | for (int f = 0; f < fields.length; f++) {
|
---|
120 | if (fields[f] instanceof SecretPropertyField) {
|
---|
121 | InputField field = (InputField) fields[f];
|
---|
122 | //String result = field.getInputResult();
|
---|
123 | propertiesData.append("# Property hidden " + printClass(fields[f].getClass()));
|
---|
124 | propertiesData.append(newLine);
|
---|
125 | property = convert(field.getProperty(), true);
|
---|
126 | propertiesData.append("#" + property + "=XXXXXXXX");
|
---|
127 | propertiesData.append(newLine);
|
---|
128 | }
|
---|
129 | else if (fields[f] instanceof ConditionalField ) {
|
---|
130 | ConditionalField confField = (ConditionalField) fields[f];
|
---|
131 | retrievePropertiesData( confField.getFields(), propertiesData );
|
---|
132 | }
|
---|
133 | else if (fields[f] instanceof InputField) {
|
---|
134 | InputField field = (InputField) fields[f];
|
---|
135 | String result = field.getInputResult();
|
---|
136 | propertiesData.append("# " + printClass(fields[f].getClass()));
|
---|
137 | propertiesData.append(newLine);
|
---|
138 |
|
---|
139 | property = convert(field.getProperty(), true);
|
---|
140 | value = convert(result, false);
|
---|
141 | propertiesData.append(property + " = " + value);
|
---|
142 | propertiesData.append(newLine);
|
---|
143 | }
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
147 | private String printClass(Class clazz) {
|
---|
148 | String name = clazz.getName();
|
---|
149 | int lastDot = name.lastIndexOf('.');
|
---|
150 | return name.substring(lastDot, name.length());
|
---|
151 | }
|
---|
152 |
|
---|
153 | private String convert(String input, boolean doSpaces) {
|
---|
154 | if (input == null) {
|
---|
155 | // this happens when a page is skipped in text mode
|
---|
156 | return "";
|
---|
157 | }
|
---|
158 | int num = input.length();
|
---|
159 | StringBuffer sb = new StringBuffer(num);
|
---|
160 |
|
---|
161 | for (int i = 0; i < num; i++) {
|
---|
162 | char c = input.charAt(i);
|
---|
163 | switch (c) {
|
---|
164 | case ' ':
|
---|
165 | if (i == 0 || doSpaces) {
|
---|
166 | sb.append('\\');
|
---|
167 | }
|
---|
168 | sb.append(' ');
|
---|
169 | break;
|
---|
170 | case '\n':
|
---|
171 | sb.append("\\n");
|
---|
172 | break;
|
---|
173 | case '\r':
|
---|
174 | sb.append("\\r");
|
---|
175 | break;
|
---|
176 | case '\\':
|
---|
177 | sb.append("\\\\");
|
---|
178 | break;
|
---|
179 | case '\t':
|
---|
180 | sb.append("\\t");
|
---|
181 | break;
|
---|
182 | case '\f':
|
---|
183 | sb.append("\\f");
|
---|
184 | break;
|
---|
185 | case '=':
|
---|
186 | sb.append("\\=");
|
---|
187 | break;
|
---|
188 | case ':':
|
---|
189 | sb.append("\\:");
|
---|
190 | break;
|
---|
191 | case '#':
|
---|
192 | sb.append("\\#");
|
---|
193 | break;
|
---|
194 | case '!':
|
---|
195 | sb.append("\\!");
|
---|
196 | break;
|
---|
197 |
|
---|
198 | default:
|
---|
199 | if ( (c < 0x0020) || (c > 0x007e) ) {
|
---|
200 | sb.append("\\u")
|
---|
201 | .append(hex( (c >> 12) & 0xF))
|
---|
202 | .append(hex( (c >> 8) & 0xF))
|
---|
203 | .append(hex( (c >> 4) & 0xF))
|
---|
204 | .append(hex(c & 0xF));
|
---|
205 | }
|
---|
206 | else {
|
---|
207 | sb.append(c);
|
---|
208 | }
|
---|
209 | }
|
---|
210 | }
|
---|
211 | return sb.toString();
|
---|
212 | }
|
---|
213 |
|
---|
214 | private char hex(int val) {
|
---|
215 | return hexidecimals[ (val & 0xF)];
|
---|
216 | }
|
---|
217 |
|
---|
218 | }
|
---|