Statistics
| Revision:

root / tmp / org.txm.core / src / groovy / org / txm / scripts / i18n / ReverseI18nDict.groovy @ 187

History | View | Annotate | Download (3.1 kB)

1
package org.txm.scripts.i18n
2

    
3
import org.txm.utils.BiHashMap
4

    
5
/**
6
 * Bihashmap 
7
 * Check any missing key in both of messages.properties and Message.java files
8
 * @author mdecorde
9
 *
10
 */
11
class ReverseI18nDict extends BiHashMap<String, String> {
12
        File propFile;
13
        File messageFile;
14
        
15
        def messageKeys = new HashSet<String>()
16
        
17
        public ReverseI18nDict(File propFile) {
18
                this(propFile, null)
19
        }
20
        
21
        public ReverseI18nDict(File propFile, File messageFile) {
22
                this.propFile = propFile
23
                this.messageFile = messageFile
24
                BufferedReader reader = new BufferedReader(new InputStreamReader(
25
                                new FileInputStream(propFile), "iso-8859-1"));
26
                String line = reader.readLine();
27
                while (line != null) {
28
                        if (!line.startsWith("#")) {
29
                                String[] split = line.split("=", 2);
30
                                if (split.length == 2) {
31
                                        this.put(split[0], split[1]);
32
                                }
33
                        }
34
                        line = reader.readLine();
35
                }
36
                
37
                if (messageFile != null) {
38
                        BufferedReader reader2 = new BufferedReader(new InputStreamReader(new FileInputStream(messageFile), "iso-8859-1"));
39
                        String line2 = reader2.readLine();
40
                        while (line2 != null) {
41
                                line2 = line2.trim();
42
                                if (line2.startsWith("public static String")) {
43
                                        line2 = line2.substring(21, line2.length() -1);
44
                                        messageKeys << line2
45
                                }
46
                                line2 = reader2.readLine();
47
                        }
48
                }
49
        }
50
        
51
        public def getMissingsMessageKeys() {
52
                def missing = []
53
                for (String pKey : this.getKeys())
54
                        if (!messageKeys.contains(pKey))
55
                                missing << pKey
56
                                
57
                return missing
58
        }
59
        
60
        public getMissingPropKeys() {
61
                def missing = []
62
                def pKeys = this.getKeys()
63
                for (String mKey : messageKeys)
64
                        if (!pKeys.contains(mKey))
65
                                missing << mKey
66
                                
67
                return missing
68
        }
69
        
70
        public void saveChanges() {
71

    
72
                //Write prop File
73
                File oldFile = new File(propFile.getParentFile(), propFile.getName()+".cpy");
74
                propFile.renameTo(oldFile);
75
                propFile.withWriter("iso-8859-1") { out ->
76
                        for(String key : this.getKeys().sort()) {
77
                                out.println(key+"="+this.get(key))
78
                        }
79
                }
80
                
81
                // write message File
82
                if (messageFile == null) return;
83
                
84
                File oldFile2 = new File(messageFile.getParentFile(), messageFile.getName()+".cpy");
85
                messageFile.renameTo(oldFile2); // back up
86
                messageFile.withWriter("iso-8859-1") { out ->
87
                        // write start
88
                        out.println('''package org.txm;
89

90
import org.eclipse.osgi.util.NLS;
91

92
public class Messages extends NLS {
93
        
94
        // The Constant BUNDLE_NAME. 
95
        private static final String BUNDLE_NAME = "org.txm.messages"; //$NON-NLS-1$
96
                                ''')
97
                        
98
                        //write keys
99
                        for(String key : this.getKeys().sort())
100
                                out.println("\tpublic static String $key;");
101
                        
102
                        // write end
103
                        out.println('''
104
        static {
105
                // initialize resource bundle
106
                NLS.initializeMessages(BUNDLE_NAME, Messages.class);
107
        }
108

109
        private Messages() {
110
        }
111
}
112
                                ''')
113
                }
114
        }
115
        
116
        public static void main(String[] args) {
117
                File propFile = new File("/home/mdecorde/workspace43/org.txm.toolbox/src/java/org/txm/messages.properties")
118
                File messageFile = new File("/home/mdecorde/workspace43/org.txm.toolbox/src/java/org/txm/Messages.java")
119
                ReverseI18nDict dict = new ReverseI18nDict(propFile, messageFile);
120
                println dict.getMissingsMessageKeys()
121
                println dict.getMissingPropKeys();
122
                println dict.toString();
123
                //dict.saveChanges();
124
        }
125
}