Author: ltheussl Date: Tue Oct 9 12:57:16 2007 New Revision: 583279 URL: http://svn.apache.org/viewvc?rev=583279&view=rev Log: DOXIASITETOOLS-5: Add new DocumentRenderer interface. Clean up package structure. Add FO document renderer.
Added: maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/src/main/java/org/apache/maven/doxia/docrenderer/AbstractDocumentRenderer.java (with props) maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/src/main/java/org/apache/maven/doxia/docrenderer/DocumentRenderer.java (with props) maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/src/main/java/org/apache/maven/doxia/docrenderer/pdf/ maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/src/main/java/org/apache/maven/doxia/docrenderer/pdf/AbstractPdfRenderer.java (with props) maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/src/main/java/org/apache/maven/doxia/docrenderer/pdf/PdfRenderer.java (with props) maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/src/main/java/org/apache/maven/doxia/docrenderer/pdf/fo/ maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/src/main/java/org/apache/maven/doxia/docrenderer/pdf/fo/FoPdfRenderer.java (with props) maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/src/main/java/org/apache/maven/doxia/docrenderer/pdf/itext/ maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/src/main/java/org/apache/maven/doxia/docrenderer/pdf/itext/ITextPdfRenderer.java (with props) Modified: maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/pom.xml Modified: maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/pom.xml URL: http://svn.apache.org/viewvc/maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/pom.xml?rev=583279&r1=583278&r2=583279&view=diff ============================================================================== --- maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/pom.xml (original) +++ maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/pom.xml Tue Oct 9 12:57:16 2007 @@ -88,6 +88,11 @@ <version>${doxiaVersion}</version> </dependency> <dependency> + <groupId>org.apache.maven.doxia</groupId> + <artifactId>doxia-module-fo</artifactId> + <version>${doxiaVersion}</version> + </dependency> + <dependency> <groupId>org.codehaus.plexus</groupId> <artifactId>plexus-utils</artifactId> </dependency> Added: maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/src/main/java/org/apache/maven/doxia/docrenderer/AbstractDocumentRenderer.java URL: http://svn.apache.org/viewvc/maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/src/main/java/org/apache/maven/doxia/docrenderer/AbstractDocumentRenderer.java?rev=583279&view=auto ============================================================================== --- maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/src/main/java/org/apache/maven/doxia/docrenderer/AbstractDocumentRenderer.java (added) +++ maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/src/main/java/org/apache/maven/doxia/docrenderer/AbstractDocumentRenderer.java Tue Oct 9 12:57:16 2007 @@ -0,0 +1,325 @@ +package org.apache.maven.doxia.docrenderer; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.Collection; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import org.apache.maven.doxia.Doxia; +import org.apache.maven.doxia.docrenderer.document.DocumentModel; +import org.apache.maven.doxia.docrenderer.document.io.xpp3.DocumentXpp3Reader; +import org.apache.maven.doxia.sink.Sink; +import org.apache.maven.doxia.parser.ParseException; +import org.apache.maven.doxia.parser.manager.ParserNotFoundException; +import org.apache.maven.doxia.module.site.SiteModule; +import org.apache.maven.doxia.module.site.manager.SiteModuleManager; + +import org.codehaus.plexus.logging.AbstractLogEnabled; + +import org.codehaus.plexus.util.FileUtils; +import org.codehaus.plexus.util.xml.pull.XmlPullParserException; + +/** + * Abstract <code>document</code> renderer. + * + * @author <a href="mailto:[EMAIL PROTECTED]">Vincent Siveton</a> + * @author ltheussl + * @version $Id$ + */ +public abstract class AbstractDocumentRenderer + extends AbstractLogEnabled + implements DocumentRenderer +{ + /** + * @plexus.requirement + */ + protected SiteModuleManager siteModuleManager; + + /** + * @plexus.requirement + */ + protected Doxia doxia; + + /** + * The common base directory of source files. + */ + private String baseDir; + + //-------------------------------------------- + // + //-------------------------------------------- + + /** + * Render a document from the files found in a Map. + * + * @param filesToProcess the Map of Files to process. The Map should contain as keys the paths of the + * source files (relative to [EMAIL PROTECTED] #getBaseDir() baseDir}), and the corresponding SiteModule as values. + * + * @param outputDirectory the output directory where the document should be generated. + * + * @param documentModel the document model, containing all the metadata, etc. + * + * @throws DocRendererException if any + * @throws IOException if any + */ + public abstract void render( Map filesToProcess, File outputDirectory, DocumentModel documentModel ) + throws DocRendererException, IOException; + + //-------------------------------------------- + // + //-------------------------------------------- + + /** [EMAIL PROTECTED] */ + public void render( Collection files, File outputDirectory, DocumentModel documentModel ) + throws DocRendererException, IOException + { + render( getFilesToProcess( files ), outputDirectory, documentModel ); + } + + /** [EMAIL PROTECTED] */ + public void render( File baseDirectory, File outputDirectory, DocumentModel documentModel ) + throws DocRendererException, IOException + { + render( getFilesToProcess( baseDirectory ), outputDirectory, documentModel ); + } + + /** + * Render a document from the files found in baseDirectory. This just forwards to + * [EMAIL PROTECTED] #render(File,File,DocumentModel)} with a new DocumentModel. + * + * @param baseDirectory the directory containing the source files. + * This should follow the standard Maven convention, ie containing all the site modules. + * + * @param outputDirectory the output directory where the document should be generated. + * + * @throws DocRendererException if any + * @throws IOException if any + */ + public void render( File baseDirectory, File outputDirectory ) + throws DocRendererException, IOException + { + render( baseDirectory, outputDirectory, new DocumentModel() ); + } + + /** + * Render a document from the files found in baseDirectory. + * + * @param baseDirectory the directory containing the source files. + * This should follow the standard Maven convention, ie containing all the site modules. + * + * @param outputDirectory the output directory where the document should be generated. + * + * @param documentDescriptor a file containing the document model. + * If this file does not exist or is null, some default settings will be used. + * + * @throws DocRendererException if any + * @throws IOException if any + */ + public void render( File baseDirectory, File outputDirectory, File documentDescriptor ) + throws DocRendererException, IOException + { + if ( ( documentDescriptor == null ) || ( !documentDescriptor.exists() ) ) + { + getLogger().warn( "No documentDescriptor found: using default settings!" ); + + render( baseDirectory, outputDirectory ); + } + else + { + render( getFilesToProcess( baseDirectory ), outputDirectory, readDocumentModel( documentDescriptor ) ); + } + } + + /** + * Returns a Map of files to process. The Map contains as keys the paths of the source files + * (relative to [EMAIL PROTECTED] #getBaseDir() baseDir}), and the corresponding SiteModule as values. + * + * @param baseDirectory the directory containing the source files. + * This should follow the standard Maven convention, ie containing all the site modules. + * @return a Map of files to process. + * @throws IOException in case of a problem reading the files under baseDirectory. + */ + public Map getFilesToProcess( File baseDirectory ) + throws IOException + { + if ( !baseDirectory.isDirectory() ) + { + getLogger().warn( "No files found to process!" ); + + return new HashMap(); + } + + setBaseDir( baseDirectory.getAbsolutePath() ); + + Map filesToProcess = new HashMap(); + + for ( Iterator i = siteModuleManager.getSiteModules().iterator(); i.hasNext(); ) + { + SiteModule module = (SiteModule) i.next(); + + File moduleBasedir = new File( baseDirectory, module.getSourceDirectory() ); + + if ( moduleBasedir.exists() ) + { + // TODO: handle in/excludes + List docs = FileUtils.getFiles( moduleBasedir, "**/*." + module.getExtension(), null, false ); + + for ( Iterator j = docs.iterator(); j.hasNext(); ) + { + String filePath = ( (File) j.next() ).getPath(); + + filesToProcess.put( filePath, module ); + } + } + } + + return filesToProcess; + } + + /** + * Returns a Map of files to process. The Map contains as keys the paths of the source files + * (relative to [EMAIL PROTECTED] #getBaseDir() baseDir}), and the corresponding SiteModule as values. + * + * @param files The Collection of source files. + * @return a Map of files to process. + */ + public Map getFilesToProcess( Collection files ) + { + // ---------------------------------------------------------------------- + // Map all the file names to parser ids + // ---------------------------------------------------------------------- + + Map filesToProcess = new HashMap(); + + for ( Iterator it = siteModuleManager.getSiteModules().iterator(); it.hasNext(); ) + { + SiteModule siteModule = (SiteModule) it.next(); + + String extension = "." + siteModule.getExtension(); + + String sourceDirectory = File.separator + siteModule.getSourceDirectory() + File.separator; + + for ( Iterator j = files.iterator(); j.hasNext(); ) + { + String file = (String) j.next(); + + // first check if the file path contains one of the recognized source dir identifiers + // (there's trouble if a pathname contains 2 identifiers), then match file extensions (not unique). + + if ( file.indexOf( sourceDirectory ) != -1 ) + { + filesToProcess.put( file, siteModule ); + } + else if ( file.endsWith( extension ) ) + { + // don't overwrite if it's there already + if ( !filesToProcess.containsKey( file ) ) + { + filesToProcess.put( file, siteModule ); + } + } + } + } + + return filesToProcess; + } + + /** [EMAIL PROTECTED] */ + public DocumentModel readDocumentModel( File documentDescriptor ) + throws DocRendererException, IOException + { + DocumentModel documentModel; + + try + { + documentModel = new DocumentXpp3Reader().read( new FileReader( documentDescriptor ) ); + } + catch ( XmlPullParserException e ) + { + throw new DocRendererException( "Error parsing document descriptor", e ); + } + + return documentModel; + } + + /** + * Sets the current base directory. + * + * @param newDir the absolute path to the base directory to set. + */ + public void setBaseDir( String newDir ) + { + this.baseDir = newDir; + } + + /** + * Return the current base directory. + * + * @return the current base directory. + */ + public String getBaseDir() + { + return this.baseDir; + } + + //-------------------------------------------- + // + //-------------------------------------------- + + /** + * Parse a source document into a sink. + * + * @param fullDocPath absolute path to the source document. + * @param parserId determines the parser to use. + * @param sink the sink to receive the events. + * @throws DocRendererException in case of a parsing error. + * @throws IOException if the source document cannot be opened. + */ + protected void parse( String fullDocPath, String parserId, Sink sink ) + throws DocRendererException, IOException + { + try + { + FileReader reader = new FileReader( fullDocPath ); + + doxia.parse( reader, parserId, sink ); + } + catch ( ParserNotFoundException e ) + { + throw new DocRendererException( "No parser '" + parserId + + "' found for " + fullDocPath + ": " + e.getMessage() ); + } + catch ( ParseException e ) + { + throw new DocRendererException( "Error parsing " + fullDocPath + ": " + e.getMessage(), e ); + } + finally + { + sink.flush(); + } + } + +} Propchange: maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/src/main/java/org/apache/maven/doxia/docrenderer/AbstractDocumentRenderer.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/src/main/java/org/apache/maven/doxia/docrenderer/AbstractDocumentRenderer.java ------------------------------------------------------------------------------ svn:keywords = "Author Date Id Revision" Added: maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/src/main/java/org/apache/maven/doxia/docrenderer/DocumentRenderer.java URL: http://svn.apache.org/viewvc/maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/src/main/java/org/apache/maven/doxia/docrenderer/DocumentRenderer.java?rev=583279&view=auto ============================================================================== --- maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/src/main/java/org/apache/maven/doxia/docrenderer/DocumentRenderer.java (added) +++ maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/src/main/java/org/apache/maven/doxia/docrenderer/DocumentRenderer.java Tue Oct 9 12:57:16 2007 @@ -0,0 +1,95 @@ +package org.apache.maven.doxia.docrenderer; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.io.File; +import java.io.IOException; +import java.util.Collection; + +import org.apache.maven.doxia.docrenderer.document.DocumentModel; + +/** + * Base interface for rendering documents from a set of input files. + * + * @author <a href="mailto:[EMAIL PROTECTED]">Vincent Siveton</a> + * @author ltheussl + * @version $Id$ + */ +public interface DocumentRenderer +{ + /** Plexus lookup role. */ + String ROLE = DocumentRenderer.class.getName(); + + /** + * Render a document from a set of files, depending on a rendering context. + * + * @param files the path name Strings (relative to a common base directory) + * of files to include in the document generation. + * + * @param outputDirectory the output directory where the document should be generated. + * + * @param documentModel the document model, containing all the metadata, etc. + * If the model contains a TOC, only the files found in this TOC are rendered, + * otherwise all files from the Collection of files will be processed. + * + * @throws DocRendererException if any. + * @throws IOException if any. + */ + void render( Collection files, File outputDirectory, DocumentModel documentModel ) + throws DocRendererException, IOException; + + /** + * Render a document from the files found in a source directory, depending on a rendering context. + * + * @param baseDirectory the directory containing the source files. + * This should follow the standard Maven convention, ie containing all the site modules. + * + * @param outputDirectory the output directory where the document should be generated. + * + * @param documentModel the document model, containing all the metadata, etc. + * If the model contains a TOC, only the files found in this TOC are rendered, + * otherwise all files found under baseDirectory will be processed. + * + * @throws DocRendererException if any + * @throws IOException if any + */ + void render( File baseDirectory, File outputDirectory, DocumentModel documentModel ) + throws DocRendererException, IOException; + + /** + * Read a document model from a file. + * + * @param documentDescriptor a document descriptor file that contains the document model. + * + * @return the document model, containing all the metadata, etc. + * + * @throws DocRendererException if any + * @throws IOException if any + */ + DocumentModel readDocumentModel( File documentDescriptor ) + throws DocRendererException, IOException; + + /** + * Get the output extension associated with this DocumentRenderer. + * + * @return the ouput extension. + */ + String getOutputExtension(); +} Propchange: maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/src/main/java/org/apache/maven/doxia/docrenderer/DocumentRenderer.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/src/main/java/org/apache/maven/doxia/docrenderer/DocumentRenderer.java ------------------------------------------------------------------------------ svn:keywords = "Author Date Id Revision" Added: maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/src/main/java/org/apache/maven/doxia/docrenderer/pdf/AbstractPdfRenderer.java URL: http://svn.apache.org/viewvc/maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/src/main/java/org/apache/maven/doxia/docrenderer/pdf/AbstractPdfRenderer.java?rev=583279&view=auto ============================================================================== --- maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/src/main/java/org/apache/maven/doxia/docrenderer/pdf/AbstractPdfRenderer.java (added) +++ maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/src/main/java/org/apache/maven/doxia/docrenderer/pdf/AbstractPdfRenderer.java Tue Oct 9 12:57:16 2007 @@ -0,0 +1,43 @@ +package org.apache.maven.doxia.docrenderer.pdf; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.apache.maven.doxia.docrenderer.AbstractDocumentRenderer; + +/** + * Abstract pdf renderer, this doesn't depend on the framework. + * + * @author ltheussl + * @version $Id$ + */ +public abstract class AbstractPdfRenderer + extends AbstractDocumentRenderer + implements PdfRenderer +{ + /** + * Get the output extension associated with this renderer. + * + * @return the ouput extension: "pdf". + */ + public String getOutputExtension() + { + return "pdf"; + } +} Propchange: maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/src/main/java/org/apache/maven/doxia/docrenderer/pdf/AbstractPdfRenderer.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/src/main/java/org/apache/maven/doxia/docrenderer/pdf/AbstractPdfRenderer.java ------------------------------------------------------------------------------ svn:keywords = "Author Date Id Revision" Added: maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/src/main/java/org/apache/maven/doxia/docrenderer/pdf/PdfRenderer.java URL: http://svn.apache.org/viewvc/maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/src/main/java/org/apache/maven/doxia/docrenderer/pdf/PdfRenderer.java?rev=583279&view=auto ============================================================================== --- maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/src/main/java/org/apache/maven/doxia/docrenderer/pdf/PdfRenderer.java (added) +++ maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/src/main/java/org/apache/maven/doxia/docrenderer/pdf/PdfRenderer.java Tue Oct 9 12:57:16 2007 @@ -0,0 +1,47 @@ +package org.apache.maven.doxia.docrenderer.pdf; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.io.File; + +import org.apache.maven.doxia.docrenderer.DocumentRenderer; +import org.apache.maven.doxia.docrenderer.DocRendererException; + +/** + * PDF renderer interface. + * + * @author ltheussl + * @version $Id$ + */ +public interface PdfRenderer extends DocumentRenderer +{ + /** Plexus lookup role. */ + String ROLE = PdfRenderer.class.getName(); + + /** + * Generate a final pdf ouput file from an intermediate format file. + * + * @param inputFile eg a fo or an itext file. + * @param pdfFile the pdf file to generate. + * @throws DocRendererException if any. + */ + void generatePdf( File inputFile, File pdfFile ) + throws DocRendererException; +} Propchange: maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/src/main/java/org/apache/maven/doxia/docrenderer/pdf/PdfRenderer.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/src/main/java/org/apache/maven/doxia/docrenderer/pdf/PdfRenderer.java ------------------------------------------------------------------------------ svn:keywords = "Author Date Id Revision" Added: maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/src/main/java/org/apache/maven/doxia/docrenderer/pdf/fo/FoPdfRenderer.java URL: http://svn.apache.org/viewvc/maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/src/main/java/org/apache/maven/doxia/docrenderer/pdf/fo/FoPdfRenderer.java?rev=583279&view=auto ============================================================================== --- maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/src/main/java/org/apache/maven/doxia/docrenderer/pdf/fo/FoPdfRenderer.java (added) +++ maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/src/main/java/org/apache/maven/doxia/docrenderer/pdf/fo/FoPdfRenderer.java Tue Oct 9 12:57:16 2007 @@ -0,0 +1,189 @@ +package org.apache.maven.doxia.docrenderer.pdf.fo; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; + +import java.util.Iterator; +import java.util.Map; + +import javax.xml.transform.TransformerException; + +import org.apache.maven.doxia.docrenderer.DocRendererException; +import org.apache.maven.doxia.docrenderer.document.DocumentModel; +import org.apache.maven.doxia.docrenderer.document.DocumentTOCItem; +import org.apache.maven.doxia.docrenderer.pdf.AbstractPdfRenderer; +import org.apache.maven.doxia.module.site.SiteModule; +import org.apache.maven.doxia.module.fo.FoAggregateSink; +import org.apache.maven.doxia.module.fo.FoUtils; + +import org.codehaus.plexus.util.StringUtils; + +/** + * PDF renderer that uses Doxia's FO module. + * + * @author ltheussl + * @version $Id$ + */ +public class FoPdfRenderer + extends AbstractPdfRenderer +{ + /** + * Converts a FO file to a PDF file using FOP. + * + * @param foFile the FO file. + * @param pdfFile the target PDF file. + * @throws DocRendererException In case of a conversion problem. + * @see org.apache.maven.doxia.module.fo.FoUtils#convertFO2PDF(File,File,String); + */ + public void generatePdf( File foFile, File pdfFile ) + throws DocRendererException + { + getLogger().debug( "Generating: " + pdfFile ); + + try + { + FoUtils.convertFO2PDF( foFile, pdfFile, null ); + } + catch ( TransformerException e ) + { + throw new DocRendererException( "Error creating PDF from " + foFile + ": " + e.getMessage() ); + } + } + + + /** [EMAIL PROTECTED] */ + public void render( Map files, File outputDirectory, DocumentModel model ) + throws DocRendererException, IOException + { + String outputName = model.getOutputName(); + + if ( outputName == null ) + { + getLogger().info( "No outputName is defined in the document descriptor. Using 'target.pdf'" ); + + model.setOutputName( "target" ); + } + else if ( outputName.lastIndexOf( "." ) != -1 ) + { + model.setOutputName( outputName.substring( 0, outputName.lastIndexOf( "." ) ) ); + } + + outputName = model.getOutputName(); + + File outputFOFile = new File( outputDirectory, outputName + ".fo" ); + + if ( !outputFOFile.getParentFile().exists() ) + { + outputFOFile.getParentFile().mkdirs(); + } + + File pdfOutputFile = new File( outputDirectory, outputName + ".pdf" ); + + if ( !pdfOutputFile.getParentFile().exists() ) + { + pdfOutputFile.getParentFile().mkdirs(); + } + + FoAggregateSink sink = new FoAggregateSink( new FileWriter( outputFOFile ) ); + + sink.setDocumentModel( model ); + + sink.beginDocument(); + + sink.coverPage(); + + sink.toc(); + + if ( ( model.getToc() == null ) || ( model.getToc().getItems() == null ) ) + { + getLogger().info( "No TOC is defined in the document descriptor. Merging all documents." ); + + for ( Iterator j = files.keySet().iterator(); j.hasNext(); ) + { + String key = (String) j.next(); + + SiteModule module = (SiteModule) files.get( key ); + + sink.setDocumentName( key ); + // TODO: sink.setDocumentTitle( "Title" ); ??? + + String fullDocPath = getBaseDir() + File.separator + + module.getSourceDirectory() + File.separator + key; + + parse( fullDocPath, module.getParserId(), sink ); + } + + } + else + { + for ( Iterator k = model.getToc().getItems().iterator(); k.hasNext(); ) + { + DocumentTOCItem tocItem = (DocumentTOCItem) k.next(); + + if ( tocItem.getRef() == null ) + { + getLogger().info( "No ref defined for tocItem " + tocItem.getName() ); + + continue; + } + + String href = StringUtils.replace( tocItem.getRef(), "\\", "/" ); + + if ( href.lastIndexOf( "." ) != -1 ) + { + href = href.substring( 0, href.lastIndexOf( "." ) ); + } + + for ( Iterator i = siteModuleManager.getSiteModules().iterator(); i.hasNext(); ) + { + SiteModule module = (SiteModule) i.next(); + + File moduleBasedir = new File( getBaseDir(), module.getSourceDirectory() ); + + // TODO: handle fml exclude more generally (via modules exclude?) + if ( moduleBasedir.exists() && !"fml".equals( module.getExtension() ) ) + { + String doc = href + "." + module.getExtension(); + + File source = new File( moduleBasedir, doc ); + + if ( source.exists() ) + { + sink.setDocumentName( doc ); + + sink.setDocumentTitle( tocItem.getName() ); + + parse( source.getPath(), module.getParserId(), sink ); + } + } + } + } + + } + + sink.endDocument(); + + generatePdf( outputFOFile, pdfOutputFile ); + } + +} Propchange: maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/src/main/java/org/apache/maven/doxia/docrenderer/pdf/fo/FoPdfRenderer.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/src/main/java/org/apache/maven/doxia/docrenderer/pdf/fo/FoPdfRenderer.java ------------------------------------------------------------------------------ svn:keywords = "Author Date Id Revision" Added: maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/src/main/java/org/apache/maven/doxia/docrenderer/pdf/itext/ITextPdfRenderer.java URL: http://svn.apache.org/viewvc/maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/src/main/java/org/apache/maven/doxia/docrenderer/pdf/itext/ITextPdfRenderer.java?rev=583279&view=auto ============================================================================== --- maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/src/main/java/org/apache/maven/doxia/docrenderer/pdf/itext/ITextPdfRenderer.java (added) +++ maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/src/main/java/org/apache/maven/doxia/docrenderer/pdf/itext/ITextPdfRenderer.java Tue Oct 9 12:57:16 2007 @@ -0,0 +1,372 @@ +package org.apache.maven.doxia.docrenderer.pdf.itext; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.FileWriter; +import java.io.IOException; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.Date; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.OutputKeys; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerConfigurationException; +import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; +import javax.xml.transform.stream.StreamSource; + +import org.apache.maven.doxia.docrenderer.DocRendererException; +import org.apache.maven.doxia.docrenderer.document.DocumentModel; +import org.apache.maven.doxia.docrenderer.pdf.AbstractPdfRenderer; +import org.apache.maven.doxia.module.itext.ITextSink; +import org.apache.maven.doxia.module.itext.ITextUtil; +import org.apache.maven.doxia.module.site.SiteModule; +import org.apache.xml.utils.DefaultErrorHandler; +import org.w3c.dom.DOMException; +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import org.xml.sax.SAXException; + +import com.lowagie.text.ElementTags; + +/** + * Abstract <code>document</code> render with the <code>iText</code> framework + * + * @author <a href="mailto:[EMAIL PROTECTED]">Vincent Siveton</a> + * @author ltheussl + * @version $Id$ + */ +public class ITextPdfRenderer + extends AbstractPdfRenderer +{ + /** The xslt style sheet used to transform a Document to an iText file. */ + private static final String XSLT_RESOURCE = "org/apache/maven/doxia/docrenderer/itext/xslt/TOC.xslt"; + + /** The TransformerFactory. */ + private static final TransformerFactory TRANSFORMER_FACTORY = TransformerFactory.newInstance(); + + /** The DocumentBuilderFactory. */ + private static final DocumentBuilderFactory DOCUMENT_BUILDER_FACTORY = DocumentBuilderFactory.newInstance(); + + static + { + TRANSFORMER_FACTORY.setErrorListener( new DefaultErrorHandler() ); + } + + /** + * Converts an iText file to a PDF file using the iText framework. + * + * @param iTextFile the iText file. + * @param pdfFile the target PDF file. + * @throws DocRendererException In case of a conversion problem. + */ + public void generatePdf( File iTextFile, File pdfFile ) + throws DocRendererException + { + getLogger().debug( "Generating : " + pdfFile ); + + try + { + ITextUtil.writePdf( new FileInputStream( iTextFile ), new FileOutputStream( pdfFile ) ); + } + catch ( IOException e ) + { + throw new DocRendererException( "Cannot create PDF from " + iTextFile + ": " + e.getMessage(), e ); + } + catch ( RuntimeException e ) + { + throw new DocRendererException( "Error creating PDF from " + iTextFile + ": " + e.getMessage(), e ); + } + } + + /** [EMAIL PROTECTED] */ + public void render( Map files, File outputDirectory, DocumentModel model ) + throws DocRendererException, IOException + { + String outputName = model.getOutputName(); + + if ( outputName == null ) + { + getLogger().info( "No outputName is defined in the document descriptor. Using 'target.pdf'" ); + + model.setOutputName( "target" ); + } + else if ( outputName.lastIndexOf( "." ) != -1 ) + { + model.setOutputName( outputName.substring( 0, outputName.lastIndexOf( "." ) ) ); + } + +// TODO: adjust from o.a.m.d.docrenderer.itext.AbstractITextRender +// if ( ( model.getToc() == null ) || ( model.getToc().getItems() == null ) ) +// { +// getLogger().info( "No TOC is defined in the document descriptor. Generating all documents." ); + + for ( Iterator j = files.keySet().iterator(); j.hasNext(); ) + { + String key = (String) j.next(); + + SiteModule module = (SiteModule) files.get( key ); + + + String fullDocPath = getBaseDir() + File.separator + + module.getSourceDirectory() + File.separator + key; + System.err.println( "fullDocPath: " + fullDocPath ); + String iTextFileName = key.substring( 0, key.indexOf( "." ) + 1 ) + "xml"; + System.err.println( "iTextFileName: " + iTextFileName ); + + File iTextFile = new File( outputDirectory, iTextFileName ); + if ( !iTextFile.getParentFile().exists() ) + { + iTextFile.getParentFile().mkdirs(); + } + + String pdfFileName = key.substring( 0, key.indexOf( "." ) + 1 ) + getOutputExtension(); + System.err.println( "pdfFileName: " + pdfFileName ); + + File pdfFile = new File( outputDirectory, pdfFileName ); + if ( !pdfFile.getParentFile().exists() ) + { + pdfFile.getParentFile().mkdirs(); + } + + parse( fullDocPath, module, iTextFile ); + + generatePdf( iTextFile, pdfFile ); + } +/* + } + else + { + // TODO: adjust from o.a.m.d.docrenderer.itext.AbstractITextRender + } +*/ + + } + + + //-------------------------------------------- + // + //-------------------------------------------- + + + /** + * Parse a source document and emit results into a sink. + * + * @param fullDocPath absolute path to the source document. + * @param module the site module associated with the source document (determines the parser to use). + * @param iTextFile the resulting iText xml file. + * @throws DocRendererException in case of a parsing problem. + * @throws IOException if the source and/or target document cannot be opened. + */ + private void parse( String fullDocPath, SiteModule module, File iTextFile ) + throws DocRendererException, IOException + { + ITextSink sink = new ITextSink( new FileWriter( iTextFile ) ); + + sink.setClassLoader( new URLClassLoader( new URL[] { iTextFile.getParentFile().toURL() } ) ); + + parse( fullDocPath, module.getParserId(), sink ); + + sink.close(); + } + + /** + * Merge all iTextFiles to a single one. + * + * @param iTextFiles list of iText xml files. + * @return Document. + * @throws DocRendererException if any. + * @throws IOException if any. + */ + private Document generateDocument( List iTextFiles ) + throws DocRendererException, IOException + { + Document document; + + try + { + document = DOCUMENT_BUILDER_FACTORY.newDocumentBuilder().newDocument(); + } + catch ( ParserConfigurationException e ) + { + throw new DocRendererException( "Error building document :" + e.getMessage() ); + } + + document.appendChild( document.createElement( ElementTags.ITEXT ) ); // Used only to set a root + + for ( int i = 0; i < iTextFiles.size(); i++ ) + { + File iTextFile = (File) iTextFiles.get( i ); + + Document iTextDocument; + + try + { + iTextDocument = DOCUMENT_BUILDER_FACTORY.newDocumentBuilder().parse( iTextFile ); + } + catch ( SAXException e ) + { + throw new DocRendererException( "SAX Error : " + e.getMessage() ); + } + catch ( ParserConfigurationException e ) + { + throw new DocRendererException( "Error parsing configuration : " + e.getMessage() ); + } + + // Only one chapter per doc + Node chapter = iTextDocument.getElementsByTagName( ElementTags.CHAPTER ).item( 0 ); + + try + { + document.getDocumentElement().appendChild( document.importNode( chapter, true ) ); + } + catch ( DOMException e ) + { + throw new DocRendererException( "Error appending chapter for " + iTextFile + " : " + e.getMessage() ); + } + } + + return document; + } + + /** + * Initialize the transformer object. + * + * @return an instance of a transformer object. + * @throws DocRendererException if any. + */ + private Transformer initTransformer() + throws DocRendererException + { + try + { + Transformer transformer = TRANSFORMER_FACTORY.newTransformer( new StreamSource( AbstractPdfRenderer.class + .getResourceAsStream( "/" + XSLT_RESOURCE ) ) ); + + transformer.setErrorListener( TRANSFORMER_FACTORY.getErrorListener() ); + + transformer.setOutputProperty( OutputKeys.OMIT_XML_DECLARATION, "false" ); + + transformer.setOutputProperty( OutputKeys.INDENT, "yes" ); + + transformer.setOutputProperty( OutputKeys.METHOD, "xml" ); + + transformer.setOutputProperty( OutputKeys.ENCODING, "UTF-8" ); + + return transformer; + } + catch ( TransformerConfigurationException e ) + { + throw new DocRendererException( "Error configuring Transformer for " + XSLT_RESOURCE + ": " + + e.getMessage() ); + } + catch ( IllegalArgumentException e ) + { + throw new DocRendererException( "Error configuring Transformer for " + XSLT_RESOURCE + ": " + + e.getMessage() ); + } + } + + /** + * Add transformer parameters from a DocumentModel. + * + * @param transformer the Transformer to set the parameters. + * @param documentModel the DocumentModel to take the parameters from. + */ + private void addTransformerParameters( Transformer transformer, DocumentModel documentModel ) + { + if ( documentModel.getMeta().getTitle() != null ) + { + transformer.setParameter( "title", documentModel.getMeta().getTitle() ); + } + + if ( documentModel.getMeta().getAuthor() != null ) + { + transformer.setParameter( "author", documentModel.getMeta().getAuthor() ); + } + + transformer.setParameter( "creationdate", new Date().toString() ); + + if ( documentModel.getMeta().getSubject() != null ) + { + transformer.setParameter( "subject", documentModel.getMeta().getSubject() ); + } + + if ( documentModel.getMeta().getKeywords() != null ) + { + transformer.setParameter( "keywords", documentModel.getMeta().getKeywords() ); + } + + transformer.setParameter( "producer", "Generated with Doxia by " + System.getProperty( "user.name" ) ); + + if ( ITextUtil.isPageSizeSupported( documentModel.getMeta().getTitle() ) ) + { + transformer.setParameter( "pagesize", documentModel.getMeta().getPageSize() ); + } + else + { + transformer.setParameter( "pagesize", "A4" ); + } + + transformer.setParameter( "frontPageHeader", "" ); + + if ( documentModel.getMeta().getTitle() != null ) + { + transformer.setParameter( "frontPageTitle", documentModel.getMeta().getTitle() ); + } + + transformer.setParameter( "frontPageFooter", "Generated date " + new Date().toString() ); + } + + /** + * Transform a document to an iTextFile. + * + * @param documentModel the DocumentModel to take the parameters from. + * @param document the Document to transform. + * @param iTextFile the resulting iText xml file. + * @throws DocRendererException in case of a transformation error. + */ + private void transform( DocumentModel documentModel, Document document, File iTextFile ) + throws DocRendererException + { + Transformer transformer = initTransformer(); + + addTransformerParameters( transformer, documentModel ); + + try + { + transformer.transform( new DOMSource( document ), new StreamResult( iTextFile ) ); + } + catch ( TransformerException e ) + { + throw new DocRendererException( "Error transforming Document " + document + ": " + e.getMessage() ); + } + } +} Propchange: maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/src/main/java/org/apache/maven/doxia/docrenderer/pdf/itext/ITextPdfRenderer.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/src/main/java/org/apache/maven/doxia/docrenderer/pdf/itext/ITextPdfRenderer.java ------------------------------------------------------------------------------ svn:keywords = "Author Date Id Revision"