KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > core > utility > XlateUtil


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 package com.jcorporate.expresso.core.utility;
66
67 import org.apache.oro.text.regex.Pattern;
68 import org.apache.oro.text.regex.PatternCompiler;
69 import org.apache.oro.text.regex.PatternMatcher;
70 import org.apache.oro.text.regex.Perl5Compiler;
71 import org.apache.oro.text.regex.Perl5Matcher;
72
73 import java.io.BufferedReader JavaDoc;
74 import java.io.File JavaDoc;
75 import java.io.FileReader JavaDoc;
76 import java.io.IOException JavaDoc;
77 import java.util.ArrayList JavaDoc;
78 import java.util.HashMap JavaDoc;
79 import java.util.Iterator JavaDoc;
80
81
82 /**
83  * Simple utility to read the english and another language
84  * strings bundle and report where additional translation is required
85  */

86 public class XlateUtil {
87     private static final int DEFAULT_HASHMAP_SPACE = 600;
88     private static HashMap JavaDoc english = new HashMap JavaDoc(DEFAULT_HASHMAP_SPACE);
89     private static HashMap JavaDoc otherLang = new HashMap JavaDoc(DEFAULT_HASHMAP_SPACE);
90     private static PatternCompiler compiler = new Perl5Compiler();
91     private static PatternMatcher matcher = new Perl5Matcher();
92
93     /**
94      * Main method so that XlateUtil can be launched from a command line
95      *
96      * @param args[] Command line arguments
97      */

98     public static void main(String JavaDoc[] args)
99             throws Exception JavaDoc {
100         if (args.length == 0) {
101             usage();
102
103             return;
104         }
105
106         String JavaDoc theDir = args[0];
107
108         if (args.length < 2) {
109             compare(theDir);
110
111             return;
112         }
113
114         String JavaDoc theLang = args[1];
115         System.out.println("Comparing " + theLang + " to default:");
116         compare(theDir, theLang);
117     } /* main */
118
119
120     /**
121      * Returns a sample usage message in case of errors.
122      */

123     public static void usage() {
124         System.out.println("Usage: ");
125         System.out.println("\tXlateUtil <Message Bundle Directory> [language to inspect]");
126     }
127
128     /**
129      * Compares all MessagesBundles against all directories.
130      */

131     public static void compare(String JavaDoc theDir)
132             throws Exception JavaDoc, IOException JavaDoc {
133         System.out.println("Comparing all Messages Bundles in directory " +
134                 theDir);
135
136         Pattern p = compiler.compile("^(MessagesBundle_).[a-z]+(\\.properties)$", Perl5Compiler.READ_ONLY_MASK);
137         java.io.File JavaDoc dir = new java.io.File JavaDoc(theDir);
138
139         if (!dir.isDirectory()) {
140             System.err.println(theDir + " Is not a valid pathname");
141
142             return;
143         }
144
145         loadDefaultBundle(theDir);
146
147         File JavaDoc[] fileList = dir.listFiles();
148         int matchCount = 0;
149
150         for (int i = 0; i < fileList.length; i++) {
151
152             //If it matches then we go
153
boolean result;
154             synchronized (matcher) {
155                 result = matcher.matches(fileList[i].getName(), p);
156             }
157
158             if (result) {
159                 BufferedReader JavaDoc other = new BufferedReader JavaDoc(new FileReader JavaDoc(fileList[i]));
160                 compareOneFile(other, fileList[i].getName());
161                 matchCount++;
162             }
163         }
164         if (matchCount == 0) {
165             System.err.println("No MessagesBundle found in directory " +
166                     theDir);
167
168             return;
169         } else {
170             System.out.println("Matched Against : " + matchCount + " file(s)");
171         }
172     }
173
174     /**
175      * Loads the english bundle into memory for quick compare.
176      */

177     public static void loadDefaultBundle(String JavaDoc theDir)
178             throws Exception JavaDoc {
179         BufferedReader JavaDoc en = new BufferedReader JavaDoc(new FileReader JavaDoc(theDir + "/MessagesBundle.properties"));
180         String JavaDoc oneKey = null;
181         String JavaDoc oneString = null;
182         String JavaDoc oneLine = en.readLine();
183
184         while (oneLine != null) {
185             if (oneLine.length() > 0) {
186                 oneKey = oneLine.substring(0, oneLine.indexOf("="));
187                 oneString = oneLine.substring(oneLine.indexOf("=") + 1);
188
189                 if (english.containsKey(oneKey)) {
190                     if (oneString.equals((String JavaDoc) english.get(oneKey))) {
191                         System.out.println("Found duplicate resource line for key : " +
192                                 oneKey);
193                     } else {
194                         System.out.println("Found duplicate key with different values: " +
195                                 oneKey);
196                     }
197                 }
198
199                 english.put(oneKey, oneString);
200             }
201
202             oneLine = en.readLine();
203         }
204
205         System.out.println("Read " + english.size() +
206                 " messages from English Language Bundle");
207     }
208
209     /**
210      * Given a buffered reader, checks against the default language
211      *
212      * @param other The Buffered Reader of the other MessagesBundle*.properties
213      * file to read
214      */

215     public static void compareOneFile(BufferedReader JavaDoc other, String JavaDoc fileName)
216             throws Exception JavaDoc {
217         otherLang.clear();
218         System.out.println("\n\n-----------------------------------------------------");
219         System.out.println("Comparing Against: " + fileName);
220
221         String JavaDoc oneKey = null;
222         String JavaDoc oneString = null;
223         String JavaDoc oneLine = null;
224         oneLine = other.readLine();
225
226         while (oneLine != null) {
227             if (oneLine.length() != 0) {
228                 if (oneLine.charAt(0) != '#') {
229                     if (oneLine.indexOf("=") <= 0) {
230                         System.err.println("Invalid key in " + fileName +
231                                 " file:" + oneLine);
232                     } else {
233                         oneKey = oneLine.substring(0, oneLine.indexOf("="));
234                         oneString = oneLine.substring(oneLine.indexOf("=") + 1);
235                         otherLang.put(oneKey, oneString);
236                     }
237                 }
238             }
239
240             oneLine = other.readLine();
241         }
242
243         /* Now do the compares */
244         String JavaDoc englishString = null;
245         String JavaDoc otherString = null;
246         ArrayList JavaDoc newKeys = new ArrayList JavaDoc();
247         ArrayList JavaDoc identicalKeys = new ArrayList JavaDoc();
248
249         for (Iterator JavaDoc eachKey = english.keySet().iterator();
250              eachKey.hasNext();) {
251             oneKey = (String JavaDoc) eachKey.next();
252             englishString = (String JavaDoc) english.get(oneKey);
253             otherString = (String JavaDoc) otherLang.get(oneKey);
254
255             if (otherString == null) {
256                 newKeys.add(oneKey + "=" + englishString);
257             } else {
258                 if (otherString.equals(englishString)) {
259                     identicalKeys.add(oneKey + "=" + otherString);
260                 }
261             }
262         } /* for each key */
263
264         if (newKeys.size() > 0) {
265             System.out.println("New Keys Requiring Translation:\n\n");
266
267             for (Iterator JavaDoc i = newKeys.iterator(); i.hasNext();) {
268                 System.out.println((String JavaDoc) i.next());
269             }
270         }
271         if (identicalKeys.size() > 0) {
272             System.out.println("\n\n------------------------------\n\n");
273             System.out.println("Keys Identical in both files - translation may be required:\n\n");
274
275             for (Iterator JavaDoc e = identicalKeys.iterator(); e.hasNext();) {
276                 System.out.println((String JavaDoc) e.next());
277             }
278         }
279
280         System.out.println("Read " + otherLang.size() +
281                 " messages from Language Bundle");
282
283         if (newKeys.size() == 0 && identicalKeys.size() == 0) {
284             System.out.println("\tChecking Language File Complete... No Changes Necessary");
285         }
286     }
287
288     /**
289      * Compares the files for a particular language.
290      *
291      * @param theDir The directory to look in.
292      * @param theLang The language to check
293      */

294     public static void compare(String JavaDoc theDir, String JavaDoc theLang)
295             throws Exception JavaDoc {
296         BufferedReader JavaDoc other = new BufferedReader JavaDoc(new FileReader JavaDoc(theDir + "/MessagesBundle_" +
297                 theLang +
298                 ".properties"));
299         compareOneFile(other,
300                 theDir + "/MessagesBundle_" + theLang + ".properties");
301     } /* compare */
302
303
304 }
Popular Tags