1 /* -------------------------------------------------------------------
2 * Java source file for the class JavaHelp
3 *
4 * Copyright (c), 2002, Masahiro Takatsuka.
5 * All Rights Researved.
6 *
7 * Original Author: Masahiro Takatsuka (masa@jbeans.net)
8 * $Author: takatsukam $
9 *
10 * $Date: 2003/07/26 13:20:03 $
11 *
12 * $Id: XMLJavaHelp.java,v 1.2 2003/07/26 13:20:03 takatsukam Exp $
13 *
14 * Reference: Document no:
15 * ___ ___
16 *
17 * To Do:
18 * ___
19 *
20 ------------------------------------------------------------------- */
21
22 /* --------------------------- Package ---------------------------- */
23 package net.jbeans.ant.taskdef;
24
25 /* ------------------ Import classes (packages) ------------------- *//package-summary/html">color="#329900"> ------------------ Import classes (packages) ------------------- *//package-summary.html">color="#329900">/* ------------------ Import classes (packages) ------------------- *//package-summary.html">color="#329900"> ------------------ Import classes (packages) ------------------- */
26 import java.io.*;
27 import java.util.*;
28
29 import org.apache.tools.ant.*;
30 import org.apache.tools.ant.taskdefs.*;
31 import org.apache.tools.ant.types.*;
32 import org.apache.tools.ant.util.*;
33 import org.apache.xalan.processor.TransformerFactoryImpl;
34
35 /*====================================================================
36 Implementation of class JavaHelp
37 ====================================================================*/
38 /***
39 * This task makes it easy to generate JavaHelp system files for a
40 * given DocBook xml code.
41 *
42 * @version $Revision: 1.2 $
43 * @author Masahiro Takatsuka (masa@jbeans.net)
44 * @see Task
45 */
46
47 public class XMLJavaHelp extends MatchingTask {
48 private Path src;
49 private File srcDir = null;
50 private File destDir = null;
51 private File[] compileList = new File[0];
52 private String style = null;
53 private String indexer = "com.sun.java.help.search.Indexer";
54 private String locale = "en_US";
55 private String suffix = ".xml";
56
57 public XMLJavaHelp() {
58 super();
59 }
60
61 /***
62 * Set the source directories to find the source Java files.
63 */
64 public void setSrcdir(Path srcDir) {
65 if (this.src == null) {
66 this.src = srcDir;
67 } else {
68 this.src.append(srcDir);
69 }
70 }
71
72 /*** Gets the source dirs to find the source java files. */
73 public Path getSrcdir() {
74 return this.src;
75 }
76
77 public void setDestdir(File dir) {
78 this.destDir = dir;
79 }
80
81 public File getDestdir() {
82 return this.destDir;
83 }
84
85 public void setStyle(String style) {
86 this.style = style;
87 }
88
89 public String getStyle() {
90 return this.style;
91 }
92
93 public void setIndexer(String indexer) {
94 this.indexer = indexer;
95 }
96
97 public String getIndexer() {
98 return this.indexer;
99 }
100
101 public void setLocale(String locale) {
102 this.locale = locale;
103 }
104
105 public String getLocale() {
106 return this.locale;
107 }
108
109 public void setSuffix(String suffix) {
110 this.suffix = suffix;
111 }
112
113 public String getSuffix() {
114 return this.suffix;
115 }
116
117 private void checkParameters() throws BuildException {
118 if (this.style == null) {
119 String msg = "style attribute must be set!";
120 throw new BuildException(msg);
121 }
122
123 if (this.src == null) {
124 throw new BuildException("srcdir attribute must be set!",
125 getLocation());
126
127 }
128 if (src.size() == 0) {
129 throw new BuildException("srcdir attribute must be set!",
130 getLocation());
131 }
132
133 if (destDir != null && !destDir.isDirectory()) {
134 throw new BuildException("destination directory \""
135 + destDir
136 + "\" does not exist "
137 + "or is not a directory", getLocation());
138 }
139
140 }
141
142 /***
143 * Clear the list of files to be compiled and copied..
144 */
145 private void resetFileLists() {
146 this.compileList = new File[0];
147 }
148
149 /***
150 * Scans the directory looking for source files to be compiled.
151 * The results are returned in the class variable compileList
152 */
153 private void scanDir(File srcDir, File destDir, String[] files) {
154 GlobPatternMapper m = new GlobPatternMapper();
155 m.setFrom("*" + this.suffix);
156 m.setTo("*.html"); // javahelp map file
157 SourceFileScanner sfs = new SourceFileScanner(this);
158 File[] newFiles = sfs.restrictAsFiles(files, srcDir, destDir, m);
159
160 if (newFiles.length > 0) {
161 File[] newCompileList = new File[this.compileList.length +
162 newFiles.length];
163 System.arraycopy(this.compileList, 0, newCompileList, 0,
164 this.compileList.length);
165 System.arraycopy(newFiles, 0, newCompileList,
166 this.compileList.length, newFiles.length);
167 this.compileList = newCompileList;
168 }
169 }
170
171 /***
172 * Executes the task.
173 */
174 public void execute() throws BuildException {
175 checkParameters();
176 resetFileLists();
177
178 // scan source directories and dest directory to build up
179 // compile lists
180 String[] list = this.src.list();
181 for (int i = 0; i < list.length; i++) {
182 File srcDir = getProject().resolveFile(list[i]);
183 if (!srcDir.exists()) {
184 throw new BuildException("srcdir \""
185 + srcDir.getPath()
186 + "\" does not exist!", getLocation());
187 }
188
189 DirectoryScanner ds = this.getDirectoryScanner(srcDir);
190 String[] files = ds.getIncludedFiles();
191
192 scanDir(srcDir, destDir != null ? destDir : srcDir, files);
193 }
194
195 compile();
196 }
197
198 private String[] extractDirs(Path path, String filename) {
199 String[] result = null;
200 String[] list = path.list();
201
202 for (int i = 0; i < list.length; i++) {
203 int index = filename.indexOf(list[i]);
204 int index2 = filename.lastIndexOf(File.separator);
205 if (index != -1) {
206 result = new String[2];
207 result[0] = list[i];
208 result[1] = filename.substring(list[i].length() + 1, index2);
209 return result;
210 }
211 }
212 return result;
213 }
214
215 private void compile() {
216 if (this.compileList.length > 0) {
217 log("Compiling " + this.compileList.length +
218 " source file"
219 + (this.compileList.length == 1 ? "" : "s")
220 + (this.destDir != null ? " to " + this.destDir : "")
221 + "using " + this.style);
222 }
223
224 XSLTProcess style = new XSLTProcess();
225 style.setProcessor("trax");
226 style.setProject(project);
227 style.setStyle(this.style);
228
229 for (int i = 0; i < this.compileList.length; i++) {
230 File xmlfile = this.compileList[i];
231 log("Compiling " + xmlfile, Project.MSG_VERBOSE);
232
233 String aPath = xmlfile.getAbsolutePath();
234 String[] dir_name = extractDirs(this.src, aPath);
235 log("aPath " + aPath, Project.MSG_VERBOSE);
236 log("srcdirstr " + dir_name[0], Project.MSG_VERBOSE);
237 log("dstdirstr " + this.destDir+File.separator+dir_name[1], Project.MSG_VERBOSE);
238
239 style.setIn(xmlfile);
240 File outfile = new File(this.destDir, dir_name[1] + File.separator + "dummy");
241 style.setOut(outfile);
242 style.execute();
243
244 //now delete "dummy" file
245 Delete del = new Delete();
246 del.setProject(project);
247 del.setFile(outfile);
248 del.execute();
249
250 // now generate JavaHelp index files.
251 log("Generating JavaHelp index file.", Project.MSG_INFO);
252 Java java = new Java();
253 java.setProject(project);
254 File outDir = new File(this.destDir, dir_name[1]);
255 java.setDir(outDir);
256 java.setClassname(this.indexer);
257 java.setFork(true);
258 java.createArg().setValue("-locale");
259 java.createArg().setValue(this.locale);
260 java.createArg().setValue(".");
261 java.executeJava();
262 }
263 }
264 }
This page was automatically generated by Maven