Author: ilgrosso
Date: Tue Feb 26 13:15:08 2013
New Revision: 1450159
URL: http://svn.apache.org/r1450159
Log:
[COCOON3-99] Applying provided patch with some minor rework
Modified:
cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/fop/FopSerializer.java
Modified: cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/fop/FopSerializer.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/fop/FopSerializer.java?rev=1450159&r1=1450158&r2=1450159&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/fop/FopSerializer.java (original)
+++ cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/fop/FopSerializer.java Tue Feb 26 13:15:08 2013
@@ -19,6 +19,10 @@
package org.apache.cocoon.optional.pipeline.components.sax.fop;
import java.io.OutputStream;
+import java.net.URL;
+import java.util.Map;
+import org.apache.avalon.framework.configuration.Configuration;
+import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
import org.apache.cocoon.pipeline.ProcessingException;
import org.apache.cocoon.pipeline.caching.CacheKey;
@@ -30,11 +34,19 @@ import org.apache.fop.apps.FOPException;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FopFactory;
import org.apache.xmlgraphics.util.MimeConstants;
-import org.xml.sax.ContentHandler;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
public class FopSerializer extends AbstractSAXSerializer implements CachingPipelineComponent {
- private final static FopFactory FOP_FACTORY = FopFactory.newInstance();
+ /**
+ * This class log.
+ */
+ private static final Logger LOG = LoggerFactory.getLogger(FopSerializer.class);
+
+ private static final FopFactory FOP_FACTORY = FopFactory.newInstance();
+
+ private static final String USER_CONFIG_PATH = "userConfigPath";
private String outputFormat;
@@ -47,11 +59,13 @@ public class FopSerializer extends Abstr
/**
* Create a new FOP serializer that produces the specified mime
- *
+ *
* @param outputFormat the output's mime type
* @see {@link MimeConstants}
*/
- public FopSerializer(String outputFormat) {
+ public FopSerializer(final String outputFormat) {
+ super();
+
if (outputFormat == null) {
throw new IllegalArgumentException("The parameter 'outputFormat' mustn't be null.");
}
@@ -59,17 +73,49 @@ public class FopSerializer extends Abstr
this.outputFormat = outputFormat;
}
+ /**
+ * {@inheritDoc}
+ *
+ * @see org.apache.cocoon.pipeline.component.PipelineComponent#setConfiguration(java.util.Map)
+ *
+ * If you want to set an user configuration on the FOP factory, just add the 'userConfigPath' sitemap parameter,
+ * whose value is relative path to the COB-INF/ path. Example:
+ *
+ * <map:serialize type="fo2pdf"> <map:parameter name="userConfigPath"
+ * value="fopconf/fop.xconf"/> </map:serialize>
+ *
+ * This permits to store the user configuration (together with the fonts you might want to load) in the cocoon block
+ * sources.
+ */
+ @Override
+ public void setConfiguration(final Map<String, ? extends Object> configuration) {
+ super.setConfiguration(configuration);
+
+ if (configuration.containsKey(USER_CONFIG_PATH)) {
+ final String userConfigPath = (String) configuration.get(USER_CONFIG_PATH);
+ final URL userConfigURL = this.getClass().getResource("/COB-INF/" + userConfigPath);
+
+ final DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder();
+ try {
+ final Configuration config = cfgBuilder.build(userConfigURL.openStream());
+ FOP_FACTORY.setUserConfig(config);
+ } catch (Exception e) {
+ LOG.error("User configuration not set", e);
+ }
+ }
+
+ }
+
+ @Override
public CacheKey constructCacheKey() {
return new SimpleCacheKey();
}
@Override
- public void setOutputStream(OutputStream outputStream) {
+ public void setOutputStream(final OutputStream outputStream) {
try {
- Fop fop = FOP_FACTORY.newFop(this.outputFormat, outputStream);
- ContentHandler fopContentHandler = fop.getDefaultHandler();
-
- this.setContentHandler(fopContentHandler);
+ final Fop fop = FOP_FACTORY.newFop(this.outputFormat, outputStream);
+ this.setContentHandler(fop.getDefaultHandler());
} catch (FOPException e) {
throw new ProcessingException("Impossible to initialize FOPSerializer", e);
}
lmpx.com only provides a reader for public news (NNTP) servers. It is not
affiliated with the servers or forums shown here and is not responsible for
the content of articles, which is written by their respective authors.