xmlgraphics-batik-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dewe...@apache.org
Subject cvs commit: xml-batik/sources/org/apache/batik/transcoder/image JPEGTranscoder.java
Date Mon, 12 Nov 2001 15:37:22 GMT
deweese     01/11/12 07:37:22

  Modified:    sources/org/apache/batik/dom/traversal DOMTreeWalker.java
               sources/org/apache/batik/transcoder/image
                        JPEGTranscoder.java
  Log:
  1) Rewrote nextSibling/prevSibling in tree walker to use a loop
     rather than recursion, otherwise on large documents the stack
     get's blown out when looking for stylesheet processing instructions
     (Does this really require a full search of the DOM tree????)
  2) Implemented an improved version of the Patch in PR 3912. Thanks to
     Michael Hartle <mhartle@hartle-klug.com> for the bug report and
     proposed patch.
  PR: 3912
  Obtained from: Michael Hartle <mhartle@hartle-klug.com>
  Submitted by: Michael Hartle <mhartle@hartle-klug.com>
  Reviewed by: Thomas DeWeese <deweese@apache.org>
  
  Revision  Changes    Path
  1.3       +52 -43    xml-batik/sources/org/apache/batik/dom/traversal/DOMTreeWalker.java
  
  Index: DOMTreeWalker.java
  ===================================================================
  RCS file: /home/cvs/xml-batik/sources/org/apache/batik/dom/traversal/DOMTreeWalker.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- DOMTreeWalker.java	2001/10/15 11:32:56	1.2
  +++ DOMTreeWalker.java	2001/11/12 15:37:22	1.3
  @@ -20,7 +20,7 @@
    * interface.
    *
    * @author <a href="mailto:stephane@hillion.org">Stephane Hillion</a>
  - * @version $Id: DOMTreeWalker.java,v 1.2 2001/10/15 11:32:56 hillion Exp $
  + * @version $Id: DOMTreeWalker.java,v 1.3 2001/11/12 15:37:22 deweese Exp $
    */
   public class DOMTreeWalker implements TreeWalker {
   
  @@ -291,31 +291,35 @@
        * Returns the previous sibling of the given node.
        */
       protected Node previousSibling(Node n, Node root) {
  -        if (n == root) {
  -            return null;
  -        }
  -        Node result = n.getPreviousSibling();
  -        if (result == null) {
  -            result = n.getParentNode();
  -            if (result == null || result == root) {
  +        while (true) {
  +            if (n == root) {
                   return null;
               }
  -            if (acceptNode(result) == NodeFilter.FILTER_SKIP) {
  -                return previousSibling(result, root);
  +            Node result = n.getPreviousSibling();
  +            if (result == null) {
  +                result = n.getParentNode();
  +                if (result == null || result == root) {
  +                    return null;
  +                }
  +                if (acceptNode(result) == NodeFilter.FILTER_SKIP) {
  +                    n = result;
  +                    continue;
  +                }
  +                return null;
               }
  -            return null;
  -        }
  -        switch (acceptNode(result)) {
  -        case NodeFilter.FILTER_ACCEPT:
  -            return result;
  -        case NodeFilter.FILTER_SKIP:
  -            Node t = lastChild(result);
  -            if (t != null) {
  -                return t;
  +            switch (acceptNode(result)) {
  +            case NodeFilter.FILTER_ACCEPT:
  +                return result;
  +            case NodeFilter.FILTER_SKIP:
  +                Node t = lastChild(result);
  +                if (t != null) {
  +                    return t;
  +                }
  +                // Fall through
  +            default: // NodeFilter.FILTER_REJECT
  +                n = result;
  +                continue;
               }
  -            // Fall through
  -        default: // NodeFilter.FILTER_REJECT
  -            return previousSibling(result, root);
           }
       }
   
  @@ -323,31 +327,36 @@
        * Returns the next sibling of the given node.
        */
       protected Node nextSibling(Node n, Node root) {
  -        if (n == root) {
  -            return null;
  -        }
  -        Node result = n.getNextSibling();
  -        if (result == null) {
  -            result = n.getParentNode();
  -            if (result == null || result == root) {
  +        while (true) {
  +            if (n == root) {
                   return null;
               }
  -            if (acceptNode(result) == NodeFilter.FILTER_SKIP) {
  -                return nextSibling(result, root);
  +            Node result = n.getNextSibling();
  +            if (result == null) {
  +                result = n.getParentNode();
  +                if (result == null || result == root) {
  +                    return null;
  +                }
  +                if (acceptNode(result) == NodeFilter.FILTER_SKIP) {
  +                    n = result;
  +                    continue;
  +                }
  +                return null;
               }
  -            return null;
  -        }
  -        switch (acceptNode(result)) {
  -        case NodeFilter.FILTER_ACCEPT:
  -            return result;
  -        case NodeFilter.FILTER_SKIP:
  -            Node t = firstChild(result);
  -            if (t != null) {
  -                return t;
  +
  +            switch (acceptNode(result)) {
  +            case NodeFilter.FILTER_ACCEPT:
  +                return result;
  +            case NodeFilter.FILTER_SKIP:
  +                Node t = firstChild(result);
  +                if (t != null) {
  +                    return t;
  +                }
  +                // Fall through
  +            default: // NodeFilter.FILTER_REJECT
  +                n = result;
  +                continue;
               }
  -            // Fall through
  -        default: // NodeFilter.FILTER_REJECT
  -            return nextSibling(result, root);
           }
       }
   
  
  
  
  1.5       +69 -1     xml-batik/sources/org/apache/batik/transcoder/image/JPEGTranscoder.java
  
  Index: JPEGTranscoder.java
  ===================================================================
  RCS file: /home/cvs/xml-batik/sources/org/apache/batik/transcoder/image/JPEGTranscoder.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- JPEGTranscoder.java	2001/03/28 06:51:24	1.4
  +++ JPEGTranscoder.java	2001/11/12 15:37:22	1.5
  @@ -11,10 +11,12 @@
   import com.sun.image.codec.jpeg.JPEGCodec;
   import com.sun.image.codec.jpeg.JPEGEncodeParam;
   import com.sun.image.codec.jpeg.JPEGImageEncoder;
  +
   import java.awt.Color;
   import java.awt.image.BufferedImage;
   import java.io.IOException;
   import java.io.OutputStream;
  +
   import org.apache.batik.transcoder.TranscoderException;
   import org.apache.batik.transcoder.TranscoderOutput;
   import org.apache.batik.transcoder.TranscodingHints;
  @@ -24,7 +26,7 @@
    * This class is an <tt>ImageTranscoder</tt> that produces a JPEG image.
    *
    * @author <a href="mailto:Thierry.Kormann@sophia.inria.fr">Thierry Kormann</a>
  - * @version $Id: JPEGTranscoder.java,v 1.4 2001/03/28 06:51:24 tkormann Exp $
  + * @version $Id: JPEGTranscoder.java,v 1.5 2001/11/12 15:37:22 deweese Exp $
    */
   public class JPEGTranscoder extends ImageTranscoder {
   
  @@ -53,6 +55,11 @@
       public void writeImage(BufferedImage img, TranscoderOutput output)
               throws TranscoderException {
           OutputStream ostream = output.getOutputStream();
  +        // The outputstream wrapper protects the JPEG encoder from
  +        // exceptions due to stream closings.  If it gets an exception
  +        // it nulls out the stream and just ignores any future calls.
  +        ostream = new OutputStreamWrapper(ostream);
  +
           if (ostream == null) {
               throw new TranscoderException(
                   Messages.formatMessage("jpeg.badoutput", null));
  @@ -112,6 +119,67 @@
                   return (q > 0 && q <= 1f);
               } else {
                   return false;
  +            }
  +        }
  +    }
  +
  +    /**
  +     *  This class will never throw an IOException, instead it eats
  +     * them and then ignores any future calls to it's interface.
  +     */
  +    private static class OutputStreamWrapper extends OutputStream {
  +        OutputStream os;
  +        /**
  +         * Constructs a wrapper around <tt>os</tt> that will not throw
  +         * IOExceptions.
  +         * <@param os>The Stream to wrap.
  +         */
  +        OutputStreamWrapper(OutputStream os) {
  +            this.os = os;
  +        }
  +
  +        public void close() throws IOException { 
  +            if (os == null) return; 
  +            try {
  +                os.close();
  +            } catch (IOException ioe) {
  +                os = null;
  +            }
  +        }
  +
  +        public void flush() throws IOException { 
  +            if (os == null) return; 
  +            try {
  +                os.flush();
  +            } catch (IOException ioe) {
  +                os = null;
  +            }
  +        }
  +
  +        public void write(byte[] b) throws IOException { 
  +            if (os == null) return; 
  +            try {
  +                os.write(b);
  +            } catch (IOException ioe) {
  +                os = null;
  +            }
  +        }
  +        
  +        public void write(byte[] b, int off, int len) throws IOException { 
  +            if (os == null) return; 
  +            try {
  +                os.write(b, off, len);
  +            } catch (IOException ioe) {
  +                os = null;
  +            }
  +        }
  +        
  +        public void write(int b)  throws IOException { 
  +            if (os == null) return; 
  +            try {
  +                os.write(b);
  +            } catch (IOException ioe) {
  +                os = null;
               }
           }
       }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: batik-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-dev-help@xml.apache.org


Mime
View raw message