Statistics
| Revision:

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

History | View | Annotate | Download (9 kB)

1 1223 mdecorde
// Copyright © 2010-2013 ENS de Lyon.
2 1223 mdecorde
// Copyright © 2007-2010 ENS de Lyon, CNRS, INRP, University of
3 1223 mdecorde
// Lyon 2, University of Franche-Comté, University of Nice
4 1223 mdecorde
// Sophia Antipolis, University of Paris 3.
5 1223 mdecorde
//
6 1223 mdecorde
// The TXM platform is free software: you can redistribute it
7 1223 mdecorde
// and/or modify it under the terms of the GNU General Public
8 1223 mdecorde
// License as published by the Free Software Foundation,
9 1223 mdecorde
// either version 2 of the License, or (at your option) any
10 1223 mdecorde
// later version.
11 1223 mdecorde
//
12 1223 mdecorde
// The TXM platform is distributed in the hope that it will be
13 1223 mdecorde
// useful, but WITHOUT ANY WARRANTY; without even the implied
14 1223 mdecorde
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 1223 mdecorde
// PURPOSE. See the GNU General Public License for more
16 1223 mdecorde
// details.
17 1223 mdecorde
//
18 1223 mdecorde
// You should have received a copy of the GNU General
19 1223 mdecorde
// Public License along with the TXM platform. If not, see
20 1223 mdecorde
// http://www.gnu.org/licenses.
21 1223 mdecorde
22 1223 mdecorde
//
23 1223 mdecorde
// This file is part of the TXM platform.
24 1223 mdecorde
//
25 1223 mdecorde
// The TXM platform is free software: you can redistribute it and/or modif y
26 1223 mdecorde
// it under the terms of the GNU General Public License as published by
27 1223 mdecorde
// the Free Software Foundation, either version 3 of the License, or
28 1223 mdecorde
// (at your option) any later version.
29 1223 mdecorde
//
30 1223 mdecorde
// The TXM platform is distributed in the hope that it will be useful,
31 1223 mdecorde
// but WITHOUT ANY WARRANTY; without even the implied warranty of
32 1223 mdecorde
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33 1223 mdecorde
// GNU General Public License for more details.
34 1223 mdecorde
//
35 1223 mdecorde
// You should have received a copy of the GNU General Public License
36 1223 mdecorde
// along with the TXM platform.  If not, see <http://www.gnu.org/licenses/>.
37 1223 mdecorde
//
38 1223 mdecorde
//
39 1223 mdecorde
//
40 1223 mdecorde
// $LastChangedDate: 2013-07-25 16:24:13 +0200 (jeu. 25 juil. 2013) $
41 1223 mdecorde
// $LastChangedRevision: 2490 $
42 1223 mdecorde
// $LastChangedBy: mdecorde $
43 1223 mdecorde
//
44 1223 mdecorde
package org.txm.rcp.translate.i18n;
45 1223 mdecorde
46 1223 mdecorde
import java.io.BufferedReader;
47 1223 mdecorde
import java.io.File;
48 1223 mdecorde
import java.io.FileInputStream;
49 1223 mdecorde
import java.io.InputStreamReader;
50 1223 mdecorde
import java.util.LinkedList;
51 1223 mdecorde
52 1223 mdecorde
// TODO: Auto-generated Javadoc
53 1223 mdecorde
/**
54 1223 mdecorde
 * Tool to update the key of externalization files xxxx_fr.properties
55 1223 mdecorde
 * adds _NA to missing keys
56 1223 mdecorde
 *
57 1223 mdecorde
 * @author mdecorde
58 1223 mdecorde
 *
59 1223 mdecorde
 */
60 1223 mdecorde
public class ExternalizationFilesUpdater {
61 1223 mdecorde
62 1223 mdecorde
        /** The dirfiles. */
63 1223 mdecorde
        LinkedList<File> dirfiles = new LinkedList<File>();
64 1223 mdecorde
65 1223 mdecorde
        /** The propertyfiles. */
66 1223 mdecorde
        HashMap<String, File> propertyfiles = new HashMap<String, File>();
67 1223 mdecorde
68 1223 mdecorde
        /** The fileentries. */
69 1223 mdecorde
        HashMap<File, List<String>> fileentries = new HashMap<String, List<String>>();
70 1223 mdecorde
71 1223 mdecorde
        /** The fileentriesvalues. */
72 1223 mdecorde
        HashMap<File, HashMap<String, String>> fileentriesvalues = new HashMap<String, HashMap<String, String>>();
73 1223 mdecorde
74 1223 mdecorde
        /**
75 1223 mdecorde
         * Gets the entries.
76 1223 mdecorde
         *
77 1223 mdecorde
         * @param file the file
78 1223 mdecorde
         * @return the entries
79 1223 mdecorde
         */
80 1223 mdecorde
        public List<String> getEntries(File file)
81 1223 mdecorde
        {
82 1223 mdecorde
                List<String> entries = [];
83 1223 mdecorde
                BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file) , "iso-8859-1"));
84 1223 mdecorde
                String line = reader.readLine();
85 1223 mdecorde
                while (line != null) {
86 1223 mdecorde
                        String[] split = line.split("=", 2);
87 1223 mdecorde
                        if (split.length > 0) {
88 1223 mdecorde
                                String key = split[0];
89 1223 mdecorde
                                entries.add(key);
90 1223 mdecorde
                        }
91 1223 mdecorde
                        line = reader.readLine();
92 1223 mdecorde
                }
93 1223 mdecorde
                return entries;
94 1223 mdecorde
        }
95 1223 mdecorde
96 1223 mdecorde
        /**
97 1223 mdecorde
         * Gets the values.
98 1223 mdecorde
         *
99 1223 mdecorde
         * @param file the file
100 1223 mdecorde
         * @return the values
101 1223 mdecorde
         */
102 1223 mdecorde
        public HashMap<String, String> getValues(File file) {
103 1223 mdecorde
                HashMap<String, String> values = new HashMap<String, String>();
104 1223 mdecorde
                BufferedReader reader = new BufferedReader(new InputStreamReader(
105 1223 mdecorde
                                new FileInputStream(file), "iso-8859-1"));
106 1223 mdecorde
                String line = reader.readLine();
107 1223 mdecorde
                while (line != null) {
108 1223 mdecorde
                        String[] split = line.split("=");
109 1223 mdecorde
                        if (split.length > 0) {
110 1223 mdecorde
                                String key = split[0];
111 1223 mdecorde
                                String concat = "";
112 1223 mdecorde
                                for (int i = 1; i < split.length; i++)
113 1223 mdecorde
                                        concat += split[i];
114 1223 mdecorde
                                values.put(key, concat);
115 1223 mdecorde
                        }
116 1223 mdecorde
                        line = reader.readLine();
117 1223 mdecorde
                }
118 1223 mdecorde
                return values;
119 1223 mdecorde
        }
120 1223 mdecorde
121 1223 mdecorde
        /**
122 1223 mdecorde
         * Process found directories.
123 1223 mdecorde
         */
124 1223 mdecorde
        public void processFoundDirectories()
125 1223 mdecorde
        {
126 1223 mdecorde
                for (String dirfile : dirfiles) {
127 1223 mdecorde
                        println "DIRECTORY : "+dirfile
128 1223 mdecorde
                        List<File> files = propertyfiles.get(dirfile)
129 1223 mdecorde
                        for (File f : files) {
130 1223 mdecorde
                                this.fileentries.put(f, getEntries(f));
131 1223 mdecorde
                                this.fileentriesvalues.put(f, getValues(f));
132 1223 mdecorde
                        }
133 1223 mdecorde
                        File reference = null;
134 1223 mdecorde
                        if (files.get(0).getName().startsWith("m")) {
135 1223 mdecorde
                                reference = new File(dirfile,"messages.properties");
136 1223 mdecorde
                        }
137 1223 mdecorde
                        else if (files.get(0).getName().startsWith("b"))
138 1223 mdecorde
                                reference = new File(dirfile,"bundle.properties");
139 1223 mdecorde
140 1223 mdecorde
                        if (reference != null && reference.exists()) {
141 1223 mdecorde
                                for (File f : files) {
142 1223 mdecorde
                                        if (f != reference) {
143 1223 mdecorde
                                                List<String> refentries = this.fileentries.get(reference).clone();
144 1223 mdecorde
                                                List<String> tmp1 = this.fileentries.get(f).clone();
145 1223 mdecorde
                                                tmp1.removeAll(refentries);
146 1223 mdecorde
147 1223 mdecorde
                                                refentries = this.fileentries.get(reference).clone();
148 1223 mdecorde
                                                List<String> tmp = this.fileentries.get(f);
149 1223 mdecorde
                                                refentries.removeAll(tmp);
150 1223 mdecorde
                                                for (String missing : refentries) {
151 1223 mdecorde
                                                        this.fileentriesvalues.get(f).put(missing,"N/A_"+this.fileentriesvalues.get(reference).get(missing)); // put entry's value
152 1223 mdecorde
                                                }
153 1223 mdecorde
154 1223 mdecorde
                                                this.fileentries.put(f, this.fileentries.get(reference)); // update file entries
155 1223 mdecorde
156 1223 mdecorde
                                                if (tmp1.size() > 0 || refentries.size() > 0)
157 1223 mdecorde
                                                        println " "+f.getName()
158 1223 mdecorde
                                                if (tmp1.size() > 0)
159 1223 mdecorde
                                                        println "  Removed keys : "+tmp1;
160 1223 mdecorde
                                                if (refentries.size() > 0)
161 1223 mdecorde
                                                        println "  Added keys : "+refentries;
162 1223 mdecorde
                                        }
163 1223 mdecorde
                                }
164 1223 mdecorde
                        }
165 1223 mdecorde
                }
166 1223 mdecorde
        }
167 1223 mdecorde
168 1223 mdecorde
        /**
169 1223 mdecorde
         * Update files.
170 1223 mdecorde
         */
171 1223 mdecorde
        public void updateFiles()
172 1223 mdecorde
        {
173 1223 mdecorde
                for(String dirfile : dirfiles)
174 1223 mdecorde
                {
175 1223 mdecorde
                        List<File> files = propertyfiles.get(dirfile);
176 1223 mdecorde
                        for(File f : files)
177 1223 mdecorde
                        {
178 1223 mdecorde
                                List<String> entries =  this.fileentries.get(f);
179 1223 mdecorde
                                HashMap<String, String> values = this.fileentriesvalues.get(f);
180 1223 mdecorde
181 1223 mdecorde
                                Writer writer = new OutputStreamWriter(new FileOutputStream(f) , "iso-8859-1");
182 1223 mdecorde
                                for(String entry : entries)
183 1223 mdecorde
                                {
184 1223 mdecorde
                                        writer.write(entry+"="+values.get(entry)+"\n");
185 1223 mdecorde
                                }
186 1223 mdecorde
                                writer.close()
187 1223 mdecorde
                        }
188 1223 mdecorde
                }
189 1223 mdecorde
        }
190 1223 mdecorde
191 1223 mdecorde
        /**
192 1223 mdecorde
         * Creates the missing files.
193 1223 mdecorde
         *
194 1223 mdecorde
         * @param suffix the suffix
195 1223 mdecorde
         */
196 1223 mdecorde
        public void createMissingFiles(String suffix)
197 1223 mdecorde
        {
198 1223 mdecorde
                println "Looking for missing messages files "+ suffix;
199 1223 mdecorde
                for(String dirfile : dirfiles)
200 1223 mdecorde
                {
201 1223 mdecorde
                        //println "DIRECTORY : "+dirfile
202 1223 mdecorde
                        File reference = null;
203 1223 mdecorde
                        String lookingname = "";
204 1223 mdecorde
                        List<File> files = propertyfiles.get(dirfile)
205 1223 mdecorde
                        if(files.get(0).getName().startsWith("m"))
206 1223 mdecorde
                        {
207 1223 mdecorde
                                reference = new File(dirfile,"messages.properties");
208 1223 mdecorde
                                lookingname = "messages_"+suffix+".properties"
209 1223 mdecorde
                        }
210 1223 mdecorde
                        else if (files.get(0).getName().startsWith("b"))
211 1223 mdecorde
                        {
212 1223 mdecorde
                                reference = new File(dirfile,"bundle.properties");
213 1223 mdecorde
                                lookingname = "bundle_"+suffix+".properties"
214 1223 mdecorde
                        }
215 1223 mdecorde
216 1223 mdecorde
                        boolean create = true;
217 1223 mdecorde
                        if(reference != null && reference.exists())
218 1223 mdecorde
                        {
219 1223 mdecorde
                                for(File f : files)
220 1223 mdecorde
                                {
221 1223 mdecorde
                                        if(f.getName() == lookingname)
222 1223 mdecorde
                                                create = false;
223 1223 mdecorde
                                }
224 1223 mdecorde
                        }
225 1223 mdecorde
                        if(create)
226 1223 mdecorde
                        {
227 1223 mdecorde
                                new File(dirfile,lookingname).createNewFile();
228 1223 mdecorde
                                println "Create file " +new File(dirfile,lookingname)
229 1223 mdecorde
                        }
230 1223 mdecorde
                }
231 1223 mdecorde
        }
232 1223 mdecorde
233 1223 mdecorde
        /* (non-Javadoc)
234 1223 mdecorde
         * @see java.lang.Object#toString()
235 1223 mdecorde
         */
236 1223 mdecorde
        public String toString()
237 1223 mdecorde
        {
238 1223 mdecorde
                String rez = "";
239 1223 mdecorde
                for(String dirfile : dirfiles)
240 1223 mdecorde
                {
241 1223 mdecorde
                        rez += dirfile+"\n";
242 1223 mdecorde
                        for(File f : propertyfiles.get(dirfile))
243 1223 mdecorde
                                rez += "  "+f.getName()+"\n"
244 1223 mdecorde
                }
245 1223 mdecorde
                return rez;
246 1223 mdecorde
        }
247 1223 mdecorde
248 1223 mdecorde
        /**
249 1223 mdecorde
         * Scan directory.
250 1223 mdecorde
         *
251 1223 mdecorde
         * @param directory the directory
252 1223 mdecorde
         */
253 1223 mdecorde
        public void scanDirectory(File directory)
254 1223 mdecorde
        {
255 1223 mdecorde
                if(!directory.exists())
256 1223 mdecorde
                {
257 1223 mdecorde
                        println "directory '$directory' does not exists"
258 1223 mdecorde
                        return;
259 1223 mdecorde
                }
260 1223 mdecorde
261 1223 mdecorde
                println "scan directory : "+directory.getAbsolutePath();
262 1223 mdecorde
                LinkedList<File> files = new LinkedList<File>();
263 1223 mdecorde
                files.add(directory);
264 1223 mdecorde
265 1223 mdecorde
                while(!files.isEmpty())
266 1223 mdecorde
                {
267 1223 mdecorde
                        File current = files.removeFirst();
268 1223 mdecorde
                        if(current.isDirectory())
269 1223 mdecorde
                        {
270 1223 mdecorde
                                List<String> currentpfiles = [];
271 1223 mdecorde
                                for(File sfile : current.listFiles())
272 1223 mdecorde
                                {
273 1223 mdecorde
                                        if(sfile.isDirectory())
274 1223 mdecorde
                                                files.add(sfile);
275 1223 mdecorde
                                        else if(sfile.getName().endsWith(".properties") && ( sfile.getName().startsWith("messages") || sfile.getName().startsWith("bundle")) )
276 1223 mdecorde
                                                currentpfiles.add(sfile)
277 1223 mdecorde
                                }
278 1223 mdecorde
                                if(currentpfiles.size() > 0)
279 1223 mdecorde
                                {
280 1223 mdecorde
                                        dirfiles.add(current.getAbsolutePath());
281 1223 mdecorde
                                        propertyfiles.put(current.getAbsolutePath(), currentpfiles);
282 1223 mdecorde
                                }
283 1223 mdecorde
                        }
284 1223 mdecorde
                }
285 1223 mdecorde
        }
286 1223 mdecorde
287 1223 mdecorde
        /**
288 1223 mdecorde
         * The main method.
289 1223 mdecorde
         *
290 1223 mdecorde
         * @param args the arguments
291 1223 mdecorde
         */
292 1223 mdecorde
        public static void main(String[] args)
293 1223 mdecorde
        {
294 1223 mdecorde
                String userdir = System.getProperty("user.home");
295 1223 mdecorde
296 1223 mdecorde
                println "\nRCP\n";
297 1223 mdecorde
298 1223 mdecorde
                ExternalizationFilesUpdater scanner = new ExternalizationFilesUpdater();
299 1223 mdecorde
//                scanner.scanDirectory(new File(userdir, "workspace37/org.txm.rcp")); // find directories with a messages.properties file
300 1223 mdecorde
//                scanner.createMissingFiles("fr"); // create messages_fr.properties files when a messages.properties is found
301 1223 mdecorde
//                scanner.processFoundDirectories(); // find missing and obsolets keys
302 1223 mdecorde
//                scanner.updateFiles(); // update messages files content
303 1223 mdecorde
304 1223 mdecorde
                println "\nTOOLBOX\n";
305 1223 mdecorde
306 1223 mdecorde
                scanner = new ExternalizationFilesUpdater();
307 1223 mdecorde
                scanner.scanDirectory(new File(userdir, "workspace43/org.txm.rcp/src/main/java"));
308 1223 mdecorde
                scanner.createMissingFiles("fr");
309 1223 mdecorde
                scanner.createMissingFiles("ru");
310 1223 mdecorde
                scanner.processFoundDirectories();
311 1223 mdecorde
                scanner.updateFiles();
312 1223 mdecorde
        }
313 1223 mdecorde
}