Updated Branches: refs/heads/master 64cf212e9 -> f9bf77b1b
CAMEL-6666: Allow to load fop user config from classpath. Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/f9bf77b1 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/f9bf77b1 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/f9bf77b1 Branch: refs/heads/master Commit: f9bf77b1b443eb05424c1a32f8a0f777528ab8b5 Parents: 64cf212 Author: Claus Ibsen <davscl...@apache.org> Authored: Sat Aug 24 12:21:09 2013 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Sat Aug 24 12:21:09 2013 +0200 ---------------------------------------------------------------------- .../apache/camel/component/fop/FopEndpoint.java | 37 +++++++++-------- .../camel/component/fop/FopEndpointTest.java | 11 ++++- .../src/test/resources/myconf/testcfg.xml | 42 ++++++++++++++++++++ 3 files changed, 70 insertions(+), 20 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/f9bf77b1/components/camel-fop/src/main/java/org/apache/camel/component/fop/FopEndpoint.java ---------------------------------------------------------------------- diff --git a/components/camel-fop/src/main/java/org/apache/camel/component/fop/FopEndpoint.java b/components/camel-fop/src/main/java/org/apache/camel/component/fop/FopEndpoint.java index 7e73959..24042d7 100644 --- a/components/camel-fop/src/main/java/org/apache/camel/component/fop/FopEndpoint.java +++ b/components/camel-fop/src/main/java/org/apache/camel/component/fop/FopEndpoint.java @@ -17,8 +17,7 @@ package org.apache.camel.component.fop; import java.io.IOException; - -import org.xml.sax.SAXException; +import java.io.InputStream; import org.apache.avalon.framework.configuration.Configuration; import org.apache.avalon.framework.configuration.ConfigurationException; @@ -27,8 +26,9 @@ import org.apache.camel.Consumer; import org.apache.camel.Processor; import org.apache.camel.Producer; import org.apache.camel.impl.DefaultEndpoint; -import org.apache.fop.apps.FOPException; +import org.apache.camel.util.ResourceHelper; import org.apache.fop.apps.FopFactory; +import org.xml.sax.SAXException; /** * Represents a Fop endpoint. @@ -41,7 +41,6 @@ public class FopEndpoint extends DefaultEndpoint { public FopEndpoint(String uri, FopComponent component, String remaining) { super(uri, component); this.remaining = remaining; - this.fopFactory = FopFactory.newInstance(); } public Producer createProducer() throws Exception { @@ -62,25 +61,25 @@ public class FopEndpoint extends DefaultEndpoint { public void setUserConfigURL(String userConfigURL) { this.userConfigURL = userConfigURL; - updateConfigurations(); } - private void updateConfigurations() { + private static void updateConfigurations(InputStream is, FopFactory fopFactory) throws SAXException, IOException, ConfigurationException { DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder(); - Configuration cfg; - try { - cfg = cfgBuilder.buildFromFile(this.userConfigURL); - } catch (SAXException e) { - throw new RuntimeException(e); - } catch (IOException e) { - throw new RuntimeException(e); - } catch (ConfigurationException e) { - throw new RuntimeException(e); + Configuration cfg = cfgBuilder.build(is); + fopFactory.setUserConfig(cfg); + } + + @Override + protected void doStart() throws Exception { + super.doStart(); + + if (fopFactory == null) { + fopFactory = FopFactory.newInstance(); } - try { - fopFactory.setUserConfig(cfg); - } catch (FOPException e) { - throw new RuntimeException(e); + + if (userConfigURL != null) { + InputStream is = ResourceHelper.resolveMandatoryResourceAsInputStream(getCamelContext().getClassResolver(), userConfigURL); + updateConfigurations(is, fopFactory); } } } http://git-wip-us.apache.org/repos/asf/camel/blob/f9bf77b1/components/camel-fop/src/test/java/org/apache/camel/component/fop/FopEndpointTest.java ---------------------------------------------------------------------- diff --git a/components/camel-fop/src/test/java/org/apache/camel/component/fop/FopEndpointTest.java b/components/camel-fop/src/test/java/org/apache/camel/component/fop/FopEndpointTest.java index 3acfc6a..ed9a6e4 100644 --- a/components/camel-fop/src/test/java/org/apache/camel/component/fop/FopEndpointTest.java +++ b/components/camel-fop/src/test/java/org/apache/camel/component/fop/FopEndpointTest.java @@ -46,7 +46,16 @@ public class FopEndpointTest extends CamelTestSupport { @Test public void specifyCustomUserConfigurationFile() throws Exception { FopEndpoint customConfiguredEndpoint = context() - .getEndpoint("fop:application/pdf?userConfigURL=src/test/data/conf/testcfg.xml", + .getEndpoint("fop:application/pdf?userConfigURL=file:src/test/data/conf/testcfg.xml", + FopEndpoint.class); + float customSourceResolution = customConfiguredEndpoint.getFopFactory().getSourceResolution(); + assertEquals(96.0, customSourceResolution, 0.1); + } + + @Test + public void specifyCustomUserConfigurationFileClasspath() throws Exception { + FopEndpoint customConfiguredEndpoint = context() + .getEndpoint("fop:application/pdf?userConfigURL=myconf/testcfg.xml", FopEndpoint.class); float customSourceResolution = customConfiguredEndpoint.getFopFactory().getSourceResolution(); assertEquals(96.0, customSourceResolution, 0.1); http://git-wip-us.apache.org/repos/asf/camel/blob/f9bf77b1/components/camel-fop/src/test/resources/myconf/testcfg.xml ---------------------------------------------------------------------- diff --git a/components/camel-fop/src/test/resources/myconf/testcfg.xml b/components/camel-fop/src/test/resources/myconf/testcfg.xml new file mode 100644 index 0000000..9cbdd61 --- /dev/null +++ b/components/camel-fop/src/test/resources/myconf/testcfg.xml @@ -0,0 +1,42 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> +<fop version="1.0"> + + <!-- Strict user configuration --> + <strict-configuration>true</strict-configuration> + + <!-- Strict FO validation --> + <strict-validation>true</strict-validation> + + <!-- Base URL for resolving relative URLs --> + <base>./</base> + + <!-- Font Base URL for resolving relative font URLs --> + <font-base>./</font-base> + + <!-- Source resolution in dpi (dots/pixels per inch) for determining the size of pixels in SVG and bitmap images, default: 72dpi --> + <source-resolution>96</source-resolution> + <!-- Target resolution in dpi (dots/pixels per inch) for specifying the target resolution for generated bitmaps, default: 72dpi --> + <target-resolution>72</target-resolution> + + <!-- default page-height and page-width, in case + value is specified as auto --> + <default-page-settings height="11in" width="8.26in"/> + + <!-- etc. etc..... --> +</fop>