1 package org.apache.maven.jxr;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.jxr.ant.DirectoryScanner;
23 import org.apache.maven.jxr.log.Log;
24 import org.apache.maven.jxr.pacman.FileManager;
25 import org.apache.maven.jxr.pacman.PackageManager;
26
27 import java.io.File;
28 import java.io.FileNotFoundException;
29 import java.io.IOException;
30 import java.util.Iterator;
31 import java.util.LinkedList;
32 import java.util.List;
33 import java.util.Locale;
34
35
36
37
38
39
40
41 public class JXR
42 {
43
44
45
46 private Log log;
47
48
49
50
51 private static final String[] DEFAULT_INCLUDES = {"**/*.java"};
52
53
54
55
56 private String dest = "";
57
58 private Locale locale;
59
60 private String inputEncoding;
61
62 private String outputEncoding;
63
64
65
66
67 private String javadocLinkDir;
68
69
70
71
72
73 private JavaCodeTransform transformer;
74
75
76
77
78 private String revision;
79
80
81
82
83 private String[] excludes = null;
84
85
86
87
88 private String[] includes = DEFAULT_INCLUDES;
89
90
91
92
93
94
95
96
97
98 public void processPath( PackageManager packageManager, String source, String bottom )
99 throws IOException
100 {
101 this.transformer = new JavaCodeTransform( packageManager );
102
103 DirectoryScanner ds = new DirectoryScanner();
104
105
106 ds.setExcludes( excludes );
107 ds.setIncludes( includes );
108 ds.addDefaultExcludes();
109
110 File dir = new File( source );
111
112 if ( !dir.exists() )
113 {
114 if ( !dir.mkdirs() )
115 {
116 throw new IllegalStateException(
117 "Your source directory does not exist and could not be created:" + source );
118 }
119 }
120
121 ds.setBasedir( source );
122 ds.scan();
123
124
125
126 String[] files = ds.getIncludedFiles();
127
128 for ( int i = 0; i < files.length; ++i )
129 {
130 String src = source + System.getProperty( "file.separator" ) + files[i];
131
132 if ( isJavaFile( src ) )
133 {
134 transform( src, getDestination( source, src ), bottom );
135 }
136
137 }
138 }
139
140
141
142
143
144
145
146 public static boolean isJavaFile( String filename )
147 {
148 File file = new File( filename );
149 return filename.endsWith( ".java" ) && file.length() > 0;
150 }
151
152
153
154
155
156
157
158 public static boolean isHtmlFile( String filename )
159 {
160 return filename.endsWith( ".html" );
161 }
162
163
164
165
166
167
168 public String getDest()
169 {
170 return this.dest;
171 }
172
173
174
175
176 public void setDest( String dest )
177 {
178 this.dest = dest;
179 }
180
181
182
183
184 public void setLocale( Locale locale )
185 {
186 this.locale = locale;
187 }
188
189
190
191
192 public void setInputEncoding( String inputEncoding )
193 {
194 this.inputEncoding = inputEncoding;
195 }
196
197
198
199
200 public void setOutputEncoding( String outputEncoding )
201 {
202 this.outputEncoding = outputEncoding;
203 }
204
205
206
207
208 public void setJavadocLinkDir( String javadocLinkDir )
209 {
210
211 this.javadocLinkDir = javadocLinkDir;
212 }
213
214
215
216
217 public void setTransformer( JavaCodeTransform transformer )
218 {
219 this.transformer = transformer;
220 }
221
222
223
224
225 public void setRevision( String revision )
226 {
227 this.revision = revision;
228 }
229
230
231
232
233 public void setLog( Log log )
234 {
235 this.log = log;
236 }
237
238
239
240
241
242
243
244
245
246
247 public void xref( List sourceDirs, String templateDir, String windowTitle, String docTitle, String bottom )
248 throws IOException, JxrException
249 {
250
251 FileManager fileManager = new FileManager();
252 fileManager.setEncoding( inputEncoding );
253
254 PackageManager pkgmgr = new PackageManager( log, fileManager );
255 pkgmgr.setExcludes( excludes );
256 pkgmgr.setIncludes( includes );
257
258
259 for ( Iterator i = sourceDirs.iterator(); i.hasNext(); )
260 {
261 String path = (String) i.next();
262 path = new File( path ).getCanonicalPath();
263
264 pkgmgr.process( path );
265
266 processPath( pkgmgr, path, bottom );
267 }
268
269
270 DirectoryIndexer indexer = new DirectoryIndexer( pkgmgr, dest );
271 indexer.setOutputEncoding( outputEncoding );
272 indexer.setTemplateDir( templateDir );
273 indexer.setWindowTitle( windowTitle );
274 indexer.setDocTitle( docTitle );
275 indexer.setBottom( bottom );
276 indexer.process( log );
277 }
278
279
280
281
282
283
284
285
286
287
288
289
290
291 private String getDestination( String source, String filename )
292 {
293
294
295 String dest = filename.substring( source.length(), filename.length() );
296
297 int start = 0;
298 int end = dest.indexOf( ".java" );
299
300 if ( end != -1 )
301 {
302
303 dest = dest.substring( start, end );
304 }
305
306
307 dest = this.getDest() + dest;
308
309
310
311 dest = dest + ".html";
312
313 return dest;
314 }
315
316
317
318
319
320
321
322
323
324
325 private void transform( String source, String dest, String bottom )
326 throws IOException
327 {
328 log.debug( source + " -> " + dest );
329
330
331 String javadoc = javadocLinkDir != null ? getRelativeLink( dest, javadocLinkDir ) : null;
332 transformer.transform( source, dest, locale, inputEncoding, outputEncoding, javadoc,
333 this.revision, bottom );
334 }
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350 private static String getRelativeLink( String fromDir, String toDir )
351 throws IOException
352 {
353 StringBuffer toLink = new StringBuffer();
354 StringBuffer fromLink = new StringBuffer();
355
356
357 List parents = new LinkedList();
358 File f = new File( toDir );
359 f = f.getCanonicalFile();
360 while ( f != null )
361 {
362 parents.add( f );
363 f = f.getParentFile();
364 }
365
366
367 f = new File( fromDir );
368 if ( !f.isDirectory() )
369 {
370
371 f = f.getParentFile();
372 }
373 f = f.getCanonicalFile();
374 f = f.getParentFile();
375 boolean found = false;
376 while ( f != null && !found )
377 {
378 for ( int i = 0; i < parents.size(); ++i )
379 {
380 File parent = (File) parents.get( i );
381 if ( f.equals( parent ) )
382 {
383
384
385 for ( int j = 0; j < i; ++j )
386 {
387 File p = (File) parents.get( j );
388 toLink.insert( 0, p.getName() + "/" );
389 }
390 found = true;
391 break;
392 }
393 }
394 f = f.getParentFile();
395 fromLink.append( "../" );
396 }
397
398 if ( !found )
399 {
400 throw new FileNotFoundException( fromDir + " and " + toDir + " have no common parent." );
401 }
402
403 return fromLink.append( toLink.toString() ).toString();
404 }
405
406 public void setExcludes( String[] excludes )
407 {
408 this.excludes = excludes;
409 }
410
411
412 public void setIncludes( String[] includes )
413 {
414 if ( includes == null )
415 {
416
417 this.includes = DEFAULT_INCLUDES;
418 }
419 else
420 {
421 this.includes = includes;
422 }
423 }
424 }