Source code: org/esau/ptarmigan/impl/filter/ASXFilter.java
1 /* $Header: /cvsroot/ptarmigan/ptarmigan/src/java/org/esau/ptarmigan/impl/filter/ASXFilter.java,v 1.2 2002/09/16 04:05:07 reedesau Exp $ */
2
3 package org.esau.ptarmigan.impl.filter;
4
5 import org.xml.sax.Attributes;
6 import org.xml.sax.SAXException;
7
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10
11 /**
12 * ASXFilter -- transforms ASX playlist to ptarmigan playlist
13 * <p>
14 * Example source:
15 * <pre>
16 * <ASX version="3.0">
17 * <ABSTRACT>This is a sample asx file used as playlist</ABSTRACT>
18 * <TITLE>Windows Media Playlist Demo</TITLE>
19 * <AUTHOR>Reed Esau</AUTHOR>
20 * <COPYRIGHT>2002 Reed Esau</COPYRIGHT>
21 * <ENTRY>
22 * <REF href="File1.asf" />
23 * <ABSTRACT>Link to a local file on the same folder</ABSTRACT>
24 * </ENTRY>
25 * <ENTRY>
26 * <REF href="mms://media.phm.lu/File2.asf" />
27 * <ABSTRACT>a file streamed from a media server on the network</ABSTRACT>
28 * </ENTRY>
29 * <ENTRY>
30 * <REF href="File3.mp3" />
31 * <ABSTRACT>ASX can support any file type supported by WMP</ABSTRACT>
32 * </ENTRY>
33 * <ENTRY>
34 * <REF href="File4.avi" />
35 * <ABSTRACT>you can even mix audio files and video files in the same ASX</ABSTRACT>
36 * </ENTRY>
37 * </ASX>
38 * </pre>
39 * The equivalent XSLT would appear thus:
40 * <pre>
41 * <xsl:template match="/ASX">
42 * <pl:playlist>
43 * <pl:properties>
44 * <pl:title><xsl:value-of select='TITLE'/></pl:title>
45 * <pl:author><xsl:value-of select='AUTHOR'/></pl:author>
46 * <pl:copyright><xsl:value-of select='COPYRIGHT'/></pl:copyright>
47 * <pl:summary><xsl:value-of select='ABSTRACT'/></pl:summary>
48 * <pl:entry-count><xsl:value-of select='count(ENTRY)'/></pl:entry-count>
49 * </pl:properties>
50 * <xsl:if test="$include_entries = 'true'">
51 * <xsl:apply-templates select="ENTRY"/>
52 * </xsl:if>
53 * </pl:playlist>
54 * </xsl:template>
55 *
56 * <xsl:template match="ENTRY">
57 * <pl:entry>
58 * <pl:path><xsl:value-of select='REF/@href'/></pl:path>
59 * <pl:title><xsl:value-of select='ABSTRACT'/></pl:title>
60 * </pl:entry>
61 * </xsl:template>
62 * </pre>
63 * TODO: recursively scan all playlists found.
64 *
65 * @author Reed Esau
66 * @version $Revision: 1.2 $ $Date: 2002/09/16 04:05:07 $
67 */
68 public final class ASXFilter extends XMLPlaylistFilter {
69
70 public ASXFilter() throws SAXException {
71 }
72
73 /** is the text file an XML file with that dodgy ASX marker?
74 *
75 * Note: ASX files may not start with "<?xml"
76 * */
77 public boolean isMatch(String sniff_chars) {
78 log.debug("sniffing for <ASX");
79 return sniff_chars.indexOf("<ASX") >= 0;
80 }
81
82 /** from the asx 'structure' */
83 static final String ASX = "ASX";
84 static final String ASX_TITLE = "TITLE";
85 static final String ASX_AUTHOR = "AUTHOR";
86 static final String ASX_COPYRIGHT = "COPYRIGHT";
87 static final String ASX_ABSTRACT = "ABSTRACT";
88 static final String ASX_ENTRY = "ENTRY";
89 static final String ASX_ENTRY_REF = "REF";
90 static final String ASX_ENTRY_REF_HREF = "href";
91 static final String ASX_ENTRY_ABSTRACT = "ABSTRACT"; // same as ASX_ABSTRACT
92
93 /**
94 * Filter a start element event.
95 */
96 public void startElement(String uri, String local_name, String qName, Attributes atts) throws SAXException {
97
98 //log.debug("startelement: local_name=" + local_name);
99
100 if (ASX_ENTRY.equals(local_name)) {
101 setInEntry(true);
102 }
103 else if (ASX_ENTRY_REF.equals(local_name) && atts.getLength() > 0) {
104 setEntryPath( atts.getValue(ASX_ENTRY_REF_HREF) );
105 }
106 else if (ASX.equals(local_name)) {
107
108 m_media_properties.setMimeType("video/x-ms-asf");
109
110 startPlaylist();
111 }
112 }
113
114 /**
115 * Filter an end element event.
116 */
117 public void endElement(String uri, String local_name, String qName) throws SAXException {
118
119 //log.debug("ENDELEMENT: local_name=" + local_name);
120
121 if (ASX_ABSTRACT.equals(local_name)) {
122 if (getInEntry())
123 setEntryTitle( getChars() );
124 else
125 setSummary( getChars() );
126 }
127 else if (ASX_ENTRY.equals(local_name)) {
128 if ( getIsPropsDone() == false)
129 writeProperties();
130
131 writeEntry();
132
133 setInEntry(false);
134 }
135 else if (ASX_TITLE.equals(local_name)) {
136 setTitle( getChars() );
137 }
138 else if (ASX_AUTHOR.equals(local_name)) {
139 setAuthor( getChars() );
140 }
141 else if (ASX_COPYRIGHT.equals(local_name)) {
142 setCopyright( getChars() );
143 }
144 else if (ASX.equals(local_name)) {
145 endPlaylist();
146 }
147 }
148
149
150 /**
151 * logging object
152 */
153 static Log log = LogFactory.getLog(ASXFilter.class);
154 }
155
156 /*
157 PTARMIGAN MODIFIED BSD LICENSE
158
159 Copyright (c) 2002, Reed Esau (reed.esau@pobox.com) All rights reserved.
160
161 Redistribution and use in source and binary forms, with or without
162 modification, are permitted provided that the following conditions are
163 met:
164
165 Redistributions of source code must retain the above copyright notice,
166 this list of conditions and the following disclaimer.
167
168 Redistributions in binary form must reproduce the above copyright notice,
169 this list of conditions and the following disclaimer in the documentation
170 and/or other materials provided with the distribution.
171
172 Neither the name of the Ptarmigan Project
173 (http://ptarmigan.sourceforge.net) nor the names of its contributors may
174 be used to endorse or promote products derived from this software without
175 specific prior written permission.
176
177 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
178 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
179 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
180 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
181 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
182 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
183 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
184 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
185 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
186 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
187 POSSIBILITY OF SUCH DAMAGE.
188 */