Index: pom.xml =================================================================== --- pom.xml (revision 3044) +++ pom.xml (working copy) @@ -83,6 +83,11 @@ 2.6.2 runtime + + org.apache.xmlgraphics + fop + 0.92beta + Index: src/main/java/org/codehaus/mojo/docbook/DocumentTransformer.java =================================================================== --- src/main/java/org/codehaus/mojo/docbook/DocumentTransformer.java (revision 3044) +++ src/main/java/org/codehaus/mojo/docbook/DocumentTransformer.java (working copy) @@ -23,8 +23,12 @@ */ package org.codehaus.mojo.docbook; +import java.io.BufferedOutputStream; import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; import java.io.IOException; +import java.io.OutputStream; import java.net.URI; import java.net.URISyntaxException; import java.util.Arrays; @@ -38,9 +42,14 @@ import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.TransformerFactoryConfigurationError; +import javax.xml.transform.sax.SAXResult; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; +import org.apache.fop.apps.FOPException; +import org.apache.fop.apps.Fop; +import org.apache.fop.apps.FopFactory; +import org.apache.fop.apps.MimeConstants; import org.apache.maven.plugin.logging.Log; import org.codehaus.plexus.compiler.util.scan.InclusionScanException; import org.codehaus.plexus.compiler.util.scan.StaleSourceScanner; @@ -65,6 +74,10 @@ protected URI stylesheetLocation; + private boolean generateHtml; + + private boolean generatePdf; + /** * @param log * @param sourceDirectory @@ -94,7 +107,14 @@ { StaleSourceScanner scanner = new StaleSourceScanner( 0, Collections.singleton( "**/*.xml" ), Collections.EMPTY_SET ); - scanner.addSourceMapping( new SuffixMapping( ".xml", ".html" ) ); + if (generateHtml) + { + scanner.addSourceMapping( new SuffixMapping( ".xml", ".html" ) ); + } + if ( generatePdf ) + { + scanner.addSourceMapping( new SuffixMapping( ".xml", ".pdf" ) ); + } Set staleDocbookFiles = scanner.getIncludedSources( this.sourceDirectory, this.outputDirectory ); if ( staleDocbookFiles.size() > 0 ) @@ -140,37 +160,94 @@ throws TransformerException, URISyntaxException { this.log.info( "Transforming " + docbookFiles.size() + " Docbook stale file(s)" ); - Source docbookStyleSheetSource = new StreamSource( this.stylesheetLocation.resolve( "xhtml/docbook.xsl" ) - .toString() ); - Transformer documentTransformer = TransformerFactory.newInstance().newTransformer( docbookStyleSheetSource ); + Transformer xhtmlTransformer = null; + Transformer xslfoTransformer = null; + FopFactory fopFactory = null; URI olinkDBURI = new File( this.databaseDirectory + File.separator + "olinkdb.xml" ).toURI(); - documentTransformer.setParameter( "target.database.document", olinkDBURI.toString() ); - documentTransformer.setParameter( "generate.toc", "" ); + if ( generateHtml ) + { + Source docbookStyleSheetSource = new StreamSource( this.stylesheetLocation.resolve( "xhtml/docbook.xsl" ) + .toString() ); + xhtmlTransformer = TransformerFactory.newInstance().newTransformer( docbookStyleSheetSource ); + xhtmlTransformer.setParameter( "target.database.document", olinkDBURI.toString() ); + xhtmlTransformer.setParameter( "generate.toc", "" ); + } + if ( generatePdf ) + { + Source docbookStyleSheetSource = new StreamSource( this.stylesheetLocation.resolve( "fo/docbook.xsl" ) + .toString() ); + xslfoTransformer = TransformerFactory.newInstance().newTransformer( docbookStyleSheetSource ); + xslfoTransformer.setParameter( "target.database.document", olinkDBURI.toString() ); + } + this.log.debug( "Style sheet loaded." ); Iterator filesIterator = docbookFiles.iterator(); + if ( generatePdf ) + { + fopFactory = FopFactory.newInstance(); + } while ( filesIterator.hasNext() ) { File docbookFile = (File) filesIterator.next(); - this.log.debug( "Processing " + this.sourceDirectory + File.separator + docbookFile ); - Source source = new StreamSource( docbookFile ); - String relativePath = docbookFile.getAbsolutePath().substring( - (int) this.sourceDirectory.getAbsolutePath() - .length() ); - File resultFile = new File( this.outputDirectory, relativePath - .substring( 0, relativePath.lastIndexOf( '.' ) ) - + ".html" ); - Result result = new StreamResult( resultFile.getAbsolutePath() ); - - documentTransformer.setParameter( "current.docid", OLinkDBUpdater.computeFileID( relativePath ) ); - // TODO: Parametrize this !!!! - documentTransformer - .setParameter( "html.stylesheet", this.pathToResources( relativePath ) + "css/xhtml.css" ); - documentTransformer.transform( source, result ); - this.log.debug( "Generated " + this.databaseDirectory + File.separator + docbookFile ); + if ( generateHtml ) + { + transformXhtml( xhtmlTransformer, docbookFile ); + } + if ( generatePdf ) + { + transformPdf( xslfoTransformer, docbookFile, fopFactory ); + } } } + private void transformXhtml(Transformer documentTransformer, File docbookFile) throws TransformerException { + this.log.debug( "Processing " + this.sourceDirectory + File.separator + docbookFile ); + Source source = new StreamSource( docbookFile ); + String relativePath = docbookFile.getAbsolutePath().substring( + (int) this.sourceDirectory.getAbsolutePath() + .length() ); + File resultFile = new File( this.outputDirectory, relativePath + .substring( 0, relativePath.lastIndexOf( '.' ) ) + + ".html" ); + Result result = new StreamResult( resultFile.getAbsolutePath() ); + + documentTransformer.setParameter( "current.docid", OLinkDBUpdater.computeFileID( relativePath ) ); + // TODO: Parametrize this !!!! + documentTransformer + .setParameter( "html.stylesheet", this.pathToResources( relativePath ) + "css/xhtml.css" ); + documentTransformer.transform( source, result ); + this.log.debug( "Generated " + this.databaseDirectory + File.separator + docbookFile ); + } + + private void transformPdf(Transformer documentTransformer, File docbookFile, FopFactory fopFactory) throws TransformerException { + this.log.debug( "Processing " + this.sourceDirectory + File.separator + docbookFile ); + Source source = new StreamSource( docbookFile ); + String relativePath = docbookFile.getAbsolutePath().substring( + (int) this.sourceDirectory.getAbsolutePath() + .length() ); + File resultFile = new File( this.outputDirectory, relativePath + .substring( 0, relativePath.lastIndexOf( '.' ) ) + + ".pdf" ); + documentTransformer.setParameter( "current.docid", OLinkDBUpdater.computeFileID( relativePath ) ); + try { + OutputStream out = new BufferedOutputStream(new FileOutputStream(resultFile.getAbsolutePath())); + try { + Fop fop = fopFactory.newFop( MimeConstants.MIME_PDF, out ); + Result intermediate = new SAXResult( fop.getDefaultHandler() ); + + documentTransformer.transform( source, intermediate ); + } catch ( FOPException e ) { + this.log.error( e ); + } finally { + out.close(); + } + } catch (Exception e) { + this.log.error( e ); + } + this.log.debug( "Generated " + this.databaseDirectory + File.separator + docbookFile ); + } + protected String pathToResources( String relativePath ) { StringBuffer pathToResources = new StringBuffer(); @@ -182,4 +259,18 @@ } return pathToResources.toString(); } + + /** + * Enables a specified output format. + * @param format the format + */ + public void enableOutputFormat(String format) { + if ( "xhtml".equalsIgnoreCase(format) ) { + generateHtml = true; + } + if ( "pdf".equalsIgnoreCase(format) ) { + generatePdf = true; + } + + } } Index: src/main/java/org/codehaus/mojo/docbook/TransformMojo.java =================================================================== --- src/main/java/org/codehaus/mojo/docbook/TransformMojo.java (revision 3044) +++ src/main/java/org/codehaus/mojo/docbook/TransformMojo.java (working copy) @@ -40,9 +40,7 @@ import org.codehaus.plexus.compiler.util.scan.InclusionScanException; /** - * Transforms a set of Docbook files into XHTML output. Currently there is only support for - * XHTML output, though is planned to add all kind of outputs available in the standard - * stylesheets. + * Transforms a set of Docbook files into XHTML and PDF output. * * @author jgonzalez * @goal transform @@ -93,6 +91,15 @@ * @parameter expression="${outputEncoding}" default-value="UTF-8" */ private String outputEncoding; + + /** + * Specifies the list of desired output formats. Example: + * <outputFormats> + * <param>xhtml</param> + * <param>pdf</param> + * </outputFormats> + */ + private String[] outputFormats = new String[] { "xhtml", "pdf" }; /** * Specifies the stylesheet location, useful if you want to use a local copy or a @@ -144,6 +151,9 @@ this.databaseDirectory, this.outputDirectory, stylesheetLocationURI ); + for ( int i = 0; i < outputFormats.length; i++) { + documentTransformer.enableOutputFormat( outputFormats[i] ); + } olinkDBUpdater.update(); documentTransformer.transform(); }