001    /* PAVLOV -- Multiple Choice Study System
002     * Copyright (C) 2000 - 2004 T.J. Willis
003     * 
004     * This program is free software; you can redistribute it and/or
005     * modify it under the terms of the GNU General Public License
006     * as published by the Free Software Foundation; either version 2
007     * of the License, or (at your option) any later version.
008     *      
009     * This program is distributed in the hope that it will be useful,
010     * but WITHOUT ANY WARRANTY; without even the implied warranty of
011     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012     * GNU General Public License for more details.
013     *          
014     * You should have received a copy of the GNU General Public License
015     * along with this program; if not, write to the Free Software
016     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
017     *  
018     * $Header: /cvsroot/pavlov/net/sourceforge/pavlov/randommedia/ImageContentLoader.java,v 1.3 2004/05/10 14:58:54 tj_willis Exp $
019     */ 
020    package net.sourceforge.pavlov.randommedia;
021    
022    import java.net.URL;
023    import javax.swing.ImageIcon;
024    import java.net.URLConnection;
025    import java.io.InputStream;
026    import net.sourceforge.pavlov.zipUtilities.*;
027    import org.apache.log4j.*;
028    
029    /**
030     * Describe class <code>ImageContentLoader</code> here.
031     *
032     * @author <a href="mailto:tj_willis@users.sourceforge.net">T.J. Willis</a>
033     * @version 1.0
034     */
035    public class ImageContentLoader 
036        implements ContentLoader 
037    {
038        private Category cat
039            = Category.getInstance(ImageContentLoader.class.getName());
040        /**
041         * Describe <code>loadContent</code> method here.
042         *
043         * @param url an <code>URL</code> value
044         * @return an <code>Object</code> value
045         */
046        public Object loadContent(URL url)
047        {
048            if(url==null)return null;
049            ImageIcon tmp = null;
050            try { 
051                //      String bar = url.toString();
052                //      if(bar==null) return (CacheObject)nullNotifier("url.toString() == null");
053                //      if(bar.startsWith("jar:file:"))
054                if(JarUtilities.isInAJar(url))
055                {
056                    URLConnection la = url.openConnection();
057                    InputStream is = la.getInputStream();
058                    
059                    int entrySize = la.getContentLength(); //(int)entry.getSize();
060                    byte byteData[] = new byte[entrySize];
061                    int readLen = 0;
062                    int totalLen = 0;
063                    while( readLen!= -1)
064                    {
065                        readLen = is.read(byteData,totalLen,entrySize - totalLen);
066                        if(readLen != -1)
067                            totalLen += readLen;
068                        if(readLen == 0)
069                            break;
070                    }
071                    tmp = new ImageIcon(byteData);
072                }
073                else
074                {
075                    tmp = new ImageIcon(url);
076                }
077            } catch (Exception ex) {
078                cat.warn("Exception loading content",ex);
079            }catch (Error er) {
080                cat.error("Error loading content",er);
081            }
082            CacheObject foo = new CacheObject(url,tmp);
083            return foo;
084        }
085    
086    
087    //     /**
088    //      * I don't see any reason this shouldn't work, but it
089    //      * coredumps in windoze...
090    //      */
091    //     private Object OldLoadContent(URL url)
092    //     {
093    //      ImageIcon tmp = null;
094    // //   System.out.println("ImageContentLoader: " + url);
095    
096    //      // getting odd "Corrupt JPEG data: xxx extraneous bytes"
097    //      // and "invalid marker..." errors...  
098    //      // and sun.awt.image.ImageFormatException
099    //      try {
100    //          tmp = new ImageIcon(url);
101    //      } catch (Exception ex) {
102    //          
103    //          return null;
104    //      }
105    
106    //      return new CacheObject(url,tmp);
107    //     }
108    
109    }