Author: vsiveton
Date: Mon Jun 1 12:31:02 2009
New Revision: 780635
URL: http://svn.apache.org/viewvc?rev=780635&view=rev
Log:
DOXIA-327: Add PDF metadata
o set the foUserAgent with non empty documentModel meta
o updated the renderer
Modified:
maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/src/main/java/org/apache/maven/doxia/docrenderer/pdf/fo/FoPdfRenderer.java
maven/doxia/doxia/trunk/doxia-modules/doxia-module-fo/src/main/java/org/apache/maven/doxia/module/fo/FoUtils.java
Modified:
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=780635&r1=780634&r2=780635&view=diff
==============================================================================
---
maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/src/main/java/org/apache/maven/doxia/docrenderer/pdf/fo/FoPdfRenderer.java
(original)
+++
maven/doxia/doxia-sitetools/trunk/doxia-doc-renderer/src/main/java/org/apache/maven/doxia/docrenderer/pdf/fo/FoPdfRenderer.java
Mon Jun 1 12:31:02 2009
@@ -62,31 +62,8 @@
public void generatePdf( File inputFile, File pdfFile )
throws DocumentRendererException
{
- if ( getLogger().isDebugEnabled() )
- {
- getLogger().debug( "Generating: " + pdfFile );
- }
-
- try
- {
- FoUtils.convertFO2PDF( inputFile, pdfFile, null );
- }
- catch ( TransformerException e )
- {
- if ( ( e.getCause() != null ) && ( e.getCause() instanceof
SAXParseException ) )
- {
- SAXParseException sax = (SAXParseException) e.getCause();
-
- StringBuffer sb = new StringBuffer();
- sb.append( "Error creating PDF from " ).append(
inputFile.getAbsolutePath() ).append( ":" )
- .append( sax.getLineNumber() ).append( ":" ).append(
sax.getColumnNumber() ).append( "\n" );
- sb.append( e.getMessage() );
-
- throw new DocumentRendererException( sb.toString() );
- }
-
- throw new DocumentRendererException( "Error creating PDF from " +
inputFile + ": " + e.getMessage() );
- }
+ // Should take care of the document model for the metadata...
+ generatePdf( inputFile, pdfFile, null );
}
/** {...@inheritdoc} */
@@ -169,7 +146,7 @@
IOUtil.close( writer );
}
- generatePdf( outputFOFile, pdfOutputFile );
+ generatePdf( outputFOFile, pdfOutputFile, documentModel );
}
private void mergeAllSources( Map filesToProcess, FoAggregateSink sink )
@@ -258,4 +235,41 @@
}
}
}
+
+ /**
+ * @param inputFile
+ * @param pdfFile
+ * @param documentModel could be null
+ * @throws DocumentRendererException if any
+ * @since 1.1.1
+ */
+ private void generatePdf( File inputFile, File pdfFile, DocumentModel
documentModel )
+ throws DocumentRendererException
+ {
+ if ( getLogger().isDebugEnabled() )
+ {
+ getLogger().debug( "Generating: " + pdfFile );
+ }
+
+ try
+ {
+ FoUtils.convertFO2PDF( inputFile, pdfFile, null, documentModel );
+ }
+ catch ( TransformerException e )
+ {
+ if ( ( e.getCause() != null ) && ( e.getCause() instanceof
SAXParseException ) )
+ {
+ SAXParseException sax = (SAXParseException) e.getCause();
+
+ StringBuffer sb = new StringBuffer();
+ sb.append( "Error creating PDF from " ).append(
inputFile.getAbsolutePath() ).append( ":" )
+ .append( sax.getLineNumber() ).append( ":" ).append(
sax.getColumnNumber() ).append( "\n" );
+ sb.append( e.getMessage() );
+
+ throw new DocumentRendererException( sb.toString() );
+ }
+
+ throw new DocumentRendererException( "Error creating PDF from " +
inputFile + ": " + e.getMessage() );
+ }
+ }
}
Modified:
maven/doxia/doxia/trunk/doxia-modules/doxia-module-fo/src/main/java/org/apache/maven/doxia/module/fo/FoUtils.java
URL:
http://svn.apache.org/viewvc/maven/doxia/doxia/trunk/doxia-modules/doxia-module-fo/src/main/java/org/apache/maven/doxia/module/fo/FoUtils.java?rev=780635&r1=780634&r2=780635&view=diff
==============================================================================
---
maven/doxia/doxia/trunk/doxia-modules/doxia-module-fo/src/main/java/org/apache/maven/doxia/module/fo/FoUtils.java
(original)
+++
maven/doxia/doxia/trunk/doxia-modules/doxia-module-fo/src/main/java/org/apache/maven/doxia/module/fo/FoUtils.java
Mon Jun 1 12:31:02 2009
@@ -38,7 +38,9 @@
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.MimeConstants;
+import org.apache.maven.doxia.document.DocumentModel;
import org.codehaus.plexus.util.IOUtil;
+import org.codehaus.plexus.util.StringUtils;
/**
* <code>FO Sink</code> utilities.
@@ -55,21 +57,42 @@
/**
* Converts an FO file to a PDF file using FOP.
*
- * @param fo the FO file.
- * @param pdf the target PDF file.
- * @param resourceDir The base directory for relative path resolution.
+ * @param fo the FO file, not null.
+ * @param pdf the target PDF file, not null.
+ * @param resourceDir The base directory for relative path resolution,
could be null.
* If null, defaults to the parent directory of fo.
+ * @param documentModel the document model to add PDF metadatas like
author, title and keywords, could be null.
* @throws javax.xml.transform.TransformerException In case of a
conversion problem.
+ * @see 1.1.1
*/
- public static void convertFO2PDF( File fo, File pdf, String resourceDir )
+ public static void convertFO2PDF( File fo, File pdf, String resourceDir,
DocumentModel documentModel )
throws TransformerException
{
FOUserAgent foUserAgent = FOP_FACTORY.newFOUserAgent();
-
foUserAgent.setBaseURL( getBaseURL( fo, resourceDir ) );
+ foUserAgent.setCreator( System.getProperty( "user.name" ) );
- OutputStream out = null;
+ if ( documentModel != null )
+ {
+ // http://xmlgraphics.apache.org/fop/embedding.html#user-agent
+ String authors = documentModel.getMeta().getAllAuthorNames();
+ if ( StringUtils.isNotEmpty( authors ) )
+ {
+ foUserAgent.setAuthor( authors );
+ }
+ String title = documentModel.getMeta().getTitle();
+ if ( StringUtils.isNotEmpty( title ) )
+ {
+ foUserAgent.setTitle( title );
+ }
+ String keywords = documentModel.getMeta().getAllKeyWords();
+ if ( StringUtils.isNotEmpty( keywords ) )
+ {
+ foUserAgent.setKeywords( keywords );
+ }
+ }
+ OutputStream out = null;
try
{
try
@@ -82,7 +105,6 @@
}
Result res = null;
-
try
{
Fop fop = FOP_FACTORY.newFop( MimeConstants.MIME_PDF,
foUserAgent, out );
@@ -94,7 +116,6 @@
}
Transformer transformer = null;
-
try
{
// identity transformer
@@ -114,6 +135,22 @@
}
/**
+ * Converts an FO file to a PDF file using FOP.
+ *
+ * @param fo the FO file, not null.
+ * @param pdf the target PDF file, not null.
+ * @param resourceDir The base directory for relative path resolution,
could be null.
+ * If null, defaults to the parent directory of fo.
+ * @throws javax.xml.transform.TransformerException In case of a
conversion problem.
+ * @see #convertFO2PDF(File, File, String, DocumentModel)
+ */
+ public static void convertFO2PDF( File fo, File pdf, String resourceDir )
+ throws TransformerException
+ {
+ convertFO2PDF( fo, pdf, resourceDir, null );
+ }
+
+ /**
* Returns a base URL to be used by the FOUserAgent.
*
* @param fo the FO file.