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/SequencedMediaDirectory.java,v 1.5 2004/05/13 17:25:00 tj_willis Exp $
019     */ 
020    package net.sourceforge.pavlov.randommedia;
021    
022    import java.util.Enumeration;
023    import java.util.Vector;
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    /**
032     * A filesystem directory full of media items which can be accessed in sequence.
033     * @author <a href="mailto:tj_willis@users.sourceforge.net">T.J. Willis</a>
034     * @version 1.0
035     */
036    public class SequencedMediaDirectory extends AbstractMediaDirectory
037    {
038        private int cursor;
039        private boolean playedOnce;
040        private static Category cat 
041            = Category.getInstance(SequencedMediaDirectory.class.getName());    
042    
043        /**
044         * Creates and initializes a SequencedMediaDirectory object with the given
045         * file and fileFilter.
046         * @param f a <code>File</code> value
047         * @param filt a <code>ZipCapableFileFilter</code> value
048         */
049        public SequencedMediaDirectory(File f, ZipCapableFileFilter filt)
050        {
051            super(f,filt);
052            cat.setLevel(Level.WARN);
053            cat.debug("dir is " + f);
054            cursor = 0;
055        }
056    
057        /**
058         * Describe <code>playedOnce</code> method here.
059         *
060         * @return a <code>boolean</code> value
061         */
062        public boolean playedOnce()
063        {
064            return playedOnce;
065        }
066    
067        /**
068         * Describe <code>getCurrentURL</code> method here.
069         *
070         * @return an <code>URL</code> value
071         */
072        public URL getCurrentURL()
073        {
074            if(cursor<0) cursor=0;
075            if(cursor>=getSize()-1)
076                {
077                    playedOnce = true;
078                    cursor = 0; // wrapping
079                }
080            cursor++;
081            return getURLIndex(cursor);
082        }
083    
084        /**
085         * Describe <code>getCursorNumber</code> method here.
086         *
087         * @return an <code>int</code> value
088         */
089        public int getCursorNumber()
090        {
091            return cursor;
092        }
093    
094        /**
095         * Describe <code>refresh</code> method here.
096         *
097         */
098        public void refresh()
099        {
100            if(getDirectory()==null) return;
101            //         // if filename has a "!" it's inside a JAR
102            //         int x = file.toString().indexOf('!');
103            //         if(x>0) 
104            //         {
105            //           refreshJar();
106            //           return;
107            //         }
108            if(JarUtilities.isInAJar(getDirectory())){
109                refreshJar();
110                return;
111            }
112            //assert file.isDirectory(): "Not a directory";
113            FileNameComparator comp = new FileNameComparator();
114    
115            File fileArr[] = listFiles(); //file.listFiles();
116            //        if( isAJar(file) )
117            //{
118            //     java.util.JarFile foo = new java.util.jar.JarFile(file);
119            //     java.util.Enumeration en = foo.entries();
120            //     java.util.ArrayList ents = new java.util.ArrayList();
121            //     while (en.hasMoreElements()) {
122            //       ents.add(e.nextElement());
123            //     } 
124            //     fileArr = new File[1];
125            //     fileArr = (File []) ents.toArray(fileArr);
126            //}
127            if(fileArr==null) return;
128            //int mySize = java.lang.reflect.Array.getLength(fileArr);
129            //if(mySize==0) return;
130            
131            //Object fileArr[] = file.listFiles();
132            java.util.Arrays.sort(fileArr,comp);
133    
134    
135            clearURLs();//urls = new Vector(mySize,100);
136            //FIXED: use an iterator
137            //for(int i=0;i<mySize;i++)
138                //    {
139            for(File f : fileArr) {
140    
141                if(f==null){
142                        continue;
143                }
144                if(f.isDirectory()) continue;
145                if(acceptFile(f)){
146                    //String lala = entryURL(tmp.getName());
147                    //cat.debug("entry url is : " + lala);
148                    try {
149                        URL u = f.toURL(); //entryURL(f.getName());
150                        addURL(u);
151                    } catch (Exception e){
152                        cat.warn("Can't add url",e);// don't add it
153                    }
154                }
155                
156            }
157        }
158        
159        
160        /**
161         * Describe <code>refreshJar</code> method here.
162         *
163         */
164        private void refreshJar()
165        {
166            //cat.debug("Detected jarfile");
167            //File file = new File("/win/pup/pluglets/feedback/SouthPark.jar");
168            String fname, subdirstring;
169            try {
170                //FIXME: string processing particularly bad
171                String xname = getDirectoryString(); //file.toString();
172                int i = xname.indexOf('!');
173                if(i<0) return;
174                int j = xname.lastIndexOf(':')+1;
175                if(j<0)j=0;
176                fname = xname.substring(j,i);
177                subdirstring = xname.substring(i+2); 
178                //file = new File(fname); 
179                setDirectory(new File(fname)); 
180            } catch (Exception e) {
181                cat.warn("Error parsing jar entry: " + getDirectoryString(),e);
182                // can't parse the file correctly
183                return;
184            }
185            //String subdirstring = "resources/images/wrongs";
186    
187            //cat.debug("Parsed filename is <" + fname +">");
188            //cat.debug("Parsed subdirname is <" + subdirstring +">");
189    
190            //String baseURLstring = "jar:file:" + getDirectoryString() + "!/" + subdirstring;
191            JarFile jarfile = null;
192            try {
193                jarfile = new JarFile(getDirectory());
194            } catch (Exception ex) {
195                cat.warn("Can't create jarfile reference: " + getDirectory(),ex);
196            }
197    
198            clearURLs(); //urls = new Vector(100,50);
199            Enumeration<JarEntry> entries = jarfile.entries();
200            int mySize = 0;
201            while(entries.hasMoreElements())
202                {
203                    JarEntry tmp = (JarEntry)entries.nextElement();
204                    if(tmp==null)
205                        {
206                            continue;
207                        }
208                    //cat.debug("Scanning entry: " + tmp.getName() );
209                    if(    tmp.getName().startsWith(subdirstring) 
210                           && acceptEntry(tmp) )
211                        {
212                            try {
213                                URL u;
214                                u = JarUtilities.makeJarURL(getDirectoryString(), 
215                                                            tmp.getName());
216                                //new URL("jar:file:" + getDirectoryString() + "!/" + tmp.getName() );
217                                //cat.debug("Accepting entry: " + u);
218                                addURL(u);
219                            } catch (Exception e) {
220                                cat.warn("Can't make jar url " + tmp.getName(),e);
221                            }
222    
223                        }
224                }
225        }
226    }
227