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 <[EMAIL PROTECTED]> for the bug report and
     proposed patch.
  PR: 3912
  Obtained from: Michael Hartle <[EMAIL PROTECTED]>
  Submitted by: Michael Hartle <[EMAIL PROTECTED]>
  Reviewed by: Thomas DeWeese <[EMAIL PROTECTED]>
  
  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:[EMAIL PROTECTED]";>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:[EMAIL PROTECTED]";>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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to