vgritsenko 2002/09/21 21:49:16 Modified: . Tag: cocoon_2_0_3_branch changes.xml src/java/org/apache/cocoon/serialization Tag: cocoon_2_0_3_branch FOPSerializer.java fop.xmap Log: Fix bug 6533; and allow relative path to the FOP user config. Revision Changes Path No revision No revision 1.138.2.56 +9 -1 xml-cocoon2/changes.xml Index: changes.xml =================================================================== RCS file: /home/cvs/xml-cocoon2/changes.xml,v retrieving revision 1.138.2.55 retrieving revision 1.138.2.56 diff -u -r1.138.2.55 -r1.138.2.56 --- changes.xml 21 Sep 2002 17:31:41 -0000 1.138.2.55 +++ changes.xml 22 Sep 2002 04:49:15 -0000 1.138.2.56 @@ -39,6 +39,14 @@ </devs> <release version="@version@" date="@date@"> + <action dev="VG" type="update"> + FOP user configuration can now be specified as URL or relative path + as the value of the >user-config< element. + </action> + <action dev="VG" type="fix" fixes-bug="6533"> + Load default FOP configuration only once, do not overwrite any user + configuration. + </action> <action dev="VG" type="fix" fixes-bug="11856" due-to="Stefan Seifert" due-to-email="[EMAIL PROTECTED]"> Remove unnessesary code in SVGBuilder. This also fixes intermittent ClassCastExceptions in Batik code. No revision No revision 1.7.2.3 +91 -49 xml-cocoon2/src/java/org/apache/cocoon/serialization/Attic/FOPSerializer.java Index: FOPSerializer.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/serialization/Attic/FOPSerializer.java,v retrieving revision 1.7.2.2 retrieving revision 1.7.2.3 diff -u -r1.7.2.2 -r1.7.2.3 --- FOPSerializer.java 17 Jul 2002 13:23:41 -0000 1.7.2.2 +++ FOPSerializer.java 22 Sep 2002 04:49:15 -0000 1.7.2.3 @@ -53,33 +53,42 @@ import org.apache.avalon.framework.configuration.Configurable; import org.apache.avalon.framework.configuration.Configuration; import org.apache.avalon.framework.configuration.ConfigurationException; -import org.apache.avalon.framework.logger.Loggable; import org.apache.avalon.framework.logger.LogKitLogger; +import org.apache.avalon.framework.component.Composable; +import org.apache.avalon.framework.component.ComponentManager; +import org.apache.avalon.framework.component.ComponentException; import org.apache.cocoon.caching.CacheValidity; import org.apache.cocoon.caching.Cacheable; import org.apache.cocoon.caching.NOPCacheValidity; import org.apache.cocoon.components.renderer.ExtendableRendererFactory; import org.apache.cocoon.components.renderer.RendererFactory; +import org.apache.cocoon.components.url.URLFactory; import org.apache.cocoon.util.ClassUtils; +import org.apache.cocoon.environment.URLFactorySourceResolver; +import org.apache.cocoon.environment.Source; import org.apache.fop.apps.Driver; import org.apache.fop.apps.Options; import org.apache.fop.messaging.MessageHandler; import org.apache.fop.render.Renderer; +import org.apache.fop.configuration.ConfigurationParser; import java.io.OutputStream; +import java.io.File; +import java.net.MalformedURLException; /** * @author ? + * @author <a href="mailto:[EMAIL PROTECTED]">Vadim Gritsenko</a> * @version CVS $Id$ */ public class FOPSerializer extends AbstractSerializer -implements Configurable, Cacheable { +implements Composable, Configurable, Cacheable { /** - * The <code>Options</code> used by FOP. + * The Renderer Factory to use */ - protected Options options; + protected static RendererFactory factory = ExtendableRendererFactory.getRendererFactoryImplementation(); /** * The <code>Driver</code> which is FOP. @@ -92,77 +101,105 @@ protected Renderer renderer; /** - * The Renderer Factory to use - */ - protected static RendererFactory factory = ExtendableRendererFactory.getRendererFactoryImplementation(); - - /** - * The current <code>mime-type</code>. - */ + * The current <code>mime-type</code>. + */ protected String mimetype; /** * The renderer name if configured */ protected String rendererName; - + /** * Should we set the content length ? */ protected boolean setContentLength = true; - /** This logger is used for FOP */ + /** + * This logger is used for FOP + */ protected org.apache.avalon.framework.logger.Logger logger; + /** It is used to make sure that default Options loaded only once. */ + private static boolean configured = false; + + /** Manager to get URLFactory from. */ + protected ComponentManager manager; + /** - * Set the configurations for this serializer. - */ - public void configure(Configuration conf) - throws ConfigurationException { + * Set the component manager for this serializer. + */ + public void compose(ComponentManager componentManager) + throws ComponentException { + this.manager = componentManager; + } + + /** + * Set the configurations for this serializer. + */ + public void configure(Configuration conf) throws ConfigurationException { + this.logger = new LogKitLogger(getLogger().getChildLogger("fop")); MessageHandler.setScreenLogger(this.logger); - String userConfig = null; - java.io.File userConfigFile = null; - if (conf != null) { - Configuration child = conf.getChild("user-config"); - if (child != null) { + // FIXME: VG: Initialize static FOP configuration with defaults, only once. + // FOP has static config, but that's going to change in the near future. + // Then this code should be reviewed. + synchronized (FOPSerializer.class) { + if (!configured) { try { - userConfig = child.getAttribute("src"); - } catch(Exception ex) { - // No config file specified + if (getLogger().isDebugEnabled()) { + getLogger().debug("Loading default configuration"); + } + new Options(); + } catch (Exception e) { + getLogger().error("Cannot load default configuration. Proceeding.", e); } + configured = true; } - - this.setContentLength = conf.getChild("set-content-length").getValueAsBoolean(true); } - // Check for null, use external or inbuilt config. - if(userConfig != null) { + this.setContentLength = conf.getChild("set-content-length").getValueAsBoolean(true); + + // Old syntax: Attribute src of element user-config contains file + String configUrl = conf.getChild("user-config").getAttribute("src", null); + if (configUrl != null) { try { - userConfigFile = new java.io.File(userConfig); - options = new Options(userConfigFile); - getLogger().debug("Using config file " + userConfig); - } catch (Exception ex) { - getLogger().error("Cannot load config " + userConfig, ex); - throw new ConfigurationException("Cannot load config " + userConfig, ex); + // VG: Old version of serializer supported only files + configUrl = new File(configUrl).toURL().toExternalForm(); + } catch (MalformedURLException e) { + getLogger().warn("Can not load config file " + configUrl, e); + configUrl = null; } } else { + // New syntax: Element user-config contains URL + configUrl = conf.getChild("user-config").getValue(null); + } + if(configUrl != null) { + URLFactory urlFactory = null; + Source configSource = null; try { - options = new Options(); - getLogger().debug("Using default config file"); + urlFactory = (URLFactory)manager.lookup(URLFactory.ROLE); + URLFactorySourceResolver urlResolver = new URLFactorySourceResolver(urlFactory, manager); + configSource = urlResolver.resolve(configUrl); + if (getLogger().isDebugEnabled()) { + getLogger().debug("Loading configuration from " + configSource.getSystemId()); + } + configSource.toSAX(new ConfigurationParser()); } catch (Exception e) { - getLogger().error("Cannot load default config ", e); + getLogger().warn("Cannot load configuration from " + configUrl); + throw new ConfigurationException("Cannot load configuration from " + configUrl, e); + } finally { + manager.release(urlFactory); + if (configSource != null) { + configSource.recycle(); + } } } // Get the mime type. this.mimetype = conf.getAttribute("mime-type"); - getLogger().debug("FOPSerializer mime-type:" + mimetype); - // Using the Renderer Factory, get the default renderer - // for this MIME type. - this.renderer = factory.createRenderer(mimetype); // Iterate through the parameters, looking for a renderer reference Configuration[] parameters = conf.getChildren("parameter"); for (int i = 0; i < parameters.length; i++) { @@ -177,6 +214,11 @@ } } } + if (this.renderer == null) { + // Using the Renderer Factory, get the default renderer + // for this MIME type. + this.renderer = factory.createRenderer(mimetype); + } // Do we have a renderer yet? if (this.renderer == null ) { @@ -201,21 +243,22 @@ public void setOutputStream(OutputStream out) { // load the fop driver this.driver = new Driver(); - this.driver.setLogger(this.logger); if (this.rendererName == null) { this.renderer = factory.createRenderer(mimetype); } else { try { this.renderer = (Renderer)ClassUtils.newInstance(this.rendererName); - } catch (Exception ex) { - this.getLogger().error("Cannot load class " + this.rendererName, ex); - throw new RuntimeException("Cannot load class " + this.rendererName); + } catch (Exception e) { + if (getLogger().isWarnEnabled()) { + getLogger().warn("Cannot load class " + this.rendererName, e); + } + throw new RuntimeException("Cannot load class " + this.rendererName + "(" + e + ")"); } } this.driver.setRenderer(this.renderer); this.driver.setOutputStream(out); - this.setContentHandler( this.driver.getContentHandler() ); + setContentHandler(this.driver.getContentHandler()); } /** @@ -247,7 +290,6 @@ */ public void recycle() { super.recycle(); - this.options = null; this.driver = null; this.renderer = null; } 1.1.2.1 +16 -3 xml-cocoon2/src/java/org/apache/cocoon/serialization/Attic/fop.xmap Index: fop.xmap =================================================================== RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/serialization/Attic/fop.xmap,v retrieving revision 1.1 retrieving revision 1.1.2.1 diff -u -r1.1 -r1.1.2.1 --- fop.xmap 9 Mar 2002 06:28:26 -0000 1.1 +++ fop.xmap 22 Sep 2002 04:49:15 -0000 1.1.2.1 @@ -2,7 +2,20 @@ <xmap xpath="/sitemap/components/serializers" unless="serializer[@name='fo2pdf']"> - <map:serializer name="fo2pdf" src="org.apache.cocoon.serialization.FOPSerializer" mime-type="application/pdf"/> - <map:serializer name="fo2ps" src="org.apache.cocoon.serialization.FOPSerializer" mime-type="application/postscript"/> - <map:serializer name="fo2pcl" src="org.apache.cocoon.serialization.FOPSerializer" mime-type="vnd.hp-PCL"/> + <map:serializer logger="sitemap.serializer.fo2pdf" name="fo2pdf" src="org.apache.cocoon.serialization.FOPSerializer" mime-type="application/pdf"> + <!-- This element specifies URL to FOP user configuration file. + It can be absolute file URL or relative to the servlet context. + Examples: + + <user-config>file:/C:/cocoon/fop-config.xml</user-config> + <user-config>WEB-INF/fop-config.xml</user-config> + --> + + <!-- Should serializer set content length header or not? + Default is true. + <set-content-length>true</set-content-length> + --> + </map:serializer> + <map:serializer logger="sitemap.serializer.fo2ps" name="fo2ps" src="org.apache.cocoon.serialization.FOPSerializer" mime-type="application/postscript"/> + <map:serializer logger="sitemap.serializer.fo2pcl" name="fo2pcl" src="org.apache.cocoon.serialization.FOPSerializer" mime-type="vnd.hp-PCL"/> </xmap>
---------------------------------------------------------------------- In case of troubles, e-mail: [EMAIL PROTECTED] To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]