1 16 package org.apache.cocoon.serialization; 17 18 import java.awt.Color ; 19 import java.io.OutputStream ; 20 import java.io.Serializable ; 21 22 import org.apache.avalon.excalibur.pool.Poolable; 23 import org.apache.avalon.framework.configuration.Configurable; 24 import org.apache.avalon.framework.configuration.Configuration; 25 import org.apache.avalon.framework.configuration.ConfigurationException; 26 import org.apache.avalon.framework.context.Context; 27 import org.apache.avalon.framework.context.ContextException; 28 import org.apache.avalon.framework.context.Contextualizable; 29 import org.apache.batik.transcoder.Transcoder; 30 import org.apache.batik.transcoder.TranscoderInput; 31 import org.apache.batik.transcoder.TranscoderOutput; 32 import org.apache.batik.transcoder.TranscodingHints; 33 import org.apache.batik.transcoder.TranscoderException; 34 import org.apache.batik.util.ParsedURL; 35 import org.apache.cocoon.Constants; 36 import org.apache.cocoon.caching.CacheableProcessingComponent; 37 import org.apache.cocoon.components.transcoder.ExtendableTranscoderFactory; 38 import org.apache.cocoon.components.transcoder.TranscoderFactory; 39 import org.apache.cocoon.components.url.ParsedContextURLProtocolHandler; 40 import org.apache.cocoon.components.url.ParsedResourceURLProtocolHandler; 41 import org.apache.cocoon.util.ClassUtils; 42 import org.apache.cocoon.xml.dom.SVGBuilder; 43 import org.apache.commons.lang.BooleanUtils; 44 import org.apache.excalibur.source.SourceValidity; 45 import org.apache.excalibur.source.impl.validity.NOPValidity; 46 import org.w3c.dom.Document ; 47 import org.xml.sax.SAXException ; 48 49 56 public class SVGSerializer extends SVGBuilder 57 implements Serializer, Configurable, Poolable, CacheableProcessingComponent, Contextualizable { 58 59 62 public void contextualize(Context context) throws ContextException { 63 ParsedContextURLProtocolHandler.setContext( 64 (org.apache.cocoon.environment.Context)context.get(Constants.CONTEXT_ENVIRONMENT_CONTEXT)); 65 ParsedURL.registerHandler(new ParsedContextURLProtocolHandler()); 66 ParsedURL.registerHandler(new ParsedResourceURLProtocolHandler()); 67 } 68 69 70 private OutputStream output; 71 72 73 private String mimetype; 74 75 76 Transcoder transcoder; 77 78 79 TranscoderFactory factory = ExtendableTranscoderFactory.getTranscoderFactoryImplementation(); 80 81 83 85 88 public void setOutputStream(OutputStream out) { 89 this.output = out; 90 91 } 94 95 104 107 public void configure(Configuration conf) throws ConfigurationException { 108 this.mimetype = conf.getAttribute("mime-type"); 109 if (getLogger().isDebugEnabled()) { 110 getLogger().debug("mime-type: " + mimetype); 111 } 112 113 this.transcoder = factory.createTranscoder(mimetype); 116 117 Configuration[] parameters = conf.getChildren("parameter"); 119 for (int i = 0; i < parameters.length; i++) { 120 String name = parameters[i].getAttribute("name"); 121 if ("transcoder".equals(name)) { 122 String transcoderName = parameters[i].getAttribute("value"); 123 try { 124 this.transcoder = (Transcoder)ClassUtils.newInstance(transcoderName); 125 } catch (Exception ex) { 126 if (getLogger().isDebugEnabled()) { 127 getLogger().debug("Cannot load class " + transcoderName, ex); 128 } 129 throw new ConfigurationException("Cannot load class " + transcoderName, ex); 130 } 131 } 132 } 133 if (this.transcoder == null ) { 135 throw new ConfigurationException( 136 "Could not autodetect transcoder for SVGSerializer and " 137 + "no transcoder was specified in the sitemap configuration." 138 ); 139 } 140 141 for (int i = 0; i < parameters.length; i++ ) { 144 String name = parameters[i].getAttribute("name"); 145 if ("transcoder".equals(name)) { 148 continue; 149 } 150 151 try { 153 name = ("KEY_" + name).toUpperCase(); 155 TranscodingHints.Key key = (TranscodingHints.Key) 157 (transcoder.getClass().getField(name).get(transcoder)); 158 Object value; 159 String keyType = parameters[i].getAttribute("type", "STRING").toUpperCase(); 160 if ("FLOAT".equals(keyType)) { 161 value = new Float (parameters[i].getAttributeAsFloat("value")); 163 } else if ("INTEGER".equals(keyType)) { 164 value = new Integer (parameters[i].getAttributeAsInteger("value")); 166 } else if ("BOOLEAN".equals(keyType)) { 167 value = BooleanUtils.toBooleanObject(parameters[i].getAttributeAsBoolean("value")); 169 } else if ("COLOR".equals(keyType)) { 170 String stringValue = parameters[i].getAttribute("value"); 172 if (stringValue.startsWith("#")) { 173 stringValue = stringValue.substring(1); 174 } 175 value = new Color (Integer.parseInt(stringValue, 16)); 176 } else { 177 value = parameters[i].getAttribute("value", ""); 179 } 180 if(getLogger().isDebugEnabled()) { 181 getLogger().debug("Adding hint \"" + name + "\" with value \"" + value.toString() + "\""); 182 } 183 transcoder.addTranscodingHint(key, value); 184 } catch (ClassCastException ex) { 185 throw new ConfigurationException("Specified key (" + name + ") is not a valid Batik Transcoder key.", ex); 187 } catch (ConfigurationException ex) { 188 throw new ConfigurationException("Name or value not specified.", ex); 189 } catch (IllegalAccessException ex) { 190 throw new ConfigurationException("Cannot access the key for parameter \"" + name + "\"", ex); 191 } catch (NoSuchFieldException ex) { 192 throw new ConfigurationException("No field available for parameter \"" + name + "\"", ex); 193 } 194 } 195 } 196 197 200 public void notify(Document doc) throws SAXException { 201 202 try { 203 TranscoderInput transInput = new TranscoderInput(doc); 204 205 TranscoderOutput transOutput = new TranscoderOutput(this.output); 207 transcoder.transcode(transInput, transOutput); 208 } catch (TranscoderException ex) { 209 if (ex.getException() != null) { 210 if (getLogger().isDebugEnabled()) { 211 getLogger().debug("Got transcoder exception writing image, rethrowing nested exception", ex); 212 } 213 throw new SAXException ("Exception writing image", ex.getException()); 214 } 215 216 if (getLogger().isDebugEnabled()) { 217 getLogger().debug("Got transcoder exception writing image, rethrowing", ex); 218 } 219 throw new SAXException ("Exception writing image", ex); 220 } catch (Exception ex) { 221 if (getLogger().isDebugEnabled()) { 222 getLogger().debug("Got exception writing image, rethrowing", ex); 223 } 224 throw new SAXException ("Exception writing image", ex); 225 } 226 } 227 228 231 public String getMimeType() { 232 return mimetype; 233 } 234 235 243 public Serializable getKey() { 244 return "1"; 245 } 246 247 255 public SourceValidity getValidity() { 256 return NOPValidity.SHARED_INSTANCE; 257 } 258 259 269 public boolean shouldSetContentLength() { 270 return true; 271 } 272 } 273 | Popular Tags |