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