Statistics
| Revision:

root / tmp / org.txm.translate.rcp / src / org / txm / rcp / translate / i18n / FixExternalizedDuplicatedStrings.groovy @ 1326

History | View | Annotate | Download (3.7 kB)

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

84 1223 mdecorde
                                        for(String line : lines)
85 1223 mdecorde
                                        {
86 1223 mdecorde
                                                for(String key : dictionnary.keySet())
87 1223 mdecorde
                                                {
88 1223 mdecorde
                                                        String old = line;
89 1223 mdecorde
                                                        //line = line.replaceAll(, "Messages."+dictionnary[key]);
90 1223 mdecorde
                                                        line = (line =~ /Messages\.$key([^0-9]|$)/).replaceAll("Messages."+dictionnary[key]+'$1')
91 1223 mdecorde

92 1223 mdecorde
                                                        if(old != line)
93 1223 mdecorde
                                                                System.out.println(f.getAbsolutePath()+":"+old+": replaced "+"Messages\\."+key+"[^0-9] Messages."+dictionnary[key]);
94 1223 mdecorde
                                                }
95 1223 mdecorde
                                                writer.writeLine(line);
96 1223 mdecorde
                                        }
97 1223 mdecorde
                                }
98 1223 mdecorde
                        }
99 1223 mdecorde
                }
100 1223 mdecorde

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