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.util;
017:
018: import java.io.BufferedWriter;
019: import java.io.File;
020: import java.io.FileWriter;
021: import java.io.IOException;
022: import java.util.Date;
023:
024: import org.tp23.antinstaller.Installer;
025: import org.tp23.antinstaller.input.CommentOutput;
026: import org.tp23.antinstaller.input.InputField;
027: import org.tp23.antinstaller.input.LargeSelectInput;
028: import org.tp23.antinstaller.input.OutputField;
029: import org.tp23.antinstaller.input.SelectInput;
030: import org.tp23.antinstaller.page.Page;
031:
032: /**
033: * <p>Outputs the text from Pages as a Java Properties file. The file produced is compatible
034: * with java.util.Properties. </p>
035: * <p>It can be used as a stub for creating language packs, the default text is included as a guide but can be deleted</p>
036: * @author Paul Hinds
037: */
038: public class LangPackFileRenderer {
039:
040: private static String newLine = System
041: .getProperty("line.separator");
042: private static final char[] hexidecimals = { '0', '1', '2', '3',
043: '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
044:
045: public LangPackFileRenderer() {
046: }
047:
048: public void renderProperties(Installer installer, File baseDir,
049: String locale) throws IOException {
050: Page[] pages = installer.getPages();
051:
052: StringBuffer propertiesData = new StringBuffer();
053: propertiesData
054: .append("### Ant Installer - language pack auto generated on ");
055: propertiesData.append(new Date().toString());
056: propertiesData.append(newLine);
057: propertiesData.append(newLine);
058:
059: propertiesData.append("finishButtonText = "
060: + installer.getFinishButtonText());
061: propertiesData.append(newLine);
062: propertiesData.append(newLine);
063:
064: String property = null;
065: String value = null;
066:
067: for (int i = 0; i < pages.length; i++) {
068: OutputField[] fields = pages[i].getOutputField();
069:
070: propertiesData.append(newLine);
071: propertiesData.append("## Text from Page:"
072: + pages[i].getName());
073: propertiesData.append(newLine);
074: property = "page." + pages[i].getName() + ".displayText";
075: value = convert(pages[i].getDisplayText(), false);
076: propertiesData.append(property + " = " + value);
077: propertiesData.append(newLine);
078:
079: retrievePropertiesData(fields, propertiesData);
080:
081: }
082: // create the stub
083: if (locale != null) {
084: File languagePackStub = new File(baseDir.getAbsolutePath(),
085: "LanguagePack_" + locale + ".properties");
086: FileWriter fos = new FileWriter(languagePackStub);
087: BufferedWriter writer = new BufferedWriter(fos);
088: writer.write(propertiesData.toString());
089: writer.flush();
090: fos.close();
091: } else {
092: // create the default
093: File languagePack = new File(baseDir.getAbsolutePath(),
094: "LanguagePack.properties");
095: FileWriter fos = new FileWriter(languagePack);
096: BufferedWriter writer = new BufferedWriter(fos);
097: writer.write(propertiesData.toString());
098: writer.flush();
099: fos.close();
100: }
101: }
102:
103: private void retrievePropertiesData(OutputField[] fields,
104: StringBuffer propertiesData) {
105: String property = null;
106: String value = null;
107: String explProperty = null;
108: String explValue = null;
109:
110: for (int f = 0; f < fields.length; f++) {
111:
112: // use getName() for comments
113: if (fields[f] instanceof CommentOutput) {
114: property = fields[f].getName() + ".displayText";
115: value = convert(fields[f].getDisplayText(), false);
116: propertiesData.append(property + " = " + value);
117: propertiesData.append(newLine);
118:
119: if (fields[f].getExplanatoryText() != null
120: && fields[f].getExplanatoryText().trim()
121: .length() > 0) {
122: explProperty = fields[f].getName()
123: + ".explanatoryText";
124: explValue = convert(fields[f].getExplanatoryText(),
125: false);
126: propertiesData.append(explProperty + " = "
127: + explValue);
128: propertiesData.append(newLine);
129: }
130: }
131: // use getProperty for input types
132: else {
133: InputField iField = (InputField) fields[f];
134: property = iField.getProperty() + ".displayText";
135: value = convert(iField.getDisplayText(), false);
136: propertiesData.append(property + " = " + value);
137: propertiesData.append(newLine);
138: if (iField.getExplanatoryText() != null
139: && iField.getExplanatoryText().trim().length() > 0) {
140: explProperty = iField.getProperty()
141: + ".explanatoryText";
142: explValue = convert(iField.getExplanatoryText(),
143: false);
144: propertiesData.append(explProperty + " = "
145: + explValue);
146: propertiesData.append(newLine);
147: }
148: if (iField instanceof SelectInput) {
149: SelectInput selectInput = (SelectInput) iField;
150: for (int o = 0; o < selectInput.getOptions().length; o++) {
151: SelectInput.Option option = selectInput
152: .getOptions()[o];
153: property = selectInput.getProperty() + "."
154: + (o + 1) + ".displayText";
155: value = convert(option.getText(), false);
156: propertiesData.append(property + " = " + value);
157: propertiesData.append(newLine);
158: }
159: }
160: if (fields[f] instanceof LargeSelectInput) {
161: LargeSelectInput selectInput = (LargeSelectInput) iField;
162: for (int o = 0; o < selectInput.getOptions().length; o++) {
163: LargeSelectInput.Option option = selectInput
164: .getOptions()[o];
165: property = selectInput.getProperty() + "."
166: + (o + 1) + ".displayText";
167: value = convert(option.getText(), false);
168: propertiesData.append(property + " = " + value);
169: propertiesData.append(newLine);
170: }
171: }
172: }
173: }
174: }
175:
176: private String convert(String input, boolean doSpaces) {
177: if (input == null) {
178: // this happens when a page is skipped in text mode
179: return "";
180: }
181: int num = input.length();
182: StringBuffer sb = new StringBuffer(num);
183:
184: for (int i = 0; i < num; i++) {
185: char c = input.charAt(i);
186: switch (c) {
187: case ' ':
188: if (i == 0 || doSpaces) {
189: sb.append('\\');
190: }
191: sb.append(' ');
192: break;
193: case '\n':
194: sb.append("\\n");
195: break;
196: case '\r':
197: sb.append("\\r");
198: break;
199: case '\\':
200: sb.append("\\\\");
201: break;
202: case '\t':
203: sb.append("\\t");
204: break;
205: case '\f':
206: sb.append("\\f");
207: break;
208: case '=':
209: sb.append("\\=");
210: break;
211: case ':':
212: sb.append("\\:");
213: break;
214: case '#':
215: sb.append("\\#");
216: break;
217: case '!':
218: sb.append("\\!");
219: break;
220:
221: default:
222: if ((c < 0x0020) || (c > 0x007e)) {
223: sb.append("\\u").append(hex((c >> 12) & 0xF))
224: .append(hex((c >> 8) & 0xF)).append(
225: hex((c >> 4) & 0xF)).append(
226: hex(c & 0xF));
227: } else {
228: sb.append(c);
229: }
230: }
231: }
232: return sb.toString();
233: }
234:
235: private char hex(int val) {
236: return hexidecimals[(val & 0xF)];
237: }
238:
239: }
|