KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > nbbuild > LocMakeNBM


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

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 /** Runs the makenbm task for each locale specified in the
30  * global property locmakenbm.locales.
31  * NOTE: Currently this runs makelnbm, since the new
32  * functionality in that hasn't been merged into makenbm
33  * yet.
34  *
35  * @author Jerry Huth (email: jerry@solidstep.com)
36  */

37 public class LocMakeNBM extends Task {
38
39   protected String JavaDoc locales = null ;
40   protected String JavaDoc mainDir = null ;
41   protected File topDir = null ;
42   protected String JavaDoc fileName = null ;
43   protected String JavaDoc moduleName = null ;
44   protected String JavaDoc baseFileName = null ;
45   protected boolean deleteInfo = false ;
46   protected String JavaDoc nbmIncludes = null ;
47   protected String JavaDoc modInfo = null ;
48   protected String JavaDoc findLocBundle = "." ; // relative to the directory
49
// corresponding to the module's
50
// codename
51
protected File locBundle = null ; // path to localizing bundle - overrides
52
// findLocBundle
53
protected String JavaDoc locIncludes = null ; // comma-separated list of
54
// "<locale>:<pattern>" elements
55

56   public void setLocales( String JavaDoc s) {
57     locales = s ;
58   }
59   public void setMainDir( String JavaDoc s) {
60     mainDir = s ;
61   }
62   public void setTopDir( File f) {
63     topDir = f ;
64   }
65   public void setModule(String JavaDoc module) {
66       this.moduleName = module;
67       log("Setting moduleName = '"+moduleName+"'", Project.MSG_VERBOSE);
68   }
69   public void setFile( String JavaDoc s) {
70     fileName = s ;
71     log("Setting fileName = '"+fileName+"'", Project.MSG_VERBOSE);
72     if( !fileName.substring( fileName.length() - 4).equals( ".nbm")) { //NOI18N
73
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 JavaDoc s) {
81     nbmIncludes = s ;
82   }
83   public void setModInfo( String JavaDoc s) {
84     modInfo = s ;
85   }
86   public void setLocBundle( File f) {
87     locBundle = f ;
88   }
89   public void setFindLocBundle( String JavaDoc s) {
90     findLocBundle = s ;
91   }
92   public void setLocIncludes( String JavaDoc 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 JavaDoc locs, loc ;
108     StringTokenizer stok ;
109     LinkedList<String JavaDoc> build_locales = new LinkedList<String JavaDoc>() ;
110
111     // Set default values. //
112
if( mainDir == null) {
113       mainDir = new String JavaDoc( "netbeans") ; //NOI18N
114
}
115     if( topDir == null) {
116       topDir = getProject().getBaseDir() ;
117     }
118     
119     if (( modInfo == null) && (moduleName != null)) {
120         // load module info frommodule jarfile
121
File f = new File (topDir,moduleName.replace('/', File.separatorChar));
122         java.util.jar.JarFile JavaDoc jf;
123         try {
124             jf= new java.util.jar.JarFile JavaDoc(f);
125         } catch (java.io.IOException JavaDoc ioe) {
126             throw new BuildException("I/O error during opening module jarfile", ioe, this.getLocation());
127         }
128         java.util.jar.Manifest JavaDoc mani;
129         try {
130             mani = jf.getManifest();
131         } catch (java.io.IOException JavaDoc 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 JavaDoc attr = mani.getMainAttributes();
136             String JavaDoc cname = attr.getValue("OpenIDE-Module");
137             String JavaDoc 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     // Print a warning and stop if the topDir doesn't exist. //
148
if( printMissingDirWarning()) {
149       return ;
150     }
151
152     locs = getLocales() ;
153     if( locs == null || locs.trim().equals( "")) { //NOI18N
154
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     // I couldn't get it to work unless I explicitly added the task def here. //
161
getProject().addTaskDefinition("makelnbm", MakeLNBM.class); //NOI18N
162

163     // Get a list of the locales for which localized files exist. //
164
stok = new StringTokenizer( locs, ",") ; //NOI18N
165
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     // For each locale that we need to build an NBM for. //
177
ListIterator<String JavaDoc> iterator = build_locales.listIterator() ;
178     while( iterator.hasNext()) {
179
180       // Build the NBM for this locale. //
181
buildNbm( iterator.next()) ;
182     }
183   }
184
185   /** Build the NBM for this locale. */
186   protected void buildNbm( String JavaDoc locale) throws BuildException {
187     MakeLNBM makenbm ;
188     LinkedList<String JavaDoc> list = new LinkedList<String JavaDoc>() ;
189     String JavaDoc includes = new String JavaDoc() ;
190     File licenseFile ;
191     boolean first_time ;
192     Delete del ;
193
194     // Delete the Info directory if desired. //
195
if( deleteInfo) {
196       del = (Delete) getProject().createTask("delete"); //NOI18N
197
del.init() ;
198       del.setDir( new File( topDir.getAbsolutePath() + File.separator + "Info")) ; //NOI18N
199
del.execute() ;
200       del.setDir( new File( topDir.getAbsolutePath() + File.separator + "Info_" + //NOI18N
201
locale)) ;
202       del.execute() ;
203     }
204     else {
205
206       // Move the Info_<locale> dir to Info. //
207
switchInfo( true, locale) ;
208     }
209
210     makenbm = (MakeLNBM) getProject().createTask("makelnbm"); //NOI18N
211
makenbm.init() ;
212
213     makenbm.setModInfo( modInfo) ;
214     makenbm.setLangCode( locale) ;
215     String JavaDoc 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 JavaDoc distbase = getProject().getProperty("dist.base"); //NOI18N
221
if (distbase != null) {
222 // try {
223
int idx = fname.lastIndexOf('/');
224             makenbm.setDistribution(distbase + "/" + fname.substring(idx + 1)); //NOI18N
225
// } catch (MalformedURLException e) {
226
// throw new BuildException(e, getLocation());
227
// }
228
}
229     licenseFile = getLicenseFile( locale) ;
230     if( licenseFile != null) {
231       MakeLNBM.Blurb blurb = makenbm.createLicense() ;
232       blurb.setFile( licenseFile) ;
233     }
234
235     // Set the localizing bundle specified, or look for it. //
236
if( locBundle != null) {
237       setLocBundle( makenbm, getSpecificLocBundleFile( locBundle, locale)) ;
238     }
239     else {
240       setLocBundle( makenbm, findLocBundle( makenbm, locale)) ;
241     }
242
243     // Set up the signing data if it's specified. //
244
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     // Get the list of include patterns for this locale. //
254
addLocalePatterns( list, locale) ;
255
256     // Create a comma-separated list of include patterns. //
257
first_time = true ;
258     for (String JavaDoc s1: list) {
259       if( !first_time) {
260     includes += "," ; //NOI18N
261
}
262       includes += s1 ;
263       first_time = false ;
264     }
265     // Add any extra includes that were specified. //
266
if( nbmIncludes != null && !nbmIncludes.trim().equals( "")) { //NOI18N
267
if( !first_time) {
268     includes += "," ; //NOI18N
269
}
270       includes += nbmIncludes ;
271     }
272     makenbm.setIncludes( includes) ;
273
274     makenbm.execute() ;
275
276     // Move the Info dir to Info_<locale>. //
277
switchInfo( false, locale) ;
278   }
279
280   /** Return the license file associated with this locale if there is
281    * one.
282    */

283   protected File getLicenseFile( String JavaDoc locale) {
284     String JavaDoc license_prop_name = locale + ".license.file" ; //NOI18N
285
String JavaDoc 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 JavaDoc locale) {
295     File dir ;
296
297     if( to_info) {
298       dir = new File( topDir.getAbsolutePath() + File.separator + "Info_" + locale) ; //NOI18N
299
dir.renameTo( new File( topDir.getAbsolutePath() + File.separator + "Info")) ; //NOI18N
300
}
301     else {
302       dir = new File( topDir.getAbsolutePath() + File.separator + "Info") ; //NOI18N
303
dir.renameTo( new File( topDir.getAbsolutePath() + File.separator + "Info_" + //NOI18N
304
locale)) ;
305     }
306   }
307
308   /** Get the localized version of the NBM filename. */
309   protected String JavaDoc getLocalizedFileName( String JavaDoc locale) {
310     return( baseFileName + "_" + locale + ".nbm") ; //NOI18N
311
}
312
313   protected String JavaDoc getLocales() {
314     if( locales != null) {
315       return( locales) ;
316     }
317     return( getGlobalProp( "locmakenbm.locales")) ; //NOI18N
318
}
319
320   /** See if there are any files for the given locale. */
321   protected boolean hasFilesInLocale( String JavaDoc loc) {
322     FileSet fs ;
323     boolean ret = true ;
324
325     // Setup a fileset to find files in this locale. //
326
fs = new FileSet() ;
327     fs.setDir( topDir) ;
328     addLocalePatterns( fs, loc) ;
329
330     // See if there are any localized files for this locale. //
331
String JavaDoc[] inc_files = fs.getDirectoryScanner(getProject()).getIncludedFiles();
332     if( inc_files.length == 0) {
333       ret = false ;
334     }
335
336     return( ret) ;
337   }
338
339   /** Add the patterns to include the localized files for the given locale. */
340   protected void addLocalePatterns( FileSet fs,
341                     String JavaDoc loc) {
342     LinkedList<String JavaDoc> list = new LinkedList<String JavaDoc>() ;
343
344     // Get the list of patterns for this locale. //
345
addLocalePatterns( list, loc) ;
346
347     for (String JavaDoc s: list) {
348       // Add it to the includes list. //
349
fs.createInclude().setName(s) ;
350     }
351
352   }
353
354   protected void addLocalePatterns( LinkedList<String JavaDoc> list,
355                     String JavaDoc loc) {
356 // String dir = new String() ;
357
String JavaDoc re = new String JavaDoc() ;
358
359
360 // dir = mainDir ; // modified for clusterization
361
// re = dir + "/**/*_" + loc + ".*" ; // pattern is: ${dir}/**/*_${locale}.* //NOI18N
362
// list.add( new String( re)) ;
363
// re = dir + "/**/" + loc + "/" ; // pattern is: ${dir}/${locale}/ //NOI18N
364
// list.add( new String( re)) ;
365

366     re = "**/*_" + loc + ".*" ; // pattern is: ${dir}/**/*_${locale}.* //NOI18N
367
list.add(re) ;
368     re = "**/" + loc + "/" ; // pattern is: ${dir}/${locale}/ //NOI18N
369
list.add(re) ;
370
371     addLocIncludes( list, loc) ;
372
373     // For ja locale, include these other variants. //
374
if( loc.equals( "ja")) { //NOI18N
375
addLocalePatterns( list, "ja_JP.PCK") ; //NOI18N
376
addLocalePatterns( list, "ja_JP.eucJP") ; //NOI18N
377
addLocalePatterns( list, "ja_JP.SJIS") ; //NOI18N
378
addLocalePatterns( list, "ja_JP.UTF-8") ; //NOI18N
379
addLocalePatterns( list, "ja_JP.UTF8") ; //NOI18N
380
}
381   }
382
383   protected void addLocIncludes( LinkedList<String JavaDoc> list,
384                  String JavaDoc loc) {
385     StringTokenizer tkzr ;
386     String JavaDoc locInc, incLocale, incPattern ;
387     int idx ;
388
389     if( locIncludes == null) {
390       return ;
391     }
392
393     // For each locale-specific include. //
394
tkzr = new StringTokenizer( locIncludes, ",\n\t ") ; //NOI18N
395
while( tkzr.hasMoreTokens()) {
396       locInc = tkzr.nextToken() ;
397       idx = locInc.indexOf( ":") ; //NOI18N
398
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 JavaDoc getGlobalProp( String JavaDoc name) {
412     String JavaDoc ret ;
413     ret = getProject().getProperty(name);
414
415     // Don't return empty strings or strings whose value contains a //
416
// property that isn't set. //
417
if( ret != null) {
418       if( ret.trim().equals( "")) { //NOI18N
419
ret = null ;
420       }
421       else if( ret.indexOf( "${") != -1) { //NOI18N
422
ret = null ;
423       }
424     }
425     return( ret) ;
426   }
427
428   protected String JavaDoc getKeystore() {
429     return( getGlobalProp( "locmakenbm.keystore")) ; //NOI18N
430
}
431
432   protected String JavaDoc getStorepass() {
433     return( getGlobalProp( "locmakenbm.storepass")) ; //NOI18N
434
}
435
436   protected String JavaDoc getAlias() {
437     return( getGlobalProp( "locmakenbm.alias")) ; //NOI18N
438
}
439
440   /** If the topDir doesn't exist, warn the user and return true. */
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   /** If the localizing bundle is there, use it. */
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())) ) ; //NOI18N
460
}
461   }
462
463   protected String JavaDoc getSrcDir( File file) {
464     InputStreamReader isr ;
465     FileInputStream fis ;
466     char[] buf = new char[ 200] ;
467     String JavaDoc s = null ;
468     int idx, len ;
469
470     try {
471
472       // Read the srcdir from the file that locjar wrote. //
473
fis = new FileInputStream( file) ;
474       isr = new InputStreamReader( fis) ;
475       len = isr.read( buf) ;
476       if( len != -1) {
477     if( buf[ len-1] == '\n') { //NOI18N
478
len-- ;
479     }
480     s = new String JavaDoc( buf, 0, len) ;
481     idx = s.indexOf( "=") ; //NOI18N
482
if( idx != -1) {
483       s = s.substring( idx + 1) ;
484       s.trim() ;
485     }
486     else {
487       s = null ;
488     }
489       }
490     }
491     catch( Exception JavaDoc 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 JavaDoc locale) {
501     File srcdirfile, locdir ;
502     int index ;
503     String JavaDoc s, srcdir = null ;
504
505     // See if the file containing the srcdir is there. //
506
srcdirfile = new File( topDir.getAbsolutePath() + File.separator +
507                "srcdir.properties") ; //NOI18N
508
if( srcdirfile.exists()) {
509       srcdir = getSrcDir( srcdirfile) ;
510     }
511 // if( srcdir == null) {
512
// throw new BuildException( "ERROR: Could not get source dir from: " + srcdirfile.getPath()) ;
513
// }
514

515     // Get the codename of this module. //
516
index = modInfo.indexOf( "/") ; //NOI18N
517
if( index != -1) {
518       s = modInfo.substring( 0, index) ;
519     }
520     else {
521       s = new String JavaDoc( modInfo) ;
522     }
523
524     // Convert to pathname and set the loc bundle. //
525
s = s.replace( '.', '/') ; //NOI18N
526
locdir = new File( getRelPath( srcdir + "/" + s, findLocBundle). //NOI18N
527
replace( '/', File.separatorChar)) ; //NOI18N
528
return( getDefaultLocBundleFile( locdir, locale)) ;
529   }
530
531   protected File getDefaultLocBundleFile( File dir,
532                       String JavaDoc locale) {
533     return( new File( dir.getPath() + File.separator + "Bundle_" + locale + ".properties")) ; //NOI18N
534
}
535
536   protected File getSpecificLocBundleFile( File enBundle,
537                        String JavaDoc locale) {
538     String JavaDoc path = enBundle.getPath() ;
539     int idx = path.lastIndexOf( '.') ; //NOI18N
540
if( idx != -1) {
541       return( new File( path.substring( 0, idx) + "_" + locale + path.substring( idx))) ; //NOI18N
542
}
543     else {
544       return( new File( path + "_" + locale)) ; //NOI18N
545
}
546   }
547
548   /** This supports ".." path elements at the start of path2. */
549   protected String JavaDoc getRelPath( String JavaDoc path1,
550                    String JavaDoc path2) {
551     int idx1, idx2 ;
552
553     if( path2.equals( ".")) { //NOI18N
554
return( path1) ;
555     }
556
557     // For each ".." element in path2. //
558
while( true) {
559       idx2 = path2.indexOf( "..") ; //NOI18N
560
if( idx2 == -1) {
561     break ;
562       }
563
564       // Strip off the ".." //
565
path2 = path2.substring( 2) ;
566
567       // Strip off the slash if it starts with slash. //
568
idx2 = path2.indexOf( "/") ; //NOI18N
569
if( idx2 == 0) {
570     path2 = path2.substring( 1) ;
571       }
572
573       // Strip off the last element of path1. //
574
idx1 = path1.lastIndexOf( "/") ; //NOI18N
575
if( idx1 != -1) {
576     path1 = path1.substring( 0, idx1) ;
577       }
578     }
579
580     return( path1 + "/" + path2) ; //NOI18N
581
}
582
583 }
584
Popular Tags