Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

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   *   &lt;ASX version="3.0"&gt;
17   *       &lt;ABSTRACT&gt;This is a sample asx file used as playlist&lt;/ABSTRACT&gt;
18   *       &lt;TITLE&gt;Windows Media Playlist Demo&lt;/TITLE&gt;
19   *       &lt;AUTHOR&gt;Reed Esau&lt;/AUTHOR&gt;
20   *       &lt;COPYRIGHT&gt;2002 Reed Esau&lt;/COPYRIGHT&gt;
21   *       &lt;ENTRY&gt;
22   *               &lt;REF href="File1.asf" /&gt;
23   *               &lt;ABSTRACT&gt;Link to a local file on the same folder&lt;/ABSTRACT&gt;
24   *       &lt;/ENTRY&gt;
25   *       &lt;ENTRY&gt;
26   *               &lt;REF href="mms://media.phm.lu/File2.asf" /&gt;
27   *               &lt;ABSTRACT&gt;a file streamed from a media server on the network&lt;/ABSTRACT&gt;
28   *       &lt;/ENTRY&gt;
29   *       &lt;ENTRY&gt;
30   *               &lt;REF href="File3.mp3" /&gt;
31   *               &lt;ABSTRACT&gt;ASX can support any file type supported by WMP&lt;/ABSTRACT&gt;
32   *       &lt;/ENTRY&gt;
33   *       &lt;ENTRY&gt;
34   *               &lt;REF href="File4.avi" /&gt;
35   *               &lt;ABSTRACT&gt;you can even mix audio files and video files in the same ASX&lt;/ABSTRACT&gt;
36   *       &lt;/ENTRY&gt;
37   *   &lt;/ASX&gt;
38   * </pre>
39   * The equivalent XSLT would appear thus:
40   * <pre>
41   *   &lt;xsl:template match="/ASX"&gt;
42   *       &lt;pl:playlist&gt;
43   *           &lt;pl:properties&gt;
44   *               &lt;pl:title&gt;&lt;xsl:value-of select='TITLE'/&gt;&lt;/pl:title&gt;
45   *               &lt;pl:author&gt;&lt;xsl:value-of select='AUTHOR'/&gt;&lt;/pl:author&gt;
46   *               &lt;pl:copyright&gt;&lt;xsl:value-of select='COPYRIGHT'/&gt;&lt;/pl:copyright&gt;
47   *               &lt;pl:summary&gt;&lt;xsl:value-of select='ABSTRACT'/&gt;&lt;/pl:summary&gt;
48   *               &lt;pl:entry-count&gt;&lt;xsl:value-of select='count(ENTRY)'/&gt;&lt;/pl:entry-count&gt;
49   *           &lt;/pl:properties&gt;
50   *           &lt;xsl:if test="$include_entries = 'true'"&gt;
51   *               &lt;xsl:apply-templates select="ENTRY"/&gt;
52   *           &lt;/xsl:if&gt;
53   *       &lt;/pl:playlist&gt;
54   *   &lt;/xsl:template&gt;
55   *
56   *   &lt;xsl:template match="ENTRY"&gt;
57   *       &lt;pl:entry&gt;
58   *           &lt;pl:path&gt;&lt;xsl:value-of select='REF/@href'/&gt;&lt;/pl:path&gt;
59   *           &lt;pl:title&gt;&lt;xsl:value-of select='ABSTRACT'/&gt;&lt;/pl:title&gt;
60   *       &lt;/pl:entry&gt;
61   *   &lt;/xsl:template&gt;
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 */