KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > scenario > tools > util > TranslationCheck


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002, 2003 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Nicolas Modrzyk
22  * Contributor(s): _________________________.
23  */

24
25 package org.objectweb.cjdbc.scenario.tools.util;
26
27 import java.io.BufferedReader JavaDoc;
28 import java.io.File JavaDoc;
29 import java.io.FileReader JavaDoc;
30 import java.io.FilenameFilter JavaDoc;
31 import java.util.ArrayList JavaDoc;
32 import java.util.Enumeration JavaDoc;
33 import java.util.ResourceBundle JavaDoc;
34
35 import org.objectweb.cjdbc.common.i18n.Translate;
36
37 /**
38  * This looks at the language file and the java code to gather information on
39  * what translation tags are correct, missing ...
40  *
41  * @author <a HREF="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
42  */

43 public class TranslationCheck
44 {
45   static final String JavaDoc TRANSLATE_TAG = "Translate.get(";
46   static final String JavaDoc LOGGER_TAG = "logger.";
47   static final String JavaDoc LOGGER_TEST = "logger.is";
48   static final String JavaDoc EXCEPTION_TAG = "Exception(";
49   static final int TRANSLATE_TAG_LENGTH = TRANSLATE_TAG.length();
50   private String JavaDoc translationFile = Translate.CJDBC_LANGUAGE_FILE;
51   private ArrayList JavaDoc ignore;
52   private final String JavaDoc userDir = System.getProperty("cjdbc.dir");
53
54   int analysedXslFile = 0;
55   int analyseJavaFile = 0;
56
57   private ArrayList JavaDoc xslKeys;
58   private ArrayList JavaDoc missingXslKeys;
59   private ArrayList JavaDoc usedXslKeys;
60   private ArrayList JavaDoc configurationKeys;
61   private ArrayList JavaDoc javaKeys;
62   private ArrayList JavaDoc invalidKeys;
63   // when the key is present but no text is associated to it
64
private ArrayList JavaDoc missingTranslations;
65   private ArrayList JavaDoc missingLogger;
66   private ArrayList JavaDoc javaFiles;
67
68   /**
69    * This is a small class for storing invalid tag found in java files
70    */

71   class InvalidTag
72   {
73     String JavaDoc file;
74     String JavaDoc tmp;
75     String JavaDoc description;
76     int line;
77
78     InvalidTag(String JavaDoc description, String JavaDoc file, String JavaDoc tmp, int line)
79     {
80       this.description = description;
81       this.file = file;
82       this.tmp = tmp;
83       this.line = line;
84     }
85   }
86
87   /**
88    * Creates a new translation checker object. This starts analysing the java
89    * code when instanciated
90    */

91   public TranslationCheck()
92   {
93
94     ignore = new ArrayList JavaDoc();
95     configurationKeys = new ArrayList JavaDoc();
96     javaKeys = new ArrayList JavaDoc();
97     invalidKeys = new ArrayList JavaDoc();
98     missingTranslations = new ArrayList JavaDoc();
99     missingLogger = new ArrayList JavaDoc();
100     xslKeys = new ArrayList JavaDoc();
101     missingXslKeys = new ArrayList JavaDoc();
102     usedXslKeys = new ArrayList JavaDoc();
103     javaFiles = new ArrayList JavaDoc();
104
105     ignore.add("Translate.java");
106     getMissingTranslations();
107     analyseXsl();
108     analyseJava();
109   }
110
111   /**
112    * Computes the missing translations in the configuration file
113    *
114    * @return an <code>ArrayList</code> of tags where the associated
115    * translation is an empty string
116    */

117   private void getMissingTranslations()
118   {
119     ResourceBundle JavaDoc rb = ResourceBundle.getBundle(translationFile);
120     Enumeration JavaDoc e = rb.getKeys();
121     while (e.hasMoreElements())
122     {
123       String JavaDoc translation = (String JavaDoc) e.nextElement();
124       if (translation.trim().startsWith("##"))
125         continue;
126       configurationKeys.add(translation);
127       if (rb.getString(translation).equals(""))
128         missingTranslations.add(translation);
129     }
130   }
131
132   private void analyseXsl()
133   {
134     ResourceBundle JavaDoc keys = ResourceBundle.getBundle("c-jdbc-xsl");
135     for (Enumeration JavaDoc e = keys.getKeys(); e.hasMoreElements(); xslKeys.add(e
136         .nextElement()))
137     {
138       // the loop does everything
139
}
140     File JavaDoc f = new File JavaDoc(userDir + "/xml");
141     File JavaDoc[] xsls = f.listFiles(new FilenameFilter JavaDoc()
142     {
143       public boolean accept(File JavaDoc dir, String JavaDoc name)
144       {
145         if (name.endsWith(".xsl"))
146           return true;
147         else
148           return false;
149       }
150     });
151     for (int i = 0; i < xsls.length; i++)
152       analyseXslFile(xsls[i]);
153   }
154
155   private void analyseXslFile(File JavaDoc f)
156   {
157     //System.out.println("Analyse XSL file:" + f.getName());
158
analysedXslFile++;
159     try
160     {
161       BufferedReader JavaDoc br = new BufferedReader JavaDoc(new FileReader JavaDoc(f));
162       String JavaDoc read = "";
163       String JavaDoc oi18n = "<i18n>";
164       String JavaDoc ci18n = "</i18n>";
165       int oi18nl = oi18n.length();
166       int ci18nl = oi18nl + 1;
167       String JavaDoc i18n = "";
168       while ((read = br.readLine()) != null)
169       {
170         int indexOpen = 0, indexClose = 0;
171         while ((indexOpen = read.indexOf(oi18n)) != -1)
172         {
173           indexClose = read.indexOf(ci18n);
174           i18n = read.substring(indexOpen + oi18nl, indexClose).trim();
175           if (xslKeys.contains(i18n) == false)
176           {
177             missingXslKeys.add(i18n);
178           }
179           else
180             usedXslKeys.add(i18n);
181           read = read.substring(0, indexOpen) + i18n
182               + read.substring(indexClose + ci18nl);
183         }
184       }
185     }
186     catch (Exception JavaDoc e)
187     {
188       e.printStackTrace();
189     }
190
191   }
192
193   /**
194    * Starts analysing the java code
195    */

196   private void analyseJava()
197   {
198     File JavaDoc f = new File JavaDoc(userDir + "/src/org");
199     // use f as root directory
200
searchForKeys(f);
201   }
202
203   /**
204    * Recursivly searches the file target for java files
205    *
206    * @param target file to search into
207    */

208   private void searchForKeys(File JavaDoc target)
209   {
210     if (target.isDirectory())
211     {
212       File JavaDoc[] list = target.listFiles();
213       for (int i = 0; i < list.length; i++)
214         searchForKeys(list[i]);
215     }
216     else
217     {
218       String JavaDoc path = target.getAbsolutePath();
219       if (path.indexOf("CVS") == -1
220           && (path.indexOf("controller") != -1 || path.indexOf("console") != -1))
221       {
222         if (target.getName().endsWith(".java"))
223           searchJavaFile(target);
224       }
225     }
226   }
227
228   /**
229    * Should indicate whether the tag is valid or not
230    *
231    * @param tmp taken from java file after translate tag has been found
232    * @return true if tmp contains no '+' sign, false otherwise We could use
233    * regexp in the near future for better guessing
234    */

235   private boolean isValidTranslateTag(String JavaDoc tmp)
236   {
237     return (tmp.indexOf('+') == -1);
238   }
239
240   /**
241    * We found a valid tag so let's take the key from it
242    *
243    * @param tmp string that could be a key
244    * @return true if tmp was a key and was added to the javaKeys list, false if
245    * we should further process it.
246    */

247   private boolean processValidTag(String JavaDoc tmp)
248   {
249     if ((tmp.charAt(0) == '\"') && (tmp.charAt(tmp.length() - 1) == '\"'))
250     {
251       javaKeys.add(tmp.substring(1, tmp.length() - 1));
252       return true;
253     }
254     else
255       return false;
256   }
257
258   /**
259    * We found an invalid tag, so let's store it.
260    *
261    * @param target file where it was found
262    * @param tmp the tag content
263    * @param line where the invalid tag was found
264    */

265   private void processUnValidTag(String JavaDoc description, File JavaDoc target, String JavaDoc tmp,
266       int line)
267   {
268     invalidKeys.add(new InvalidTag(description, target.getName(), tmp, line));
269   }
270
271   /**
272    * We have found a java file. We first check it is not in the ignore list and
273    * then we look for the Translate.get string
274    *
275    * @param target file to process
276    */

277   private void searchJavaFile(File JavaDoc target)
278   {
279     if (ignore.contains(target.getName()))
280       return;
281     analyseJavaFile++;
282     javaFiles.add(target.getName());
283
284     String JavaDoc twoLinesBefore = "";
285     String JavaDoc previousLine = "";
286     String JavaDoc read = "";
287     String JavaDoc tmp = "";
288     try
289     {
290       BufferedReader JavaDoc br = new BufferedReader JavaDoc(new FileReader JavaDoc(target));
291       int index = -1;
292       int line = 0;
293       while ((read = br.readLine()) != null)
294       {
295         line++;
296         // look for exception tag
297
if (read.indexOf(EXCEPTION_TAG) != -1)
298         {
299           while (read.indexOf(';') == -1)
300           {
301             read = read.trim() + br.readLine().trim();
302             line++;
303           }
304           if (read.indexOf(TRANSLATE_TAG) == -1)
305           {
306             if (read.indexOf(EXCEPTION_TAG + "\"") != -1)
307             {
308               if (previousLine.indexOf(TRANSLATE_TAG) == -1
309                   && twoLinesBefore.indexOf(TRANSLATE_TAG) == -1)
310                 missingLogger.add(new InvalidTag("EXCEPTION", target.getName(),
311                     System.getProperty("line.separator")
312                         + twoLinesBefore.trim()
313                         + System.getProperty("line.separator")
314                         + previousLine.trim()
315                         + System.getProperty("line.separator") + read.trim(),
316                     line));
317             }
318             else
319             {
320               // Exception method so don't count it
321
}
322           }
323         }
324
325         // look for logger tag
326
if (read.indexOf(LOGGER_TAG) != -1)
327         {
328           while (read.indexOf(';') == -1)
329           {
330             read = read.trim() + br.readLine().trim();
331             line++;
332           }
333           if (read.indexOf(TRANSLATE_TAG) == -1
334               && read.indexOf(LOGGER_TEST) == -1)
335           {
336             // Can be like:
337
// String msg = Translate.get(...
338
// so check the lines before
339
if (previousLine.indexOf(TRANSLATE_TAG) == -1
340                 && twoLinesBefore.indexOf(TRANSLATE_TAG) == -1)
341               missingLogger.add(new InvalidTag("LOGGER", target.getName(),
342                   System.getProperty("line.separator") + twoLinesBefore.trim()
343                       + System.getProperty("line.separator")
344                       + previousLine.trim()
345                       + System.getProperty("line.separator") + read.trim(),
346                   line));
347           }
348         }
349
350         // Look for translation tag
351
if ((index = read.indexOf(TRANSLATE_TAG)) != -1)
352         {
353           while (read.indexOf(';') == -1)
354           {
355             read = read.trim() + br.readLine().trim();
356             line++;
357           }
358           read = read.trim();
359           index = read.indexOf(TRANSLATE_TAG);
360           tmp = read.substring(index + TRANSLATE_TAG_LENGTH);
361           tmp = tmp.trim();
362
363           if (tmp.indexOf(",") != -1)
364             tmp = tmp.substring(0, tmp.indexOf(","));
365           if (tmp.lastIndexOf(')') > tmp.length() - 4)
366           {
367             try
368             {
369               tmp = tmp.substring(0, tmp.indexOf(')', tmp.length() - 4));
370             }
371             catch (Exception JavaDoc e)
372             {
373               System.out.println("-->Substring failed in :" + target.getName());
374             }
375           }
376
377           boolean valid = isValidTranslateTag(tmp);
378
379           if (valid && processValidTag(tmp))
380           {
381             // There is nothing to do. proceeValidTag add the tag if possible.
382
// if failed, if to else section
383
}
384           else
385             processUnValidTag("TRANSLATE", target, tmp, line);
386         }
387         twoLinesBefore = previousLine;
388         previousLine = read;
389       }
390     }
391     catch (Exception JavaDoc ignore)
392     {
393       ignore.printStackTrace();
394     }
395   }
396
397   /**
398    * @return Returns the configurationKeys.
399    */

400   public ArrayList JavaDoc getConfigurationKeys()
401   {
402     return configurationKeys;
403   }
404
405   /**
406    * @return Returns the javaKeys.
407    */

408   public ArrayList JavaDoc getJavaKeys()
409   {
410     return javaKeys;
411   }
412
413   /**
414    * @return Returns the invalidKeys.
415    */

416   public ArrayList JavaDoc getinvalidKeys()
417   {
418     return invalidKeys;
419   }
420
421   /**
422    * We need to know if all the valid keys in the java code are in the
423    * configuration file
424    *
425    * @return true if all are there... will probably be false at this stage
426    * though !
427    */

428   public boolean isTranslationUpToDate()
429   {
430     return configurationKeys.containsAll(javaKeys);
431   }
432
433   /**
434    * Display the state of translation work
435    */

436   public void displayTranslationState()
437   {
438     System.out.println("Translation up to date:" + isTranslationUpToDate());
439     System.out.println("### UNUSED XSL KEYS FROM PROPERTIES ###");
440     ArrayList JavaDoc xslNotUsed = (ArrayList JavaDoc) xslKeys.clone();
441     xslNotUsed.removeAll(usedXslKeys);
442     for (int i = 0; i < xslNotUsed.size(); i++)
443       System.out.println(xslNotUsed.get(i));
444     System.out.println("### MISSING XSL KEYS (" + missingXslKeys.size()
445         + ") ###");
446     for (int i = 0; i < missingXslKeys.size(); i++)
447       System.out.println(missingXslKeys.get(i));
448     System.out.println("### MISSING TRANSLATIONS ("
449         + missingTranslations.size() + ") ###");
450     for (int i = 0; i < missingTranslations.size(); i++)
451       System.out.println(missingTranslations.get(i));
452     System.out
453         .println("### ANALYSED JAVA FILES (" + javaFiles.size() + ") ###");
454     //for (int i = 0; i < javaFiles.size(); i++)
455
// System.out.println(javaFiles.get(i));
456
System.out.println("### INVALID TAGS IN JAVA (" + invalidKeys.size()
457         + ") ###");
458     for (int i = 0; i < invalidKeys.size(); i++)
459     {
460       InvalidTag tag = (InvalidTag) (invalidKeys.get(i));
461       System.out.println("[" + tag.file + ":line:" + tag.line + "]:" + tag.tmp);
462     }
463     System.out.println("### MISSING TAGS IN CONFIGURATION ###");
464     int count = 0;
465     for (int i = 0; i < javaKeys.size(); i++)
466     {
467       String JavaDoc key = (String JavaDoc) javaKeys.get(i);
468       if (!configurationKeys.contains(key))
469       {
470         System.out.println(key);
471         count++;
472       }
473     }
474     System.out.println("\t ### TOTAL MISSING TAGS " + count);
475     System.out.println("### MISSING TAGS IN JAVA ###");
476     int count2 = 0;
477     for (int i = 0; i < configurationKeys.size(); i++)
478     {
479       String JavaDoc key = (String JavaDoc) configurationKeys.get(i);
480       if (!javaKeys.contains(key))
481       {
482         count2++;
483         System.out.println(key);
484       }
485     }
486     System.out.println("\t ### TOTAL MISSING TAGS " + count2);
487     System.out.println("### MISSING LOGGERS IN JAVA (" + missingLogger.size()
488         + ") ###");
489     for (int i = 0; i < missingLogger.size(); i++)
490     {
491       InvalidTag tag = (InvalidTag) (missingLogger.get(i));
492       System.out.println(" %%% [" + tag.description + "][" + tag.file
493           + ":line:" + tag.line + "] %%%" + tag.tmp);
494     }
495     System.out.println("*******************************************");
496     System.out.println("************** SUMMARY ********************");
497     System.out.println("### NUMBER OF ANALYSED XSL FILES:" + analysedXslFile);
498     System.out.println("### NUMBER OF ANALYSED JAVA FILES:" + analyseJavaFile);
499     System.out.println("### UNUSED XSL KEYS FROM PROPERTIES ("
500         + xslNotUsed.size() + ") ###");
501     System.out.println("### MISSING XSL KEYS (" + missingXslKeys.size()
502         + ") ###");
503     System.out.println("### MISSING TRANSLATION STRINGS IN PROPERTY FILE ("
504         + missingTranslations.size() + ") ###");
505     System.out.println("### INVALID TAGS IN JAVA (" + invalidKeys.size()
506         + ") ###");
507     System.out.println("### MISSING CONFIGURATION TAGS " + count);
508     System.out.println("### MISSING JAVA TAGS " + count2);
509     System.out.println("### MISSING LOGGERS IN JAVA (" + missingLogger.size()
510         + ") ###");
511     System.out.println("*******************************************");
512   }
513
514 }
Popular Tags