diff options
-rw-r--r-- | src/java/org/apache/fop/apps/FopConfParser.java | 53 | ||||
-rw-r--r-- | src/java/org/apache/fop/cli/CommandLineOptions.java | 7 | ||||
-rw-r--r-- | src/java/org/apache/fop/fonts/FontManagerConfigurator.java | 16 | ||||
-rw-r--r-- | test/config/relative-uri/base_font.xconf | 5 | ||||
-rw-r--r-- | test/config/relative-uri/base_no-font.xconf | 4 | ||||
-rw-r--r-- | test/config/relative-uri/no-base_font.xconf | 4 | ||||
-rw-r--r-- | test/config/relative-uri/no-base_no-font.xconf | 3 | ||||
-rw-r--r-- | test/java/org/apache/fop/apps/FopConfParserTestCase.java | 38 |
8 files changed, 101 insertions, 29 deletions
diff --git a/src/java/org/apache/fop/apps/FopConfParser.java b/src/java/org/apache/fop/apps/FopConfParser.java index b4918ef30..b0fa40f45 100644 --- a/src/java/org/apache/fop/apps/FopConfParser.java +++ b/src/java/org/apache/fop/apps/FopConfParser.java @@ -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); diff --git a/src/java/org/apache/fop/cli/CommandLineOptions.java b/src/java/org/apache/fop/cli/CommandLineOptions.java index 41fcdcac5..ad8019a7d 100644 --- a/src/java/org/apache/fop/cli/CommandLineOptions.java +++ b/src/java/org/apache/fop/cli/CommandLineOptions.java @@ -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); diff --git a/src/java/org/apache/fop/fonts/FontManagerConfigurator.java b/src/java/org/apache/fop/fonts/FontManagerConfigurator.java index fc2ce06a6..72c1684b6 100644 --- a/src/java/org/apache/fop/fonts/FontManagerConfigurator.java +++ b/src/java/org/apache/fop/fonts/FontManagerConfigurator.java @@ -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) { diff --git a/test/config/relative-uri/base_font.xconf b/test/config/relative-uri/base_font.xconf new file mode 100644 index 000000000..872c102e7 --- /dev/null +++ b/test/config/relative-uri/base_font.xconf @@ -0,0 +1,5 @@ +<?xml version="1.0" encoding="UTF-8"?> +<fop version="1.0"> + <base>relative/</base> + <font-base>fonts/</font-base> +</fop> diff --git a/test/config/relative-uri/base_no-font.xconf b/test/config/relative-uri/base_no-font.xconf new file mode 100644 index 000000000..97d378511 --- /dev/null +++ b/test/config/relative-uri/base_no-font.xconf @@ -0,0 +1,4 @@ +<?xml version="1.0" encoding="UTF-8"?> +<fop version="1.0"> + <base>relative/</base> +</fop> diff --git a/test/config/relative-uri/no-base_font.xconf b/test/config/relative-uri/no-base_font.xconf new file mode 100644 index 000000000..165cc1231 --- /dev/null +++ b/test/config/relative-uri/no-base_font.xconf @@ -0,0 +1,4 @@ +<?xml version="1.0" encoding="UTF-8"?> +<fop version="1.0"> + <font-base>fonts/</font-base> +</fop> diff --git a/test/config/relative-uri/no-base_no-font.xconf b/test/config/relative-uri/no-base_no-font.xconf new file mode 100644 index 000000000..211d94212 --- /dev/null +++ b/test/config/relative-uri/no-base_no-font.xconf @@ -0,0 +1,3 @@ +<?xml version="1.0" encoding="UTF-8"?> +<fop version="1.0"> +</fop> diff --git a/test/java/org/apache/fop/apps/FopConfParserTestCase.java b/test/java/org/apache/fop/apps/FopConfParserTestCase.java index 3c2930942..dc794fdb8 100644 --- a/test/java/org/apache/fop/apps/FopConfParserTestCase.java +++ b/test/java/org/apache/fop/apps/FopConfParserTestCase.java @@ -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()); + } + } |