KickJava   Java API By Example, From Geeks To Geeks.

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


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.FileSet;
28 import org.apache.tools.ant.types.Path;
29
30 /** This task runs the JHIndexer task multiple times, once for each
31  * language, and automatically creates the appropriate regular
32  * expressions to choose the files for each language. This task
33  * greatly reduces the amount of Ant code required to run JHIndexer
34  * especially when there are helpsets for multiple languages in the
35  * same directory tree.
36  *
37  * @author Jerry Huth (email: jerry@solidstep.com)
38  */

39 public class LocJHIndexer extends MatchingTask {
40
41   protected File basedir = null ;
42   protected String JavaDoc dbdir = null ;
43   protected String JavaDoc locales = null ;
44   protected String JavaDoc jhall = null ;
45
46   /** Specify a regular expression to find <samp>jhall.jar</samp>
47    * (JavaHelp tools library).
48    */

49   public void setJhall( String JavaDoc jhall) {
50     this.jhall = jhall;
51   }
52
53   /** Get the jhall jar file to use. */
54   protected String JavaDoc getJhall() {
55     String JavaDoc ret = null ;
56     String JavaDoc prop = null ;
57
58     // Use the attribute if specified. //
59
if( jhall != null) {
60       ret = jhall ;
61     }
62     else {
63
64       // Else look for the global property. //
65
prop = getProject().getProperty("locjhindexer.jhall");
66       if( prop != null) {
67     ret = prop ;
68       }
69     }
70
71     return( ret) ;
72   }
73
74   /** Set the location of the docs helpsets' base dir. */
75   public void setBasedir( File dir) {
76     basedir = dir ;
77   }
78
79   /** Set the name of the search database directory (which is under
80    * <samp>basedir/&lt;locale></samp>)
81    */

82   public void setDbdir( String JavaDoc dir) {
83     dbdir = dir ;
84   }
85
86   /** Set a comma-separated list of locales which have helpsets. */
87   public void setLocales( String JavaDoc s) {
88     locales = s ;
89   }
90
91   /** Get the locales for which we'll look for helpsets. */
92   protected String JavaDoc getLocales() {
93     if( locales != null) {
94       return( locales) ;
95     }
96     return(getProject().getProperty("locjhindexer.locales"));
97   }
98
99   public void execute() throws BuildException {
100     String JavaDoc locs = getLocales() ;
101     String JavaDoc helpset_locs = null ;
102     StringTokenizer tokenizer = null ;
103     String JavaDoc loc = null ;
104
105     if( getJhall() == null)
106       throw new BuildException( "Must specify the jhall attribute") ;
107     if( dbdir == null || dbdir.trim().equals( ""))
108       throw new BuildException( "Must specify the dbdir attribute") ;
109     if( basedir == null)
110       throw new BuildException( "Must specify the basedir attribute") ;
111     if( locs == null || locs.trim().equals( ""))
112       throw new BuildException( "Must specify the locales attribute") ;
113
114     // I couldn't get it to work unless I explicitly added the task def here. //
115
getProject().addTaskDefinition("jhindexer", JHIndexer.class);
116
117     // For each locale. //
118
tokenizer = new StringTokenizer( locs, ", ") ;
119     while( tokenizer.hasMoreTokens()) {
120       loc = tokenizer.nextToken() ;
121
122       // If there's a helpset for this locale. //
123
if( hasHelpset( loc)) {
124
125     // Add it to the list of locales that have helpsets. //
126
if( helpset_locs == null) {
127       helpset_locs = new String JavaDoc( loc) ;
128     }
129     else {
130       helpset_locs += "," + loc ;
131     }
132       }
133     }
134
135     // For each locale. //
136
if( helpset_locs != null) {
137       tokenizer = new StringTokenizer( helpset_locs, ", ") ;
138       while( tokenizer.hasMoreTokens()) {
139     loc = tokenizer.nextToken() ;
140
141     // Run the JHIndexer for this locale. //
142
RunForLocale( loc) ;
143       }
144     }
145   }
146
147   /** See if there's a helpset for this locale. */
148   protected boolean hasHelpset( String JavaDoc loc) {
149     boolean ret = false ;
150     LocHelpsetFilter filter = new LocHelpsetFilter( loc) ;
151     File files[] ;
152
153     files = basedir.listFiles( filter) ;
154     if( files != null && files.length > 0) {
155       ret = true ;
156     }
157
158     return( ret) ;
159   }
160
161   // Run JHIndexer for the given locale. //
162
protected void RunForLocale( String JavaDoc locale) throws BuildException {
163     JHIndexer jhindexer ;
164
165     jhindexer = (JHIndexer) getProject().createTask("jhindexer");
166     jhindexer.init() ;
167
168     jhindexer.setIncludes( locale + "/**/*.htm*") ;
169     jhindexer.setExcludes( locale + "/" + dbdir + "/" + "," +
170                locale + "/credits.htm*") ;
171     jhindexer.setBasedir( new File( basedir + "/")) ;
172     jhindexer.setDb( new File( basedir + "/" + locale + "/" + dbdir)) ;
173     jhindexer.setLocale( locale) ;
174     setJHLib( jhindexer) ;
175
176     jhindexer.execute() ;
177   }
178
179   protected void setJHLib( JHIndexer jhindexer) {
180     String JavaDoc jhlib, dir, regexp ;
181     int idx, i ;
182     FileSet fs ;
183     File file ;
184     LinkedList<String JavaDoc> dirs, regexps ;
185     StringTokenizer st ;
186     Path path ;
187
188     // For each regular expression. //
189
dirs = new LinkedList<String JavaDoc>() ;
190     regexps = new LinkedList<String JavaDoc>() ;
191     jhlib = getJhall() ;
192     st = new StringTokenizer( jhlib, " \n,") ;
193     while( st.hasMoreTokens()) {
194       regexp = st.nextToken() ;
195
196       // Break the regular expression up into directory and file //
197
// components. //
198
idx = regexp.lastIndexOf( "/") ;
199       dir = regexp.substring( 0, idx) ;
200       file = new File( dir) ;
201       if( file.exists()) {
202     dirs.add( dir) ;
203     regexps.add( regexp.substring( idx+1)) ;
204       }
205     }
206
207     if( dirs.size() > 0) {
208       path = jhindexer.createClasspath() ;
209       for( i = 0; i < dirs.size(); i++) {
210     dir = dirs.get( i) ;
211     regexp = regexps.get( i) ;
212     fs = new FileSet() ;
213     fs.setDir( new File( dir)) ;
214     fs.setIncludes( regexp) ;
215     path.addFileset( fs) ;
216       }
217     }
218     else {
219       throw new BuildException( "jhall not found.") ;
220     }
221   }
222
223   protected class LocHelpsetFilter implements FilenameFilter {
224     protected String JavaDoc locale = null ;
225
226     public LocHelpsetFilter( String JavaDoc loc) {
227       locale = loc ;
228     }
229
230     public boolean accept(File dir,
231               String JavaDoc name) {
232       return( name.endsWith( "_" + locale + ".hs")) ;
233     }
234   }
235
236 }
237
238
Popular Tags