001 package helliker.id3;
002
003 import java.io.IOException;
004 import java.io.FileReader;
005 import java.io.BufferedReader;
006 import java.io.FileOutputStream;
007 import java.io.PrintWriter;
008 import java.io.File;
009 import java.io.FileNotFoundException;
010 import java.util.LinkedList;
011 import java.util.Comparator;
012 import java.util.Iterator;
013 import java.util.Collections;
014 import java.util.Arrays;
015
016 /*
017 Copyright (C) 2001,2002 Jonathan Hilliker
018 This library is free software; you can redistribute it and/or
019 modify it under the terms of the GNU Lesser General Public
020 License as published by the Free Software Foundation; either
021 version 2.1 of the License, or (at your option) any later version.
022 This library is distributed in the hope that it will be useful,
023 but WITHOUT ANY WARRANTY; without even the implied warranty of
024 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
025 Lesser General Public License for more details.
026 You should have received a copy of the GNU Lesser General Public
027 License along with this library; if not, write to the Free Software
028 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
029 */
030 /**
031 * Description: This class is treated as a winamp styled playlist. It is
032 * essentially a collection of MP3Files that can be initalised from a file.
033 * Playlists can also be written based on it's data.
034 */
035
036 public class Playlist extends LinkedList {
037
038 // Types of playlists
039 /**
040 * The Winamp M3u Format
041 */
042 public static int WINAMP_FORMAT = 0;
043 /**
044 * The MusicMatch format
045 */
046 public static int MUSICMATCH_FORMAT = 1;
047
048 /**
049 * the Playlist Extension
050 */
051 private static final String PLAYLIST_EXT = ".m3u";
052
053
054 /**
055 * Sorts this playlist by using the default path comparisons. Uses
056 * Collections.sort().
057 */
058 public void sort() {
059 Collections.sort(this);
060 }
061
062
063 /**
064 * Sorts this playlist by using the specified comparator. Uses
065 * Collections.sort().
066 *
067 *@param cmp the comparator to use
068 */
069 public void sort(Comparator cmp) {
070 Collections.sort(this, cmp);
071 }
072
073
074 /**
075 * Reads in mp3s from a directory and adds them to this playlist. By default,
076 * subdirectories will be recursed and the files will be sorted.
077 *
078 *@param dir the directory to look for mp3s in
079 *@exception IOException if the files specified is not a directory
080 */
081 public void addDirectory(File dir) throws IOException {
082 addDirectory(dir, true, true);
083 }
084
085
086 /**
087 * Reads in mp3s from a directory and adds them to the this playlist. The
088 * recurse parameter should be set if you wish to grab mp3s from
089 * subdirectories as well. If the sort parameter is true the files in each
090 * directory will be sorted before added.
091 *
092 *@param dir the directory to look for mp3s in
093 *@param recurse whether or not to recurse subdirectories
094 *@param sort whether or not to sort each directory
095 *@exception IOException if the file specified is not a directory
096 */
097 public void addDirectory(File dir, boolean recurse, boolean sort)
098 throws IOException {
099
100 if (dir.isDirectory()) {
101 File[] files = dir.listFiles(new MP3FileFilter(true));
102
103 if (sort) {
104 Arrays.sort(files);
105 }
106
107 MP3File mp3 = null;
108
109 for (int i = 0; i < files.length; i++) {
110 if (files[i].isFile()) {
111 try {
112 mp3 = new MP3File(files[i]);
113 this.add(mp3);
114 } catch (Exception e) {
115 // Do nothing. Bad mp3, don't add.
116 }
117 } else if (recurse) {
118 addDirectory(files[i], recurse, sort);
119 }
120 }
121 } else {
122 throw new IOException("Error loading playlist from a directory: "
123 + dir.getAbsolutePath() + " is not a " + "directory");
124 }
125 }
126
127
128 /**
129 * Load a playlist from a file. This works with Winamp and MusicMatch
130 * playlists. The format parameter should be either WINAMP_FORMAT or
131 * MUSICMATCH_FORMAT.
132 *
133 *@param m3uFile the playlist file
134 *@param format the type of playlist to load
135 *@exception PlaylistException if the file is corrupt
136 *@exception FileNotFoundException if an error occurs
137 *@exception IOException if an error occurs
138 */
139 public void loadFromFile(File m3uFile, int format)
140 throws PlaylistException, IOException {
141
142 FileReader in = new FileReader(m3uFile);
143 BufferedReader reader = new BufferedReader(in);
144
145 if (format == WINAMP_FORMAT) {
146 loadWinampFile(reader, m3uFile);
147 } else if (format == MUSICMATCH_FORMAT) {
148 loadMusicMatchFile(reader);
149 }
150
151 reader.close();
152 in.close();
153 }
154
155
156 /**
157 * Loads the contents of a winamp playlist.
158 *
159 *@param reader the stream to read from
160 *@param m3uFile the Playlist File
161 *@exception PlaylistException if an error occurs
162 *@exception IOException if an error occurs
163 */
164 private void loadWinampFile(BufferedReader reader, File m3uFile)
165 throws PlaylistException, IOException {
166
167 String str = reader.readLine();
168 MP3File mp3 = null;
169
170 if (str.equals("#EXTM3U")) {
171 while ((str = reader.readLine()) != null) {
172 if (str.indexOf("EXTINF:") != -1) {
173 str = reader.readLine();
174
175 if (str.length() != 0) {
176 // Check to see if the path in the file is an absolute
177 // path. In Unix an absolute path begins with forward
178 // slash, in Windows it will have a colon.
179 if (str.charAt(0) != '/' || str.indexOf(":") != -1) {
180 str = m3uFile.getAbsoluteFile().getParent() + File.separator + str;
181 }
182 }
183
184 try {
185 mp3 = new MP3File(str);
186 this.add(mp3);
187 } catch (Exception e) {
188 // Do nothing, just don't add it
189 }
190 } else {
191 throw new PlaylistException();
192 }
193 }
194 } else {
195 throw new PlaylistException();
196 }
197 }
198
199
200 /**
201 * Loads the contents of a MusicMatch playlist.
202 *
203 *@param reader the stream to read from
204 *@exception PlaylistException if an error occurs
205 *@exception IOException if an error occurs
206 */
207 private void loadMusicMatchFile(BufferedReader reader)
208 throws PlaylistException, IOException {
209
210 String str;
211
212 while ((str = reader.readLine()) != null) {
213 try {
214 this.add(new MP3File(str));
215 } catch (Exception e) {
216 // Ignore this file
217 }
218 }
219 }
220
221
222 /**
223 * Writes this playlist in the format specified. This can be either
224 * WINAMP_FORMAT or MUSICMATCH_FORMAT. If the destination is a directory, then
225 * the file will be created in that directory with the same name as the
226 * directory with a ".m3u" extension added. If the destination is a file then
227 * the playlist will be saved to that file. If the format argument is invalid
228 * the file will not be written.
229 *
230 *@param dest where to save the playlist
231 *@param format the format to write the file in
232 *@exception IOException if an error occurs
233 */
234 public void writeToFile(File dest, int format) throws IOException {
235 File m3u = dest;
236
237 if (dest.isDirectory()) {
238 m3u = new File(dest, dest.getName() + PLAYLIST_EXT);
239 }
240
241 FileOutputStream out = new FileOutputStream(m3u);
242 PrintWriter printer = new PrintWriter(out);
243
244 if (format == WINAMP_FORMAT) {
245 writeWinampFile(printer);
246 } else if (format == MUSICMATCH_FORMAT) {
247 writeMusicMatchFile(printer);
248 }
249
250 printer.close();
251 out.close();
252 }
253
254
255 /**
256 * Write the contents of this playlist in Winamp format.
257 *
258 *@param printer the stream to write to
259 *@exception IOException if an error occurs
260 */
261 private void writeWinampFile(PrintWriter printer) throws IOException {
262 Iterator it = this.iterator();
263 MP3File mp3;
264 String artist = new String();
265 String title = new String();
266 String display = new String();
267
268 printer.println("#EXTM3U");
269
270 while (it.hasNext()) {
271 mp3 = (MP3File) it.next();
272 printer.print("#EXTINF:");
273 printer.print(mp3.getPlayingTime());
274 printer.print(",");
275
276 try {
277 artist = mp3.getArtist();
278 } catch (ID3v2FormatException e) {
279 ;
280 }
281
282 try {
283 title = mp3.getTitle();
284 } catch (ID3v2FormatException e) {
285 ;
286 }
287
288 if ((artist.length() != 0) && (title.length() != 0)) {
289 display = artist + " - " + title;
290 } else if (title.length() != 0) {
291 display = title;
292 } else {
293 display = mp3.getFileName();
294 display = display.substring(0, display.length() - 4);
295 }
296
297 printer.println(display);
298 printer.println(mp3.getPath());
299 }
300 }
301
302
303 /**
304 * Writes the contents of this playlist in MusicMatch format.
305 *
306 *@param printer the stream to write to
307 *@exception IOException if an error occurs
308 */
309 private void writeMusicMatchFile(PrintWriter printer) throws IOException {
310 // Written by David Barron, thanx
311 Iterator it = this.iterator();
312 MP3File mp3;
313
314 while (it.hasNext()) {
315 mp3 = (MP3File) it.next();
316 printer.println(mp3.getPath());
317 }
318 }
319
320
321 /**
322 * Calls toString on every object contained within (very long).
323 *
324 *@return a string representation of this object
325 */
326 public String toString() {
327 StringBuffer buf = new StringBuffer();
328 Iterator it = this.iterator();
329
330 while (it.hasNext()) {
331 buf.append(it.next());
332 }
333
334 return buf.toString();
335 }
336
337 }
338