Statistics
| Revision:

root / tmp / org.txm.translate.rcp / src / org / txm / rcp / translate / i18n / IsStringUsedInPlugin.groovy @ 1254

History | View | Annotate | Download (1.5 kB)

1 1223 mdecorde
package org.txm.rcp.translate.i18n
2 1223 mdecorde
3 1223 mdecorde
/**
4 1223 mdecorde
 * Display the String and key not used in the plugin messages
5 1223 mdecorde
 * @author mdecorde
6 1223 mdecorde
 *
7 1223 mdecorde
 */
8 1223 mdecorde
class IsStringUsedInPlugin {
9 1223 mdecorde
        Properties props;
10 1223 mdecorde
11 1223 mdecorde
        public boolean scan(File propertyFile, File pluginFile) {
12 1223 mdecorde
                if(!(propertyFile.exists() && propertyFile.canRead() && propertyFile.canWrite() && propertyFile.isFile()))
13 1223 mdecorde
                {
14 1223 mdecorde
                        println "error file : "+propertyFile
15 1223 mdecorde
                        return false;
16 1223 mdecorde
                }
17 1223 mdecorde
18 1223 mdecorde
                props = new Properties();
19 1223 mdecorde
                propertyFile.withReader("iso-8859-1") { input->
20 1223 mdecorde
                        props.load(input);
21 1223 mdecorde
                        input.close()
22 1223 mdecorde
                }
23 1223 mdecorde
24 1223 mdecorde
                String content = pluginFile.getText();
25 1223 mdecorde
                def toRemove = [];
26 1223 mdecorde
                for(def key : props.keySet()) {
27 1223 mdecorde
                        if (!content.contains("\"%"+key+"\"")) {
28 1223 mdecorde
                                println "not used $key "+props.get(key);
29 1223 mdecorde
                                toRemove << key
30 1223 mdecorde
                        }
31 1223 mdecorde
                }
32 1223 mdecorde
33 1223 mdecorde
                for(String key : toRemove) {
34 1223 mdecorde
                        props.remove(key);
35 1223 mdecorde
                }
36 1223 mdecorde
37 1223 mdecorde
38 1223 mdecorde
                if (toRemove == 0) {
39 1223 mdecorde
                        println "nothing to do"
40 1223 mdecorde
                        return false;
41 1223 mdecorde
                }
42 1223 mdecorde
                return true
43 1223 mdecorde
        }
44 1223 mdecorde
45 1223 mdecorde
        public boolean writeFixes(File propertyFile) {
46 1223 mdecorde
                if (props == null) return false;
47 1223 mdecorde
                propertyFile.withWriter("iso-8859-1") { output ->
48 1223 mdecorde
                        props.store(output, "")
49 1223 mdecorde
                }
50 1223 mdecorde
                return true;
51 1223 mdecorde
        }
52 1223 mdecorde
53 1223 mdecorde
        public static void main(String[] args) {
54 1223 mdecorde
                String userhome = System.getProperty("user.home")
55 1223 mdecorde
                File propertyFile = new File(userhome, "workspace43/org.txm.rcp/OSGI-INF/l10n/bundle.properties");
56 1223 mdecorde
                File pluginFile = new File(userhome, "workspace43/org.txm.rcp/plugin.xml");
57 1223 mdecorde
                IsStringUsedInPlugin fixer = new IsStringUsedInPlugin();
58 1223 mdecorde
                //uncomment to remove unused Strings
59 1223 mdecorde
                if (fixer.scan(propertyFile, pluginFile)) {
60 1223 mdecorde
                        fixer.writeFixes(propertyFile)
61 1223 mdecorde
                }
62 1223 mdecorde
        }
63 1223 mdecorde
}