You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by hb...@apache.org on 2018/02/07 08:22:11 UTC

[maven-doxia] branch master updated: [DOXIA-409] Upgrade to FOP 2.2

This is an automated email from the ASF dual-hosted git repository.

hboutemy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-doxia.git


The following commit(s) were added to refs/heads/master by this push:
     new 4686c00  [DOXIA-409] Upgrade to FOP 2.2
4686c00 is described below

commit 4686c008218981d612ab35349f66756118c2804d
Author: Sylwester Lachiewicz <sl...@gmail.com>
AuthorDate: Sun Jan 14 00:56:38 2018 +0100

    [DOXIA-409] Upgrade to FOP 2.2
    
    Fix by: Michael Simacek <msimacek@redhat.com
    Source: http://pkgs.fedoraproject.org/cgit/rpms/maven-doxia.git/tree/0004-Port-to-fop-2.0.patch
---
 doxia-modules/doxia-module-fo/pom.xml              |  2 +-
 .../org/apache/maven/doxia/module/fo/FoUtils.java  | 58 ++++++----------------
 2 files changed, 16 insertions(+), 44 deletions(-)

diff --git a/doxia-modules/doxia-module-fo/pom.xml b/doxia-modules/doxia-module-fo/pom.xml
index 74e3bb2..428753a 100644
--- a/doxia-modules/doxia-module-fo/pom.xml
+++ b/doxia-modules/doxia-module-fo/pom.xml
@@ -85,7 +85,7 @@ under the License.
     <dependency>
       <groupId>org.apache.xmlgraphics</groupId>
       <artifactId>fop</artifactId>
-      <version>0.95</version>
+      <version>2.2</version>
     </dependency>
     <dependency>
       <groupId>log4j</groupId>
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
index a1e3efa..41a76c1 100644
--- 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
@@ -24,6 +24,7 @@ import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.OutputStream;
+import java.net.URI;
 import java.util.Date;
 
 import javax.xml.transform.Result;
@@ -38,6 +39,7 @@ import org.apache.fop.apps.FOPException;
 import org.apache.fop.apps.FOUserAgent;
 import org.apache.fop.apps.Fop;
 import org.apache.fop.apps.FopFactory;
+import org.apache.fop.apps.FopFactoryBuilder;
 import org.apache.fop.apps.MimeConstants;
 import org.apache.maven.doxia.document.DocumentModel;
 import org.codehaus.plexus.util.IOUtil;
@@ -52,28 +54,11 @@ import org.codehaus.plexus.util.StringUtils;
  */
 public class FoUtils
 {
-    /** To reuse the FopFactory **/
-    private static final FopFactory FOP_FACTORY = FopFactory.newInstance();
-
     /** To reuse the TransformerFactory **/
     private static final TransformerFactory TRANSFORMER_FACTORY = TransformerFactory.newInstance();
 
-    /**
-     * 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.
-     * @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.
-     * @since 1.1.1
-     */
-    public static void convertFO2PDF( File fo, File pdf, String resourceDir, DocumentModel documentModel )
-        throws TransformerException
-    {
-        FOUserAgent foUserAgent = getDefaultUserAgent( fo, resourceDir );
 
+    private static void prepareUserAgent( FOUserAgent foUserAgent, DocumentModel documentModel ) {
         if ( documentModel != null && documentModel.getMeta() != null )
         {
             // http://xmlgraphics.apache.org/fop/embedding.html#user-agent
@@ -113,8 +98,6 @@ public class FoUtils
         {
             foUserAgent.setCreationDate( new Date() );
         }
-
-        convertFO2PDF( fo, pdf, resourceDir, foUserAgent );
     }
 
     /**
@@ -124,16 +107,13 @@ public class FoUtils
      * @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 foUserAgent the FOUserAgent to use.
-     *      May be null, in which case a default user agent will be used.
+     * @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.
      * @since 1.1.1
      */
-    public static void convertFO2PDF( File fo, File pdf, String resourceDir, FOUserAgent foUserAgent )
+    public static void convertFO2PDF( File fo, File pdf, String resourceDir, DocumentModel documentModel )
         throws TransformerException
     {
-        FOUserAgent userAgent = ( foUserAgent == null ? getDefaultUserAgent( fo, resourceDir ) : foUserAgent );
-
         OutputStream out = null;
         try
         {
@@ -149,7 +129,11 @@ public class FoUtils
             Result res = null;
             try
             {
-                Fop fop = FOP_FACTORY.newFop( MimeConstants.MIME_PDF, userAgent, out );
+                URI baseURI = getBaseURI( fo, resourceDir );
+                FopFactory fopFactory = new FopFactoryBuilder( baseURI ).build();
+                FOUserAgent userAgent = fopFactory.newFOUserAgent();
+                prepareUserAgent( userAgent, documentModel );
+                Fop fop = fopFactory.newFop( MimeConstants.MIME_PDF, userAgent, out );
                 res = new SAXResult( fop.getDefaultHandler() );
             }
             catch ( FOPException e )
@@ -193,34 +177,22 @@ public class FoUtils
     }
 
     /**
-     * Returns a base URL to be used by the FOUserAgent.
+     * Returns a base URI.
      *
      * @param fo the FO file.
      * @param resourceDir the resource directory.
-     * @return String.
+     * @return URI.
      */
-    private static String getBaseURL( File fo, String resourceDir )
+    private static URI getBaseURI( File fo, String resourceDir )
     {
-        String url = null;
-
         if ( resourceDir == null )
         {
-            url = "file:///" + fo.getParent() + "/";
+            return fo.getParentFile().toURI();
         }
         else
         {
-            url = "file:///" + resourceDir + "/";
+            return new File( resourceDir + "/" ).toURI();
         }
-
-        return url;
-    }
-
-    private static FOUserAgent getDefaultUserAgent( File fo, String resourceDir )
-    {
-        FOUserAgent foUserAgent = FOP_FACTORY.newFOUserAgent();
-        foUserAgent.setBaseURL( getBaseURL( fo, resourceDir ) );
-
-        return foUserAgent;
     }
 
     private FoUtils()

-- 
To stop receiving notification emails like this one, please contact
hboutemy@apache.org.