tcurdt      2003/01/02 12:47:29

  Modified:    src/java/org/apache/cocoon/generation
                        ImageDirectoryGenerator.java
  Log:
  factored out the image stuff
  
  Revision  Changes    Path
  1.8       +9 -216    
xml-cocoon2/src/java/org/apache/cocoon/generation/ImageDirectoryGenerator.java
  
  Index: ImageDirectoryGenerator.java
  ===================================================================
  RCS file: 
/home/cvs/xml-cocoon2/src/java/org/apache/cocoon/generation/ImageDirectoryGenerator.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- ImageDirectoryGenerator.java      31 Dec 2002 16:25:44 -0000      1.7
  +++ ImageDirectoryGenerator.java      2 Jan 2003 20:47:29 -0000       1.8
  @@ -52,6 +52,9 @@
   
   import org.apache.avalon.framework.CascadingRuntimeException;
   import org.apache.log.Hierarchy;
  +import org.apache.cocoon.util.ImageProperties;
  +import org.apache.cocoon.util.ImageUtils;
  +import org.apache.cocoon.util.FileFormatException;
   import org.xml.sax.SAXException;
   
   import java.io.*;
  @@ -71,33 +74,8 @@
       protected static String IMAGE_COMMENT_ATTR_NAME = "comment";
   
       /**
  -     * TODO: as long as the generator is pooled we could use a single instance to 
reduce object creations
  -     * TODO: don't open the file twice
  -     */
  -    final private class ImageProperties {
  -        int width;
  -        int height;
  -        char[] comment;
  -
  -        public ImageProperties(int width, int height, char[] comment) {
  -            this.width = width;
  -            this.height = height;
  -            this.comment = comment;
  -        }
  -
  -        public String toString() {
  -            StringBuffer sb = new StringBuffer();
  -            sb.append(width).append("x").append(height);
  -            if (comment != null) {
  -                sb.append(" (").append(comment).append(")");
  -            }
  -            return (sb.toString());
  -        }
  -    }
  -
  -    /**
        * Extends the <code>setNodeAttributes</code> method from the
  -     * <code>DirectoryGenerator</code> by adding width and height attributes
  +     * <code>DirectoryGenerator</code> by adding width, height and comment 
attributes
        * if the path is a GIF or a JPEG file.
        */
       protected void setNodeAttributes(File path) throws SAXException {
  @@ -106,7 +84,7 @@
               return;
           }
           try {
  -            ImageProperties p = getImageProperties(path);
  +            ImageProperties p = ImageUtils.getImageProperties(path);
               if (p != null) {
                   if (getLogger().isDebugEnabled()) {
                       getLogger().debug(String.valueOf(path) + " = " + 
String.valueOf(p));
  @@ -116,6 +94,9 @@
                   if (p.comment != null) attributes.addAttribute("", 
IMAGE_COMMENT_ATTR_NAME, IMAGE_COMMENT_ATTR_NAME, "CDATA", String.valueOf(p.comment));
               }
           }
  +        catch (FileFormatException e) {
  +            throw new SAXException(e);
  +        }
           catch (FileNotFoundException e) {
               throw new SAXException(e);
           }
  @@ -124,192 +105,4 @@
           }
       }
   
  -    private ImageProperties getImageProperties(File file) throws 
FileNotFoundException, IOException {
  -        String type = getFileType(file);
  -        if ("gif".equals(type)) {
  -            return (getGifProperties(file));
  -        }
  -        else if ("jpeg".equals(type)) {
  -            return (getJpegProperties(file));
  -        }
  -        else {
  -            if (getLogger().isDebugEnabled()) {
  -                getLogger().debug("cannot handle type " + type + ". not a known 
image");
  -            }
  -            return (null);
  -        }
  -    }
  -
  -    private ImageProperties getJpegProperties(File file) throws 
FileNotFoundException, IOException {
  -        BufferedInputStream in = null;
  -        try {
  -            in = new BufferedInputStream(new FileInputStream(file));
  -            // check for "magic" header
  -            byte[] buf = new byte[2];
  -            int count = in.read(buf, 0, 2);
  -            if (count < 2) {
  -                if (getLogger().isErrorEnabled()) {
  -                    getLogger().error("Not a valid Jpeg file!");
  -                }
  -                return (null);
  -            }
  -            if ((buf[0]) != (byte) 0xFF || (buf[1]) != (byte) 0xD8) {
  -                if (getLogger().isErrorEnabled()) {
  -                    getLogger().error("Not a valid Jpeg file!");
  -                }
  -                return (null);
  -            }
  -
  -            int width = 0;
  -            int height = 0;
  -            char[] comment = null;
  -
  -            boolean hasDims = false;
  -            boolean hasComment = false;
  -            int ch = 0;
  -
  -            while (ch != 0xDA && !(hasDims && hasComment)) {
  -                /* Find next marker (JPEG markers begin with 0xFF) */
  -                while (ch != 0xFF) {
  -                    ch = in.read();
  -                }
  -                /* JPEG markers can be padded with unlimited 0xFF's */
  -                while (ch == 0xFF) {
  -                    ch = in.read();
  -                }
  -                /* Now, ch contains the value of the marker. */
  -
  -                int length = 256 * in.read();
  -                length += in.read();
  -                if (length < 2) {
  -                    if (getLogger().isErrorEnabled()) {
  -                        getLogger().error("Not a valid Jpeg file!");
  -                    }
  -                    return (null);
  -                }
  -                /* Now, length contains the length of the marker. */
  -
  -                /*
  -                if (getLogger().isDebugEnabled()) {
  -                    getLogger().debug("marker 0x" + Integer.toHexString(ch) + " 
len=" + length);
  -                }
  -                */
  -
  -                if (ch >= 0xC0 && ch <= 0xC3) {
  -                    in.read();
  -                    height = 256 * in.read();
  -                    height += in.read();
  -                    width = 256 * in.read();
  -                    width += in.read();
  -                    for (int foo = 0; foo < length - 2 - 5; foo++) {
  -                        int i = in.read();
  -                    }
  -                    hasDims = true;
  -                }
  -                else if (ch == 0xFE) {
  -                    // that's the comment marker
  -                    comment = new char[length-2];
  -                    for (int foo = 0; foo < length - 2; foo++)
  -                        comment[foo] = (char) in.read();
  -                    hasComment = true;
  -                }
  -                else {
  -                    // just skip marker
  -                    for (int foo = 0; foo < length - 2; foo++) {
  -                        int i = in.read();
  -                    }
  -                }
  -            }
  -            return (new ImageProperties(width, height, comment));
  -
  -        }
  -        finally {
  -            if (in != null) {
  -                try {
  -                    in.close();
  -                }
  -                catch (IOException e) {
  -                    if (getLogger().isErrorEnabled()) {
  -                        getLogger().error("close stream", e);
  -                    }
  -                }
  -            }
  -        }
  -    }
  -
  -    private ImageProperties getGifProperties(File file) throws 
FileNotFoundException, IOException {
  -        BufferedInputStream in = null;
  -        try {
  -            in = new BufferedInputStream(new FileInputStream(file));
  -            byte[] buf = new byte[10];
  -            int count = in.read(buf, 0, 10);
  -            if (count < 10) {
  -                if (getLogger().isErrorEnabled()) {
  -                    getLogger().error("Not a valid GIF file!");
  -                }
  -                return (null);
  -            }
  -            if ((buf[0]) != (byte) 'G' || (buf[1]) != (byte) 'I' || (buf[2]) != 
(byte) 'F') {
  -                if (getLogger().isErrorEnabled()) {
  -                    getLogger().error("Not a valid GIF file!");
  -                }
  -                return (null);
  -            }
  -
  -            int w1 = ((int) buf[6] & 0xff) | (buf[6] & 0x80);
  -            int w2 = ((int) buf[7] & 0xff) | (buf[7] & 0x80);
  -            int h1 = ((int) buf[8] & 0xff) | (buf[8] & 0x80);
  -            int h2 = ((int) buf[9] & 0xff) | (buf[9] & 0x80);
  -
  -            int width = w1 + (w2 << 8);
  -            int height = h1 + (h2 << 8);
  -
  -            return (new ImageProperties(width, height, null));
  -
  -        }
  -        finally {
  -            if (in != null) {
  -                try {
  -                    in.close();
  -                }
  -                catch (IOException e) {
  -                    if (getLogger().isErrorEnabled()) {
  -                        getLogger().error("close stream", e);
  -                    }
  -                }
  -            }
  -        }
  -    }
  -
  -    private String getFileType(File file) throws FileNotFoundException, IOException 
{
  -        BufferedInputStream in = null;
  -        try {
  -            in = new BufferedInputStream(new FileInputStream(file));
  -            byte[] buf = new byte[3];
  -            int count = in.read(buf, 0, 3);
  -
  -            if (count < 3)
  -                return (null);
  -
  -            if ((buf[0]) == (byte) 'G' && (buf[1]) == (byte) 'I' && (buf[2]) == 
(byte) 'F')
  -                return ("gif");
  -
  -            if ((buf[0]) == (byte) 0xFF && (buf[1]) == (byte) 0xD8)
  -                return ("jpeg");
  -
  -            return (null);
  -        }
  -        finally {
  -            if (in != null) {
  -                try {
  -                    in.close();
  -                }
  -                catch (IOException e) {
  -                    if (getLogger().isErrorEnabled()) {
  -                        getLogger().error("close stream", e);
  -                    }
  -                }
  -            }
  -        }
  -    }
   }
  
  
  

----------------------------------------------------------------------
In case of troubles, e-mail:     [EMAIL PROTECTED]
To unsubscribe, e-mail:          [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to