1 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 ; 74 import java.io.File ; 75 import java.io.FileReader ; 76 import java.io.IOException ; 77 import java.util.ArrayList ; 78 import java.util.HashMap ; 79 import java.util.Iterator ; 80 81 82 86 public class XlateUtil { 87 private static final int DEFAULT_HASHMAP_SPACE = 600; 88 private static HashMap english = new HashMap (DEFAULT_HASHMAP_SPACE); 89 private static HashMap otherLang = new HashMap (DEFAULT_HASHMAP_SPACE); 90 private static PatternCompiler compiler = new Perl5Compiler(); 91 private static PatternMatcher matcher = new Perl5Matcher(); 92 93 98 public static void main(String [] args) 99 throws Exception { 100 if (args.length == 0) { 101 usage(); 102 103 return; 104 } 105 106 String theDir = args[0]; 107 108 if (args.length < 2) { 109 compare(theDir); 110 111 return; 112 } 113 114 String theLang = args[1]; 115 System.out.println("Comparing " + theLang + " to default:"); 116 compare(theDir, theLang); 117 } 118 119 120 123 public static void usage() { 124 System.out.println("Usage: "); 125 System.out.println("\tXlateUtil <Message Bundle Directory> [language to inspect]"); 126 } 127 128 131 public static void compare(String theDir) 132 throws Exception , IOException { 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 dir = new java.io.File (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 [] fileList = dir.listFiles(); 148 int matchCount = 0; 149 150 for (int i = 0; i < fileList.length; i++) { 151 152 boolean result; 154 synchronized (matcher) { 155 result = matcher.matches(fileList[i].getName(), p); 156 } 157 158 if (result) { 159 BufferedReader other = new BufferedReader (new FileReader (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 177 public static void loadDefaultBundle(String theDir) 178 throws Exception { 179 BufferedReader en = new BufferedReader (new FileReader (theDir + "/MessagesBundle.properties")); 180 String oneKey = null; 181 String oneString = null; 182 String 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 ) 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 215 public static void compareOneFile(BufferedReader other, String fileName) 216 throws Exception { 217 otherLang.clear(); 218 System.out.println("\n\n-----------------------------------------------------"); 219 System.out.println("Comparing Against: " + fileName); 220 221 String oneKey = null; 222 String oneString = null; 223 String 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 244 String englishString = null; 245 String otherString = null; 246 ArrayList newKeys = new ArrayList (); 247 ArrayList identicalKeys = new ArrayList (); 248 249 for (Iterator eachKey = english.keySet().iterator(); 250 eachKey.hasNext();) { 251 oneKey = (String ) eachKey.next(); 252 englishString = (String ) english.get(oneKey); 253 otherString = (String ) 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 } 263 264 if (newKeys.size() > 0) { 265 System.out.println("New Keys Requiring Translation:\n\n"); 266 267 for (Iterator i = newKeys.iterator(); i.hasNext();) { 268 System.out.println((String ) 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 e = identicalKeys.iterator(); e.hasNext();) { 276 System.out.println((String ) 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 294 public static void compare(String theDir, String theLang) 295 throws Exception { 296 BufferedReader other = new BufferedReader (new FileReader (theDir + "/MessagesBundle_" + 297 theLang + 298 ".properties")); 299 compareOneFile(other, 300 theDir + "/MessagesBundle_" + theLang + ".properties"); 301 } 302 303 304 } | Popular Tags |