rossb       01/02/21 15:42:57

  Modified:    src/org/apache/cocoon/serialization Tag: xml-cocoon2
                        SVGSerializer.java
  Log:
  Added support for configuring the Batik transcoders. It's messy but works -
  all are welcome to cut it down in size!  :-)
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.1.2.25  +84 -19    
xml-cocoon/src/org/apache/cocoon/serialization/Attic/SVGSerializer.java
  
  Index: SVGSerializer.java
  ===================================================================
  RCS file: 
/home/cvs/xml-cocoon/src/org/apache/cocoon/serialization/Attic/SVGSerializer.java,v
  retrieving revision 1.1.2.24
  retrieving revision 1.1.2.25
  diff -u -r1.1.2.24 -r1.1.2.25
  --- SVGSerializer.java        2001/02/20 21:06:46     1.1.2.24
  +++ SVGSerializer.java        2001/02/21 23:42:54     1.1.2.25
  @@ -18,23 +18,20 @@
   import java.io.*;
   import java.awt.*;
   import java.awt.image.*;
  +import java.util.Iterator;
   import org.xml.sax.*;
   import org.xml.sax.ext.*;
   import org.w3c.dom.*;
   import org.w3c.dom.svg.*;
  -
  -import org.apache.batik.refimpl.transcoder.*;
   import org.apache.batik.transcoder.*;
  -import org.apache.batik.refimpl.transcoder.AbstractTranscoder;
  -
  -import java.io.ByteArrayInputStream;
  -import java.io.ByteArrayOutputStream;
  +import org.apache.cocoon.util.ClassUtils;
   
   /**
  - * A Batik based Serializer for generating PNG/JPG images
  + * A Batik based Serializer for generating PNG/JPEG images
    *
    * @author <a href="mailto:[EMAIL PROTECTED]">Davanum Srinivas</a>
  - * @version CVS $Revision: 1.1.2.24 $ $Date: 2001/02/20 21:06:46 $
  + * @author <a href="mailto:[EMAIL PROTECTED]">Ross Burton</a>
  + * @version CVS $Revision: 1.1.2.25 $ $Date: 2001/02/21 23:42:54 $
    */
   public class SVGSerializer extends SVGBuilder implements Composer, 
Serializer, Configurable, PoolClient {
   
  @@ -62,22 +59,92 @@
       private OutputStream output=null;
       /** The current <code>mime-type</code>. */
       private String mimetype = null;
  +    /** The current <code>Transcoder</code>.  */
  +    Transcoder transcoder = null;
   
       /**
        * Set the <code>OutputStream</code> where the XML should be serialized.
        */
       public void setOutputStream(OutputStream out) {
  -        this.output=new BufferedOutputStream(out);
  +        this.output = new BufferedOutputStream(out);
       }
  -
  +    
       /**
        * Set the configurations for this serializer.
        */
       public void configure(Configuration conf) throws ConfigurationException {
  -        this.config = conf;
  -
  -        mimetype = this.config.getAttribute("mime-type");
  +        this.mimetype = conf.getAttribute("mime-type");
           log.debug("SVGSerializer mime-type:" + mimetype);
  +        // TODO: take the mime type and create a transcoder from it
  +        // allow the parameter "transcoder" to override it however
  +        String transcoderName = null; // TODO: whatever the factory will say 
it is
  +       
  +        // Iterate through the parameters, looking for a transcoder reference
  +        for (Iterator i = conf.getChildren("parameter"); i.hasNext(); ) {
  +            Configuration paramConf = (Configuration)i.next();
  +            String name = paramConf.getAttribute("name");
  +            if ("transcoder".equals(name)) {
  +                transcoderName = paramConf.getAttribute("value");
  +            }
  +        }
  +        // Now try creating this transcoder
  +        try {
  +            this.transcoder = 
(Transcoder)ClassUtils.newInstance(transcoderName);
  +        } catch (Exception ex) {
  +            log.error("Cannot load  class " + transcoderName, ex);
  +            throw new ConfigurationException("Cannot load class " + 
transcoderName, ex);
  +        }
  +        // Now run through the other parameters, using them as hints
  +        // to the transcoder
  +        for (Iterator i = conf.getChildren("parameter"); i.hasNext(); ) {
  +            Configuration paramConf = (Configuration)i.next();
  +            String name = paramConf.getAttribute("name");
  +            // Skip over the parameters we've dealt with. Ensure this
  +            // is kept in sync with the above list!
  +            if ("transcoder".equals(name)) continue;
  +            // Now try and get the hints out
  +            try {
  +                // Turn it into a key name (assume the current Batik style 
continues!
  +                name = ("KEY_" + name).toUpperCase();
  +                // Use reflection to get a reference to the key object
  +                TranscodingHints.Key key = (TranscodingHints.Key)
  +                    (transcoder.getClass().getField(name).get(transcoder));
  +                Object value;
  +                String keyType = paramConf.getAttribute("type", 
"STRING").toUpperCase();
  +                if ("FLOAT".equals(keyType)) {
  +                    // Can throw an exception.
  +                    value = new 
Float(paramConf.getAttributeAsFloat("value"));
  +                } else if ("INTEGER".equals(keyType)) {
  +                    // Can throw an exception.
  +                    value = new 
Integer(paramConf.getAttributeAsInt("value"));
  +                } else if ("BOOLEAN".equals(keyType)) {
  +                    // Can throw an exception.
  +                    value = new 
Boolean(paramConf.getAttributeAsBoolean("value"));
  +                } else if ("COLOR".equals(keyType)) {
  +                  // Can throw an exception
  +                  String stringValue = paramConf.getAttribute("value");
  +                  if (stringValue.startsWith("#")) {
  +                    stringValue = stringValue.substring(1);
  +                  }
  +                  value = new Color(Integer.parseInt(stringValue, 16));
  +                } else {
  +                    // Assume String, and get the value. Allow an empty 
string.
  +                    value = paramConf.getValue("");
  +                }
  +                // TODO: if (logger.isDebug()) 
  +                log.debug("SVG Serializer: adding hint \"" + name + "\" with 
value \"" + value.toString() + "\"");
  +                transcoder.addTranscodingHint(key, value);
  +            } catch (ClassCastException ex) {
  +                // This is only thrown from the String keyType... line
  +                throw new ConfigurationException("Specified key (" + name + 
") is not a valid Batik Transcoder key.", ex);
  +            } catch (ConfigurationException ex) {
  +                throw new ConfigurationException("Name or value not 
specified.", ex);
  +            } catch (IllegalAccessException ex) {
  +                throw new ConfigurationException("Cannot access the key for 
parameter \"" + name + "\"", ex);
  +            } catch (NoSuchFieldException ex) {
  +                throw new ConfigurationException("No field available for 
parameter \"" + name + "\"", ex);
  +            }
  +        }
       }
   
       /**
  @@ -128,14 +195,12 @@
        */
       public void notify(Document doc) throws SAXException {
           try {
  -            TranscoderFactory transcoderFactory =
  -                
ConcreteTranscoderFactory.getTranscoderFactoryImplementation();
  -            AbstractTranscoder transcoder = (AbstractTranscoder)
  -                transcoderFactory.createTranscoder(mimetype);
  -            transcoder.transcodeToStream(doc,this.output);
  +            TranscoderInput transInput = new TranscoderInput(doc);
  +            TranscoderOutput transOutput = new TranscoderOutput(this.output);
  +            transcoder.transcode(transInput, transOutput);
               this.output.flush();
           } catch (Exception ex) {
  -            log.warn("SVGSerializer: Exception writing image", ex);
  +            log.error("SVGSerializer: Exception writing image", ex);
               throw new SAXException("Exception writing image ", ex);
           }
       }
  
  
  

Reply via email to