Source code: org/esau/ptarmigan/impl/filter/PlaylistFilter.java
1 /* $Header: /cvsroot/ptarmigan/ptarmigan/src/java/org/esau/ptarmigan/impl/filter/PlaylistFilter.java,v 1.3 2002/09/19 03:47:35 reedesau Exp $ */
2
3 package org.esau.ptarmigan.impl.filter;
4
5 import java.io.File;
6 import java.io.IOException;
7 import java.net.MalformedURLException;
8 import java.net.URL;
9
10 import org.xml.sax.SAXException;
11
12 import org.apache.commons.logging.Log;
13 import org.apache.commons.logging.LogFactory;
14
15 import org.esau.ptarmigan.util.HelperURL;
16 import org.esau.ptarmigan.impl.CharacterStreamSource;
17
18 /**
19 * PlaylistFilter -- transforms XML playlist to ptarmigan playlist
20 * <p>
21 * All playlists are presently implemented as CharacterStreamSource
22 *
23 * @author Reed Esau
24 * @version $Revision: 1.3 $ $Date: 2002/09/19 03:47:35 $
25 */
26 public abstract class PlaylistFilter extends BaseFilter implements CharacterStreamSource {
27
28 public PlaylistFilter() throws SAXException {
29 resetData();
30 }
31
32 /** can this parser handle the text file? */
33 public abstract boolean isMatch(String sniff_chars) throws IOException;
34
35 public boolean getIncludeEntries() {
36 return m_include_entries;
37 }
38 public void setIncludeEntries(boolean include_entries) {
39 m_include_entries = include_entries;
40 }
41
42 /** provide a directory by which relative paths will be resolved */
43 public void setBaseDir(File base_dir) {
44 m_base_dir = base_dir;
45 }
46
47 //
48 // internal
49 //
50
51 String getNamespaceURI() {
52 return NS_URI;
53 }
54
55 String getNamespacePrefix() {
56 return NS_PREFIX;
57 }
58
59 /** begin */
60 void startPlaylist() throws SAXException {
61 super.startElement(NS_URI, TAG_PLAYLIST, NS_PREFIX+TAG_PLAYLIST, EMPTY_ATTRS);
62 }
63
64 /** end */
65 void endPlaylist() throws SAXException {
66
67 if (getIsPropsDone() == false) {
68 writeProperties();
69 }
70
71 super.endElement(NS_URI, TAG_PLAYLIST, NS_PREFIX+TAG_PLAYLIST);
72 }
73
74 /** output pl:properties */
75 void writeProperties() throws SAXException {
76
77 super.startElement(NS_URI, TAG_PROPS, NS_PREFIX+TAG_PROPS, EMPTY_ATTRS);
78
79 if (m_title != null)
80 write(TAG_PROPS_TITLE ,m_title );
81
82 if (m_author != null)
83 write(TAG_PROPS_AUTHOR ,m_author );
84
85 if (m_copyright != null)
86 write(TAG_PROPS_COPYRIGHT ,m_copyright);
87
88 if (m_summary != null)
89 write(TAG_PROPS_SUMMARY ,m_summary );
90
91 if (m_entry_count >= 0)
92 write(TAG_PROPS_ENTRY_COUNT, Integer.toString(m_entry_count) );
93
94 super.endElement(NS_URI, TAG_PROPS, NS_PREFIX+TAG_PROPS);
95
96 is_props_done = true;
97 }
98
99 /** output pl:entry and reset entry values */
100 void writeEntry() throws SAXException {
101
102 if (log.isDebugEnabled())
103 log.debug("writeEntry: url=" + m_entry_url + " m_include_entries=" + m_include_entries);
104
105 if (is_props_done == false)
106 writeProperties();
107
108 if (m_include_entries == false)
109 return;
110
111 if (m_entry_url == null)
112 return;
113
114 super.startElement(NS_URI, TAG_ENTRY, NS_PREFIX+TAG_ENTRY, EMPTY_ATTRS);
115
116 if (m_entry_url != null)
117 write(TAG_ENTRY_URL, m_entry_url.toExternalForm());
118
119 if (m_entry_title != null)
120 write(TAG_ENTRY_TITLE, m_entry_title);
121
122 if (m_entry_duration > 0) {
123 write(TAG_ENTRY_DURATION, Integer.toString(m_entry_duration));
124 }
125
126 m_entry_url = null; // reset!
127 m_entry_title = null;
128 m_entry_duration = 0;
129
130 super.endElement(NS_URI, TAG_ENTRY, NS_PREFIX+TAG_ENTRY);
131 }
132
133
134 //
135 // state members
136 //
137
138 boolean getInEntry() {
139 return in_entry;
140 }
141 void setInEntry(boolean state) {
142 in_entry = state;
143 }
144
145 boolean getIsPropsDone() {
146 return is_props_done;
147 }
148
149 void setTitle(String title) {
150 m_title = title;
151 }
152
153 void setAuthor(String author) {
154 m_author = author;
155 }
156
157 void setCopyright(String copyright) {
158 m_copyright = copyright;
159 }
160
161 void setSummary(String summary) {
162 m_summary = summary;
163 }
164
165 void setEntryCount(int entry_count) {
166 m_entry_count = entry_count;
167 }
168
169 /**
170 * attempt to build a URL from the path; if unsuccessful the entry isn't written
171 *
172 * It's okay to pass in a relative path, where the playlist's directory
173 * will be used.
174 */
175 void setEntryPath(String path) {
176 try {
177 log.debug("setEntryPath: building entry using basedir=" + m_base_dir);
178 m_entry_url = HelperURL.toURL(m_base_dir, path);
179 }
180 catch (MalformedURLException e) {
181 log.warn("url-problem trying to derive a URL from ["
182 + path + "]; entry skipped");
183 }
184 catch (IOException e) {
185 log.warn("io-problem trying to derive a URL from ["
186 + path + "]; entry skipped");
187 }
188 }
189 void setEntryPath(URL entry_path) {
190 m_entry_url = entry_path;
191 }
192
193 void setEntryTitle(String entry_title) {
194 m_entry_title = entry_title;
195 }
196
197 void setEntryDuration(int entry_duration) {
198 m_entry_duration = entry_duration;
199 }
200
201 //
202 // constants
203 //
204
205 static final String NS_URI = "http://esau.org/ns/ptarmigan/playlist";
206 static final String NS_PREFIX = "pl:";
207
208 /** from the playlist.xsd schema */
209 static final String TAG_PLAYLIST = "playlist";
210
211 static final String TAG_PROPS = "properties";
212 static final String TAG_PROPS_TITLE = "title";
213 static final String TAG_PROPS_AUTHOR = "author";
214 static final String TAG_PROPS_COPYRIGHT = "copyright";
215 static final String TAG_PROPS_SUMMARY = "summary";
216 static final String TAG_PROPS_ENTRY_COUNT = "entry-count";
217 static final String TAG_PROPS_DURATION_TOTAL = "duration-total";
218
219 static final String TAG_ENTRY = "entry";
220 static final String TAG_ENTRY_URL = "url";
221 static final String TAG_ENTRY_TITLE = "title";
222 static final String TAG_ENTRY_DURATION = "duration";
223
224 //
225 // resetter
226 //
227
228 public void resetData() {
229 m_base_dir = null;
230
231 m_include_entries = false;
232
233 in_entry = false;
234 is_props_done = false;
235
236 m_title = null;
237 m_author = null;
238 m_copyright = null;
239 m_summary = null;
240 m_entry_count = -1;
241
242 m_entry_url = null;
243 m_entry_title = null;
244 m_entry_duration = 0;
245 }
246
247 //
248 // data members
249 //
250
251 /** the directory from which relative pathnames will be normalized */
252 private File m_base_dir;
253
254 private boolean m_include_entries;
255
256 private boolean in_entry;
257
258 private boolean is_props_done;
259
260 private String m_title;
261 private String m_author;
262 private String m_copyright;
263 private String m_summary;
264 private int m_entry_count;
265
266 private URL m_entry_url;
267 private String m_entry_title;
268 private int m_entry_duration;
269
270 /**
271 * logging object
272 */
273 static Log log = LogFactory.getLog(PlaylistFilter.class);
274 }
275
276 /*
277 PTARMIGAN MODIFIED BSD LICENSE
278
279 Copyright (c) 2002, Reed Esau (reed.esau@pobox.com) All rights reserved.
280
281 Redistribution and use in source and binary forms, with or without
282 modification, are permitted provided that the following conditions are
283 met:
284
285 Redistributions of source code must retain the above copyright notice,
286 this list of conditions and the following disclaimer.
287
288 Redistributions in binary form must reproduce the above copyright notice,
289 this list of conditions and the following disclaimer in the documentation
290 and/or other materials provided with the distribution.
291
292 Neither the name of the Ptarmigan Project
293 (http://ptarmigan.sourceforge.net) nor the names of its contributors may
294 be used to endorse or promote products derived from this software without
295 specific prior written permission.
296
297 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
298 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
299 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
300 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
301 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
302 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
303 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
304 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
305 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
306 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
307 POSSIBILITY OF SUCH DAMAGE.
308 */