Author: vhennebert
Date: Fri Nov 15 07:13:53 2013
New Revision: 1542190

URL: http://svn.apache.org/r1542190
Log:
FOP-2312: font-base configuration setting not working as expected
Use the config file URI to resolve relative <base> / <font-base> URIs.
Fall back to default base URI only if the config input stream has no URI.

Added:
    xmlgraphics/fop/trunk/test/config/relative-uri/
    xmlgraphics/fop/trunk/test/config/relative-uri/base_font.xconf
    xmlgraphics/fop/trunk/test/config/relative-uri/base_no-font.xconf
    xmlgraphics/fop/trunk/test/config/relative-uri/no-base_font.xconf
    xmlgraphics/fop/trunk/test/config/relative-uri/no-base_no-font.xconf
Modified:
    xmlgraphics/fop/trunk/src/java/org/apache/fop/apps/FopConfParser.java
    xmlgraphics/fop/trunk/src/java/org/apache/fop/cli/CommandLineOptions.java
    
xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/FontManagerConfigurator.java
    
xmlgraphics/fop/trunk/test/java/org/apache/fop/apps/FopConfParserTestCase.java

Modified: xmlgraphics/fop/trunk/src/java/org/apache/fop/apps/FopConfParser.java
URL: 
http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/src/java/org/apache/fop/apps/FopConfParser.java?rev=1542190&r1=1542189&r2=1542190&view=diff
==============================================================================
--- xmlgraphics/fop/trunk/src/java/org/apache/fop/apps/FopConfParser.java 
(original)
+++ xmlgraphics/fop/trunk/src/java/org/apache/fop/apps/FopConfParser.java Fri 
Nov 15 07:13:53 2013
@@ -70,16 +70,7 @@ public class FopConfParser {
      */
     public FopConfParser(InputStream fopConfStream, EnvironmentProfile enviro)
             throws SAXException, IOException {
-        DefaultConfigurationBuilder cfgBuilder = new 
DefaultConfigurationBuilder();
-        Configuration cfg;
-        try {
-            cfg = cfgBuilder.build(fopConfStream);
-        } catch (ConfigurationException e) {
-            throw new FOPException(e);
-        }
-        // The default base URI is taken from the directory in which the 
fopConf resides
-        fopFactoryBuilder = new 
FopFactoryBuilder(enviro).setConfiguration(cfg);
-        configure(enviro.getDefaultBaseURI(), enviro.getResourceResolver(), 
cfg);
+        this(fopConfStream, enviro.getDefaultBaseURI(), enviro);
     }
 
     /**
@@ -94,7 +85,8 @@ public class FopConfParser {
      */
     public FopConfParser(InputStream fopConfStream, URI defaultBaseURI,
             ResourceResolver resourceResolver) throws SAXException, 
IOException {
-        this(fopConfStream, 
EnvironmentalProfileFactory.createDefault(defaultBaseURI, resourceResolver));
+        this(fopConfStream, defaultBaseURI,
+                EnvironmentalProfileFactory.createDefault(defaultBaseURI, 
resourceResolver));
     }
 
     /**
@@ -123,6 +115,20 @@ public class FopConfParser {
     }
 
     /**
+     * Constructor that takes the FOP conf and a default base URI and uses the 
default URI resolver.
+     *
+     * @param fopConfFile the FOP conf file
+     * @param defaultBaseURI the default base URI
+     * @throws SAXException if a SAX error was thrown parsing the FOP conf
+     * @throws IOException if an I/O error is thrown while parsing the FOP conf
+     */
+    public FopConfParser(File fopConfFile, URI defaultBaseURI) throws 
SAXException, IOException {
+        this(new FileInputStream(fopConfFile), fopConfFile.toURI(),
+                EnvironmentalProfileFactory.createDefault(defaultBaseURI,
+                        
ResourceResolverFactory.createDefaultResourceResolver()));
+    }
+
+    /**
      * Constructor that parses the FOP conf and uses the URI resolver given.
      *
      * @param fopConfFile the FOP conf file
@@ -132,11 +138,24 @@ public class FopConfParser {
      */
     public FopConfParser(File fopConfFile, ResourceResolver resourceResolver)
             throws SAXException, IOException {
-        this(new FileInputStream(fopConfFile),
-                fopConfFile.getAbsoluteFile().getParentFile().toURI(), 
resourceResolver);
+        this(new FileInputStream(fopConfFile), fopConfFile.toURI(), 
resourceResolver);
+    }
+
+    private FopConfParser(InputStream fopConfStream, URI baseURI, 
EnvironmentProfile enviro)
+            throws SAXException, IOException {
+        DefaultConfigurationBuilder cfgBuilder = new 
DefaultConfigurationBuilder();
+        Configuration cfg;
+        try {
+            cfg = cfgBuilder.build(fopConfStream);
+        } catch (ConfigurationException e) {
+            throw new FOPException(e);
+        }
+        // The default base URI is taken from the directory in which the 
fopConf resides
+        fopFactoryBuilder = new 
FopFactoryBuilder(enviro).setConfiguration(cfg);
+        configure(baseURI, enviro.getResourceResolver(), cfg);
     }
 
-    private void configure(final URI defaultBaseURI, final ResourceResolver 
resourceResolver,
+    private void configure(final URI baseURI, final ResourceResolver 
resourceResolver,
             Configuration cfg) throws FOPException {
         if (log.isDebugEnabled()) {
             log.debug("Initializing FopFactory Configuration");
@@ -174,7 +193,7 @@ public class FopConfParser {
         if (cfg.getChild("base", false) != null) {
             try {
                 URI confUri = 
InternalResourceResolver.getBaseURI(cfg.getChild("base").getValue(null));
-                fopFactoryBuilder.setBaseURI(defaultBaseURI.resolve(confUri));
+                fopFactoryBuilder.setBaseURI(baseURI.resolve(confUri));
             } catch (URISyntaxException use) {
                 LogUtil.handleException(log, use, strict);
             }
@@ -242,8 +261,8 @@ public class FopConfParser {
         }
 
         // configure font manager
-        new FontManagerConfigurator(cfg, fopFactoryBuilder.getBaseURI(), 
resourceResolver).configure(
-                fopFactoryBuilder.getFontManager(), strict);
+        new FontManagerConfigurator(cfg, baseURI, 
fopFactoryBuilder.getBaseURI(), resourceResolver)
+                .configure(fopFactoryBuilder.getFontManager(), strict);
 
         // configure image loader framework
         configureImageLoading(cfg.getChild("image-loading", false), strict);

Modified: 
xmlgraphics/fop/trunk/src/java/org/apache/fop/cli/CommandLineOptions.java
URL: 
http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/src/java/org/apache/fop/cli/CommandLineOptions.java?rev=1542190&r1=1542189&r2=1542190&view=diff
==============================================================================
--- xmlgraphics/fop/trunk/src/java/org/apache/fop/cli/CommandLineOptions.java 
(original)
+++ xmlgraphics/fop/trunk/src/java/org/apache/fop/cli/CommandLineOptions.java 
Fri Nov 15 07:13:53 2013
@@ -21,10 +21,8 @@ package org.apache.fop.cli;
 
 // java
 import java.io.File;
-import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.IOException;
-import java.io.InputStream;
 import java.io.PrintStream;
 import java.net.URI;
 import java.util.Locale;
@@ -47,7 +45,6 @@ import org.apache.fop.apps.FopFactory;
 import org.apache.fop.apps.FopFactoryBuilder;
 import org.apache.fop.apps.FopFactoryConfig;
 import org.apache.fop.apps.MimeConstants;
-import org.apache.fop.apps.io.ResourceResolverFactory;
 import org.apache.fop.pdf.PDFAMode;
 import org.apache.fop.pdf.PDFEncryptionManager;
 import org.apache.fop.pdf.PDFEncryptionParams;
@@ -1018,9 +1015,7 @@ public class CommandLineOptions {
             
fopFactoryBuilder.setComplexScriptFeatures(useComplexScriptFeatures);
         } else {
             try {
-                InputStream userConfig = new FileInputStream(userConfigFile);
-                FopConfParser fopConfParser = new FopConfParser(userConfig, 
baseURI,
-                        
ResourceResolverFactory.createDefaultResourceResolver());
+                FopConfParser fopConfParser = new 
FopConfParser(userConfigFile, baseURI);
                 fopFactoryBuilder = fopConfParser.getFopFactoryBuilder();
             } catch (SAXException e) {
                 throw new FOPException(e);

Modified: 
xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/FontManagerConfigurator.java
URL: 
http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/FontManagerConfigurator.java?rev=1542190&r1=1542189&r2=1542190&view=diff
==============================================================================
--- 
xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/FontManagerConfigurator.java
 (original)
+++ 
xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/FontManagerConfigurator.java
 Fri Nov 15 07:13:53 2013
@@ -48,20 +48,24 @@ public class FontManagerConfigurator {
 
     private final Configuration cfg;
 
-    private final URI defaultBaseUri;
+    private final URI baseURI;
+
+    private final URI fallbackURI;
 
     private final ResourceResolver resourceResolver;
 
     /**
      * Main constructor
      * @param cfg the font manager configuration object
-     * @param defaultBaseUri the default URI base to use for URI resolution
+     * @param baseURI the URI against which to resolve relative URIs
+     * @param fallbackURI the URI to use as a fallback if font-base is 
unspecified
      * @param resourceResolver the resource resolver
      */
-    public FontManagerConfigurator(Configuration cfg, URI defaultBaseUri,
+    public FontManagerConfigurator(Configuration cfg, URI baseURI, URI 
fallbackURI,
             ResourceResolver resourceResolver) {
         this.cfg = cfg;
-        this.defaultBaseUri = defaultBaseUri;
+        this.baseURI = baseURI;
+        this.fallbackURI = fallbackURI;
         this.resourceResolver = resourceResolver;
     }
 
@@ -77,13 +81,13 @@ public class FontManagerConfigurator {
                 URI fontBase = 
InternalResourceResolver.getBaseURI(cfg.getChild("font-base")
                                                                       
.getValue(null));
                 
fontManager.setResourceResolver(ResourceResolverFactory.createInternalResourceResolver(
-                        defaultBaseUri.resolve(fontBase), resourceResolver));
+                        baseURI.resolve(fontBase), resourceResolver));
             } catch (URISyntaxException use) {
                 LogUtil.handleException(log, use, true);
             }
         } else {
             
fontManager.setResourceResolver(ResourceResolverFactory.createInternalResourceResolver(
-                    defaultBaseUri, resourceResolver));
+                    fallbackURI, resourceResolver));
         }
         // caching (fonts)
         if (cfg.getChild("use-cache", false) != null) {

Added: xmlgraphics/fop/trunk/test/config/relative-uri/base_font.xconf
URL: 
http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/test/config/relative-uri/base_font.xconf?rev=1542190&view=auto
==============================================================================
--- xmlgraphics/fop/trunk/test/config/relative-uri/base_font.xconf (added)
+++ xmlgraphics/fop/trunk/test/config/relative-uri/base_font.xconf Fri Nov 15 
07:13:53 2013
@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<fop version="1.0">
+  <base>relative/</base>
+  <font-base>fonts/</font-base>
+</fop>

Added: xmlgraphics/fop/trunk/test/config/relative-uri/base_no-font.xconf
URL: 
http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/test/config/relative-uri/base_no-font.xconf?rev=1542190&view=auto
==============================================================================
--- xmlgraphics/fop/trunk/test/config/relative-uri/base_no-font.xconf (added)
+++ xmlgraphics/fop/trunk/test/config/relative-uri/base_no-font.xconf Fri Nov 
15 07:13:53 2013
@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<fop version="1.0">
+  <base>relative/</base>
+</fop>

Added: xmlgraphics/fop/trunk/test/config/relative-uri/no-base_font.xconf
URL: 
http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/test/config/relative-uri/no-base_font.xconf?rev=1542190&view=auto
==============================================================================
--- xmlgraphics/fop/trunk/test/config/relative-uri/no-base_font.xconf (added)
+++ xmlgraphics/fop/trunk/test/config/relative-uri/no-base_font.xconf Fri Nov 
15 07:13:53 2013
@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<fop version="1.0">
+  <font-base>fonts/</font-base>
+</fop>

Added: xmlgraphics/fop/trunk/test/config/relative-uri/no-base_no-font.xconf
URL: 
http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/test/config/relative-uri/no-base_no-font.xconf?rev=1542190&view=auto
==============================================================================
--- xmlgraphics/fop/trunk/test/config/relative-uri/no-base_no-font.xconf (added)
+++ xmlgraphics/fop/trunk/test/config/relative-uri/no-base_no-font.xconf Fri 
Nov 15 07:13:53 2013
@@ -0,0 +1,3 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<fop version="1.0">
+</fop>

Modified: 
xmlgraphics/fop/trunk/test/java/org/apache/fop/apps/FopConfParserTestCase.java
URL: 
http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/test/java/org/apache/fop/apps/FopConfParserTestCase.java?rev=1542190&r1=1542189&r2=1542190&view=diff
==============================================================================
--- 
xmlgraphics/fop/trunk/test/java/org/apache/fop/apps/FopConfParserTestCase.java 
(original)
+++ 
xmlgraphics/fop/trunk/test/java/org/apache/fop/apps/FopConfParserTestCase.java 
Fri Nov 15 07:13:53 2013
@@ -19,6 +19,7 @@
 
 package org.apache.fop.apps;
 
+import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.URI;
@@ -127,4 +128,41 @@ public class FopConfParserTestCase {
         builder.setPreferRenderer(true);
         assertTrue(buildFactory().getRendererFactory().isRendererPreferred());
     }
+
+    @Test
+    public void testRelativeURINoBaseNoFont() throws Exception {
+        checkRelativeURIs("test/config/relative-uri/no-base_no-font.xconf",
+                "", "");
+    }
+
+    @Test
+    public void testRelativeURINoBaseFont() throws Exception {
+        checkRelativeURIs("test/config/relative-uri/no-base_font.xconf",
+                "", "test/config/relative-uri/fonts/");
+    }
+
+    @Test
+    public void testRelativeURIBaseNoFont() throws Exception {
+        checkRelativeURIs("test/config/relative-uri/base_no-font.xconf",
+                "test/config/relative-uri/relative/", 
"test/config/relative-uri/relative/");
+    }
+
+    @Test
+    public void testRelativeURIBaseFont() throws Exception {
+        checkRelativeURIs("test/config/relative-uri/base_font.xconf",
+                "test/config/relative-uri/relative/", 
"test/config/relative-uri/fonts/");
+    }
+
+    private void checkRelativeURIs(String conf, String expectedBase, String 
expectedFontBase)
+            throws SAXException, IOException {
+        File configFile = new File(conf);
+        URI currentDir = new File(".").getCanonicalFile().toURI();
+        FopConfParser parser = new FopConfParser(configFile, currentDir);
+        FopFactoryBuilder fopFactoryBuilder = parser.getFopFactoryBuilder();
+        assertEquals("base URI", currentDir.resolve(expectedBase),
+                fopFactoryBuilder.getBaseURI());
+        assertEquals("font base", currentDir.resolve(expectedFontBase),
+                
fopFactoryBuilder.getFontManager().getResourceResolver().getBaseURI());
+    }
+
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to