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