1 34 87 89 import java.io.BufferedReader ; 90 import java.io.File ; 91 import java.io.FileInputStream ; 92 import java.io.IOException ; 93 import java.io.InputStreamReader ; 94 import java.util.Hashtable ; 95 import java.util.Vector ; 96 import org.apache.tools.ant.BuildException; 97 import org.apache.tools.ant.DirectoryScanner; 98 import org.apache.tools.ant.Project; 99 import org.apache.tools.ant.taskdefs.MatchingTask; 100 import org.apache.tools.ant.types.FileSet; 101 import org.apache.tools.ant.util.FileUtils; 102 103 107 public class LocalizingKeyFinder extends MatchingTask { 108 111 private String bundle; 112 115 private String startToken; 116 119 private String endToken; 120 123 private String bundleEncoding; 124 127 private Vector filesets = new Vector (); 128 131 private Hashtable resourceMap = new Hashtable (); 132 135 private FileUtils fileUtils = FileUtils.newFileUtils(); 136 139 private boolean loaded = false; 140 143 public void setBundle(String bundle) { 144 this.bundle = bundle; 145 } 146 149 public void setStartToken(String startToken) { 150 this.startToken = startToken; 151 } 152 155 public void setEndToken(String endToken) { 156 this.endToken = endToken; 157 } 158 162 public void setBundleEncoding(String bundleEncoding) { 163 this.bundleEncoding = bundleEncoding; 164 } 165 168 public void addFileset(FileSet set) { 169 filesets.addElement(set); 170 } 171 174 public void execute() throws BuildException { 175 if (bundle == null) { 176 throw new BuildException("The bundle attribute must be set.", 177 getLocation()); 178 } 179 if (startToken == null) { 180 throw new BuildException("The starttoken attribute must be set.", 181 getLocation()); 182 } 183 if (endToken == null) { 184 throw new BuildException("The endtoken attribute must be set.", 185 getLocation()); 186 } 187 if (bundleEncoding == null) { 188 bundleEncoding = "Cp1252"; 189 } 190 191 loadResourceMaps(); 192 translate(); 193 } 194 213 private void loadResourceMaps() throws BuildException { 214 processBundle(bundle, true); 215 } 216 219 private void processBundle(final String bundleFile, 220 final boolean checkLoaded) throws BuildException { 221 final File propsFile = new File (bundleFile + ".properties"); 222 log(propsFile + "[ BUNDLE PROPERTIES FILE ]"); 223 FileInputStream ins = null; 224 try { 225 ins = new FileInputStream (propsFile); 226 loaded = true; 227 log("Using " + propsFile, Project.MSG_DEBUG); 228 loadResourceMap(ins); 229 } catch (IOException ioe) { 230 log(propsFile + " not found.", Project.MSG_DEBUG); 231 if (!loaded && checkLoaded) { 232 throw new BuildException(ioe.getMessage()); 233 } 234 } 235 } 236 240 private void loadResourceMap(FileInputStream ins) throws BuildException { 241 try { 242 BufferedReader in = null; 243 InputStreamReader isr = new InputStreamReader (ins, bundleEncoding); 244 in = new BufferedReader (isr); 245 String line = null; 246 while ((line = in.readLine()) != null) { 247 if (line.trim().length() > 1 249 && ('#' != line.charAt(0) || '!' != line.charAt(0))) { 250 int sepIndex = line.indexOf('='); 252 if (-1 == sepIndex) { 253 sepIndex = line.indexOf(':'); 254 } 255 if (-1 == sepIndex) { 256 for (int k = 0; k < line.length(); k++) { 257 if (Character.isSpaceChar(line.charAt(k))) { 258 sepIndex = k; 259 break; 260 } 261 } 262 } 263 if (-1 != sepIndex) { 265 String key = line.substring(0, sepIndex).trim(); 266 String value = line.substring(sepIndex + 1).trim(); 267 while (value.endsWith("\\")) { 269 value = value.substring(0, value.length() - 1); 270 if ((line = in.readLine()) != null) { 271 value = value + line.trim(); 272 } else { 273 break; 274 } 275 } 276 if (key.length() > 0) { 277 if (resourceMap.get(key) == null) { 279 resourceMap.put(key, value); 280 } 281 } 282 } 283 } 284 } 285 if (in != null) { 286 in.close(); 287 } 288 } catch (IOException ioe) { 289 throw new BuildException(ioe.getMessage()); 290 } 291 } 292 302 private void translate() throws BuildException { 303 for (int i = 0; i < filesets.size(); i++) { 304 FileSet fs = (FileSet) filesets.elementAt(i); 305 DirectoryScanner ds = fs.getDirectoryScanner(getProject()); 306 String [] srcFiles = ds.getIncludedFiles(); 307 int srcFilesCount = srcFiles.length; 308 if (srcFilesCount == 1) { 309 log("Checking 1 file in " + fs.getDir(getProject()), Project.MSG_INFO ); 310 } else { 311 log("Checking " + srcFilesCount + " files in " + fs.getDir(getProject()), Project.MSG_INFO); 312 } 313 int bugCount = 0; 314 int warningCount = 0; 315 for (int j = 0; j < srcFiles.length; j++) { 316 try { 317 File src = fileUtils.resolveFile(ds.getBasedir(), 318 srcFiles[j]); 319 log("Processing " + srcFiles[j], Project.MSG_DEBUG); 320 FileInputStream fis = new FileInputStream (src); 321 BufferedReader in = new BufferedReader ( 322 new InputStreamReader (fis)); 323 String line; 324 int lineNumber = 0; 325 while ((line = in.readLine()) != null) { 326 lineNumber++; 327 int startIndex = -1; 328 int endIndex = -1; 329 outer : while (true) { 331 startIndex = line.indexOf(startToken, endIndex + 1); 332 if ((startIndex < 0) || (startIndex + 1 >= line.length()) ) { 333 break; 334 } 335 endIndex = line.indexOf(endToken, startIndex + 1); 336 if (endIndex < 0) { 337 log("\nWARNING: Line contains the start key " + 338 "but doesn't have the end key. ", Project.MSG_WARN); 339 log("WARNING: At line " + lineNumber + " in file : " + src, Project.MSG_WARN); 340 warningCount++; 341 break; 342 } 343 startIndex = line.indexOf("\"", startIndex); 344 if (startIndex < 0) { 345 if (line.indexOf("this.desc") < 0) { 347 log("WARNING: \"" + line + "\" does not have starting quote", Project.MSG_WARN); 348 warningCount++; 349 } 350 break; 351 } 352 endIndex = line.indexOf("\"", startIndex + 1); 353 if (endIndex < 0) { 354 log("WARNING: \"" + line + "\" does not have ending quote", Project.MSG_WARN); 356 warningCount++; 357 break; 358 } 359 String matches = line.substring(startIndex + 1, endIndex); 360 367 String replace = null; 368 replace = (String ) resourceMap.get(matches); 369 if (replace == null) { 371 bugCount++; 372 log("\nERROR: The key \"" + matches + "\" hasn't been defined.", Project.MSG_WARN); 373 log("ERROR: At line " + lineNumber + " in file : " + src, Project.MSG_WARN); 374 replace = matches; 375 } 376 line = line.substring(0, startIndex) + replace 377 + line.substring(endIndex + 1); 378 endIndex = startIndex + replace.length() - 1; 381 if (endIndex + 1 >= line.length()) { 382 break; 383 } 384 } 385 } 386 if (in != null) { 387 in.close(); 388 } 389 } catch (IOException ioe) { 390 throw new BuildException(ioe.getMessage(), getLocation()); 391 } 392 } 393 log("\n###########################################"); 394 log("Total error count: " + bugCount); 395 log("Total warning count: " + warningCount); 396 } 397 } 398 } | Popular Tags |