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/RandomMediaDirectory.java,v 1.6 2004/05/21 07:28:54 tj_willis Exp $ 019 */ 020 package net.sourceforge.pavlov.randommedia; 021 022 import java.io.File; 023 import java.net.URL; 024 import java.util.Enumeration; 025 import java.util.jar.*; 026 import net.sourceforge.pavlov.zipUtilities.*; 027 import org.apache.log4j.*; 028 029 /** 030 * A filesystem directory full of media items. 031 * @author <a href="mailto:tj_willis@users.sourceforge.net">T.J. Willis</a> 032 * @version 1.0 033 */ 034 public class RandomMediaDirectory extends AbstractMediaDirectory 035 { 036 private Category cat 037 = Category.getInstance(RandomMediaDirectory.class.getName()); 038 /** 039 * Creates and initializes a RandomMediaDirectory object with the 040 * given file and fileFilter. 041 * @param f a <code>File</code> value 042 * @param filt a <code>ZipCapableFileFilter</code> value 043 */ 044 public RandomMediaDirectory(File f, ZipCapableFileFilter filt) 045 { 046 super(f,filt); 047 //System.out.println("const fname = " + f.getAbsolutePath()); 048 } 049 050 /** 051 * Creates and initializes a RandomMediaDirectory object with the 052 * given file and fileFilter. 053 * @param f a <code>File</code> value 054 * @param filt a <code>ZipCapableFileFilter</code> value 055 * @param shouldRefresh a <code>boolean</code> value 056 */ 057 public RandomMediaDirectory(File f, ZipCapableFileFilter filt,boolean shouldRefresh) 058 { 059 super(f,filt,shouldRefresh); 060 } 061 062 063 /** 064 * Handles the case where the base directory is a, or is in a Jar File. 065 * 066 */ 067 // FIXME: this is a horrible, unstable hack 068 private void handleJar() 069 { 070 String baseURLstring = 071 JarUtilities.getJarFileName(getDirectoryString()); 072 073 JarFile tmp = null; 074 try { 075 tmp = new JarFile(baseURLstring); 076 } catch (Exception ex) { 077 cat.warn("Can't make jarfile: " + baseURLstring,ex); 078 } 079 080 Enumeration<JarEntry> entries = tmp.entries(); 081 //int mySize = 0; 082 083 while(entries.hasMoreElements()) 084 { 085 JarEntry xx = entries.nextElement(); 086 if(!acceptEntry(xx)){ 087 continue; 088 } 089 try { 090 091 //String tmpNm = baseURLstring+xx.getName(); 092 URL u = JarUtilities.makeJarURL(baseURLstring, 093 xx.toString()); 094 addURL(u); 095 } catch (Exception e) { 096 cat.warn("Can't make jar url: " + xx,e); 097 } 098 } 099 } 100 101 102 103 104 /** 105 * Describe <code>refresh</code> method here. 106 * 107 */ 108 public void refresh() 109 { 110 //assert file.isDirectory(): "Not a directory"; 111 if(JarUtilities.isInAJar(getDirectory())){ 112 handleJar(); 113 return; 114 } 115 116 File fileArr[] = listFiles(); 117 118 // FIXED: frequent NPE 119 clearURLs(); 120 121 for(File f : fileArr){ 122 if(f==null) 123 continue; 124 else if(f.isDirectory() || JarUtilities.isAJar(f)) 125 addSubdirectory(f); 126 else { 127 if(acceptFile(f)) { 128 try { 129 URL u = f.toURL(); //entryURL(f.getName()); 130 addURL(u); 131 } catch (Exception e){ 132 cat.warn("Can't make url: " + f,e); 133 } 134 } 135 } 136 } 137 } 138 139 } 140