1 34 87 89 import java.io.File ; 90 import java.io.FileInputStream ; 91 import java.io.IOException ; 92 import java.io.BufferedReader ; 93 import java.io.InputStreamReader ; 94 import java.io.FileOutputStream ; 95 import java.io.BufferedWriter ; 96 import java.io.OutputStreamWriter ; 97 import java.util.Vector ; 98 import java.util.Hashtable ; 99 import java.util.Locale ; 100 import org.apache.tools.ant.BuildException; 101 import org.apache.tools.ant.Project; 102 import org.apache.tools.ant.DirectoryScanner; 103 import org.apache.tools.ant.types.FileSet; 104 import org.apache.tools.ant.util.FileUtils; 105 import org.apache.tools.ant.taskdefs.MatchingTask; 106 107 112 public class TranslateToJSTL extends MatchingTask { 113 114 117 private String bundle; 118 121 private String bundleLanguage; 122 125 private String bundleCountry; 126 129 private String bundleVariant; 130 133 private File toDir; 134 137 private String srcEncoding; 138 141 private String destEncoding; 142 145 private String bundleEncoding; 146 149 private String startToken; 150 153 private String endToken; 154 158 private boolean forceOverwrite; 159 162 private Vector filesets = new Vector (); 163 166 private Hashtable resourceMap = new Hashtable (); 167 170 private Locale locale; 171 174 private FileUtils fileUtils = FileUtils.newFileUtils(); 175 178 private long[] bundleLastModified = new long[7]; 179 182 private long srcLastModified; 183 186 private long destLastModified; 187 190 private boolean loaded = false; 191 192 195 public void setBundle(String bundle) { 196 this.bundle = bundle; 197 } 198 199 202 public void setBundleLanguage(String bundleLanguage) { 203 this.bundleLanguage = bundleLanguage; 204 } 205 206 209 public void setBundleCountry(String bundleCountry) { 210 this.bundleCountry = bundleCountry; 211 } 212 213 216 public void setBundleVariant(String bundleVariant) { 217 this.bundleVariant = bundleVariant; 218 } 219 220 223 public void setToDir(File toDir) { 224 this.toDir = toDir; 225 } 226 227 230 public void setStartToken(String startToken) { 231 this.startToken = startToken; 232 } 233 234 237 public void setEndToken(String endToken) { 238 this.endToken = endToken; 239 } 240 241 245 public void setSrcEncoding(String srcEncoding) { 246 this.srcEncoding = srcEncoding; 247 } 248 249 253 public void setDestEncoding(String destEncoding) { 254 this.destEncoding = destEncoding; 255 } 256 257 261 public void setBundleEncoding(String bundleEncoding) { 262 this.bundleEncoding = bundleEncoding; 263 } 264 265 271 public void setForceOverwrite(boolean forceOverwrite) { 272 this.forceOverwrite = forceOverwrite; 273 } 274 275 278 public void addFileset(FileSet set) { 279 filesets.addElement(set); 280 } 281 282 285 public void execute() throws BuildException { 286 if (bundle == null) { 287 throw new BuildException("The bundle attribute must be set.", 288 getLocation()); 289 290 } 291 292 if (startToken == null) { 293 throw new BuildException("The starttoken attribute must be set.", 294 getLocation()); 295 } 296 297 if (startToken.length() != 1) { 298 throw new BuildException( 299 "The starttoken attribute must be a single character.", 300 getLocation()); 301 } 302 303 if (endToken == null) { 304 throw new BuildException("The endtoken attribute must be set.", 305 getLocation()); 306 } 307 308 if (endToken.length() != 1) { 309 throw new BuildException( 310 "The endtoken attribute must be a single character.", 311 getLocation()); 312 } 313 314 if (bundleLanguage == null) { 315 Locale l = Locale.getDefault(); 316 bundleLanguage = l.getLanguage(); 317 } 318 319 if (bundleCountry == null) { 320 bundleCountry = Locale.getDefault().getCountry(); 321 } 322 323 locale = new Locale (bundleLanguage, bundleCountry); 324 325 if (bundleVariant == null) { 326 Locale l = new Locale (bundleLanguage, bundleCountry); 327 bundleVariant = l.getVariant(); 328 } 329 330 if (toDir == null) { 331 throw new BuildException("The todir attribute must be set.", 332 getLocation()); 333 } 334 335 if (!toDir.exists()) { 336 toDir.mkdirs(); 337 } else { 338 if (toDir.isFile()) { 339 throw new BuildException(toDir + " is not a directory"); 340 } 341 } 342 343 if (srcEncoding == null) { 344 srcEncoding = System.getProperty("file.encoding"); 345 } 346 347 if (destEncoding == null) { 348 destEncoding = srcEncoding; 349 } 350 351 if (bundleEncoding == null) { 352 bundleEncoding = srcEncoding; 353 } 354 355 loadResourceMaps(); 356 357 translate(); 358 } 359 360 379 private void loadResourceMaps() throws BuildException { 380 Locale locale = new Locale (bundleLanguage, 381 bundleCountry, 382 bundleVariant); 383 String language = locale.getLanguage().length() > 0 ? 384 "_" + locale.getLanguage() : 385 ""; 386 String country = locale.getCountry().length() > 0 ? 387 "_" + locale.getCountry() : 388 ""; 389 String variant = locale.getVariant().length() > 0 ? 390 "_" + locale.getVariant() : 391 ""; 392 String bundleFile = bundle + language + country + variant; 393 processBundle(bundleFile, 0, false); 394 395 bundleFile = bundle + language + country; 396 processBundle(bundleFile, 1, false); 397 398 bundleFile = bundle + language; 399 processBundle(bundleFile, 2, false); 400 401 bundleFile = bundle; 402 processBundle(bundleFile, 3, false); 403 404 locale = Locale.getDefault(); 407 408 language = locale.getLanguage().length() > 0 ? 409 "_" + locale.getLanguage() : 410 ""; 411 country = locale.getCountry().length() > 0 ? 412 "_" + locale.getCountry() : 413 ""; 414 variant = locale.getVariant().length() > 0 ? 415 "_" + locale.getVariant() : 416 ""; 417 bundleEncoding = System.getProperty("file.encoding"); 418 419 bundleFile = bundle + language + country + variant; 420 processBundle(bundleFile, 4, false); 421 422 bundleFile = bundle + language + country; 423 processBundle(bundleFile, 5, false); 424 425 bundleFile = bundle + language; 426 processBundle(bundleFile, 6, true); 427 } 428 429 432 private void processBundle(final String bundleFile, final int i, 433 final boolean checkLoaded) throws BuildException { 434 final File propsFile = new File (bundleFile + ".properties"); 435 FileInputStream ins = null; 436 try { 437 ins = new FileInputStream (propsFile); 438 loaded = true; 439 bundleLastModified[i] = propsFile.lastModified(); 440 log("Using " + propsFile, Project.MSG_DEBUG); 441 loadResourceMap(ins); 442 } catch (IOException ioe) { 443 log(propsFile + " not found.", Project.MSG_DEBUG); 444 if (!loaded && checkLoaded) { 448 throw new BuildException(ioe.getMessage(), getLocation()); 449 } 450 } 451 } 452 453 457 private void loadResourceMap(FileInputStream ins) throws BuildException { 458 try { 459 BufferedReader in = null; 460 InputStreamReader isr = new InputStreamReader (ins, bundleEncoding); 461 in = new BufferedReader (isr); 462 String line = null; 463 while ((line = in.readLine()) != null) { 464 if (line.trim().length() > 1 && 466 ('#' != line.charAt(0) || '!' != line.charAt(0))) { 467 int sepIndex = line.indexOf('='); 469 if (-1 == sepIndex) { 470 sepIndex = line.indexOf(':'); 471 } 472 if (-1 == sepIndex) { 473 for (int k = 0; k < line.length(); k++) { 474 if (Character.isSpaceChar(line.charAt(k))) { 475 sepIndex = k; 476 break; 477 } 478 } 479 } 480 if (-1 != sepIndex) { 482 String key = line.substring(0, sepIndex).trim(); 483 String value = line.substring(sepIndex + 1).trim(); 484 while (value.endsWith("\\")) { 486 value = value.substring(0, value.length() - 1); 487 if ((line = in.readLine()) != null) { 488 value = value + line.trim(); 489 } else { 490 break; 491 } 492 } 493 if (key.length() > 0) { 494 if (resourceMap.get(key) == null) { 496 resourceMap.put(key, value); 497 } 498 } 499 } 500 } 501 } 502 if (in != null) { 503 in.close(); 504 } 505 } catch (IOException ioe) { 506 throw new BuildException(ioe.getMessage(), getLocation()); 507 } 508 } 509 510 522 private void translate() throws BuildException { 523 for (int i = 0; i < filesets.size(); i++) { 524 FileSet fs = (FileSet) filesets.elementAt(i); 525 DirectoryScanner ds = fs.getDirectoryScanner(getProject()); 526 String [] srcFiles = ds.getIncludedFiles(); 527 int srcFilesCount = srcFiles.length; 528 if (srcFilesCount == 1) { 529 log("Translating 1 file", Project.MSG_INFO); 530 } else { 531 log("Translating " + srcFilesCount + " files", Project.MSG_INFO); 532 } 533 for (int j = 0; j < srcFiles.length; j++) { 534 try { 535 File dest = fileUtils.resolveFile(toDir, srcFiles[j]); 536 try { 538 File destDir = new File (dest.getParent()); 539 if (!destDir.exists()) { 540 destDir.mkdirs(); 541 } 542 } catch (Exception e) { 543 log("Exception occured while trying to check/create " 544 + " parent directory. " + e.getMessage(), 545 Project.MSG_DEBUG); 546 } 547 destLastModified = dest.lastModified(); 548 File src = fileUtils.resolveFile(ds.getBasedir(), srcFiles[j]); 549 srcLastModified = src.lastModified(); 550 if (forceOverwrite 552 || destLastModified < srcLastModified 553 || destLastModified < bundleLastModified[0] 554 || destLastModified < bundleLastModified[1] 555 || destLastModified < bundleLastModified[2] 556 || destLastModified < bundleLastModified[3] 557 || destLastModified < bundleLastModified[4] 558 || destLastModified < bundleLastModified[5] 559 || destLastModified < bundleLastModified[6]) { 560 log("Processing " + srcFiles[j], 561 Project.MSG_DEBUG); 562 FileOutputStream fos = new FileOutputStream (dest); 563 BufferedWriter out 564 = new BufferedWriter (new OutputStreamWriter (fos, destEncoding)); 565 FileInputStream fis = new FileInputStream (src); 566 BufferedReader in 567 = new BufferedReader (new InputStreamReader (fis, srcEncoding)); 568 String line; 569 while ((line = in.readLine()) != null) { 570 int startIndex = -1; 571 int endIndex = -1; 572 outer: while (true) { 573 startIndex = line.indexOf(startToken, endIndex + 1); 574 if (startIndex < 0 || 575 startIndex + 1 >= line.length()) { 576 break; 577 } 578 endIndex = line.indexOf(endToken, startIndex + 1); 579 if (endIndex < 0) { 580 break; 581 } 582 String matches = line.substring(startIndex + 1, 583 endIndex); 584 590 if (matches.startsWith("mvnforum.") == false ) { 591 endIndex = endIndex - 1; 592 continue outer; 593 } 594 for (int k = 0; k < matches.length(); k++) { 597 char c = matches.charAt(k); 598 if (c == ':' || 599 c == '=' || 600 Character.isSpaceChar(c)) { 601 endIndex = endIndex - 1; 602 continue outer; 603 } 604 } 605 String replace = null; 606 replace = "<fmt:message key=\"" + matches + "\"/>"; 608 if (replace == null) { 611 log("Warning: The key: " + matches 612 + " hasn't been defined.", 613 Project.MSG_WARN); 614 replace = matches; 615 } 616 line = line.substring(0, startIndex) 617 + replace 618 + line.substring(endIndex + 1); 619 endIndex = startIndex + replace.length() - 1; 622 if (endIndex + 1 >= line.length()) { 623 break; 624 } 625 } 626 out.write(line); 627 out.newLine(); 628 } 629 if (in != null) { 630 in.close(); 631 } 632 if (out != null) { 633 out.close(); 634 } 635 } else { 636 log("Skipping " + srcFiles[j] + 637 " as destination file is up to date", 638 Project.MSG_VERBOSE); 639 } 640 } catch (IOException ioe) { 641 throw new BuildException(ioe.getMessage(), getLocation()); 642 } 643 } 644 } 645 } 646 } 647 | Popular Tags |