1 package org.apache.maven.plugin.jxr;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.net.URL;
25 import java.util.ArrayList;
26 import java.util.Calendar;
27 import java.util.Collection;
28 import java.util.Iterator;
29 import java.util.List;
30 import java.util.Locale;
31 import java.util.ResourceBundle;
32
33 import org.apache.maven.doxia.siterenderer.Renderer;
34 import org.apache.maven.jxr.JXR;
35 import org.apache.maven.jxr.JxrException;
36 import org.apache.maven.model.Organization;
37 import org.apache.maven.model.ReportPlugin;
38 import org.apache.maven.project.MavenProject;
39 import org.apache.maven.reporting.AbstractMavenReport;
40 import org.apache.maven.reporting.MavenReportException;
41 import org.codehaus.plexus.util.FileUtils;
42 import org.codehaus.plexus.util.ReaderFactory;
43 import org.codehaus.plexus.util.StringUtils;
44
45
46
47
48
49
50
51
52
53 public abstract class AbstractJxrReport
54 extends AbstractMavenReport
55 {
56
57
58
59
60
61 private MavenProject project;
62
63
64
65
66 private Renderer siteRenderer;
67
68
69
70
71
72
73
74
75
76 private File outputDirectory;
77
78
79
80
81
82
83 private String inputEncoding;
84
85
86
87
88
89
90 private String outputEncoding;
91
92
93
94
95
96
97 private String windowTitle;
98
99
100
101
102
103
104 private String docTitle;
105
106
107
108
109
110
111 private String bottom;
112
113
114
115
116
117
118
119
120 private String templateDir;
121
122
123
124
125
126
127
128 private String stylesheet;
129
130
131
132
133
134
135
136 private ArrayList excludes;
137
138
139
140
141
142
143
144 private ArrayList includes;
145
146
147
148
149
150
151
152 protected List reactorProjects;
153
154
155
156
157
158
159 protected boolean aggregate;
160
161
162
163
164
165
166
167 private boolean linkJavadoc;
168
169
170
171
172
173
174
175 protected String getOutputEncoding()
176 {
177 return ( outputEncoding == null ) ? ReaderFactory.UTF_8 : outputEncoding;
178 }
179
180
181
182
183
184
185
186 protected List pruneSourceDirs( List sourceDirs )
187 {
188 List pruned = new ArrayList( sourceDirs.size() );
189 for ( Iterator i = sourceDirs.iterator(); i.hasNext(); )
190 {
191 String dir = (String) i.next();
192 if ( !pruned.contains( dir ) && hasSources( new File( dir ) ) )
193 {
194 pruned.add( dir );
195 }
196 }
197 return pruned;
198 }
199
200
201
202
203 protected void init()
204 {
205
206
207 Collection plugin = project.getReportPlugins();
208 if ( plugin != null )
209 {
210 for ( Iterator iter = plugin.iterator(); iter.hasNext(); )
211 {
212 ReportPlugin reportPlugin = (ReportPlugin) iter.next();
213 if ( "maven-javadoc-plugin".equals( reportPlugin.getArtifactId() ) )
214 {
215 break;
216 }
217 }
218 }
219 }
220
221
222
223
224
225
226
227 private boolean hasSources( File dir )
228 {
229 boolean found = false;
230 if ( dir.exists() && dir.isDirectory() )
231 {
232 File[] files = dir.listFiles();
233 for ( int i = 0; i < files.length && !found; i++ )
234 {
235 File currentFile = files[i];
236 if ( currentFile.isFile() && currentFile.getName().endsWith( ".java" ) )
237 {
238 found = true;
239 }
240 else if ( currentFile.isDirectory() )
241 {
242 boolean hasSources = hasSources( currentFile );
243 if ( hasSources )
244 {
245 found = true;
246 }
247 }
248 }
249 }
250 return found;
251 }
252
253
254
255
256
257
258
259
260
261
262
263
264 private void createXref( Locale locale, String destinationDirectory, List sourceDirs )
265 throws IOException, JxrException
266 {
267 JXR jxr = new JXR();
268 jxr.setDest( destinationDirectory );
269 if ( StringUtils.isEmpty( inputEncoding ) )
270 {
271 String platformEncoding = System.getProperty( "file.encoding" );
272 getLog().warn( "File encoding has not been set, using platform encoding " + platformEncoding
273 + ", i.e. build is platform dependent!" );
274 }
275 jxr.setInputEncoding( inputEncoding );
276 jxr.setLocale( locale );
277 jxr.setLog( new PluginLogAdapter( getLog() ) );
278 jxr.setOutputEncoding( getOutputEncoding() );
279 jxr.setRevision( "HEAD" );
280 jxr.setJavadocLinkDir( getJavadocLocation() );
281
282 if ( excludes != null && !excludes.isEmpty() )
283 {
284 jxr.setExcludes( (String[]) excludes.toArray( new String[0] ) );
285 }
286 if ( includes != null && !includes.isEmpty() )
287 {
288 jxr.setIncludes( (String[]) includes.toArray( new String[0] ) );
289 }
290
291 jxr.xref( sourceDirs, templateDir, windowTitle, docTitle, getBottomText( project.getInceptionYear(), project
292 .getOrganization() ) );
293
294
295 copyRequiredResources( destinationDirectory );
296 }
297
298
299
300
301
302
303
304
305 private String getBottomText( String inceptionYear, Organization organization )
306 {
307 int actualYear = Calendar.getInstance().get( Calendar.YEAR );
308 String year = String.valueOf( actualYear );
309
310 String bottom = StringUtils.replace( this.bottom, "{currentYear}", year );
311
312 if ( inceptionYear == null )
313 {
314 bottom = StringUtils.replace( bottom, "{inceptionYear}-", "" );
315 }
316 else
317 {
318 if ( inceptionYear.equals( year ) )
319 {
320 bottom = StringUtils.replace( bottom, "{inceptionYear}-", "" );
321 }
322 else
323 {
324 bottom = StringUtils.replace( bottom, "{inceptionYear}", inceptionYear );
325 }
326 }
327
328 if ( organization != null && StringUtils.isNotEmpty( organization.getName() ) )
329 {
330 bottom = StringUtils.replace( bottom, "{projectOrganizationName}", organization.getName() );
331 }
332 else
333 {
334 bottom = StringUtils.replace( bottom, " {projectOrganizationName}", "" );
335 }
336
337 return bottom;
338 }
339
340
341
342
343
344
345
346 private void copyRequiredResources( String dir )
347 {
348 File stylesheetFile = new File( stylesheet );
349 File destStylesheetFile = new File( dir, "stylesheet.css" );
350
351 try
352 {
353 if ( stylesheetFile.isAbsolute() )
354 {
355 FileUtils.copyFile( stylesheetFile, destStylesheetFile );
356 }
357 else
358 {
359 URL stylesheetUrl = this.getClass().getClassLoader().getResource( stylesheet );
360 FileUtils.copyURLToFile( stylesheetUrl, destStylesheetFile );
361 }
362 }
363 catch ( IOException e )
364 {
365 getLog().warn( "An error occured while copying the stylesheet to the target directory", e );
366 }
367
368 }
369
370
371
372
373 protected Renderer getSiteRenderer()
374 {
375 return siteRenderer;
376 }
377
378
379
380
381 protected String getOutputDirectory()
382 {
383 return outputDirectory.getAbsolutePath();
384 }
385
386
387
388
389 public MavenProject getProject()
390 {
391 return project;
392 }
393
394
395
396
397
398
399
400 protected ResourceBundle getBundle( Locale locale )
401 {
402 return ResourceBundle.getBundle( "jxr-report", locale, this.getClass().getClassLoader() );
403 }
404
405
406
407
408
409 protected boolean canGenerateReport( List sourceDirs )
410 {
411 boolean canGenerate = !sourceDirs.isEmpty();
412
413 if ( aggregate && !project.isExecutionRoot() )
414 {
415 canGenerate = false;
416 }
417 return canGenerate;
418 }
419
420
421
422
423 protected void executeReport( Locale locale )
424 throws MavenReportException
425 {
426 List sourceDirs = constructSourceDirs();
427 if ( canGenerateReport( sourceDirs ) )
428 {
429
430 init();
431
432 try
433 {
434 createXref( locale, getDestinationDirectory(), sourceDirs );
435 }
436 catch ( JxrException e )
437 {
438 throw new MavenReportException( "Error while generating the HTML source code of the projet.", e );
439 }
440 catch ( IOException e )
441 {
442 throw new MavenReportException( "Error while generating the HTML source code of the projet.", e );
443 }
444 }
445 }
446
447
448
449
450
451
452 protected List constructSourceDirs()
453 {
454 List sourceDirs = new ArrayList( getSourceRoots() );
455 if ( aggregate )
456 {
457 for ( Iterator i = reactorProjects.iterator(); i.hasNext(); )
458 {
459 MavenProject project = (MavenProject) i.next();
460
461 if ( "java".equals( project.getArtifact().getArtifactHandler().getLanguage() ) )
462 {
463 sourceDirs.addAll( getSourceRoots( project ) );
464 }
465 }
466 }
467
468 sourceDirs = pruneSourceDirs( sourceDirs );
469 return sourceDirs;
470 }
471
472
473
474
475 public boolean canGenerateReport()
476 {
477 return canGenerateReport( constructSourceDirs() );
478 }
479
480
481
482
483 public boolean isExternalReport()
484 {
485 return true;
486 }
487
488
489
490
491 private String getJavadocLocation()
492 throws IOException
493 {
494 String location = null;
495 if ( linkJavadoc )
496 {
497
498
499 if ( getJavadocDir().exists() )
500 {
501
502 location = getJavadocDir().getAbsolutePath();
503 }
504 else
505 {
506
507
508
509 String stagingDirectory = System.getProperty( "stagingDirectory" );
510
511 if ( StringUtils.isNotEmpty( stagingDirectory ) )
512 {
513 String javadocDestDir = getJavadocDir().getName();
514 boolean javadocAggregate = Boolean
515 .valueOf( JxrReportUtil.getMavenJavadocPluginBasicOption( project, "aggregate", "false" ) )
516 .booleanValue();
517
518 String structureProject = JxrReportUtil.getStructure( project, false );
519
520 if ( aggregate && javadocAggregate )
521 {
522 File outputDirectory = new File( stagingDirectory, structureProject );
523 location = outputDirectory + "/" + javadocDestDir;
524 }
525 if ( !aggregate && javadocAggregate )
526 {
527 location = stagingDirectory + "/" + javadocDestDir;
528
529 String hierarchy = project.getName();
530
531 MavenProject parent = project.getParent();
532 while ( parent != null )
533 {
534 hierarchy = parent.getName();
535 parent = parent.getParent();
536 }
537 File outputDirectory = new File( stagingDirectory, hierarchy );
538 location = outputDirectory + "/" + javadocDestDir;
539 }
540 if ( aggregate && !javadocAggregate )
541 {
542 getLog().warn(
543 "The JXR plugin is configured to build an aggregated report at the root, "
544 + "not the Javadoc plugin." );
545 }
546 if ( !aggregate && !javadocAggregate )
547 {
548 location = stagingDirectory + "/" + structureProject + "/" + javadocDestDir;
549 }
550 }
551 else
552 {
553 location = getJavadocDir().getAbsolutePath();
554 }
555 }
556
557 if ( location == null )
558 {
559 getLog().warn( "Unable to locate Javadoc to link to - DISABLED" );
560 }
561 }
562
563 return location;
564 }
565
566
567
568
569
570
571 protected abstract String getDestinationDirectory();
572
573
574
575
576
577
578 protected abstract List getSourceRoots();
579
580
581
582
583
584
585
586
587 protected abstract List getSourceRoots( MavenProject project );
588
589
590
591
592
593
594 protected abstract File getJavadocDir();
595 }