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.util;
|
---|
17 |
|
---|
18 | import java.io.BufferedWriter;
|
---|
19 | import java.io.File;
|
---|
20 | import java.io.FileWriter;
|
---|
21 | import java.io.IOException;
|
---|
22 | import java.util.Date;
|
---|
23 | import java.util.Iterator;
|
---|
24 | import java.util.List;
|
---|
25 |
|
---|
26 | import org.tp23.antinstaller.Installer;
|
---|
27 | import org.tp23.antinstaller.PropertiesFileRenderer;
|
---|
28 | import org.tp23.antinstaller.input.CommentOutput;
|
---|
29 | import org.tp23.antinstaller.input.InputField;
|
---|
30 | import org.tp23.antinstaller.input.LargeSelectInput;
|
---|
31 | import org.tp23.antinstaller.input.OutputField;
|
---|
32 | import org.tp23.antinstaller.input.SecretPropertyField;
|
---|
33 | import org.tp23.antinstaller.input.ConditionalField;
|
---|
34 | import org.tp23.antinstaller.input.SelectInput;
|
---|
35 | import org.tp23.antinstaller.input.TargetSelectInput;
|
---|
36 | import org.tp23.antinstaller.input.SelectInput.Option;
|
---|
37 | import org.tp23.antinstaller.page.Page;
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * <p>Outputs the text from Pages as a Java Properties file. The file produced is compatible
|
---|
41 | * with java.util.Properties. </p>
|
---|
42 | * <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>
|
---|
43 | * @author Paul Hinds
|
---|
44 | */
|
---|
45 | public class LangPackFileRenderer{
|
---|
46 |
|
---|
47 | private static String newLine = System.getProperty("line.separator");
|
---|
48 | private static final char[] hexidecimals = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
|
---|
49 |
|
---|
50 | public LangPackFileRenderer() {
|
---|
51 | }
|
---|
52 |
|
---|
53 | public void renderProperties(Installer installer, File baseDir, String locale) throws IOException {
|
---|
54 | Page[] pages = installer.getPages();
|
---|
55 |
|
---|
56 | StringBuffer propertiesData = new StringBuffer();
|
---|
57 | propertiesData.append("### Ant Installer - language pack auto generated on ");
|
---|
58 | propertiesData.append(new Date().toString());
|
---|
59 | propertiesData.append(newLine);
|
---|
60 | propertiesData.append(newLine);
|
---|
61 |
|
---|
62 | propertiesData.append("finishButtonText = " + installer.getFinishButtonText());
|
---|
63 | propertiesData.append(newLine);
|
---|
64 | propertiesData.append(newLine);
|
---|
65 |
|
---|
66 | String property = null;
|
---|
67 | String value = null;
|
---|
68 |
|
---|
69 | for (int i = 0; i < pages.length; i++) {
|
---|
70 | OutputField[] fields = pages[i].getOutputField();
|
---|
71 |
|
---|
72 | propertiesData.append(newLine);
|
---|
73 | propertiesData.append("## Text from Page:" + pages[i].getName());
|
---|
74 | propertiesData.append(newLine);
|
---|
75 | property = "page." + pages[i].getName() + ".displayText";
|
---|
76 | value = convert(pages[i].getDisplayText(), false);
|
---|
77 | propertiesData.append(property + " = " + value);
|
---|
78 | propertiesData.append(newLine);
|
---|
79 |
|
---|
80 | retrievePropertiesData( fields, propertiesData );
|
---|
81 |
|
---|
82 | }
|
---|
83 | // create the stub
|
---|
84 | if(locale != null) {
|
---|
85 | File languagePackStub = new File(baseDir.getAbsolutePath(), "LanguagePack_" + locale + ".properties");
|
---|
86 | FileWriter fos = new FileWriter(languagePackStub);
|
---|
87 | BufferedWriter writer = new BufferedWriter(fos);
|
---|
88 | writer.write(propertiesData.toString());
|
---|
89 | writer.flush();
|
---|
90 | fos.close();
|
---|
91 | }
|
---|
92 | else {
|
---|
93 | // create the default
|
---|
94 | File languagePack = new File(baseDir.getAbsolutePath(), "LanguagePack.properties");
|
---|
95 | FileWriter fos = new FileWriter(languagePack);
|
---|
96 | BufferedWriter writer = new BufferedWriter(fos);
|
---|
97 | writer.write(propertiesData.toString());
|
---|
98 | writer.flush();
|
---|
99 | fos.close();
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | private void retrievePropertiesData( OutputField[] fields, StringBuffer propertiesData ) {
|
---|
104 | String property = null;
|
---|
105 | String value = null;
|
---|
106 | String explProperty = null;
|
---|
107 | String explValue = null;
|
---|
108 |
|
---|
109 | for (int f = 0; f < fields.length; f++) {
|
---|
110 |
|
---|
111 | // use getName() for comments
|
---|
112 | if(fields[f] instanceof CommentOutput){
|
---|
113 | property = fields[f].getName() + ".displayText";
|
---|
114 | value = convert(fields[f].getDisplayText(), false);
|
---|
115 | propertiesData.append(property + " = " + value);
|
---|
116 | propertiesData.append(newLine);
|
---|
117 |
|
---|
118 | if (fields[f].getExplanatoryText() != null && fields[f].getExplanatoryText().trim().length() > 0) {
|
---|
119 | explProperty = fields[f].getName() + ".explanatoryText";
|
---|
120 | explValue = convert(fields[f].getExplanatoryText(), false);
|
---|
121 | propertiesData.append(explProperty + " = " + explValue);
|
---|
122 | propertiesData.append(newLine);
|
---|
123 | }
|
---|
124 | }
|
---|
125 | // use getProperty for input types
|
---|
126 | else {
|
---|
127 | InputField iField = (InputField)fields[f];
|
---|
128 | property = iField.getProperty() + ".displayText";
|
---|
129 | value = convert(iField.getDisplayText(), false);
|
---|
130 | propertiesData.append(property + " = " + value);
|
---|
131 | propertiesData.append(newLine);
|
---|
132 | if (iField.getExplanatoryText() != null && iField.getExplanatoryText().trim().length() > 0) {
|
---|
133 | explProperty = iField.getProperty() + ".explanatoryText";
|
---|
134 | explValue = convert(iField.getExplanatoryText(), false);
|
---|
135 | propertiesData.append(explProperty + " = " + explValue);
|
---|
136 | propertiesData.append(newLine);
|
---|
137 | }
|
---|
138 | if(iField instanceof SelectInput) {
|
---|
139 | SelectInput selectInput = (SelectInput)iField;
|
---|
140 | for (int o = 0; o < selectInput.getOptions().length; o++) {
|
---|
141 | SelectInput.Option option = selectInput.getOptions()[o];
|
---|
142 | property = selectInput.getProperty() + "." + (o + 1) + ".displayText";
|
---|
143 | value = convert(option.getText(), false);
|
---|
144 | propertiesData.append(property + " = " + value);
|
---|
145 | propertiesData.append(newLine);
|
---|
146 | }
|
---|
147 | }
|
---|
148 | if(fields[f] instanceof LargeSelectInput) {
|
---|
149 | LargeSelectInput selectInput = (LargeSelectInput)iField;
|
---|
150 | for (int o = 0; o < selectInput.getOptions().length; o++) {
|
---|
151 | LargeSelectInput.Option option = selectInput.getOptions()[o];
|
---|
152 | property = selectInput.getProperty() + "." + (o + 1) + ".displayText";
|
---|
153 | value = convert(option.getText(), false);
|
---|
154 | propertiesData.append(property + " = " + value);
|
---|
155 | propertiesData.append(newLine);
|
---|
156 | }
|
---|
157 | }
|
---|
158 | }
|
---|
159 | }
|
---|
160 | }
|
---|
161 |
|
---|
162 | private String convert(String input, boolean doSpaces) {
|
---|
163 | if (input == null) {
|
---|
164 | // this happens when a page is skipped in text mode
|
---|
165 | return "";
|
---|
166 | }
|
---|
167 | int num = input.length();
|
---|
168 | StringBuffer sb = new StringBuffer(num);
|
---|
169 |
|
---|
170 | for (int i = 0; i < num; i++) {
|
---|
171 | char c = input.charAt(i);
|
---|
172 | switch (c) {
|
---|
173 | case ' ':
|
---|
174 | if (i == 0 || doSpaces) {
|
---|
175 | sb.append('\\');
|
---|
176 | }
|
---|
177 | sb.append(' ');
|
---|
178 | break;
|
---|
179 | case '\n':
|
---|
180 | sb.append("\\n");
|
---|
181 | break;
|
---|
182 | case '\r':
|
---|
183 | sb.append("\\r");
|
---|
184 | break;
|
---|
185 | case '\\':
|
---|
186 | sb.append("\\\\");
|
---|
187 | break;
|
---|
188 | case '\t':
|
---|
189 | sb.append("\\t");
|
---|
190 | break;
|
---|
191 | case '\f':
|
---|
192 | sb.append("\\f");
|
---|
193 | break;
|
---|
194 | case '=':
|
---|
195 | sb.append("\\=");
|
---|
196 | break;
|
---|
197 | case ':':
|
---|
198 | sb.append("\\:");
|
---|
199 | break;
|
---|
200 | case '#':
|
---|
201 | sb.append("\\#");
|
---|
202 | break;
|
---|
203 | case '!':
|
---|
204 | sb.append("\\!");
|
---|
205 | break;
|
---|
206 |
|
---|
207 | default:
|
---|
208 | if ( (c < 0x0020) || (c > 0x007e) ) {
|
---|
209 | sb.append("\\u")
|
---|
210 | .append(hex( (c >> 12) & 0xF))
|
---|
211 | .append(hex( (c >> 8) & 0xF))
|
---|
212 | .append(hex( (c >> 4) & 0xF))
|
---|
213 | .append(hex(c & 0xF));
|
---|
214 | }
|
---|
215 | else {
|
---|
216 | sb.append(c);
|
---|
217 | }
|
---|
218 | }
|
---|
219 | }
|
---|
220 | return sb.toString();
|
---|
221 | }
|
---|
222 |
|
---|
223 | private char hex(int val) {
|
---|
224 | return hexidecimals[ (val & 0xF)];
|
---|
225 | }
|
---|
226 |
|
---|
227 | }
|
---|