1 19 20 package org.netbeans.nbbuild; 21 22 import java.io.BufferedReader ; 23 import java.io.File ; 24 import java.io.FileReader ; 25 import java.io.FileWriter ; 26 import java.io.IOException ; 27 import java.util.Enumeration ; 28 import java.util.HashSet ; 29 import java.util.Hashtable ; 30 import java.util.Iterator ; 31 import java.util.StringTokenizer ; 32 import java.util.Vector ; 33 import org.apache.tools.ant.BuildException; 34 import org.apache.tools.ant.DirectoryScanner; 35 import org.apache.tools.ant.Project; 36 import org.apache.tools.ant.taskdefs.Delete; 37 import org.apache.tools.ant.taskdefs.GZip; 38 import org.apache.tools.ant.taskdefs.MatchingTask; 39 import org.apache.tools.ant.taskdefs.Tar; 40 import org.apache.tools.ant.types.FileSet; 41 import org.netbeans.nbbuild.utils.cvsutils.CvsEntries; 42 43 86 public class L10nTask extends MatchingTask { 87 private String [] modules; 88 private String [] topdirs; 89 private String localizableFile, generatedFile, changedFile,globalFile; 90 private String allFile = "l10n.list.all"; 91 private String excludePattern = ""; 92 private String includePattern = ""; 93 private String buildDir; 98 private String distDir; 99 private Vector error = new Vector (); 100 private Hashtable <String , CvsEntries> cvsEntriesCache=new Hashtable <String , CvsEntries>(); 101 private Hashtable <String , String > changed = new Hashtable <String , String >(); 102 private Hashtable <String , String > generatedFileHash = new Hashtable <String , String >(); 103 private Hashtable <String , String > fullPropHash = null ; 104 105 private Project p; 106 private boolean readGlobalFile=false; private StringBuffer [] globalsbholder= new StringBuffer [2]; 108 static boolean DEBUG = false; 109 boolean cont=false; 110 private String buildNumber; 111 String [] localizableFiles; 112 113 public void execute() throws BuildException { 114 117 if (modules == null ) { 118 throw new BuildException("Required variable not set. Set 'modules' in the calling .xml file"); 119 } 120 if (topdirs == null ) { 121 throw new BuildException("Required variable not set. Set 'topdirs' in the calling .xml file"); 122 } 123 if (distDir == null ) { 124 throw new BuildException("Required variable not set. Set 'distDir' in the calling .xml file"); 125 } 126 127 if (buildDir == null ) { 128 throw new BuildException("Required variable not set. Set 'buildDir' in the calling .xml file"); 129 } 130 if (localizableFile == null ) { 131 throw new BuildException("Required variable not set. Set 'localizableFile' in the calling .xml file"); 132 } 133 if (generatedFile == null ) { 134 throw new BuildException("Required variable not set. Set 'generatedFile' in the calling .xml file"); 135 } 136 if (changedFile == null ) { 137 throw new BuildException("Required variable not set. Set 'changedFile' in the calling .xml file"); 138 } 139 if (buildNumber == null ) { 140 throw new BuildException("Required variable not set. Set 'buildNumber' in the calling .xml file"); 141 } 142 143 File dd = new File (distDir); 146 if ( ! dd.exists() ) { 147 dd.mkdirs(); 148 } 149 150 System.out.print("topdirs:\t"); 151 for (int j=0; j<topdirs.length; j++) { 152 System.out.print(topdirs[j]+"\t"); 153 } 154 155 System.out.print("\nmodules:\t"); 156 for (int i=0; i<modules.length; i++) { 157 System.out.print(modules[i]+"\t"); 158 } 159 160 System.out.println("\ndistDir:\t"+distDir); 161 System.out.println("buildDir:\t"+buildDir); 162 System.out.println("localizableFile:\t"+localizableFile); 163 System.out.println("generatedFile:\t"+generatedFile); 164 System.out.println("changedFile:\t"+changedFile); 165 System.out.println("globalFile:\t"+globalFile); 166 System.out.println("buildNumber:\t"+buildNumber); 167 168 169 CvsEntries ce; 170 171 p=this.getProject(); 172 173 for (int i=0; i<topdirs.length; i++) { 174 for(int j=0; j<modules.length; j++) { 176 if (modules[j] != null && ! modules[j].equals("")) { 177 @SuppressWarnings ("unchecked") 178 Hashtable <String ,String > props = p.getProperties(); 179 fullPropHash = props; 180 181 184 File f = new File (topdirs[i]+File.separator+modules[j]+File.separator+localizableFile); 185 187 if ( f.exists() ) { 188 System.out.println("\t\tFILE exists"+topdirs[i]+File.separator+modules[j]+File.separator+localizableFile); 189 File topDir = new File (topdirs[i]); 190 File modDir = new File (topdirs[i]+File.separator+modules[j]); 191 192 195 localizableFiles = getLocalizableFiles(topDir, modules[j]); 196 if (localizableFiles == null ) { 197 if (DEBUG) { 198 System.out.println("\t\tNo Localizable Files for this module."+topDir+File.separator+modules[j]); 199 } 200 } else { 201 202 generatedFileHash = getGeneratedFiles(topDir, modules[j]); 204 206 211 212 for (int k=0; k<localizableFiles.length; k++) { 213 214 216 217 int lastSlashIndex = localizableFiles[k].lastIndexOf(File.separator); 218 String parentDirFullPath = localizableFiles[k].substring(0, lastSlashIndex); 219 220 if ( ! new File (parentDirFullPath+"/CVS").exists() ) { 224 System.out.println("This dir is a generated dir with no CVS dir "+parentDirFullPath); 228 229 } else { 230 231 boolean success = printToAllFile(topdirs[i], modules[j], localizableFiles); 233 if (! success) { 234 System.out.println("ERROR: Print to All File in "+topdirs[i]+", "+modules[j]+"failed."); 235 236 } 237 238 ce = cvsEntriesCache.get(parentDirFullPath); 239 240 if ( ce == null ) { 241 ce = new CvsEntries(parentDirFullPath); cvsEntriesCache.put(parentDirFullPath, ce); 243 } 244 245 if ( generatedFileHash == null ) { 246 generatedFileHash=new Hashtable <String , String >(); 247 } 248 249 250 String localizableFileOnly = localizableFiles[k].substring(localizableFiles[k].lastIndexOf(File.separator)+1); 251 if (localizableFileOnly == null) { System.out.println("NULL LOCALIZABLE FILE"); } 252 253 String ceRev = ce.getRevnoByFileName(localizableFileOnly); 254 255 if ( ceRev == null ) { 256 260 263 } else { 264 267 String genRev = generatedFileHash.get(localizableFiles[k]); 268 270 if ( genRev == null || ! ceRev.equals(genRev)) { 271 changed.put(localizableFiles[k], ceRev); 273 274 275 generatedFileHash.put(localizableFiles[k], ceRev); 277 278 279 } 281 } 282 } 283 284 } 286 boolean success = printToFile(topdirs[i], modules[j]); 288 289 290 if (! success) { 291 System.out.println("ERROR: Print to File in "+topdirs[i]+", "+modules[j]+"failed."); 292 } 293 294 File tDir = new File (buildDir+File.separator+topdirs[i]); 298 int lio = topdirs[i].lastIndexOf(File.separator); 299 String shortTopdir = topdirs[i].substring(lio+1); 300 301 302 307 mkTars(topdirs[i]+File.separator, buildDir+File.separator+shortTopdir+File.separator+modules[j]+".tar", topdirs[i]+File.separator+modules[j]+File.separator+modules[j]+"."+changedFile, modules[j]); 308 Delete delete = (Delete)p.createTask("delete"); 310 FileSet fs = new FileSet(); 311 fs.setDir(new File (topdirs[i]+File.separator+modules[j])); 312 String includes = modules[j]+"."+generatedFile+","+ modules[j]+"."+changedFile+","+ modules[j]+"."+allFile; 313 fs.setIncludes(includes); 314 delete.addFileset(fs); 315 316 delete.setVerbose(true); 317 delete.execute(); 318 320 321 323 generatedFileHash.clear(); 325 changed.clear(); 326 error.clear(); 327 328 } } } } } 334 if (DEBUG) System.out.println("ABOUT TO MAKE THE BIG TAR: "+distDir+"/l10n-"+buildNumber+".tar.gz"); 336 337 341 File bd = new File (buildDir); 342 if (bd.exists()) { 343 File tarFile = new File (buildDir+"/l10n-"+buildNumber+".tar"); 344 345 Tar tar = (Tar)p.createTask("tar"); 346 tar.setBasedir(bd); 347 tar.setDestFile(tarFile); 348 Tar.TarLongFileMode mode = new Tar.TarLongFileMode(); 349 mode.setValue(Tar.TarLongFileMode.GNU); 350 tar.setLongfile(mode); 351 tar.execute(); 353 354 if ( tarFile.exists() ) { 355 GZip gzip = (GZip)p.createTask("gzip"); 356 gzip.setSrc(new File (buildDir+"/l10n-"+buildNumber+".tar")); 357 gzip.setZipfile(new File (distDir+"/l10n-"+buildNumber+".tar.gz")); 358 gzip.execute(); 359 } else { 360 System.out.println("NO tar file, can't gzip"+buildDir+"/l10n-"+buildNumber+".tar"); 361 } 362 } else { 363 System.out.println("No files in builddir. No kit to build"); 364 } 365 366 367 368 369 } 371 public void mkTars(String srcDir, String fullTarfilePath, String fullIncludesFilePath, String module) { 372 if (DEBUG) { 374 } 377 378 File incBaseDir = new File (srcDir); 379 Tar tar = (Tar)p.createTask("tar"); 380 tar.setBasedir(new File (srcDir)); 381 tar.setDestFile(new File (fullTarfilePath)); 382 383 File incFile = new File (fullIncludesFilePath); 384 385 if (incFile.exists() ) { 386 tar.setIncludes(module+File.separator+module+"."+allFile); 387 tar.setIncludes(module+File.separator+module+"."+generatedFile); 388 tar.setIncludes(module+File.separator+module+"."+changedFile); 389 tar.setIncludesfile(incFile); 390 Tar.TarLongFileMode mode = new Tar.TarLongFileMode(); 391 mode.setValue(Tar.TarLongFileMode.GNU); 392 tar.setLongfile(mode); 393 tar.execute(); 394 } 395 396 } 397 398 public boolean printToAllFile(String fullTopDir, String module, String [] localizableFiles) { 399 try { 400 int lastSlashIndex = fullTopDir.lastIndexOf(File.separator); 401 String topDir= fullTopDir.substring(lastSlashIndex+1); 402 403 File f = new File (buildDir+ File.separator +topDir ); 404 f.mkdirs(); 405 406 FileWriter allWrite = new FileWriter (fullTopDir+File.separator+module+File.separator+module+"."+allFile); 407 408 if ( localizableFiles != null ) { 409 for (int i=0; i<localizableFiles.length; i++) { 410 int lio = localizableFiles[i].lastIndexOf(fullTopDir); 411 if (lio >= 0) { 412 String moduleFileName= localizableFiles[i].substring(lio+fullTopDir.length()+1); 413 allWrite.write(moduleFileName+"\n"); 414 } else { 415 System.out.println("Error: NO TOPDIR HERE: "+ localizableFiles[i]+ " FTD: "+fullTopDir+" LIO "+lio); 416 } 417 418 } 419 } 420 allWrite.close(); 421 422 } catch (IOException ioe) { 423 System.out.println("IOException"+ioe); 424 return false; 425 } 426 return true; 427 428 } 429 public boolean printToFile(String fullTopDir, String module) { 430 431 433 try { 434 435 int lastSlashIndex = fullTopDir.lastIndexOf(File.separator); 436 String topDir= fullTopDir.substring(lastSlashIndex+1); 437 438 442 443 FileWriter genWrite = new FileWriter (fullTopDir+File.separator+module+File.separator+module+"."+generatedFile); 444 FileWriter changedWrite = new FileWriter (fullTopDir+File.separator+module+File.separator+module+"."+changedFile); 445 446 447 if ( generatedFileHash == null ) { 448 generatedFileHash = new Hashtable <String , String >(); 449 } else { 450 451 for (Enumeration <String > g = generatedFileHash.keys() ; g.hasMoreElements() ;) { 452 String genFileKey = g.nextElement(); 453 int lioTopDir = genFileKey.lastIndexOf(topDir); 454 String moduleFileName = genFileKey.substring(lioTopDir+topDir.length()+1) ; 455 456 genWrite.write(moduleFileName+"\t"+generatedFileHash.get(genFileKey)+"\n"); 457 } 458 459 for (Enumeration <String > c = changed.keys() ; c.hasMoreElements() ;) { 462 String changedFileKey = c.nextElement(); 463 464 int lio = changedFileKey.lastIndexOf(topDir+File.separator+module); 465 String moduleFileName; 466 if (lio >= 0) { 467 468 moduleFileName=changedFileKey.substring(changedFileKey.lastIndexOf(topDir+File.separator+module)+topDir.length()+1); 469 changedWrite.write(moduleFileName+"\n"); 470 } else { 471 473 System.out.println("WARNING: L10n.list file error. Each item in your list should reference the current module. If this is a global l10n file used over several modules use the property ${l10n-module} as a place-holder. This error occurred in "+module+"."); 476 } 478 479 } 482 } 483 484 if (! error.isEmpty() ) { 485 FileWriter errorWrite = new FileWriter (buildDir+File.separator+"l10n-errors.txt", true); 486 for (Enumeration e = error.elements(); e.hasMoreElements();) { 487 String ee = (String )e.nextElement(); 488 errorWrite.write(ee+"\n"); 489 System.out.println("Error: "+ee); 490 } 491 errorWrite.close(); 492 } 493 494 495 genWrite.close(); 496 changedWrite.close(); 497 498 499 } catch (IOException ioe) { 500 System.out.println("IOException printToFile()"+ioe); 501 return false; 502 } 503 return true; 504 } 505 506 512 public String [] getLocalizableFiles(File topRoot, String module) { 513 String [] lfs = null; 514 StringBuffer [] sbholder=new StringBuffer [2]; 515 StringBuffer sbi= new StringBuffer (); 516 StringBuffer sbe = new StringBuffer (); 517 String includeS=""; 518 String excludeS=""; 519 521 if( fullPropHash == null) { 522 p = getProject() ; 523 @SuppressWarnings ("unchecked") 524 Hashtable <String ,String > props = p.getProperties(); 525 fullPropHash = props; 526 } 527 fullPropHash.put("l10n-module", module); 528 529 try { 530 File includes = new File (topRoot.getCanonicalPath() + File.separator + module + File.separator + localizableFile); 531 if ( ! includes.exists() || includes.length() <= 0 ) { 532 return lfs; 534 } 535 536 try { 537 sbholder = processListFile(includes,module); 538 } catch (java.io.IOException ioe) { 539 System.out.println("Error processing file. "+ioe); 540 } 541 542 if (sbholder != null) { 543 sbi = sbholder[0]; 544 sbe = sbholder[1]; 545 546 sbe.append(" "+excludePattern); 547 sbi.append(" "+includePattern); 548 549 if (DEBUG) { 550 System.out.println("INC "+sbi.toString()); 551 System.out.println("EXC "+sbe.toString()); 552 } 553 } 554 555 this.fileset = new FileSet(); 556 557 if ( sbi !=null) { 559 this.setIncludes(sbi.toString()); 560 } 562 563 if (sbe != null ) { 564 this.setExcludes(sbe.toString()); 565 } 568 569 DirectoryScanner ds = this.getDirectoryScanner(topRoot); 570 571 ds.scan(); 572 lfs = ds.getIncludedFiles(); 573 574 for (int k=0; k<lfs.length; k++) { 575 lfs[k] = topRoot+File.separator+lfs[k].trim(); 577 } 579 580 } catch(java.io.IOException e) { 582 System.out.println(e.getMessage()); 583 } 584 return lfs; 585 } 586 587 public Hashtable <String , String > getGeneratedFiles(File topDir, String mod) { 588 593 Hashtable <String , String > h=new Hashtable <String , String >(); 594 try { 596 597 String topDirFullPath = topDir.getCanonicalPath(); 598 600 BufferedReader inBuff = new BufferedReader (new FileReader (new File (topDir+File.separator+mod,generatedFile))); 601 boolean eof = false; 602 while (! eof) { 603 String line = inBuff.readLine(); 604 if (line==null) { 605 eof = true; 606 } else { 607 609 int tabIndex = line.indexOf("\t"); 610 if (tabIndex > 0) { 611 String filename = line.substring(0,tabIndex); 612 String revision = line.substring(tabIndex+1); 613 h.put(topDirFullPath+File.separator+filename, revision); 614 } else { 615 System.out.println("There's no tab in this line"+"["+line+"]"); 616 617 } 618 619 } 620 621 } } catch(java.io.FileNotFoundException e) { 623 return(null); 627 } catch(java.io.IOException e) { 628 System.out.println("IOException "+ e); 629 } 630 631 return h; 632 } 633 634 636 public void setTopdirs(String s) { 637 StringTokenizer st = new StringTokenizer (s); 638 String [] tops = new String [st.countTokens()]; 639 int i=0; 640 while (st.hasMoreTokens()) { 641 tops[i++]=st.nextToken(); 642 } 643 644 if (false) { 645 for (int j=0; j<tops.length; j++) { 646 System.out.println("TOPS "+tops[j]); 647 } 648 } 649 650 this.topdirs = tops; 651 652 } 653 public void setModules(String s) { 654 StringTokenizer st = new StringTokenizer (s,","); 655 HashSet <String > modSet = new HashSet <String >(); String fullMod = null; 657 658 while (st.hasMoreTokens()) { 659 fullMod=st.nextToken().trim();; 660 661 663 int index = fullMod.indexOf(File.separator); 664 if (index >= 0) { 665 fullMod=fullMod.substring(0,index); 668 669 } 671 modSet.add( fullMod ); 672 } 673 this.modules = new String [ modSet.size() ]; 674 Iterator <String > it = modSet.iterator(); 675 676 int i=0; 677 while( it.hasNext() ) 678 this.modules[i++] = it.next(); 679 680 } 681 682 public StringBuffer [] processListFile(File inc,String module) throws IOException { 683 System.out.println("Reading list file: "+inc.toString()); 684 StringBuffer [] sbholder=new StringBuffer [2]; 685 StringBuffer sbi = new StringBuffer (); 686 StringBuffer sbe = new StringBuffer (); 687 688 689 if(inc.exists() && inc.length() >0) { 690 BufferedReader br = new BufferedReader (new FileReader (inc)); 692 String line; 693 while((line=br.readLine()) != null && line.indexOf("#") <0) { 694 boolean skipit=false; 695 while (line.indexOf("${") >= 0 && skipit == false) { 698 699 String propertyName; 700 String value=""; 701 String res=""; 702 String pre=""; 703 704 propertyName=line.substring(line.indexOf("${")+2,line.indexOf("}")); 705 706 if (fullPropHash.containsKey(propertyName)) { 707 708 value=fullPropHash.get(propertyName); 709 res = line.substring(line.indexOf("}")+1); 710 pre= line.substring(0,line.indexOf("{")-1); 711 line=pre+value+res; 712 } else { 714 System.out.println("Uninterpretable property in l10n file:"+inc.toString()+". "+propertyName+" Interpreting the entire line literally."); 715 skipit=true; 716 } 717 } 718 719 if (line.indexOf("exclude") >= 0) { 720 sbe.append(" "+line.substring("exclude".length()+1)); 721 } else if (line.indexOf("read global") >= 0) { 722 723 if (globalFile != null && ! globalFile.equals("")) { 724 if (readGlobalFile==true) { 725 System.out.println("Already read it"); 726 sbi.append(" "+globalsbholder[0]); 729 sbe.append(" "+globalsbholder[1]); 730 } else { 731 globalsbholder[0]= new StringBuffer (); 732 globalsbholder[1]= new StringBuffer (); 733 StringBuffer [] globalarray = processListFile(new File (globalFile),module); 734 if (globalarray[0] != null) { 735 sbi.append(" "+globalarray[0]); 736 globalsbholder[0].append(" "+globalarray[0]); 737 } 739 if (globalarray[1] != null) { 740 sbe.append(" "+globalarray[1]); 741 globalsbholder[1].append(" "+globalarray[1]); 742 } 744 } 745 readGlobalFile = true ; 746 } 747 } else if( line.trim().startsWith( "read")) { 748 String l = line.trim() ; 749 l = l.substring( 4) ; 750 l = l.trim() ; 751 StringBuffer [] sbarr = processListFile( new File ( l), 752 module) ; 753 sbi.append( " " + sbarr[ 0]) ; 754 sbe.append( " " + sbarr[ 1]) ; 755 } else { 756 sbi.append(" "+line); 757 } 758 } br.close(); 761 } 762 sbholder[0]=sbi; 763 sbholder[1]=sbe; 764 return sbholder; 765 } 766 767 public void setDistDir(String s) { 768 this.distDir=s; 770 } 771 public void setBuildDir(String s) { 772 this.buildDir=s; 773 } 774 public void setDebug(boolean s) { 775 this.DEBUG=s; 776 } 777 public void setLocalizableFile(String s) { 778 this.localizableFile=s; 779 } 780 public void setExcludePattern(String s) { 781 this.excludePattern=s; 782 } 783 public void setIncludePattern(String s) { 784 this.includePattern=s; 785 } 786 787 public void setBuildNumber(String s) { 788 this.buildNumber=s; 789 } 790 public void setGeneratedFile(String s) { 791 this.generatedFile=s; 792 } 793 public void setChangedFile(String s) { 794 this.changedFile=s; 795 } 796 public void setGlobalFile(String s) { 797 this.globalFile=s; 798 } 799 } 800 | Popular Tags |