1 package org.apache.maven.doxia.docrenderer;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.BufferedReader;
23 import java.io.File;
24 import java.io.IOException;
25 import java.io.Reader;
26 import java.io.StringReader;
27 import java.io.StringWriter;
28 import java.util.Arrays;
29 import java.util.Collection;
30 import java.util.HashMap;
31 import java.util.Iterator;
32 import java.util.LinkedHashMap;
33 import java.util.LinkedList;
34 import java.util.List;
35 import java.util.Locale;
36 import java.util.Map;
37
38 import org.apache.maven.doxia.Doxia;
39 import org.apache.maven.doxia.document.DocumentModel;
40 import org.apache.maven.doxia.document.io.xpp3.DocumentXpp3Reader;
41 import org.apache.maven.doxia.sink.Sink;
42 import org.apache.maven.doxia.parser.ParseException;
43 import org.apache.maven.doxia.parser.Parser;
44 import org.apache.maven.doxia.parser.manager.ParserNotFoundException;
45 import org.apache.maven.doxia.logging.PlexusLoggerWrapper;
46 import org.apache.maven.doxia.module.site.SiteModule;
47 import org.apache.maven.doxia.module.site.manager.SiteModuleManager;
48 import org.apache.maven.doxia.util.XmlValidator;
49
50 import org.apache.velocity.VelocityContext;
51 import org.apache.velocity.context.Context;
52
53 import org.codehaus.plexus.component.annotations.Requirement;
54 import org.codehaus.plexus.logging.AbstractLogEnabled;
55
56 import org.codehaus.plexus.util.DirectoryScanner;
57 import org.codehaus.plexus.util.FileUtils;
58 import org.codehaus.plexus.util.IOUtil;
59 import org.codehaus.plexus.util.ReaderFactory;
60 import org.codehaus.plexus.util.xml.XmlStreamReader;
61 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
62 import org.codehaus.plexus.velocity.SiteResourceLoader;
63 import org.codehaus.plexus.velocity.VelocityComponent;
64
65
66
67
68
69
70
71
72
73 public abstract class AbstractDocumentRenderer
74 extends AbstractLogEnabled
75 implements DocumentRenderer
76 {
77 @Requirement
78 protected SiteModuleManager siteModuleManager;
79
80 @Requirement
81 protected Doxia doxia;
82
83 @Requirement
84 private VelocityComponent velocity;
85
86
87
88
89 private String baseDir;
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106 public abstract void render( Map<String, SiteModule> filesToProcess, File outputDirectory,
107 DocumentModel documentModel )
108 throws DocumentRendererException, IOException;
109
110
111
112
113
114
115 public void render( Collection<String> files, File outputDirectory, DocumentModel documentModel )
116 throws DocumentRendererException, IOException
117 {
118 render( getFilesToProcess( files ), outputDirectory, documentModel, null );
119 }
120
121
122 public void render( File baseDirectory, File outputDirectory, DocumentModel documentModel )
123 throws DocumentRendererException, IOException
124 {
125 render( baseDirectory, outputDirectory, documentModel, null );
126 }
127
128
129
130
131
132
133
134
135
136
137
138
139 public void render( Map<String, SiteModule> filesToProcess, File outputDirectory, DocumentModel documentModel,
140 DocumentRendererContext context )
141 throws DocumentRendererException, IOException
142 {
143
144 }
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161 public void render( File baseDirectory, File outputDirectory, DocumentModel documentModel,
162 DocumentRendererContext context )
163 throws DocumentRendererException, IOException
164 {
165 render( getFilesToProcess( baseDirectory ), outputDirectory, documentModel, context );
166 }
167
168
169
170
171
172
173
174
175
176
177
178
179 public void render( File baseDirectory, File outputDirectory )
180 throws DocumentRendererException, IOException
181 {
182 render( baseDirectory, outputDirectory, (DocumentModel) null );
183 }
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198 public void render( File baseDirectory, File outputDirectory, File documentDescriptor )
199 throws DocumentRendererException, IOException
200 {
201 if ( ( documentDescriptor == null ) || ( !documentDescriptor.exists() ) )
202 {
203 getLogger().warn( "No documentDescriptor found: using default settings!" );
204
205 render( baseDirectory, outputDirectory );
206 }
207 else
208 {
209 render( getFilesToProcess( baseDirectory ), outputDirectory, readDocumentModel( documentDescriptor ), null );
210 }
211 }
212
213
214
215
216
217
218
219
220
221
222
223
224 public void renderIndividual( Map<String, SiteModule> filesToProcess, File outputDirectory )
225 throws DocumentRendererException, IOException
226 {
227
228 }
229
230
231
232
233
234
235
236
237
238
239
240
241 public void renderIndividual( Map<String, SiteModule> filesToProcess, File outputDirectory,
242 DocumentRendererContext context )
243 throws DocumentRendererException, IOException
244 {
245
246 }
247
248
249
250
251
252
253
254
255
256
257
258 public Map<String, SiteModule> getFilesToProcess( File baseDirectory )
259 throws IOException, DocumentRendererException
260 {
261 if ( !baseDirectory.isDirectory() )
262 {
263 getLogger().warn( "No files found to process!" );
264
265 return new HashMap<String, SiteModule>();
266 }
267
268 setBaseDir( baseDirectory.getAbsolutePath() );
269
270 Map<String, SiteModule> filesToProcess = new LinkedHashMap<String, SiteModule>();
271 Map<String, String> duplicatesFiles = new LinkedHashMap<String, String>();
272
273 Collection<SiteModule> modules = siteModuleManager.getSiteModules();
274 for ( SiteModule module : modules )
275 {
276 File moduleBasedir = new File( baseDirectory, module.getSourceDirectory() );
277
278 if ( moduleBasedir.exists() )
279 {
280
281 @SuppressWarnings ( "unchecked" )
282 List<String> allFiles = FileUtils.getFileNames( moduleBasedir, "**/*.*", null, false );
283
284 String lowerCaseExtension = module.getExtension().toLowerCase( Locale.ENGLISH );
285 List<String> docs = new LinkedList<String>( allFiles );
286
287 for ( Iterator<String> it = docs.iterator(); it.hasNext(); )
288 {
289 String name = it.next().trim();
290
291 if ( !name.toLowerCase( Locale.ENGLISH ).endsWith( "." + lowerCaseExtension ) )
292 {
293 it.remove();
294 }
295 }
296
297 List<String> velocityFiles = new LinkedList<String>( allFiles );
298
299 for ( Iterator<String> it = velocityFiles.iterator(); it.hasNext(); )
300 {
301 String name = it.next().trim();
302
303 if ( !name.toLowerCase( Locale.ENGLISH ).endsWith( lowerCaseExtension + ".vm" ) )
304 {
305 it.remove();
306 }
307 }
308 docs.addAll( velocityFiles );
309
310 for ( String filePath : docs )
311 {
312 filePath = filePath.trim();
313
314 if ( filePath.lastIndexOf( '.') > 0 )
315 {
316 String key = filePath.substring( 0, filePath.lastIndexOf( '.') );
317
318 if ( duplicatesFiles.containsKey( key ) )
319 {
320 throw new DocumentRendererException( "Files '" + module.getSourceDirectory()
321 + File.separator + filePath + "' clashes with existing '"
322 + duplicatesFiles.get( key ) + "'." );
323 }
324
325 duplicatesFiles.put( key, module.getSourceDirectory() + File.separator + filePath );
326 }
327
328 filesToProcess.put( filePath, module );
329 }
330 }
331 }
332
333 return filesToProcess;
334 }
335
336
337
338
339
340
341
342
343 public Map<String, SiteModule> getFilesToProcess( Collection<String> files )
344 {
345
346
347
348
349 Map<String, SiteModule> filesToProcess = new HashMap<String, SiteModule>();
350
351 Collection<SiteModule> modules = siteModuleManager.getSiteModules();
352 for ( SiteModule siteModule : modules )
353 {
354 String extension = "." + siteModule.getExtension();
355
356 String sourceDirectory = File.separator + siteModule.getSourceDirectory() + File.separator;
357
358 for ( String file : files )
359 {
360
361
362
363 if ( file.indexOf( sourceDirectory ) != -1 )
364 {
365 filesToProcess.put( file, siteModule );
366 }
367 else if ( file.toLowerCase( Locale.ENGLISH ).endsWith( extension ) )
368 {
369
370 if ( !filesToProcess.containsKey( file ) )
371 {
372 filesToProcess.put( file, siteModule );
373 }
374 }
375 }
376 }
377
378 return filesToProcess;
379 }
380
381
382 public DocumentModel readDocumentModel( File documentDescriptor )
383 throws DocumentRendererException, IOException
384 {
385 DocumentModel documentModel;
386
387 Reader reader = null;
388 try
389 {
390 reader = ReaderFactory.newXmlReader( documentDescriptor );
391 documentModel = new DocumentXpp3Reader().read( reader );
392 }
393 catch ( XmlPullParserException e )
394 {
395 throw new DocumentRendererException( "Error parsing document descriptor", e );
396 }
397 finally
398 {
399 IOUtil.close( reader );
400 }
401
402 return documentModel;
403 }
404
405
406
407
408
409
410 public void setBaseDir( String newDir )
411 {
412 this.baseDir = newDir;
413 }
414
415
416
417
418
419
420 public String getBaseDir()
421 {
422 return this.baseDir;
423 }
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439 protected void parse( String fullDocPath, String parserId, Sink sink )
440 throws DocumentRendererException, IOException
441 {
442 parse( fullDocPath, parserId, sink, null );
443 }
444
445
446
447
448
449
450
451
452
453
454
455 protected void parse( String fullDocPath, String parserId, Sink sink, DocumentRendererContext context )
456 throws DocumentRendererException, IOException
457 {
458 if ( getLogger().isDebugEnabled() )
459 {
460 getLogger().debug( "Parsing file " + fullDocPath );
461 }
462
463 Reader reader = null;
464 try
465 {
466 File f = new File( fullDocPath );
467
468 Parser parser = doxia.getParser( parserId );
469 switch ( parser.getType() )
470 {
471 case Parser.XML_TYPE:
472 reader = ReaderFactory.newXmlReader( f );
473
474 if ( isVelocityFile( f ) )
475 {
476 reader = getVelocityReader( f, ( (XmlStreamReader) reader ).getEncoding(), context );
477 }
478 if ( context != null && Boolean.TRUE.equals( (Boolean) context.get( "validate" ) ) )
479 {
480 reader = validate( reader, fullDocPath );
481 }
482 break;
483
484 case Parser.TXT_TYPE:
485 case Parser.UNKNOWN_TYPE:
486 default:
487 if ( isVelocityFile( f ) )
488 {
489 reader =
490 getVelocityReader( f, ( context == null ? ReaderFactory.FILE_ENCODING
491 : context.getInputEncoding() ), context );
492 }
493 else
494 {
495 if ( context == null )
496 {
497 reader = ReaderFactory.newPlatformReader( f );
498 }
499 else
500 {
501 reader = ReaderFactory.newReader( f, context.getInputEncoding() );
502 }
503 }
504 }
505
506 sink.enableLogging( new PlexusLoggerWrapper( getLogger() ) );
507
508 doxia.parse( reader, parserId, sink );
509 }
510 catch ( ParserNotFoundException e )
511 {
512 throw new DocumentRendererException( "No parser '" + parserId
513 + "' found for " + fullDocPath + ": " + e.getMessage(), e );
514 }
515 catch ( ParseException e )
516 {
517 throw new DocumentRendererException( "Error parsing " + fullDocPath + ": " + e.getMessage(), e );
518 }
519 finally
520 {
521 IOUtil.close( reader );
522
523 sink.flush();
524 }
525 }
526
527
528
529
530
531
532
533 protected void copyResources( File outputDirectory )
534 throws IOException
535 {
536 File resourcesDirectory = new File( getBaseDir(), "resources" );
537
538 if ( !resourcesDirectory.isDirectory() )
539 {
540 return;
541 }
542
543 if ( !outputDirectory.exists() )
544 {
545 outputDirectory.mkdirs();
546 }
547
548 copyDirectory( resourcesDirectory, outputDirectory );
549 }
550
551
552
553
554
555
556
557
558 protected void copyDirectory( File source, File destination )
559 throws IOException
560 {
561 if ( source.isDirectory() && destination.isDirectory() )
562 {
563 DirectoryScanner scanner = new DirectoryScanner();
564
565 String[] includedResources = {"**/**"};
566
567 scanner.setIncludes( includedResources );
568
569 scanner.addDefaultExcludes();
570
571 scanner.setBasedir( source );
572
573 scanner.scan();
574
575 List<String> includedFiles = Arrays.asList( scanner.getIncludedFiles() );
576
577 for ( String name : includedFiles )
578 {
579 File sourceFile = new File( source, name );
580
581 File destinationFile = new File( destination, name );
582
583 FileUtils.copyFile( sourceFile, destinationFile );
584 }
585 }
586 }
587
588
589
590
591
592
593
594
595
596 protected String getOutputName( DocumentModel documentModel )
597 {
598 String outputName = documentModel.getOutputName();
599 if ( outputName == null )
600 {
601 getLogger().info( "No outputName is defined in the document descriptor. Using 'target'" );
602
603 documentModel.setOutputName( "target" );
604 }
605
606 outputName = outputName.trim();
607 if ( outputName.toLowerCase( Locale.ENGLISH ).endsWith( "." + getOutputExtension() ) )
608 {
609 outputName =
610 outputName.substring( 0, outputName.toLowerCase( Locale.ENGLISH )
611 .lastIndexOf( "." + getOutputExtension() ) );
612 }
613 documentModel.setOutputName( outputName );
614
615 return documentModel.getOutputName();
616 }
617
618
619
620
621
622
623
624
625
626
627 private Reader getVelocityReader( File f, String encoding, DocumentRendererContext context )
628 throws DocumentRendererException
629 {
630 if ( getLogger().isDebugEnabled() )
631 {
632 getLogger().debug( "Velocity render for " + f.getAbsolutePath() );
633 }
634
635 SiteResourceLoader.setResource( f.getAbsolutePath() );
636
637 Context velocityContext = new VelocityContext();
638
639 if ( context.getKeys() != null )
640 {
641 for ( int i = 0; i < context.getKeys().length; i++ )
642 {
643 String key = (String) context.getKeys()[i];
644
645 velocityContext.put( key, context.get( key ) );
646 }
647 }
648
649 StringWriter sw = new StringWriter();
650 try
651 {
652 velocity.getEngine().mergeTemplate( f.getAbsolutePath(), encoding, velocityContext, sw );
653 }
654 catch ( Exception e )
655 {
656 throw new DocumentRendererException( "Error whenn parsing Velocity file " + f.getAbsolutePath() + ": "
657 + e.getMessage(), e );
658 }
659
660 return new StringReader( sw.toString() );
661 }
662
663
664
665
666
667 private static boolean isVelocityFile( File f )
668 {
669 return FileUtils.getExtension( f.getAbsolutePath() ).toLowerCase( Locale.ENGLISH ).endsWith( "vm" );
670 }
671
672 private Reader validate( Reader source, String resource )
673 throws ParseException, IOException
674 {
675 getLogger().debug( "Validating: " + resource );
676
677 try
678 {
679 String content = IOUtil.toString( new BufferedReader( source ) );
680
681 new XmlValidator( new PlexusLoggerWrapper( getLogger() ) ).validate( content );
682
683 return new StringReader( content );
684 }
685 finally
686 {
687 IOUtil.close( source );
688 }
689 }
690 }