~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to nbbuild/antsrc/org/netbeans/nbbuild/LocJHIndexer.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
6
 * The contents of this file are subject to the terms of either the GNU
 
7
 * General Public License Version 2 only ("GPL") or the Common
 
8
 * Development and Distribution License("CDDL") (collectively, the
 
9
 * "License"). You may not use this file except in compliance with the
 
10
 * License. You can obtain a copy of the License at
 
11
 * http://www.netbeans.org/cddl-gplv2.html
 
12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
 
13
 * specific language governing permissions and limitations under the
 
14
 * License.  When distributing the software, include this License Header
 
15
 * Notice in each file and include the License file at
 
16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
 
17
 * particular file as subject to the "Classpath" exception as provided
 
18
 * by Sun in the GPL Version 2 section of the License file that
 
19
 * accompanied this code. If applicable, add the following below the
 
20
 * License Header, with the fields enclosed by brackets [] replaced by
 
21
 * your own identifying information:
 
22
 * "Portions Copyrighted [year] [name of copyright owner]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
26
 * The Original Software is NetBeans. The Initial Developer of the Original
 
27
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
 
28
 * Microsystems, Inc. All Rights Reserved.
 
29
 *
 
30
 * If you wish your version of this file to be governed by only the CDDL
 
31
 * or only the GPL Version 2, indicate your decision by adding
 
32
 * "[Contributor] elects to include this software in this distribution
 
33
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
 
34
 * single choice of license, a recipient has the option to distribute
 
35
 * your version of this file under either the CDDL, the GPL Version 2 or
 
36
 * to extend the choice of license to its licensees as provided above.
 
37
 * However, if you add GPL Version 2 code and therefore, elected the GPL
 
38
 * Version 2 license, then the option applies only if the new code is
 
39
 * made subject to such option by the copyright holder.
 
40
 */
 
41
 
 
42
package org.netbeans.nbbuild;
 
43
 
 
44
import java.io.*;
 
45
import java.util.*;
 
46
 
 
47
import org.apache.tools.ant.* ;
 
48
import org.apache.tools.ant.taskdefs.* ;
 
49
import org.apache.tools.ant.types.FileSet;
 
50
import org.apache.tools.ant.types.Path;
 
51
 
 
52
/** This task runs the JHIndexer task multiple times, once for each
 
53
 * language, and automatically creates the appropriate regular
 
54
 * expressions to choose the files for each language.  This task
 
55
 * greatly reduces the amount of Ant code required to run JHIndexer
 
56
 * especially when there are helpsets for multiple languages in the
 
57
 * same directory tree.
 
58
 *
 
59
 * @author Jerry Huth (email: jerry@solidstep.com)
 
60
 */
 
61
public class LocJHIndexer extends MatchingTask {
 
62
 
 
63
  protected File basedir = null ;
 
64
  protected String dbdir = null ;
 
65
  protected String locales = null ;
 
66
  protected String jhall = null ;
 
67
 
 
68
  /** Specify a regular expression to find <samp>jhall.jar</samp>
 
69
   * (JavaHelp tools library).
 
70
   */
 
71
  public void setJhall( String jhall) {
 
72
    this.jhall = jhall;
 
73
  }
 
74
 
 
75
  /** Get the jhall jar file to use. */
 
76
  protected String getJhall() {
 
77
    String ret = null ;
 
78
    String prop = null ;
 
79
 
 
80
    // Use the attribute if specified. //
 
81
    if( jhall != null) {
 
82
      ret = jhall ;
 
83
    }
 
84
    else {
 
85
 
 
86
      // Else look for the global property. //
 
87
      prop = getProject().getProperty("locjhindexer.jhall");
 
88
      if( prop != null) {
 
89
        ret = prop ;
 
90
      }
 
91
    }
 
92
 
 
93
    return( ret) ;
 
94
  }
 
95
 
 
96
  /** Set the location of the docs helpsets' base dir. */
 
97
  public void setBasedir( File dir) {
 
98
    basedir = dir ;
 
99
  }
 
100
 
 
101
  /** Set the name of the search database directory (which is under
 
102
   * <samp>basedir/&lt;locale></samp>)
 
103
   */
 
104
  public void setDbdir( String dir) {
 
105
    dbdir = dir ;
 
106
  }
 
107
 
 
108
  /** Set a comma-separated list of locales which have helpsets. */
 
109
  public void setLocales( String s) {
 
110
    locales = s ;
 
111
  }
 
112
 
 
113
  /** Get the locales for which we'll look for helpsets. */
 
114
  protected String getLocales() {
 
115
    if( locales != null) {
 
116
      return( locales) ;
 
117
    }
 
118
    return(getProject().getProperty("locjhindexer.locales"));
 
119
  }
 
120
 
 
121
  public void execute() throws BuildException {
 
122
    String locs = getLocales() ;
 
123
    String helpset_locs = null ;
 
124
    StringTokenizer tokenizer = null ;
 
125
    String loc = null ;
 
126
 
 
127
    if( getJhall() == null)
 
128
      throw new BuildException( "Must specify the jhall attribute") ;
 
129
    if( dbdir == null || dbdir.trim().equals( ""))
 
130
      throw new BuildException( "Must specify the dbdir attribute") ;
 
131
    if( basedir == null)
 
132
      throw new BuildException( "Must specify the basedir attribute") ;
 
133
    if( locs == null || locs.trim().equals( ""))
 
134
      throw new BuildException( "Must specify the locales attribute") ;
 
135
 
 
136
    // I couldn't get it to work unless I explicitly added the task def here. //
 
137
    getProject().addTaskDefinition("jhindexer", JHIndexer.class);
 
138
 
 
139
    // For each locale. //
 
140
    tokenizer = new StringTokenizer( locs, ", ") ;
 
141
    while( tokenizer.hasMoreTokens()) {
 
142
      loc = tokenizer.nextToken() ;
 
143
 
 
144
      // If there's a helpset for this locale. //
 
145
      if( hasHelpset( loc)) {
 
146
 
 
147
        // Add it to the list of locales that have helpsets. //
 
148
        if( helpset_locs == null) {
 
149
          helpset_locs = new String( loc) ;
 
150
        }
 
151
        else {
 
152
          helpset_locs += "," + loc ;
 
153
        }
 
154
      }
 
155
    }
 
156
 
 
157
    // For each locale. //
 
158
    if( helpset_locs != null) {
 
159
      tokenizer = new StringTokenizer( helpset_locs, ", ") ;
 
160
      while( tokenizer.hasMoreTokens()) {
 
161
        loc = tokenizer.nextToken() ;
 
162
 
 
163
        // Run the JHIndexer for this locale. //
 
164
        RunForLocale( loc) ;
 
165
      }
 
166
    }
 
167
  }
 
168
 
 
169
  /** See if there's a helpset for this locale. */
 
170
  protected boolean hasHelpset( String loc) {
 
171
    boolean ret = false ;
 
172
    LocHelpsetFilter filter = new LocHelpsetFilter( loc) ;
 
173
    File files[] ;
 
174
 
 
175
    files = basedir.listFiles( filter) ;
 
176
    if( files != null && files.length > 0) {
 
177
      ret = true ;
 
178
    }
 
179
 
 
180
    return( ret) ;
 
181
  }
 
182
 
 
183
  // Run JHIndexer for the given locale. //
 
184
  protected void RunForLocale( String locale) throws BuildException {
 
185
    JHIndexer jhindexer ;
 
186
 
 
187
    jhindexer = (JHIndexer) getProject().createTask("jhindexer");
 
188
    jhindexer.init() ;
 
189
 
 
190
    jhindexer.setIncludes( locale + "/**/*.htm*") ;
 
191
    jhindexer.setExcludes( locale + "/" + dbdir + "/" + "," +
 
192
                           locale + "/credits.htm*") ;
 
193
    jhindexer.setBasedir( new File( basedir + "/")) ;
 
194
    jhindexer.setDb( new File( basedir + "/" + locale + "/" + dbdir)) ;
 
195
    jhindexer.setLocale( locale) ;
 
196
    setJHLib( jhindexer) ;
 
197
 
 
198
    jhindexer.execute() ;
 
199
  }
 
200
 
 
201
  protected void setJHLib( JHIndexer jhindexer) {
 
202
    String jhlib, dir, regexp ;
 
203
    int idx, i ;
 
204
    FileSet fs ;
 
205
    File file ;
 
206
    LinkedList<String> dirs, regexps ;
 
207
    StringTokenizer st ;
 
208
    Path path ;
 
209
 
 
210
    // For each regular expression. //
 
211
    dirs = new LinkedList<String>() ;
 
212
    regexps = new LinkedList<String>() ;
 
213
    jhlib = getJhall() ;
 
214
    st = new StringTokenizer( jhlib, "  \n,") ;
 
215
    while( st.hasMoreTokens()) {
 
216
      regexp = st.nextToken() ;
 
217
 
 
218
      // Break the regular expression up into directory and file //
 
219
      // components.                                             //
 
220
      idx = regexp.lastIndexOf( "/") ;
 
221
      dir = regexp.substring( 0, idx) ;
 
222
      file = new File( dir) ;
 
223
      if( file.exists()) {
 
224
        dirs.add( dir) ;
 
225
        regexps.add( regexp.substring( idx+1)) ;
 
226
      }
 
227
    }
 
228
 
 
229
    if( dirs.size() > 0) {
 
230
      path = jhindexer.createClasspath() ;
 
231
      for( i = 0; i < dirs.size(); i++) {
 
232
        dir = dirs.get( i) ;
 
233
        regexp = regexps.get( i) ;
 
234
        fs = new FileSet() ;
 
235
        fs.setDir( new File( dir)) ;
 
236
        fs.setIncludes( regexp) ;
 
237
        path.addFileset( fs) ;
 
238
      }
 
239
    }
 
240
    else {
 
241
      throw new BuildException( "jhall not found.") ;
 
242
    }
 
243
  }
 
244
 
 
245
  protected class LocHelpsetFilter implements FilenameFilter {
 
246
    protected String locale = null ;
 
247
 
 
248
    public LocHelpsetFilter( String loc) {
 
249
      locale = loc ;
 
250
    }
 
251
 
 
252
    public boolean accept(File dir,
 
253
                          String name) {
 
254
      return( name.endsWith( "_" + locale + ".hs")) ;
 
255
    }
 
256
  }
 
257
 
 
258
}
 
259