Statistics
| Revision:

root / tmp / org.txm.translate.rcp / src / org / txm / rcp / translate / i18n / MergeExternalizedStringFiles.groovy @ 1242

History | View | Annotate | Download (8 kB)

1 1223 mdecorde
package org.txm.rcp.translate.i18n
2 1223 mdecorde
3 1223 mdecorde
import java.io.File;
4 1223 mdecorde
import org.eclipse.osgi.util.NLS;
5 1223 mdecorde
import org.txm.utils.DeleteDir;
6 1223 mdecorde
7 1223 mdecorde
/**
8 1223 mdecorde
 * Scan for *.properties files and merge them into one
9 1223 mdecorde
 * @author mdecorde
10 1223 mdecorde
 *
11 1223 mdecorde
 */
12 1223 mdecorde
class MergeExternalizedStringFiles {
13 1223 mdecorde
14 1223 mdecorde
        ArrayList<File> alldirs;
15 1223 mdecorde
        LinkedHashMap<File, ArrayList> data = [:];
16 1223 mdecorde
17 1223 mdecorde
        public MergeExternalizedStringFiles(File projectDir)
18 1223 mdecorde
        {
19 1223 mdecorde
                alldirs = scanDirectory(projectDir);
20 1223 mdecorde
                alldirs.sort{it.getAbsolutePath();}
21 1223 mdecorde
                //System.out.println("All dirs size: "+alldirs.size);
22 1223 mdecorde
                findMessageClasses();
23 1223 mdecorde
                mergeAll()
24 1223 mdecorde
        }
25 1223 mdecorde
26 1223 mdecorde
        public findMessageClasses()
27 1223 mdecorde
        {
28 1223 mdecorde
                File messageClass;
29 1223 mdecorde
                ArrayList<File> externalizeMessages;
30 1223 mdecorde
                for(int i = 0 ; i < alldirs.size() ; i++) {
31 1223 mdecorde
                        File dir = alldirs.get(i)
32 1223 mdecorde
                        if (!dir.isDirectory())
33 1223 mdecorde
                                continue;
34 1223 mdecorde
                        //System.out.println("Dir: "+dir);
35 1223 mdecorde
                        externalizeMessages = new ArrayList<File>();
36 1223 mdecorde
                        for (File f : dir.listFiles()) {
37 1223 mdecorde
                                if (f.getName() == "Messages.java")
38 1223 mdecorde
                                        messageClass = f
39 1223 mdecorde
                                if (f.getName() =~ "messages(_..)?\\.properties")
40 1223 mdecorde
                                        externalizeMessages << f
41 1223 mdecorde
                        }
42 1223 mdecorde
43 1223 mdecorde
                        if (messageClass != null) {
44 1223 mdecorde
                                String bundle = findBundle(messageClass);
45 1223 mdecorde
                                data.put(dir, [messageClass, bundle, externalizeMessages])
46 1223 mdecorde
                        } else {
47 1223 mdecorde
                                alldirs.remove(i);
48 1223 mdecorde
                                i--;
49 1223 mdecorde
                        }
50 1223 mdecorde
                }
51 1223 mdecorde
                println "all dirs size: "+alldirs.size()
52 1223 mdecorde
                //                for(def f : alldirs)
53 1223 mdecorde
                //                        println f
54 1223 mdecorde
        }
55 1223 mdecorde
56 1223 mdecorde
        public String findBundle(File message)
57 1223 mdecorde
        {
58 1223 mdecorde
                String ret;
59 1223 mdecorde
                message.eachLine("iso-8859-1") {
60 1223 mdecorde
                        if (it.contains("private static final String BUNDLE_NAME = ")) {
61 1223 mdecorde
                                int start = it.indexOf("\"")
62 1223 mdecorde
                                int end = it.lastIndexOf("\"")
63 1223 mdecorde
                                ret = it.substring(start+1, end)
64 1223 mdecorde
                        }
65 1223 mdecorde
                }
66 1223 mdecorde
                return ret;
67 1223 mdecorde
        }
68 1223 mdecorde
69 1223 mdecorde
        /**
70 1223 mdecorde
         * Scan directory.
71 1223 mdecorde
         *
72 1223 mdecorde
         * @param directory the directory
73 1223 mdecorde
         */
74 1223 mdecorde
        public ArrayList<File> scanDirectory(File directory)
75 1223 mdecorde
        {
76 1223 mdecorde
                ArrayList<File> dirfiles = [];
77 1223 mdecorde
78 1223 mdecorde
                if (!directory.exists()) {
79 1223 mdecorde
                        println "directory '$directory' does not exists"
80 1223 mdecorde
                        return;
81 1223 mdecorde
                }
82 1223 mdecorde
83 1223 mdecorde
                println "scan directory : "+directory.getAbsolutePath();
84 1223 mdecorde
                LinkedList<File> files = new LinkedList<File>();
85 1223 mdecorde
                files.add(directory);
86 1223 mdecorde
87 1223 mdecorde
                while (!files.isEmpty()) {
88 1223 mdecorde
                        File current = files.removeFirst();
89 1223 mdecorde
                        if (current.isDirectory() && !current.getName().startsWith(".")) {
90 1223 mdecorde
                                //println "add dir: "+current
91 1223 mdecorde
                                List<String> currentpfiles = [];
92 1223 mdecorde
                                for (File sfile : current.listFiles()) {
93 1223 mdecorde
                                        if (sfile.isDirectory())
94 1223 mdecorde
                                                files.add(sfile);
95 1223 mdecorde
                                }
96 1223 mdecorde
97 1223 mdecorde
                                dirfiles.add(current);
98 1223 mdecorde
                        }
99 1223 mdecorde
                }
100 1223 mdecorde
                return dirfiles;
101 1223 mdecorde
        }
102 1223 mdecorde
103 1223 mdecorde
        public void mergeAll()
104 1223 mdecorde
        {
105 1223 mdecorde
                def files = new ArrayList<File>(data.keySet());
106 1223 mdecorde
                println files
107 1223 mdecorde
                if (files.size() < 1)
108 1223 mdecorde
                        return;
109 1223 mdecorde
110 1223 mdecorde
                File refDir = files.get(0);
111 1223 mdecorde
                File refClass = data.get(refDir)[0];
112 1223 mdecorde
                String refBundle = data.get(refDir)[1];
113 1223 mdecorde
                def externalized = data.get(refDir)[2];
114 1223 mdecorde
115 1223 mdecorde
                System.out.println("ref dir: "+refDir);
116 1223 mdecorde
                System.out.println("ref class: "+refClass);
117 1223 mdecorde
                System.out.println("ref bundle: "+refBundle);
118 1223 mdecorde
                System.out.println("ref files: "+externalized);
119 1223 mdecorde
120 1223 mdecorde
                // get externalized ref strings
121 1223 mdecorde
                //      Lang            Key     externalized string
122 1223 mdecorde
                HashMap<String, HashMap<String, String>> externalizedLines = [:];
123 1223 mdecorde
                for (File source : externalized) {
124 1223 mdecorde
                        def lines = new HashMap<String, String>();
125 1223 mdecorde
                        externalizedLines.put(source.getName(), lines);
126 1223 mdecorde
                        source.eachLine("iso-8859-1"){
127 1223 mdecorde
                                if (it =~ ".+=.+") {
128 1223 mdecorde
                                        int idx = it.indexOf("=");
129 1223 mdecorde
                                        lines.put(it.substring(0, idx), it.substring(idx+1))
130 1223 mdecorde
                                } else if(it =~ ".+=\$") {
131 1223 mdecorde
                                        int idx = it.indexOf("=")
132 1223 mdecorde
                                        String key = it.substring(0, idx)
133 1223 mdecorde
                                        String value = ""
134 1223 mdecorde
                                        lines.put(key, value)
135 1223 mdecorde
                                }
136 1223 mdecorde
                        }
137 1223 mdecorde
                }
138 1223 mdecorde
139 1223 mdecorde
                // get references keys
140 1223 mdecorde
                HashSet<String> refKeys = new HashSet<String>();
141 1223 mdecorde
                refClass.eachLine("iso-8859-1") {
142 1223 mdecorde
                        if (it =~ ".+public static String .+")
143 1223 mdecorde
                                refKeys << it.trim()
144 1223 mdecorde
                }
145 1223 mdecorde
146 1223 mdecorde
                println "BEFORE"
147 1223 mdecorde
                println "refKeys size: "+refKeys.size();
148 1223 mdecorde
                println "fr externalizedLines size: "+externalizedLines.get("messages_fr.properties").size();
149 1223 mdecorde
                println "en externalizedLines size: "+externalizedLines.get("messages.properties").size();
150 1223 mdecorde
151 1223 mdecorde
                // for all sub dir
152 1223 mdecorde
                for (int i = 1 ; i < files.size() ; i++) {
153 1223 mdecorde
                        File srcDir = files.get(i);
154 1223 mdecorde
                        File srcClass = data.get(srcDir)[0];
155 1223 mdecorde
                        String srcBundle = data.get(srcDir)[1];
156 1223 mdecorde
157 1223 mdecorde
                        def srcExternalized = data.get(srcDir)[2];
158 1223 mdecorde
                        // merge externalized strings
159 1223 mdecorde
                        for (File source : srcExternalized) {
160 1223 mdecorde
                                def lines = externalizedLines.get(source.getName());
161 1223 mdecorde
                                source.eachLine("iso-8859-1") {
162 1223 mdecorde
                                        if (it =~ ".+=.+") {
163 1223 mdecorde
                                                int idx = it.indexOf("=")
164 1223 mdecorde
                                                String key = it.substring(0, idx)
165 1223 mdecorde
                                                String value = it.substring(idx+1)
166 1223 mdecorde
                                                if (lines.containsKey(key) && lines.get(key) != value)
167 1223 mdecorde
                                                        println "merged message of "+source.getName()+": "+it+ " >> "+lines.get(key)
168 1223 mdecorde
                                                lines.put(key, value)
169 1223 mdecorde
                                        } else if(it =~ ".+=\$") {
170 1223 mdecorde
                                                int idx = it.indexOf("=")
171 1223 mdecorde
                                                String key = it.substring(0, idx)
172 1223 mdecorde
                                                String value = ""
173 1223 mdecorde
                                                lines.put(key, value)
174 1223 mdecorde
                                        }
175 1223 mdecorde
                                }
176 1223 mdecorde
                                source.delete(); // bye bye *.properties files
177 1223 mdecorde
                        }
178 1223 mdecorde
179 1223 mdecorde
                        // merge static String
180 1223 mdecorde
                        if(!srcClass.exists())
181 1223 mdecorde
                        {
182 1223 mdecorde
                                println "file removed: "+srcClass
183 1223 mdecorde
                                println  srcDir
184 1223 mdecorde
                                println  srcClass
185 1223 mdecorde
                                println  srcBundle
186 1223 mdecorde
                                println  srcExternalized
187 1223 mdecorde
                                continue;
188 1223 mdecorde
                        }
189 1223 mdecorde
                        srcClass.eachLine("iso-8859-1"){
190 1223 mdecorde
                                if(it =~ ".+public static String .+")
191 1223 mdecorde
                                {
192 1223 mdecorde
//                                        if(refKeys.contains(it.trim()))
193 1223 mdecorde
//                                                println "Externalized key merged: "+it
194 1223 mdecorde
                                        refKeys << it.trim()
195 1223 mdecorde
                                }
196 1223 mdecorde
                        }
197 1223 mdecorde
                        srcClass.delete(); // bye bye Messages.java
198 1223 mdecorde
                }
199 1223 mdecorde
200 1223 mdecorde
                println "AFTER"
201 1223 mdecorde
                println "refKeys size: "+refKeys.size();
202 1223 mdecorde
                println "fr externalizedLines size: "+externalizedLines.get("messages_fr.properties").size();
203 1223 mdecorde
                println "en externalizedLines size: "+externalizedLines.get("messages.properties").size();
204 1223 mdecorde
205 1223 mdecorde
                //check unused externalized strings
206 1223 mdecorde
                def keysDeclared = refKeys.collect{it.substring(21, it.length() -1) };
207 1223 mdecorde
                System.out.println("number of keys declared in Message.java: "+keysDeclared.size());
208 1223 mdecorde
                def missingDeclarations = new HashMap<String, HashSet<String>>();
209 1223 mdecorde
                def missingExternalizations = new HashMap<String, HashSet<String>>();
210 1223 mdecorde
                for(def lang : externalizedLines.keySet())
211 1223 mdecorde
                {
212 1223 mdecorde
                        def keys = externalizedLines.get(lang).keySet(); // keys  in propeties files
213 1223 mdecorde
                        missingExternalizations.put(lang, new HashSet<String>());
214 1223 mdecorde
                        missingDeclarations.put(lang, new HashSet<String>());
215 1223 mdecorde
                        for(String key : keys)
216 1223 mdecorde
                                if(!keysDeclared.contains(key))
217 1223 mdecorde
                                        missingDeclarations.get(lang) << key
218 1223 mdecorde
219 1223 mdecorde
                        for(String key : keysDeclared)
220 1223 mdecorde
                                if(!keys.contains(key))
221 1223 mdecorde
                                        missingExternalizations.get(lang) << key
222 1223 mdecorde
                }
223 1223 mdecorde
224 1223 mdecorde
                println "fix missing key declaration and externalization"
225 1223 mdecorde
                for(String lang: missingExternalizations.keySet())
226 1223 mdecorde
                {
227 1223 mdecorde
                        println "Lang: "+lang
228 1223 mdecorde
                        println " missingDeclarations: "+missingDeclarations.get(lang)
229 1223 mdecorde
                        for(String key : missingDeclarations.get(lang))
230 1223 mdecorde
                                refKeys << "public static String "+key+";"
231 1223 mdecorde
                        println " missingExternalizations: "+missingExternalizations.get(lang)
232 1223 mdecorde
                        for(String key : missingExternalizations.get(lang))
233 1223 mdecorde
                                externalizedLines.get(lang).put(key, "N/A")
234 1223 mdecorde
                }
235 1223 mdecorde
236 1223 mdecorde
                println "AFTER FIX"
237 1223 mdecorde
                println "refKeys size: "+refKeys.size();
238 1223 mdecorde
                println "fr externalizedLines size: "+externalizedLines.get("messages_fr.properties").size();
239 1223 mdecorde
                println "en externalizedLines size: "+externalizedLines.get("messages.properties").size();
240 1223 mdecorde
241 1223 mdecorde
242 1223 mdecorde
                // write externalized lines
243 1223 mdecorde
                for(File source : externalized)
244 1223 mdecorde
                {
245 1223 mdecorde
                        source.withWriter("iso-8859-1") { out ->
246 1223 mdecorde
                                def hash = externalizedLines.get(source.getName())
247 1223 mdecorde
                                def keys = hash.keySet();
248 1223 mdecorde
                                keys = keys.sort()
249 1223 mdecorde
                                keys.each() {
250 1223 mdecorde
                                        out.writeLine(it+"="+hash.get(it))
251 1223 mdecorde
                                }
252 1223 mdecorde
                        }
253 1223 mdecorde
                }
254 1223 mdecorde
255 1223 mdecorde
                // write classRef File
256 1223 mdecorde
                refClass.withWriter("iso-8859-1") { out ->
257 1223 mdecorde
                        // write start
258 1223 mdecorde
                        out.writeLine('''
259 1223 mdecorde
package org.txm.scripts.;
260 1223 mdecorde

261 1223 mdecorde
import org.eclipse.osgi.util.NLS;
262 1223 mdecorde

263 1223 mdecorde
public class Messages extends NLS {
264 1223 mdecorde

265 1223 mdecorde
        // The Constant BUNDLE_NAME.
266 1223 mdecorde
        private static final String BUNDLE_NAME = "org.txm.messages"; //$NON-NLS-1$
267 1223 mdecorde

268 1223 mdecorde
                                ''')
269 1223 mdecorde
270 1223 mdecorde
                        //write keys
271 1223 mdecorde
                        for(String key : refKeys.sort())
272 1223 mdecorde
                                out.writeLine("\t"+key);
273 1223 mdecorde
274 1223 mdecorde
                        // write end
275 1223 mdecorde
                        out.writeLine('''
276 1223 mdecorde
        static {
277 1223 mdecorde
                // initialize resource bundle
278 1223 mdecorde
                NLS.initializeMessages(BUNDLE_NAME, Messages.class);
279 1223 mdecorde
        }
280 1223 mdecorde

281 1223 mdecorde
        private Messages() {
282 1223 mdecorde
        }
283 1223 mdecorde
}
284 1223 mdecorde
                                ''')
285 1223 mdecorde
                }
286 1223 mdecorde
        }
287 1223 mdecorde
288 1223 mdecorde
        public static void main(String[] args)
289 1223 mdecorde
        {
290 1223 mdecorde
                File projectdir = new File("/home/mdecorde/workspace37/org.txm.core/src/java")
291 1223 mdecorde
                def rez = new MergeExternalizedStringFiles(projectdir);
292 1223 mdecorde
        }
293 1223 mdecorde
}