|
|
ce7435 |
From b72bf32dbabf4c18cf48bdbc344227fb0b2d5110 Mon Sep 17 00:00:00 2001
|
|
|
ce7435 |
From: Michael Simacek <msimacek@redhat.com>
|
|
|
ce7435 |
Date: Mon, 17 Aug 2015 17:15:05 +0200
|
|
|
ce7435 |
Subject: [PATCH 4/4] Port to fop-2.0
|
|
|
ce7435 |
|
|
|
ce7435 |
---
|
|
|
ce7435 |
doxia-modules/doxia-module-fo/pom.xml | 2 +-
|
|
|
ce7435 |
.../org/apache/maven/doxia/module/fo/FoUtils.java | 58 ++++++----------------
|
|
|
ce7435 |
2 files changed, 16 insertions(+), 44 deletions(-)
|
|
|
ce7435 |
|
|
|
ce7435 |
diff --git a/doxia-modules/doxia-module-fo/pom.xml b/doxia-modules/doxia-module-fo/pom.xml
|
|
|
ce7435 |
index e66c736..6d261c8 100644
|
|
|
ce7435 |
--- a/doxia-modules/doxia-module-fo/pom.xml
|
|
|
ce7435 |
+++ b/doxia-modules/doxia-module-fo/pom.xml
|
|
|
ce7435 |
@@ -85,7 +85,7 @@ under the License.
|
|
|
ce7435 |
<dependency>
|
|
|
ce7435 |
<groupId>org.apache.xmlgraphics</groupId>
|
|
|
ce7435 |
<artifactId>fop</artifactId>
|
|
|
ce7435 |
- <version>0.95</version>
|
|
|
ce7435 |
+ <version>2.0</version>
|
|
|
ce7435 |
</dependency>
|
|
|
ce7435 |
<dependency>
|
|
|
ce7435 |
<groupId>log4j</groupId>
|
|
|
ce7435 |
diff --git a/doxia-modules/doxia-module-fo/src/main/java/org/apache/maven/doxia/module/fo/FoUtils.java b/doxia-modules/doxia-module-fo/src/main/java/org/apache/maven/doxia/module/fo/FoUtils.java
|
|
|
ce7435 |
index 0e7efc1..c398eaf 100644
|
|
|
ce7435 |
--- a/doxia-modules/doxia-module-fo/src/main/java/org/apache/maven/doxia/module/fo/FoUtils.java
|
|
|
ce7435 |
+++ b/doxia-modules/doxia-module-fo/src/main/java/org/apache/maven/doxia/module/fo/FoUtils.java
|
|
|
ce7435 |
@@ -24,6 +24,7 @@ import java.io.File;
|
|
|
ce7435 |
import java.io.FileOutputStream;
|
|
|
ce7435 |
import java.io.IOException;
|
|
|
ce7435 |
import java.io.OutputStream;
|
|
|
ce7435 |
+import java.net.URI;
|
|
|
ce7435 |
import java.util.Date;
|
|
|
ce7435 |
|
|
|
ce7435 |
import javax.xml.transform.Result;
|
|
|
ce7435 |
@@ -38,6 +39,7 @@ import org.apache.fop.apps.FOPException;
|
|
|
ce7435 |
import org.apache.fop.apps.FOUserAgent;
|
|
|
ce7435 |
import org.apache.fop.apps.Fop;
|
|
|
ce7435 |
import org.apache.fop.apps.FopFactory;
|
|
|
ce7435 |
+import org.apache.fop.apps.FopFactoryBuilder;
|
|
|
ce7435 |
import org.apache.fop.apps.MimeConstants;
|
|
|
ce7435 |
import org.apache.maven.doxia.document.DocumentModel;
|
|
|
ce7435 |
import org.codehaus.plexus.util.IOUtil;
|
|
|
ce7435 |
@@ -52,28 +54,11 @@ import org.codehaus.plexus.util.StringUtils;
|
|
|
ce7435 |
*/
|
|
|
ce7435 |
public class FoUtils
|
|
|
ce7435 |
{
|
|
|
ce7435 |
- /** To reuse the FopFactory **/
|
|
|
ce7435 |
- private static final FopFactory FOP_FACTORY = FopFactory.newInstance();
|
|
|
ce7435 |
-
|
|
|
ce7435 |
/** To reuse the TransformerFactory **/
|
|
|
ce7435 |
private static final TransformerFactory TRANSFORMER_FACTORY = TransformerFactory.newInstance();
|
|
|
ce7435 |
|
|
|
ce7435 |
- /**
|
|
|
ce7435 |
- * Converts an FO file to a PDF file using FOP.
|
|
|
ce7435 |
- *
|
|
|
ce7435 |
- * @param fo the FO file, not null.
|
|
|
ce7435 |
- * @param pdf the target PDF file, not null.
|
|
|
ce7435 |
- * @param resourceDir The base directory for relative path resolution, could be null.
|
|
|
ce7435 |
- * If null, defaults to the parent directory of fo.
|
|
|
ce7435 |
- * @param documentModel the document model to add PDF metadatas like author, title and keywords, could be null.
|
|
|
ce7435 |
- * @throws javax.xml.transform.TransformerException In case of a conversion problem.
|
|
|
ce7435 |
- * @since 1.1.1
|
|
|
ce7435 |
- */
|
|
|
ce7435 |
- public static void convertFO2PDF( File fo, File pdf, String resourceDir, DocumentModel documentModel )
|
|
|
ce7435 |
- throws TransformerException
|
|
|
ce7435 |
- {
|
|
|
ce7435 |
- FOUserAgent foUserAgent = getDefaultUserAgent( fo, resourceDir );
|
|
|
ce7435 |
|
|
|
ce7435 |
+ private static void prepareUserAgent( FOUserAgent foUserAgent, DocumentModel documentModel ) {
|
|
|
ce7435 |
if ( documentModel != null && documentModel.getMeta() != null )
|
|
|
ce7435 |
{
|
|
|
ce7435 |
// http://xmlgraphics.apache.org/fop/embedding.html#user-agent
|
|
|
ce7435 |
@@ -113,8 +98,6 @@ public class FoUtils
|
|
|
ce7435 |
{
|
|
|
ce7435 |
foUserAgent.setCreationDate( new Date() );
|
|
|
ce7435 |
}
|
|
|
ce7435 |
-
|
|
|
ce7435 |
- convertFO2PDF( fo, pdf, resourceDir, foUserAgent );
|
|
|
ce7435 |
}
|
|
|
ce7435 |
|
|
|
ce7435 |
/**
|
|
|
ce7435 |
@@ -124,16 +107,13 @@ public class FoUtils
|
|
|
ce7435 |
* @param pdf the target PDF file, not null.
|
|
|
ce7435 |
* @param resourceDir The base directory for relative path resolution, could be null.
|
|
|
ce7435 |
* If null, defaults to the parent directory of fo.
|
|
|
ce7435 |
- * @param foUserAgent the FOUserAgent to use.
|
|
|
ce7435 |
- * May be null, in which case a default user agent will be used.
|
|
|
ce7435 |
+ * @param documentModel the document model to add PDF metadatas like author, title and keywords, could be null.
|
|
|
ce7435 |
* @throws javax.xml.transform.TransformerException In case of a conversion problem.
|
|
|
ce7435 |
* @since 1.1.1
|
|
|
ce7435 |
*/
|
|
|
ce7435 |
- public static void convertFO2PDF( File fo, File pdf, String resourceDir, FOUserAgent foUserAgent )
|
|
|
ce7435 |
+ public static void convertFO2PDF( File fo, File pdf, String resourceDir, DocumentModel documentModel )
|
|
|
ce7435 |
throws TransformerException
|
|
|
ce7435 |
{
|
|
|
ce7435 |
- FOUserAgent userAgent = ( foUserAgent == null ? getDefaultUserAgent( fo, resourceDir ) : foUserAgent );
|
|
|
ce7435 |
-
|
|
|
ce7435 |
OutputStream out = null;
|
|
|
ce7435 |
try
|
|
|
ce7435 |
{
|
|
|
ce7435 |
@@ -149,7 +129,11 @@ public class FoUtils
|
|
|
ce7435 |
Result res = null;
|
|
|
ce7435 |
try
|
|
|
ce7435 |
{
|
|
|
ce7435 |
- Fop fop = FOP_FACTORY.newFop( MimeConstants.MIME_PDF, userAgent, out );
|
|
|
ce7435 |
+ URI baseURI = getBaseURI( fo, resourceDir );
|
|
|
ce7435 |
+ FopFactory fopFactory = new FopFactoryBuilder( baseURI ).build();
|
|
|
ce7435 |
+ FOUserAgent userAgent = fopFactory.newFOUserAgent();
|
|
|
ce7435 |
+ prepareUserAgent( userAgent, documentModel );
|
|
|
ce7435 |
+ Fop fop = fopFactory.newFop( MimeConstants.MIME_PDF, userAgent, out );
|
|
|
ce7435 |
res = new SAXResult( fop.getDefaultHandler() );
|
|
|
ce7435 |
}
|
|
|
ce7435 |
catch ( FOPException e )
|
|
|
ce7435 |
@@ -193,34 +177,22 @@ public class FoUtils
|
|
|
ce7435 |
}
|
|
|
ce7435 |
|
|
|
ce7435 |
/**
|
|
|
ce7435 |
- * Returns a base URL to be used by the FOUserAgent.
|
|
|
ce7435 |
+ * Returns a base URI.
|
|
|
ce7435 |
*
|
|
|
ce7435 |
* @param fo the FO file.
|
|
|
ce7435 |
* @param resourceDir the resource directory.
|
|
|
ce7435 |
- * @return String.
|
|
|
ce7435 |
+ * @return URI.
|
|
|
ce7435 |
*/
|
|
|
ce7435 |
- private static String getBaseURL( File fo, String resourceDir )
|
|
|
ce7435 |
+ private static URI getBaseURI( File fo, String resourceDir )
|
|
|
ce7435 |
{
|
|
|
ce7435 |
- String url = null;
|
|
|
ce7435 |
-
|
|
|
ce7435 |
if ( resourceDir == null )
|
|
|
ce7435 |
{
|
|
|
ce7435 |
- url = "file:///" + fo.getParent() + "/";
|
|
|
ce7435 |
+ return fo.getParentFile().toURI();
|
|
|
ce7435 |
}
|
|
|
ce7435 |
else
|
|
|
ce7435 |
{
|
|
|
ce7435 |
- url = "file:///" + resourceDir + "/";
|
|
|
ce7435 |
+ return new File( resourceDir + "/" ).toURI();
|
|
|
ce7435 |
}
|
|
|
ce7435 |
-
|
|
|
ce7435 |
- return url;
|
|
|
ce7435 |
- }
|
|
|
ce7435 |
-
|
|
|
ce7435 |
- private static FOUserAgent getDefaultUserAgent( File fo, String resourceDir )
|
|
|
ce7435 |
- {
|
|
|
ce7435 |
- FOUserAgent foUserAgent = FOP_FACTORY.newFOUserAgent();
|
|
|
ce7435 |
- foUserAgent.setBaseURL( getBaseURL( fo, resourceDir ) );
|
|
|
ce7435 |
-
|
|
|
ce7435 |
- return foUserAgent;
|
|
|
ce7435 |
}
|
|
|
ce7435 |
|
|
|
ce7435 |
private FoUtils()
|
|
|
ce7435 |
--
|
|
|
ce7435 |
2.5.5
|
|
|
ce7435 |
|