Statistics
| Revision:

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

History | View | Annotate | Download (3.7 kB)

1
package org.txm.scripts.i18n
2

    
3
import org.txm.utils.DeleteDir;
4

    
5
/**
6
 * Browse messages files and look for duplicated values
7
 * @author mdecorde
8
 *
9
 */
10
class FixExternalizedDuplicatedStrings {
11
        
12
        static main(args) {
13
//                File propertyFile = new File("/home/mdecorde/Bureau/Experiences/rcpapplication/messages.properties");
14
//                File messageClass = new File("/home/mdecorde/Bureau/Experiences/rcpapplication/Messages.java");
15
//                File srcdirectory = new File("/home/mdecorde/Bureau/Experiences/rcpapplication");
16
                File propertyFile = new File("/home/mdecorde/workspace37/org.txm.rcp/src/main/java/org/txm/rcpapplication/messages.properties");
17
                File messageClass = new File("/home/mdecorde/workspace37/org.txm.rcp/src/main/java/org/txm/rcpapplication/Messages.java");
18
                File srcdirectory = new File("/home/mdecorde/workspace37/org.txm.rcp/src/main/java/org/txm/rcpapplication");
19
                if (!(propertyFile.exists() && propertyFile.canRead() && propertyFile.canWrite() && propertyFile.isFile())) {
20
                        println "error file : "+propertyFile
21
                        return
22
                }
23
                
24
                // build hash of externalised Strings
25
                //                                                  key          externalized string
26
                def entries = new HashMap<String, String>()
27
                propertyFile.eachLine("iso-8859-1"){
28
                        if(it =~ ".+=.+")
29
                        {
30
                                int idx = it.indexOf("=")
31
                                String key = it.substring(0, idx)
32
                                String value = it.substring(idx+1)
33
                                entries.put(key, value)
34
                        }
35
                        else if(it =~ ".+=\$")
36
                        {
37
                                int idx = it.indexOf("=")
38
                                String key = it.substring(0, idx)
39
                                String value = ""
40
                                entries.put(key, value)
41
                        }
42
                }
43
                
44
                // invert hash
45
                def hash = entries
46
                def reverse = [:]
47
                hash.each{
48
                        if(reverse[it.value] == null)
49
                                reverse[it.value] = []
50
                        reverse[it.value] << it.key
51
                };
52
                
53
                for(def key : reverse.keySet())
54
                        if(reverse[key].size() > 1)
55
                                println "doublons of $key: "+reverse[key]
56
                
57
                // translate dictionnary
58
                def dictionnary = [:]
59
                for(def value : reverse.keySet())
60
                {
61
                        def keys = reverse[value];
62
                        def first = keys[0];
63
                        for(int i = 1 ; i < keys.size() ; i++) // the first key will not be removed
64
                        {
65
                                def key = keys.get(i)
66
                                if(key != first)
67
                                {
68
                                        dictionnary.put(key , first)
69
                                        println key+ " >> "+first
70
                                }
71
                        }
72
                }
73
                
74
                // patch source files : iterates over all java files
75
                
76
                /*def files = DeleteDir.scanDirectory(srcdirectory, true);
77
                for(File f : files)
78
                {
79
                        if(f.getName().endsWith(".java") && f.getName() != "Messages.java")
80
                        {
81
                                def lines = f.readLines("UTF-8")
82
                                f.withWriter("UTF-8"){ writer ->
83
                                        
84
                                        for(String line : lines)
85
                                        {
86
                                                for(String key : dictionnary.keySet())
87
                                                {
88
                                                        String old = line;
89
                                                        //line = line.replaceAll(, "Messages."+dictionnary[key]);
90
                                                        line = (line =~ /Messages\.$key([^0-9]|$)/).replaceAll("Messages."+dictionnary[key]+'$1')
91

92
                                                        if(old != line)
93
                                                                System.out.println(f.getAbsolutePath()+":"+old+": replaced "+"Messages\\."+key+"[^0-9] Messages."+dictionnary[key]);
94
                                                }
95
                                                writer.writeLine(line);
96
                                        }        
97
                                }
98
                        }
99
                }
100
                
101
                // remove key declaration from Messages.java
102
                String txt = messageClass.getText("UTF-8")
103
                String[] split = txt.split("\n");
104
                messageClass.withWriter "UTF-8", { writer -> 
105
                        for(String line : split)
106
                        {
107
                                if(line.contains("public static String "))
108
                                {
109
                                        for(String key : dictionnary.keySet())
110
                                        {
111
                                                if(line.contains("public static String "+key+";"))
112
                                                {
113
                                                        line = line.trim()
114
                                                        line = null
115
                                                        println "remove key: "+key
116
                                                        break;
117
                                                }
118
                                        }
119
                                        if(line != null)
120
                                                writer.writeLine(line);
121
                                }
122
                                else
123
                                        writer.writeLine(line);
124
                        }
125
                }*/
126
                
127
                // rewrite messages.properties
128
                def keytoRemove = dictionnary.keySet();
129
                propertyFile.withWriter("UTF-8") { writer ->
130
                        for(String key : entries.keySet().sort())
131
                                if(!keytoRemove.contains(key))
132
                                        writer.writeLine(key+"="+entries[key])
133
                }
134
        }
135
}