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 */
16package org.tp23.antinstaller.util;
17
18import java.io.BufferedWriter;
19import java.io.File;
20import java.io.FileWriter;
21import java.io.IOException;
22import java.util.Date;
23import java.util.Iterator;
24import java.util.List;
25
26import org.tp23.antinstaller.Installer;
27import org.tp23.antinstaller.PropertiesFileRenderer;
28import org.tp23.antinstaller.input.CommentOutput;
29import org.tp23.antinstaller.input.InputField;
30import org.tp23.antinstaller.input.LargeSelectInput;
31import org.tp23.antinstaller.input.OutputField;
32import org.tp23.antinstaller.input.SecretPropertyField;
33import org.tp23.antinstaller.input.ConditionalField;
34import org.tp23.antinstaller.input.SelectInput;
35import org.tp23.antinstaller.input.TargetSelectInput;
36import org.tp23.antinstaller.input.SelectInput.Option;
37import 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 */
45public 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();
00        }
01    }
02    
03    private void retrievePropertiesData( OutputField[] fields, StringBuffer propertiesData ) {
04        String property = null;
05        String value = null;
06        String explProperty = null;
07        String explValue = null;
08
09        for (int f = 0; f < fields.length; f++) {
10            
11            // use getName() for comments
12            if(fields[f] instanceof CommentOutput){
13                property = fields[f].getName() + ".displayText";
14                value = convert(fields[f].getDisplayText(), false);
15                propertiesData.append(property + " = " + value);
16                propertiesData.append(newLine);
17
18                if (fields[f].getExplanatoryText() != null && fields[f].getExplanatoryText().trim().length() > 0) {
19                    explProperty = fields[f].getName() + ".explanatoryText";
20                    explValue = convert(fields[f].getExplanatoryText(), false);
21                    propertiesData.append(explProperty + " = " + explValue);
22                    propertiesData.append(newLine);
23                }
24            }
25            // use getProperty for input types
26            else {
27                InputField iField = (InputField)fields[f];
28                property = iField.getProperty() + ".displayText";
29                value = convert(iField.getDisplayText(), false);
30                propertiesData.append(property + " = " + value);
31                propertiesData.append(newLine);
32                if (iField.getExplanatoryText() != null && iField.getExplanatoryText().trim().length() > 0) {
33                    explProperty = iField.getProperty() + ".explanatoryText";
34                    explValue = convert(iField.getExplanatoryText(), false);
35                    propertiesData.append(explProperty + " = " + explValue);
36                    propertiesData.append(newLine);
37                }
38                if(iField instanceof SelectInput) {
39                    SelectInput selectInput = (SelectInput)iField;
40                    for (int o = 0; o < selectInput.getOptions().length; o++) {
41                        SelectInput.Option option =  selectInput.getOptions()[o];
42                        property = selectInput.getProperty() + "." + (o + 1) + ".displayText";
43                        value = convert(option.getText(), false);
44                        propertiesData.append(property + " = " + value);
45                        propertiesData.append(newLine);
46                    }
47                }
48                if(fields[f] instanceof LargeSelectInput) {
49                    LargeSelectInput selectInput = (LargeSelectInput)iField;
50                    for (int o = 0; o < selectInput.getOptions().length; o++) {
51                        LargeSelectInput.Option option =  selectInput.getOptions()[o];
52                        property = selectInput.getProperty() + "." + (o + 1) + ".displayText";
53                        value = convert(option.getText(), false);
54                        propertiesData.append(property + " = " + value);
55                        propertiesData.append(newLine);
56                    }
57                }
58            }           
59        }
60    }
61
62    private String convert(String input, boolean doSpaces) {
63        if (input == null) {
64            // this happens when a page is skipped in text mode
65            return "";
66        }
67        int num = input.length();
68        StringBuffer sb = new StringBuffer(num);
69
70        for (int i = 0; i < num; i++) {
71            char c = input.charAt(i);
72            switch (c) {
73                case ' ':
74                    if (i == 0 || doSpaces) {
75                        sb.append('\\');
76                    }
77                    sb.append(' ');
78                    break;
79                case '\n':
80                    sb.append("\\n");
81                    break;
82                case '\r':
83                    sb.append("\\r");
84                    break;
85                case '\\':
86                    sb.append("\\\\");
87                    break;
88                case '\t':
89                    sb.append("\\t");
90                    break;
91                case '\f':
92                    sb.append("\\f");
93                    break;
94                case '=':
95                    sb.append("\\=");
96                    break;
97                case ':':
98                    sb.append("\\:");
99                    break;
00                case '#':
01                    sb.append("\\#");
02                    break;
03                case '!':
04                    sb.append("\\!");
05                    break;
06
07                default:
08                    if ( (c < 0x0020) || (c > 0x007e) ) {
09                        sb.append("\\u")
10                            .append(hex( (c >> 12) & 0xF))
11                            .append(hex( (c >> 8) & 0xF))
12                            .append(hex( (c >> 4) & 0xF))
13                            .append(hex(c & 0xF));
14                    }
15                    else {
16                        sb.append(c);
17                    }
18            }
19        }
20        return sb.toString();
21    }
22
23    private char hex(int val) {
24        return hexidecimals[ (val & 0xF)];
25    }
26
27}
28