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/RandomMediaJarFile.java,v 1.5 2004/05/10 14:58:54 tj_willis Exp $
019     */ 
020    package net.sourceforge.pavlov.randommedia;
021    
022    import java.util.Vector;
023    import java.util.Enumeration;
024    import java.util.jar.*;
025    import java.io.File;
026    import java.net.*;
027    import net.sourceforge.pavlov.zipUtilities.*;
028    import org.apache.log4j.*;
029    
030    /**
031     * FIXME: Describe class.
032     *
033     * @author <a href="mailto:tj_willis@users.sourceforge.net">T.J. Willis</a>
034     * @version 1.0
035     */
036    public class RandomMediaJarFile
037        extends RandomMediaDirectory
038    {
039        //private String baseURLstring;
040        private Category cat
041            = Category.getInstance(RandomMediaJarFile.class.getName());
042        /**
043         * Creates a new <code>RandomMediaJarFile</code> instance.
044         *
045         * @param directory a <code>File</code> value
046         * @param filt a <code>ZipCapableFileFilter</code> value
047         */
048        public RandomMediaJarFile(File directory, ZipCapableFileFilter filt)
049        {
050            super(directory,filt,false);
051            //baseURLstring = "jar:file:" + getAbsoluteDirectoryPath() +"!/";
052            //cat.debug("base url is " + baseURLstring);
053            this.refresh();
054        }
055    
056        private URL entryURL(String entName)
057        {
058            try {
059                //String tmp = baseURLstring+entName;
060                //cat.debug("entry url is : " + tmp);
061                //return new URL(tmp);
062                return JarUtilities.makeJarURL(getAbsoluteDirectoryPath(),entName);
063            } catch (Exception ex){
064                cat.warn("Error making jar URL",ex);
065            }
066            return null;
067        }
068    
069        /**
070         * Describe <code>refresh</code> method here.
071         *
072         */
073        public void refresh()
074        {
075            JarFile myDir = null;
076            try {
077                myDir = new JarFile(getDirectory());
078            } catch (Exception ex) {
079                cat.warn("Error making jar file",ex);
080                return;
081            }
082            assert myDir!=null: "JarFile is null";
083    
084            clearURLs(); //urls = new Vector(100,50);
085            Enumeration entries = myDir.entries();
086            int mySize = 0;
087            while(entries.hasMoreElements()){
088    
089                JarEntry tmp = (JarEntry)entries.nextElement();
090                if(tmp==null || tmp.isDirectory())
091                    continue;
092                else {
093                    if(acceptEntry(tmp)) //else 
094                    {
095                        URL u = entryURL(tmp.getName());
096                        addURL(u);
097                    }
098                }
099            }
100        }
101    
102    }
103