1 19 20 package org.netbeans.nbbuild; 21 22 import java.io.* ; 23 import java.util.* ; 24 25 import org.apache.tools.ant.* ; 26 import org.apache.tools.ant.taskdefs.* ; 27 import org.apache.tools.ant.types.* ; 28 29 37 public class LocMakeNBM extends Task { 38 39 protected String locales = null ; 40 protected String mainDir = null ; 41 protected File topDir = null ; 42 protected String fileName = null ; 43 protected String moduleName = null ; 44 protected String baseFileName = null ; 45 protected boolean deleteInfo = false ; 46 protected String nbmIncludes = null ; 47 protected String modInfo = null ; 48 protected String findLocBundle = "." ; protected File locBundle = null ; protected String locIncludes = null ; 56 public void setLocales( String s) { 57 locales = s ; 58 } 59 public void setMainDir( String s) { 60 mainDir = s ; 61 } 62 public void setTopDir( File f) { 63 topDir = f ; 64 } 65 public void setModule(String module) { 66 this.moduleName = module; 67 log("Setting moduleName = '"+moduleName+"'", Project.MSG_VERBOSE); 68 } 69 public void setFile( String s) { 70 fileName = s ; 71 log("Setting fileName = '"+fileName+"'", Project.MSG_VERBOSE); 72 if( !fileName.substring( fileName.length() - 4).equals( ".nbm")) { throw new BuildException( "Incorrect NBM file name \""+ s+"\". NBM file name must end in '.nbm'") ; 74 } 75 baseFileName = fileName.substring( 0, fileName.length() - 4) ; 76 } 77 public void setDeleteInfo( boolean b) { 78 deleteInfo = b ; 79 } 80 public void setNbmIncludes( String s) { 81 nbmIncludes = s ; 82 } 83 public void setModInfo( String s) { 84 modInfo = s ; 85 } 86 public void setLocBundle( File f) { 87 locBundle = f ; 88 } 89 public void setFindLocBundle( String s) { 90 findLocBundle = s ; 91 } 92 public void setLocIncludes( String s) { 93 locIncludes = s ; 94 } 95 96 public void execute() throws BuildException { 97 try { 98 really_execute() ; 99 100 } catch( BuildException be) { 101 be.printStackTrace(); 102 throw be ; 103 } 104 } 105 106 public void really_execute() throws BuildException { 107 String locs, loc ; 108 StringTokenizer stok ; 109 LinkedList<String > build_locales = new LinkedList<String >() ; 110 111 if( mainDir == null) { 113 mainDir = new String ( "netbeans") ; } 115 if( topDir == null) { 116 topDir = getProject().getBaseDir() ; 117 } 118 119 if (( modInfo == null) && (moduleName != null)) { 120 File f = new File (topDir,moduleName.replace('/', File.separatorChar)); 122 java.util.jar.JarFile jf; 123 try { 124 jf= new java.util.jar.JarFile (f); 125 } catch (java.io.IOException ioe) { 126 throw new BuildException("I/O error during opening module jarfile", ioe, this.getLocation()); 127 } 128 java.util.jar.Manifest mani; 129 try { 130 mani = jf.getManifest(); 131 } catch (java.io.IOException ioe) { 132 throw new BuildException("I/O error getting manifest from file '"+f.getAbsolutePath()+"'", ioe, this.getLocation()); 133 } 134 if ( mani != null ) { 135 java.util.jar.Attributes attr = mani.getMainAttributes(); 136 String cname = attr.getValue("OpenIDE-Module"); 137 String sver = attr.getValue("OpenIDE-Module-Specification-Version"); 138 if ((cname != null) && (!(cname.equals(""))) && (sver != null) && (!(sver.equals("")))) { 139 modInfo = cname + '/' + sver; 140 log("Gathered module information from module jarfile. Codename = '"+cname+"' and specification version = '"+sver+"'",Project.MSG_VERBOSE); 141 } else { 142 throw new BuildException("Module in file '"+f.getAbsolutePath()+"' does not have either OpenIDE-Module attribute or OpenIDE-Module-Specification-Version attributes or missing both.", this.getLocation()); 143 } 144 } 145 } 146 147 if( printMissingDirWarning()) { 149 return ; 150 } 151 152 locs = getLocales() ; 153 if( locs == null || locs.trim().equals( "")) { throw new BuildException( "Must specify 1 or more locales.") ; 155 } 156 if( fileName == null) { 157 throw new BuildException( "Must specify the file attribute.") ; 158 } 159 160 getProject().addTaskDefinition("makelnbm", MakeLNBM.class); 163 stok = new StringTokenizer( locs, ",") ; while( stok.hasMoreTokens()) { 166 loc = stok.nextToken() ; 167 log("Checking if module has files in locale '"+loc+"'", Project.MSG_VERBOSE); 168 if( hasFilesInLocale( loc)) { 169 build_locales.add( loc) ; 170 log("Module has files in locale '"+loc+"'", Project.MSG_VERBOSE); 171 } else { 172 log("Module has no files in locale '"+loc+"'", Project.MSG_VERBOSE); 173 } 174 } 175 176 ListIterator<String > iterator = build_locales.listIterator() ; 178 while( iterator.hasNext()) { 179 180 buildNbm( iterator.next()) ; 182 } 183 } 184 185 186 protected void buildNbm( String locale) throws BuildException { 187 MakeLNBM makenbm ; 188 LinkedList<String > list = new LinkedList<String >() ; 189 String includes = new String () ; 190 File licenseFile ; 191 boolean first_time ; 192 Delete del ; 193 194 if( deleteInfo) { 196 del = (Delete) getProject().createTask("delete"); del.init() ; 198 del.setDir( new File( topDir.getAbsolutePath() + File.separator + "Info")) ; del.execute() ; 200 del.setDir( new File( topDir.getAbsolutePath() + File.separator + "Info_" + locale)) ; 202 del.execute() ; 203 } 204 else { 205 206 switchInfo( true, locale) ; 208 } 209 210 makenbm = (MakeLNBM) getProject().createTask("makelnbm"); makenbm.init() ; 212 213 makenbm.setModInfo( modInfo) ; 214 makenbm.setLangCode( locale) ; 215 String fname = getLocalizedFileName( locale); 216 makenbm.setFile( new File( getProject().getBaseDir().getAbsolutePath() + 217 File.separator + fname)) ; 218 makenbm.setTopdir( topDir) ; 219 makenbm.setIsStandardInclude( false) ; 220 String distbase = getProject().getProperty("dist.base"); if (distbase != null) { 222 int idx = fname.lastIndexOf('/'); 224 makenbm.setDistribution(distbase + "/" + fname.substring(idx + 1)); } 229 licenseFile = getLicenseFile( locale) ; 230 if( licenseFile != null) { 231 MakeLNBM.Blurb blurb = makenbm.createLicense() ; 232 blurb.setFile( licenseFile) ; 233 } 234 235 if( locBundle != null) { 237 setLocBundle( makenbm, getSpecificLocBundleFile( locBundle, locale)) ; 238 } 239 else { 240 setLocBundle( makenbm, findLocBundle( makenbm, locale)) ; 241 } 242 243 if( getKeystore() != null && 245 getStorepass() != null && 246 getAlias() != null) { 247 MakeLNBM.Signature sign = makenbm.createSignature() ; 248 sign.setKeystore( new File( getKeystore())) ; 249 sign.setStorepass( getStorepass()) ; 250 sign.setAlias( getAlias()) ; 251 } 252 253 addLocalePatterns( list, locale) ; 255 256 first_time = true ; 258 for (String s1: list) { 259 if( !first_time) { 260 includes += "," ; } 262 includes += s1 ; 263 first_time = false ; 264 } 265 if( nbmIncludes != null && !nbmIncludes.trim().equals( "")) { if( !first_time) { 268 includes += "," ; } 270 includes += nbmIncludes ; 271 } 272 makenbm.setIncludes( includes) ; 273 274 makenbm.execute() ; 275 276 switchInfo( false, locale) ; 278 } 279 280 283 protected File getLicenseFile( String locale) { 284 String license_prop_name = locale + ".license.file" ; String license_prop = getProject().getProperty(license_prop_name); 286 File license = null ; 287 if( license_prop != null) { 288 license = new File( license_prop ) ; 289 } 290 return( license) ; 291 } 292 293 protected void switchInfo( boolean to_info, 294 String locale) { 295 File dir ; 296 297 if( to_info) { 298 dir = new File( topDir.getAbsolutePath() + File.separator + "Info_" + locale) ; dir.renameTo( new File( topDir.getAbsolutePath() + File.separator + "Info")) ; } 301 else { 302 dir = new File( topDir.getAbsolutePath() + File.separator + "Info") ; dir.renameTo( new File( topDir.getAbsolutePath() + File.separator + "Info_" + locale)) ; 305 } 306 } 307 308 309 protected String getLocalizedFileName( String locale) { 310 return( baseFileName + "_" + locale + ".nbm") ; } 312 313 protected String getLocales() { 314 if( locales != null) { 315 return( locales) ; 316 } 317 return( getGlobalProp( "locmakenbm.locales")) ; } 319 320 321 protected boolean hasFilesInLocale( String loc) { 322 FileSet fs ; 323 boolean ret = true ; 324 325 fs = new FileSet() ; 327 fs.setDir( topDir) ; 328 addLocalePatterns( fs, loc) ; 329 330 String [] inc_files = fs.getDirectoryScanner(getProject()).getIncludedFiles(); 332 if( inc_files.length == 0) { 333 ret = false ; 334 } 335 336 return( ret) ; 337 } 338 339 340 protected void addLocalePatterns( FileSet fs, 341 String loc) { 342 LinkedList<String > list = new LinkedList<String >() ; 343 344 addLocalePatterns( list, loc) ; 346 347 for (String s: list) { 348 fs.createInclude().setName(s) ; 350 } 351 352 } 353 354 protected void addLocalePatterns( LinkedList<String > list, 355 String loc) { 356 String re = new String () ; 358 359 360 366 re = "**/*_" + loc + ".*" ; list.add(re) ; 368 re = "**/" + loc + "/" ; list.add(re) ; 370 371 addLocIncludes( list, loc) ; 372 373 if( loc.equals( "ja")) { addLocalePatterns( list, "ja_JP.PCK") ; addLocalePatterns( list, "ja_JP.eucJP") ; addLocalePatterns( list, "ja_JP.SJIS") ; addLocalePatterns( list, "ja_JP.UTF-8") ; addLocalePatterns( list, "ja_JP.UTF8") ; } 381 } 382 383 protected void addLocIncludes( LinkedList<String > list, 384 String loc) { 385 StringTokenizer tkzr ; 386 String locInc, incLocale, incPattern ; 387 int idx ; 388 389 if( locIncludes == null) { 390 return ; 391 } 392 393 tkzr = new StringTokenizer( locIncludes, ",\n\t ") ; while( tkzr.hasMoreTokens()) { 396 locInc = tkzr.nextToken() ; 397 idx = locInc.indexOf( ":") ; if( idx != -1) { 399 incLocale = locInc.substring( 0, idx) ; 400 incPattern = locInc.substring( idx+1) ; 401 if( incLocale.equals( loc)) { 402 list.add( incPattern) ; 403 } 404 } 405 else { 406 list.add( locInc) ; 407 } 408 } 409 } 410 411 protected String getGlobalProp( String name) { 412 String ret ; 413 ret = getProject().getProperty(name); 414 415 if( ret != null) { 418 if( ret.trim().equals( "")) { ret = null ; 420 } 421 else if( ret.indexOf( "${") != -1) { ret = null ; 423 } 424 } 425 return( ret) ; 426 } 427 428 protected String getKeystore() { 429 return( getGlobalProp( "locmakenbm.keystore")) ; } 431 432 protected String getStorepass() { 433 return( getGlobalProp( "locmakenbm.storepass")) ; } 435 436 protected String getAlias() { 437 return( getGlobalProp( "locmakenbm.alias")) ; } 439 440 441 protected boolean printMissingDirWarning() { 442 boolean ret = false ; 443 if( !topDir.exists()) { 444 log( "WARNING: Skipping this task: Directory " + topDir.getPath() + 445 " doesn't exist.") ; 446 ret = true ; 447 } 448 return( ret) ; 449 } 450 451 452 protected void setLocBundle( MakeLNBM makenbm, 453 File bundle) { 454 if( bundle != null && bundle.exists()) { 455 makenbm.setLocBundle( bundle) ; 456 } 457 else { 458 log( "WARNING: Localizing bundle not found: " + 459 ((bundle==null)?(""):(bundle.getPath())) ) ; } 461 } 462 463 protected String getSrcDir( File file) { 464 InputStreamReader isr ; 465 FileInputStream fis ; 466 char[] buf = new char[ 200] ; 467 String s = null ; 468 int idx, len ; 469 470 try { 471 472 fis = new FileInputStream( file) ; 474 isr = new InputStreamReader( fis) ; 475 len = isr.read( buf) ; 476 if( len != -1) { 477 if( buf[ len-1] == '\n') { len-- ; 479 } 480 s = new String ( buf, 0, len) ; 481 idx = s.indexOf( "=") ; if( idx != -1) { 483 s = s.substring( idx + 1) ; 484 s.trim() ; 485 } 486 else { 487 s = null ; 488 } 489 } 490 } 491 catch( Exception e) { 492 System.out.println( "ERROR: " + e.getMessage()) ; 493 e.printStackTrace() ; 494 throw new BuildException() ; 495 } 496 return( s) ; 497 } 498 499 protected File findLocBundle( MakeLNBM makenbm, 500 String locale) { 501 File srcdirfile, locdir ; 502 int index ; 503 String s, srcdir = null ; 504 505 srcdirfile = new File( topDir.getAbsolutePath() + File.separator + 507 "srcdir.properties") ; if( srcdirfile.exists()) { 509 srcdir = getSrcDir( srcdirfile) ; 510 } 511 515 index = modInfo.indexOf( "/") ; if( index != -1) { 518 s = modInfo.substring( 0, index) ; 519 } 520 else { 521 s = new String ( modInfo) ; 522 } 523 524 s = s.replace( '.', '/') ; locdir = new File( getRelPath( srcdir + "/" + s, findLocBundle). replace( '/', File.separatorChar)) ; return( getDefaultLocBundleFile( locdir, locale)) ; 529 } 530 531 protected File getDefaultLocBundleFile( File dir, 532 String locale) { 533 return( new File( dir.getPath() + File.separator + "Bundle_" + locale + ".properties")) ; } 535 536 protected File getSpecificLocBundleFile( File enBundle, 537 String locale) { 538 String path = enBundle.getPath() ; 539 int idx = path.lastIndexOf( '.') ; if( idx != -1) { 541 return( new File( path.substring( 0, idx) + "_" + locale + path.substring( idx))) ; } 543 else { 544 return( new File( path + "_" + locale)) ; } 546 } 547 548 549 protected String getRelPath( String path1, 550 String path2) { 551 int idx1, idx2 ; 552 553 if( path2.equals( ".")) { return( path1) ; 555 } 556 557 while( true) { 559 idx2 = path2.indexOf( "..") ; if( idx2 == -1) { 561 break ; 562 } 563 564 path2 = path2.substring( 2) ; 566 567 idx2 = path2.indexOf( "/") ; if( idx2 == 0) { 570 path2 = path2.substring( 1) ; 571 } 572 573 idx1 = path1.lastIndexOf( "/") ; if( idx1 != -1) { 576 path1 = path1.substring( 0, idx1) ; 577 } 578 } 579 580 return( path1 + "/" + path2) ; } 582 583 } 584 | Popular Tags |