1 package tim.prune.load;
4 import java.io.IOException;
5 import java.io.InputStream;
7 import java.util.zip.ZipEntry;
8 import java.util.zip.ZipFile;
10 import tim.prune.data.AudioClip;
11 import tim.prune.data.MediaObject;
12 import tim.prune.data.Photo;
15 * Class to provide helper functions for loading media
17 public abstract class MediaHelper
20 private static GenericFileFilter _jpegFilter = null, _audioFilter = null;
24 * Construct a MediaObject for the given path
25 * @param inZipFile path to archive file (if any)
26 * @param inPath path to media file
27 * @param inSourceFile file from which data was loaded
28 * @return either Photo or AudioClip object as appropriate, or null
30 public static MediaObject createMediaObject(File inZipFile, String inPath, File inSourceFile)
32 if (inPath == null || inPath.length() < 5) return null;
33 InputStream is = null;
39 // Check if path is a URL, in which case get an input stream from it
40 if (inPath.substring(0, 5).toLowerCase().equals("http:"))
43 is = new URL(inPath).openStream();
44 data = ByteScooper.scoop(is);
47 catch (IOException ioe) {
48 System.err.println("Got ioe from url: " + ioe.getMessage());
51 // Now see if file is in the zip file
52 if (is == null && inZipFile != null && inZipFile.exists() && inZipFile.canRead())
56 zf = new ZipFile(inZipFile);
57 ZipEntry entry = zf.getEntry(inPath);
58 if (entry != null && entry.getSize() > 0)
60 data = ByteScooper.scoop(zf.getInputStream(entry));
61 // System.out.println("Size of data " + (data.length == entry.getSize()?"matches":"DOESN'T match"));
64 catch (IOException ioe) {
65 System.err.println("Got ioe from zip file: " + ioe.getMessage());
68 // Clean up input streams
71 } catch (IOException ioe) {}
74 } catch (IOException ioe) {}
78 // Create Photo or AudioClip using this entry
79 String filename = new File(inPath).getName();
81 if (_jpegFilter.acceptFilename(inPath)) {
82 return new Photo(data, filename, url);
84 else if (_audioFilter.acceptFilename(inPath)) {
85 return new AudioClip(data, filename, url);
90 // If we haven't got a result by now, try to load plain file
91 File file = new File(inPath);
92 if (inSourceFile != null && !file.isAbsolute()) {
93 file = new File(inSourceFile.getParent(), inPath);
95 // awkward construction because new File(startPath, absolutePath) doesn't work
96 return createMediaObject(file);
100 * Construct a MediaObject for the given file
101 * @param inFile file to load
102 * @return either Photo or AudioClip object as appropriate, or null
104 private static MediaObject createMediaObject(File inFile)
106 if (inFile == null) {return null;}
107 if (!inFile.exists() || !inFile.canRead() || !inFile.isFile()) {return null;}
109 // Check if filename looks like a jpeg
110 if (_jpegFilter.acceptFilename(inFile.getName())) {
111 return JpegLoader.createPhoto(inFile);
113 // Check if filename looks like an audio clip
114 if (_audioFilter.acceptFilename(inFile.getName())) {
115 return new AudioClip(inFile);
117 // Neither photo nor audio
122 * Initialise filters if necessary
124 private static void initFilters()
126 if (_jpegFilter == null) {
127 _jpegFilter = new JpegFileFilter();
128 _audioFilter = new AudioFileFilter();