Statistics
| Revision:

root / tmp / org.txm.groovy.core / src / groovy / org / txm / scripts / i18n / FindDuplicatedKeyProperties.groovy @ 1000

History | View | Annotate | Download (3.3 kB)

1
package org.txm.scripts.scripts.i18n
2

    
3
import org.txm.utils.LS;
4
import org.txm.utils.io.IOUtils;
5

    
6

    
7
def javaFileToPropertieFiles = [:]
8
def propertieFileToJavaFile = [:]
9

    
10
def langs = ["", "_fr", "_ru"] // lang pattern
11

    
12
public File getSRCDirectory(File projectDirectory) {
13
        def commons = ["src/main/java", "src/java", "src"]
14
        for (String root : commons) {
15
                if (new File(projectDirectory, root).exists()) {
16
                        return new File(projectDirectory, root)
17
                }
18
        }
19
        return projectDirectory;
20
}
21

    
22
File workspace = new File(System.getProperty("user.home"), "workspace079");
23
def allProjectDirectories = LS.list(workspace, false, false)
24
def allProjectSRCDirectories = []
25
for (File projectDirectory : allProjectDirectories) {
26
        allProjectSRCDirectories << getSRCDirectory(projectDirectory)
27
}
28

    
29
// find Messages.java files
30
def allJavaFiles = []
31
for (File projectSRCDirectory : allProjectSRCDirectories) {
32
        allJavaFiles.addAll(LS.list(projectSRCDirectory, true, false))
33
}
34
allJavaFiles = allJavaFiles.findAll { it -> return it.getName().endsWith("Messages.java") }
35

    
36
// for each Java file find its properties files
37
for (File javaFile : allJavaFiles) {
38
        println javaFile
39
        def otherFiles = LS.list(javaFile.getParentFile(), false, false)
40
        otherFiles = otherFiles.findAll { it -> return it.getName().endsWith(".properties") && it.getName().startsWith("messages") }
41
        println otherFiles
42
        javaFileToPropertieFiles[javaFile] = otherFiles
43
        for (File o : otherFiles) {
44
                propertieFileToJavaFile[o] = javaFile
45
        }
46
}
47

    
48
// for each properties file, load its properties
49
def propertiesToMessageFile = [:]
50
def messagesFileToProperties = [:]
51

    
52
def messageToProperties = [:]
53
def messageKeyToProperties = [:]
54

    
55
for (def file : propertieFileToJavaFile.keySet()) {
56
        Properties newProperties = new Properties();
57
        newProperties.load(IOUtils.getReader(file, "UTF-8"));
58
        
59
        propertiesToMessageFile[file] = newProperties;
60
        messagesFileToProperties[newProperties] = file;
61
        
62
        for (String kmessage : newProperties.keySet()) {
63
                def message = newProperties[kmessage]
64
                if (!messageToProperties.containsKey(message)) messageToProperties[message] = []
65
                messageToProperties[message] << newProperties
66
                if (!messageKeyToProperties.containsKey(message)) messageKeyToProperties[message] = []
67
                messageKeyToProperties << newProperties
68
        }
69
}
70

    
71

    
72
//////////////////
73
for (def lang : langs) {
74
        def pattenr = /messages$lang\.properties/
75

    
76
        def allPropertiesFiles = []
77
        for (File projectSRCDirectory : allProjectSRCDirectories) {
78
                allPropertiesFiles.addAll(LS.list(projectSRCDirectory, true, false))
79
        }
80
        def propertiesFiles = []
81
        for (def file : allPropertiesFiles) {
82
                if (file.getName() ==~ pattenr) {
83
                        propertiesFiles << file
84
                }
85
        }
86
        println "\n*** '$lang' properties files in $workspace***\n"
87

    
88
        def messages = [:]
89
        for (def file : propertiesFiles) {
90
                Properties newProperties = new Properties();
91
                newProperties.load(IOUtils.getReader(file, "UTF-8"));
92
                messages[file] = newProperties;
93
        }
94

    
95
        def allKeys = new HashSet();
96
        for (def file : propertiesFiles) {
97
                for (def keys : messages[file].keySet()) {
98
                        allKeys.addAll(keys)
99
                }
100
        }
101
        allKeys = allKeys.sort()
102

    
103
        for (def key : allKeys) {
104
                def values = []
105
                for (def file : propertiesFiles) {
106
                        def v = messages[file][key];
107
                        if (v != null) {
108
                                values << file.getAbsolutePath()
109
                        }
110
                }
111
                if (values.size() > 1) {
112
                        println "'$key': "+values.join("\t")
113
                }
114
        }
115
}