Source code: org/esau/ptarmigan/impl/filter/M3UFilter.java
1 /* $Header: /cvsroot/ptarmigan/ptarmigan/src/java/org/esau/ptarmigan/impl/filter/M3UFilter.java,v 1.4 2002/10/04 11:49:06 reedesau Exp $ */
2
3 package org.esau.ptarmigan.impl.filter;
4
5 import java.io.BufferedReader;
6 import java.io.IOException;
7 import java.text.ParseException;
8
9 import org.xml.sax.SAXException;
10
11 import org.apache.commons.logging.Log;
12 import org.apache.commons.logging.LogFactory;
13
14 /**
15 * M3UFilter -- transforms M3U playlist to ptarmigan playlist
16 * <p>
17 * TODO: recursively scan all playlists found.
18 *
19 * @author Reed Esau
20 * @version $Revision: 1.4 $ $Date: 2002/10/04 11:49:06 $
21 */
22 public final class M3UFilter extends SimplePlaylistFilter {
23
24 public M3UFilter() throws SAXException {
25 }
26
27 static final String M3U_MARKER = "#EXTM3U";
28
29 /** does the specified stream contain a marker for the tag? */
30 public boolean isMatch(String str) throws IOException {
31 log.debug("sniffing for " + M3U_MARKER);
32 return str.startsWith(M3U_MARKER);
33 }
34
35 /**
36 * Parse a document from a buffered source.
37 */
38 public void doParsePlaylist(BufferedReader reader)
39 throws SAXException, IOException, ParseException {
40
41 m_media_properties.setMimeType("audio/x-mpegurl");
42
43 log.debug("doParsePlaylist");
44
45 // first parse all the entries to an internal list
46
47 //TODO: do something with duration_total
48
49 final String ENTRY_PREFIX = "#EXTINF:";
50 String line;
51 int count = 0;
52 int duration_total = 0;
53
54 startPlaylist();
55
56 while ((line = reader.readLine()) != null) {
57 String trimmed = line.trim();
58
59 if (trimmed.length() == 0 || trimmed.startsWith(M3U_MARKER))
60 continue;
61
62 try {
63 if (trimmed.startsWith(ENTRY_PREFIX)) {
64 int comma_pos = trimmed.indexOf(',');
65 if (comma_pos > 0) {
66 String s_duration = trimmed.substring(ENTRY_PREFIX.length(), comma_pos);
67 int duration = Integer.parseInt(s_duration) * 1000;
68 setEntryDuration( duration ); // should be millisecs
69 setEntryTitle( line.substring(comma_pos+1) );
70 duration_total += duration;
71 }
72 }
73 else {
74 setEntryPath( trimmed );
75 writeEntry();
76 count++;
77 }
78 }
79 catch (NumberFormatException e) {
80 log.warn("unable to parse duration", e);
81 }
82 }
83
84 endPlaylist();
85 }
86
87 /**
88 * logging object
89 */
90 static Log log = LogFactory.getLog(M3UFilter.class);
91 }
92
93 /*
94 PTARMIGAN MODIFIED BSD LICENSE
95
96 Copyright (c) 2002, Reed Esau (reed.esau@pobox.com) All rights reserved.
97
98 Redistribution and use in source and binary forms, with or without
99 modification, are permitted provided that the following conditions are
100 met:
101
102 Redistributions of source code must retain the above copyright notice,
103 this list of conditions and the following disclaimer.
104
105 Redistributions in binary form must reproduce the above copyright notice,
106 this list of conditions and the following disclaimer in the documentation
107 and/or other materials provided with the distribution.
108
109 Neither the name of the Ptarmigan Project
110 (http://ptarmigan.sourceforge.net) nor the names of its contributors may
111 be used to endorse or promote products derived from this software without
112 specific prior written permission.
113
114 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
115 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
116 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
117 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
118 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
119 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
120 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
121 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
122 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
123 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
124 POSSIBILITY OF SUCH DAMAGE.
125 */