1 16 package org.apache.cocoon.serialization; 17 18 import java.io.File ; 19 import java.io.OutputStream ; 20 import java.io.Serializable ; 21 import java.net.MalformedURLException ; 22 import java.util.Map ; 23 import java.util.HashMap ; 24 25 import org.apache.avalon.framework.CascadingRuntimeException; 26 import org.apache.avalon.framework.configuration.Configurable; 27 import org.apache.avalon.framework.configuration.Configuration; 28 import org.apache.avalon.framework.configuration.ConfigurationException; 29 import org.apache.avalon.framework.logger.Logger; 30 import org.apache.avalon.framework.service.ServiceException; 31 import org.apache.avalon.framework.service.ServiceManager; 32 import org.apache.avalon.framework.service.Serviceable; 33 import org.apache.cocoon.caching.CacheableProcessingComponent; 34 import org.apache.cocoon.components.renderer.ExtendableRendererFactory; 35 import org.apache.cocoon.components.renderer.RendererFactory; 36 import org.apache.cocoon.components.source.SourceUtil; 37 import org.apache.cocoon.util.ClassUtils; 38 import org.apache.excalibur.source.Source; 39 import org.apache.excalibur.source.SourceResolver; 40 import org.apache.excalibur.source.SourceValidity; 41 import org.apache.excalibur.source.impl.validity.NOPValidity; 42 import org.apache.fop.apps.Driver; 43 import org.apache.fop.apps.Options; 44 import org.apache.fop.configuration.ConfigurationParser; 45 import org.apache.fop.messaging.MessageHandler; 46 import org.apache.fop.render.Renderer; 47 48 53 public class FOPSerializer extends AbstractSerializer implements 54 Configurable, CacheableProcessingComponent, Serviceable{ 55 56 58 61 protected final static RendererFactory factory = ExtendableRendererFactory.getRendererFactoryImplementation(); 62 63 66 protected Driver driver; 67 68 71 protected Renderer renderer; 72 73 76 protected String mimetype; 77 78 81 protected String rendererName; 82 83 86 protected boolean setContentLength = true; 87 88 91 protected Logger logger; 92 93 96 private static boolean configured = false; 97 98 101 protected ServiceManager manager; 102 103 106 public void service(ServiceManager manager) throws ServiceException { 107 this.manager = manager; 108 } 110 115 118 public void configure(Configuration conf) throws ConfigurationException { 119 120 this.logger = getLogger().getChildLogger("fop"); 121 MessageHandler.setScreenLogger(this.logger); 122 123 synchronized (FOPSerializer.class) { 127 if (!configured) { 128 try { 129 if (getLogger().isDebugEnabled()) { 130 getLogger().debug("Loading default configuration"); 131 } 132 new Options(); 133 } catch (Exception e) { 134 getLogger().error("Cannot load default configuration. Proceeding.", e); 135 } 136 configured = true; 137 } 138 } 139 140 this.setContentLength = conf.getChild("set-content-length").getValueAsBoolean(true); 141 142 String configUrl = conf.getChild("user-config").getAttribute("src", null); 144 if (configUrl != null) { 145 getLogger().warn("Attribute src of user-config element is deprecated. " 146 + "Provide Cocoon URI as value of the element instead"); 147 try { 148 configUrl = new File (configUrl).toURL().toExternalForm(); 150 } catch (MalformedURLException e) { 151 getLogger().warn("Can not load config file " + configUrl, e); 152 configUrl = null; 153 } 154 } else { 155 configUrl = conf.getChild("user-config").getValue(null); 157 } 158 159 if (configUrl != null) { 160 Source configSource = null; 161 SourceResolver resolver = null; 162 try { 163 resolver = (SourceResolver)this.manager.lookup(SourceResolver.ROLE); 164 configSource = resolver.resolveURI(configUrl); 165 if (getLogger().isDebugEnabled()) { 166 getLogger().debug("Loading configuration from " + configSource.getURI()); 167 } 168 SourceUtil.toSAX(configSource, new ConfigurationParser()); 169 } catch (Exception e) { 170 getLogger().warn("Cannot load configuration from " + configUrl); 171 throw new ConfigurationException("Cannot load configuration from " + configUrl, e); 172 } finally { 173 if (resolver != null) { 174 resolver.release(configSource); 175 manager.release(resolver); 176 } 177 } 178 } 179 180 this.mimetype = conf.getAttribute("mime-type"); 182 183 Configuration[] parameters = conf.getChildren("parameter"); 185 for (int i = 0; i < parameters.length; i++) { 186 String name = parameters[i].getAttribute("name"); 187 if ("renderer".equals(name)) { 188 this.rendererName = parameters[i].getAttribute("value"); 189 try { 190 this.renderer = (Renderer)ClassUtils.newInstance(rendererName); 191 } catch (Exception ex) { 192 getLogger().error("Cannot load class " + rendererName, ex); 193 throw new ConfigurationException("Cannot load class " + rendererName, ex); 194 } 195 } 196 } 197 if (this.renderer == null) { 198 this.renderer = factory.createRenderer(mimetype); 201 } 202 203 if (this.renderer == null ) { 205 throw new ConfigurationException( 206 "Could not autodetect renderer for FOPSerializer and " 207 + "no renderer was specified in the sitemap configuration." 208 ); 209 } 210 211 Configuration confRenderer = conf.getChild("renderer-config"); 212 if (confRenderer != null) { 213 parameters = confRenderer.getChildren("parameter"); 214 if (parameters.length > 0) { 215 Map rendererOptions = new HashMap (); 216 for (int i = 0; i < parameters.length; i++) { 217 String name = parameters[i].getAttribute("name"); 218 String value = parameters[i].getAttribute("value"); 219 220 if (getLogger().isDebugEnabled()) { 221 getLogger().debug("renderer " + String.valueOf(name) + " = " + String.valueOf(value)); 222 } 223 rendererOptions.put(name,value); 224 } 225 this.renderer.setOptions(rendererOptions); 226 } 227 } 228 } 229 230 233 public String getMimeType() { 234 return mimetype; 235 } 236 237 241 public void setOutputStream(OutputStream out) { 242 243 246 this.driver = new Driver(); 248 this.driver.setLogger(this.logger); 249 if (this.rendererName == null) { 250 this.renderer = factory.createRenderer(mimetype); 251 } else { 252 try { 253 this.renderer = (Renderer)ClassUtils.newInstance(this.rendererName); 254 } catch (Exception e) { 255 if (getLogger().isWarnEnabled()) { 256 getLogger().warn("Cannot load class " + this.rendererName, e); 257 } 258 throw new CascadingRuntimeException("Cannot load class " + this.rendererName, e); 259 } 260 } 261 this.driver.setRenderer(this.renderer); 262 this.driver.setOutputStream(out); 263 setContentHandler(this.driver.getContentHandler()); 264 } 265 266 274 public Serializable getKey() { 275 return "1"; 276 } 277 278 286 public SourceValidity getValidity() { 287 return NOPValidity.SHARED_INSTANCE; 288 } 289 290 293 public void recycle() { 294 super.recycle(); 295 this.driver = null; 296 this.renderer = null; 297 } 298 299 302 public boolean shouldSetContentLength() { 303 return this.setContentLength; 304 } 305 306 } 307 | Popular Tags |