001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.nbbuild;
043:
044: import java.io.*;
045: import java.util.*;
046:
047: import org.apache.tools.ant.*;
048: import org.apache.tools.ant.taskdefs.*;
049: import org.apache.tools.ant.types.FileSet;
050: import org.apache.tools.ant.types.Path;
051:
052: /** This task runs the JHIndexer task multiple times, once for each
053: * language, and automatically creates the appropriate regular
054: * expressions to choose the files for each language. This task
055: * greatly reduces the amount of Ant code required to run JHIndexer
056: * especially when there are helpsets for multiple languages in the
057: * same directory tree.
058: *
059: * @author Jerry Huth (email: jerry@solidstep.com)
060: */
061: public class LocJHIndexer extends MatchingTask {
062:
063: protected File basedir = null;
064: protected String dbdir = null;
065: protected String locales = null;
066: protected String jhall = null;
067:
068: /** Specify a regular expression to find <samp>jhall.jar</samp>
069: * (JavaHelp tools library).
070: */
071: public void setJhall(String jhall) {
072: this .jhall = jhall;
073: }
074:
075: /** Get the jhall jar file to use. */
076: protected String getJhall() {
077: String ret = null;
078: String prop = null;
079:
080: // Use the attribute if specified. //
081: if (jhall != null) {
082: ret = jhall;
083: } else {
084:
085: // Else look for the global property. //
086: prop = getProject().getProperty("locjhindexer.jhall");
087: if (prop != null) {
088: ret = prop;
089: }
090: }
091:
092: return (ret);
093: }
094:
095: /** Set the location of the docs helpsets' base dir. */
096: public void setBasedir(File dir) {
097: basedir = dir;
098: }
099:
100: /** Set the name of the search database directory (which is under
101: * <samp>basedir/<locale></samp>)
102: */
103: public void setDbdir(String dir) {
104: dbdir = dir;
105: }
106:
107: /** Set a comma-separated list of locales which have helpsets. */
108: public void setLocales(String s) {
109: locales = s;
110: }
111:
112: /** Get the locales for which we'll look for helpsets. */
113: protected String getLocales() {
114: if (locales != null) {
115: return (locales);
116: }
117: return (getProject().getProperty("locjhindexer.locales"));
118: }
119:
120: public void execute() throws BuildException {
121: String locs = getLocales();
122: String helpset_locs = null;
123: StringTokenizer tokenizer = null;
124: String loc = null;
125:
126: if (getJhall() == null)
127: throw new BuildException("Must specify the jhall attribute");
128: if (dbdir == null || dbdir.trim().equals(""))
129: throw new BuildException("Must specify the dbdir attribute");
130: if (basedir == null)
131: throw new BuildException(
132: "Must specify the basedir attribute");
133: if (locs == null || locs.trim().equals(""))
134: throw new BuildException(
135: "Must specify the locales attribute");
136:
137: // I couldn't get it to work unless I explicitly added the task def here. //
138: getProject().addTaskDefinition("jhindexer", JHIndexer.class);
139:
140: // For each locale. //
141: tokenizer = new StringTokenizer(locs, ", ");
142: while (tokenizer.hasMoreTokens()) {
143: loc = tokenizer.nextToken();
144:
145: // If there's a helpset for this locale. //
146: if (hasHelpset(loc)) {
147:
148: // Add it to the list of locales that have helpsets. //
149: if (helpset_locs == null) {
150: helpset_locs = new String(loc);
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 + "/" + "," + locale
192: + "/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: } else {
240: throw new BuildException("jhall not found.");
241: }
242: }
243:
244: protected class LocHelpsetFilter implements FilenameFilter {
245: protected String locale = null;
246:
247: public LocHelpsetFilter(String loc) {
248: locale = loc;
249: }
250:
251: public boolean accept(File dir, String name) {
252: return (name.endsWith("_" + locale + ".hs"));
253: }
254: }
255:
256: }
|