KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > base > ant > BuildHelpIndex


1 /*******************************************************************************
2  * Copyright (c) 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.help.internal.base.ant;
12
13 import java.io.File JavaDoc;
14
15 import org.apache.tools.ant.BuildException;
16 import org.apache.tools.ant.Task;
17 import org.eclipse.ant.core.AntCorePlugin;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IPath;
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.core.runtime.IStatus;
22 import org.eclipse.core.runtime.NullProgressMonitor;
23 import org.eclipse.core.runtime.Path;
24 import org.eclipse.help.search.HelpIndexBuilder;
25
26 /**
27  * A custom Ant task to pre-build search help index for a plug-in from within an
28  * Ant script.
29  *
30  * @since 3.1
31  */

32
33 public class BuildHelpIndex extends Task {
34     private String JavaDoc manifest;
35
36     private String JavaDoc destination;
37
38     private HelpIndexBuilder builder;
39
40     /**
41      * The default constructor.
42      */

43     public BuildHelpIndex() {
44     }
45
46     /*
47      * (non-Javadoc)
48      *
49      * @see org.apache.tools.ant.Task#execute()
50      */

51     public void execute() throws BuildException {
52         File JavaDoc file = getFile(manifest);
53         if (file == null)
54             throw new BuildException("Manifest not set."); //$NON-NLS-1$
55
File JavaDoc target = getFile(destination);
56         if (target == null)
57             throw new BuildException("Target directory not set."); //$NON-NLS-1$
58
builder = new HelpIndexBuilder();
59         builder.setManifest(file);
60         builder.setDestination(target);
61         IProgressMonitor monitor = (IProgressMonitor) getProject()
62                 .getReferences().get(AntCorePlugin.ECLIPSE_PROGRESS_MONITOR);
63         if (monitor == null)
64             monitor = new NullProgressMonitor();
65         try {
66             builder.execute(monitor);
67         } catch (CoreException e) {
68             if (e.getStatus().getSeverity()==IStatus.ERROR)
69                 throw new BuildException(e.getMessage(), e.getCause());
70             printStatus(e);
71         }
72     }
73
74     private void printStatus(CoreException e) {
75         IStatus status = e.getStatus();
76         System.out.println(e.getMessage());
77         if (status.isMultiStatus()) {
78             IStatus [] children = status.getChildren();
79             for (int i=0; i<children.length; i++) {
80                 IStatus child = children[i];
81                 System.out.println(" "+child.getMessage()); //$NON-NLS-1$
82
}
83         }
84     }
85
86     private File JavaDoc getFile(String JavaDoc fileName) {
87         if (fileName == null)
88             return null;
89         IPath path = new Path(fileName);
90         if (path.isAbsolute())
91             return new File JavaDoc(fileName);
92         File JavaDoc root = getProject().getBaseDir();
93         if (fileName.equals(".") || fileName.equals("./")) //$NON-NLS-1$ //$NON-NLS-2$
94
return root;
95         if (fileName.equals("..") || fileName.equals("../")) //$NON-NLS-1$ //$NON-NLS-2$
96
return root.getParentFile();
97         return new File JavaDoc(root, fileName);
98     }
99
100     /**
101      * The location (relative or absolute) of the manifest file (plugin.xml)
102      * that contains <code>org.eclipse.help.toc</code> extensions. If help
103      * docs that need to be indexed are in the fragment, manifest must point at
104      * the referenced fragment plug-in.
105      *
106      * @param manifest
107      * the plug-in manifest file
108      */

109
110     public void setManifest(String JavaDoc manifest) {
111         this.manifest = manifest;
112     }
113
114     /**
115      * The destination directory where the index will be placed. The final index
116      * directory will be created by appending locale subdirectories and the
117      * index directory name to the destination. For example, for index directory
118      * name defined as 'index', and for ja_Jp locale, the index data will be
119      * created in 'destination/nl/ja/Jp/index'.
120      *
121      * @param destination
122      * the base directory of the search index destination (typically
123      * plug-in or fragment root directory)
124      */

125
126     public void setDestination(String JavaDoc destination) {
127         this.destination = destination;
128     }
129 }
Popular Tags