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. git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@1542190 13f79535-47bb-0310-9956-ffa450edef68tags/fop-2_0
@@ -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)); | |||
} | |||
/** | |||
@@ -122,6 +114,20 @@ public class FopConfParser { | |||
this(fopConfFile, ResourceResolverFactory.createDefaultResourceResolver()); | |||
} | |||
/** | |||
* 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. | |||
* | |||
@@ -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); |
@@ -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); |
@@ -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) { |
@@ -0,0 +1,5 @@ | |||
<?xml version="1.0" encoding="UTF-8"?> | |||
<fop version="1.0"> | |||
<base>relative/</base> | |||
<font-base>fonts/</font-base> | |||
</fop> |
@@ -0,0 +1,4 @@ | |||
<?xml version="1.0" encoding="UTF-8"?> | |||
<fop version="1.0"> | |||
<base>relative/</base> | |||
</fop> |
@@ -0,0 +1,4 @@ | |||
<?xml version="1.0" encoding="UTF-8"?> | |||
<fop version="1.0"> | |||
<font-base>fonts/</font-base> | |||
</fop> |
@@ -0,0 +1,3 @@ | |||
<?xml version="1.0" encoding="UTF-8"?> | |||
<fop version="1.0"> | |||
</fop> |
@@ -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()); | |||
} | |||
} |