Statistics
| Revision:

root / tmp / org.txm.translate.rcp / src / org / txm / rcp / translate / i18n / I18nDiff.groovy @ 1290

History | View | Annotate | Download (2 kB)

1 1223 mdecorde
package org.txm.rcp.translate.i18n
2 1223 mdecorde
3 1223 mdecorde
import java.io.File;
4 1223 mdecorde
import java.util.HashMap;
5 1223 mdecorde
/**
6 1223 mdecorde
 * Display a diff of 2 message files
7 1223 mdecorde
 *
8 1223 mdecorde
 * @author mdecorde
9 1223 mdecorde
 *
10 1223 mdecorde
 */
11 1223 mdecorde
class I18nDiff {
12 1223 mdecorde
        Properties values1, values2;
13 1223 mdecorde
        File file1;
14 1223 mdecorde
        File file2;
15 1223 mdecorde
16 1223 mdecorde
        def notInValues1, notInValues2
17 1223 mdecorde
        I18nDiff(File file1, File file2) {
18 1223 mdecorde
                this.file1 = file1
19 1223 mdecorde
                this.file2 = file2
20 1223 mdecorde
                values1 = getValues(file1)
21 1223 mdecorde
                values2 = getValues(file2)
22 1223 mdecorde
23 1223 mdecorde
                def inter = values1.keySet().intersect(values2.keySet());
24 1223 mdecorde
                notInValues1 = values2.keySet().minus(values1.keySet());
25 1223 mdecorde
                notInValues2 = values1.keySet().minus(values2.keySet());
26 1223 mdecorde
27 1223 mdecorde
                println "not in $file1"
28 1223 mdecorde
                for(String key : notInValues1)
29 1223 mdecorde
                        if (key.trim().length() > 0)
30 1223 mdecorde
                                println key+" = "+values2.get(key)
31 1223 mdecorde
32 1223 mdecorde
                println "not in $file2"
33 1223 mdecorde
                for(String key : notInValues2)
34 1223 mdecorde
                        if (key.trim().length() > 0)
35 1223 mdecorde
                                println key+" = "+values1.get(key)
36 1223 mdecorde
37 1223 mdecorde
                //                println "values diff"
38 1223 mdecorde
                //
39 1223 mdecorde
                //                for(String key : inter ) {
40 1223 mdecorde
                //                        if(values1.get(key) != values2.get(key))
41 1223 mdecorde
                //                                println key+" = "+values1.get(key)+" != "+values2.get(key)
42 1223 mdecorde
                //                }
43 1223 mdecorde
        }
44 1223 mdecorde
45 1223 mdecorde
        public void synchronizeFromF1toF2() {
46 1223 mdecorde
47 1223 mdecorde
                for(String key : notInValues2) {
48 1223 mdecorde
                        if (key.trim().length() > 0)
49 1223 mdecorde
                                values2.setProperty(key, "NA_"+values1.get(key));
50 1223 mdecorde
                }
51 1223 mdecorde
52 1223 mdecorde
                for(String key : notInValues1)
53 1223 mdecorde
                        if (key.trim().length() > 0)
54 1223 mdecorde
                                values2.remove(key)
55 1223 mdecorde
56 1223 mdecorde
                file2.withWriter("iso-8859-1") { writer ->
57 1223 mdecorde
                        values2.store(writer, "")
58 1223 mdecorde
                }
59 1223 mdecorde
        }
60 1223 mdecorde
61 1223 mdecorde
        /**
62 1223 mdecorde
         * Gets the values.
63 1223 mdecorde
         *
64 1223 mdecorde
         * @param file the file
65 1223 mdecorde
         * @return the values
66 1223 mdecorde
         */
67 1223 mdecorde
        public Properties getValues(File file) {
68 1223 mdecorde
                Properties props = new Properties();
69 1223 mdecorde
                file.withReader("iso-8859-1") { input->
70 1223 mdecorde
                        props.load(input);
71 1223 mdecorde
                        input.close()
72 1223 mdecorde
                }
73 1223 mdecorde
                return props;
74 1223 mdecorde
        }
75 1223 mdecorde
76 1223 mdecorde
        public static void main(String[] args) {
77 1223 mdecorde
                String userhome = System.getProperty("user.home");
78 1223 mdecorde
                File f1 = new File(userhome, "workspace43/org.txm.rcp/OSGI-INF/l10n/bundle.properties")
79 1223 mdecorde
                File f2 = new File(userhome, "workspace43/org.txm.rcp/OSGI-INF/l10n/bundle_fr.properties")
80 1223 mdecorde
81 1223 mdecorde
                I18nDiff diff = new I18nDiff(f1, f2);
82 1223 mdecorde
                diff.synchronizeFromF1toF2()
83 1223 mdecorde
        }
84 1223 mdecorde
}