C0 code coverage information
Generated on Fri Oct 20 17:20:35 CEST 2006 with rcov 0.7.0
Code reported as executed by Ruby looks like this...
and this: this line is also marked as covered.
Lines considered as run by rcov, but not reported by Ruby, look like this,
and this: these lines were inferred by rcov (using simple heuristics).
Finally, here's a line marked as not executed.
Name |
Total lines |
Lines of code |
Total coverage |
Code coverage |
lib/xspf.rb
|
319
|
126
|
|
|
1 #--
2 # =============================================================================
3 # Copyright (c) 2006 Pau Garcia i Quiles (pgquiles@elpauer.org)
4 # All rights reserved.
5 #
6 # This library may be used only as allowed by either the Ruby license (or, by
7 # association with the Ruby license, the GPL). See the "doc" subdirectory of
8 # the XSPF distribution for the texts of these licenses.
9 # -----------------------------------------------------------------------------
10 # XSPF for Ruby website : http://www.elpauer.org/xspf
11 # =============================================================================
12 #++
13
14 require 'rexml/document'
15 require 'xml/xslt'
16
17 # :main: XSPF
18
19 module MetaGen #:nodoc:
20
21 # define the method
22 def self.add_method(klass, meth_name, body, meth_rdoc)
23 code = "# #{meth_rdoc}\n def #{meth_name.downcase}; begin; #{body}; rescue NoMethodError; return nil; end; end"
24
25 klass.module_eval(code)
26
27 # hook to write klass + name attrib to a file
28 if $META_RDOC
29 open($META_RDOC, 'a+') do |f|
30 f.puts("class #{klass}\n #{code}\n end")
31 end
32 end
33
34 end
35
36 # output in different formats
37 def self.add_output_format(klass, format, xspf, meth_rdoc)
38 xslt_path = "'#{File.join( File.dirname(__FILE__), %Q{xspf2#{format}.xsl} )}'"
39 code = "# #{meth_rdoc}\n def to_#{format}; xslt = XML::XSLT.new; xslt.xml = xspf; xslt.xsl = REXML::Document.new( File.new( #{xslt_path} ) ); xslt.serve; end"
40
41 klass.module_eval(code)
42
43 if $META_RDOC
44 open($META_RDOC, 'a+') do |f|
45 f.puts("class #{klass}\n #{code}\n end")
46 end
47 end
48
49 end
50
51 end
52
53 # XML Shareable Playlist Format (XSPF[http://www.xspf.org]) parser for Ruby
54 #
55 # When parsing, if a XSPF attribute or element is not set, the corresponding method will return _nil_.
56 #
57 # === Examples
58 # ==== Parse from file
59 # require 'xspf'
60 #
61 # f = File.new("playlist.xspf")
62 # x = XSPF.new(f)
63 # pl = XSPF::Playlist.new(x)
64 # tl = XSPF::Tracklist.new(pl)
65 #
66 # puts "XML version: #{x.version}"
67 # puts "XML encoding: #{ x.encoding}"
68 # puts "XSPF version: #{pl.version}"
69 # puts "Namespace: #{pl.xmlns}"
70 # puts "Playlist title: #{pl.title}"
71 # puts "Playlist creator: #{pl.creator}"
72 # puts "Playlist annotation: #{pl.annotation}"
73 # puts "Playlist info: #{pl.info}"
74 # puts "Playlist identifier: #{pl.identifier}"
75 # puts "Playlist attribution: #{pl.attribution}"
76 # puts "Tracklist: #{pl.tracklist}"
77 # tl.tracks.each do |t|
78 # puts "Track identifier: #{t.identifier}"
79 # puts "Track title: #{t.title}"
80 # puts "Track creator: #{t.creator}"
81 # puts "Track duration: #{t.duration}"
82 # puts "Track metainformation: link=#{t.meta_rel} content=#{t.meta_content}"
83 # end
84 #
85 # # Convert the XSPF document to SMIL
86 # x.to_smil
87 #
88 # # Convert the XSPF document to HTML
89 # x.to_html
90 #
91 # # Convert the XSPF document in a M3U playlist
92 # x.to_m3u
93 #
94 # ==== Parse from string
95 # playlist_document = <<-END_OF_PLAYLIST
96 # <?xml version="1.0" encoding="UTF-8"?>
97 # <playlist version="0" xmlns="http://xspf.org/ns/0/">
98 # <title>XSPlF it up!</title>
99 # <creator>Mayhem & Chaos Coordinator</creator>
100 # <annotation>Just a few songs to enjoy while you XSPlF it up!</annotation>
101 # <info>http://mayhem-chaos.net/xspf/xspf_it_up.html</info>
102 # <identifier>http://mayhem-chaos.net/xspf/xspf_it_up/1.0</identifier>
103 # <attribution>
104 # <location>ihttp://mayhem-chaos.net/xspf/xspf_it_up.html</location>
105 # </attribution>
106 # <trackList>
107 # <track>
108 # <identifier>http://musicbrainz.org/track/bdab6db0-2fd6-4166-a5fa-fbf2ff213793</identifier>
109 # <title>I Wanna Get High</title>
110 # <creator>Cypress Hill</creator>
111 # <duration>174613</duration>
112 # <meta rel="http://musicbrainz.org/track">http://musicbrainz.org/mm-2.1/track/bdab6db0-2fd6-4166-a5fa-fbf2ff213793</meta>
113 # </track>
114 # <track>
115 # <identifier>bdc846e7-6c26-4193-82a6-8d1b5a4d3429</identifier>
116 # <title>Smoke Two Joints</title>
117 # <creator>Sublime</creator>
118 # <duration>175466</duration>
119 # <meta rel="http://musicbrainz.org/track">http://musicbrainz.org/mm-2.1/track/bdc846e7-6c26-4193-82a6-8d1b5a4d3429</meta>
120 # </track>
121 # <track>
122 # <identifier>http://musicbrainz.org/track/7d9776f7-d428-40dc-a425-3c6e3dce4d58</identifier>
123 # <title>Hash Pipe</title>
124 # <creator>Weezer</creator>
125 # <duration>186533</duration>
126 # <meta rel="http://musicbrainz.org/track">http://musicbrainz.org/mm-2.1/track/7d9776f7-d428-40dc-a425-3c6e3dce4d58</meta>
127 # </track>
128 # </trackList>
129 # </playlist>
130 # END_OF_PLAYLIST
131 #
132 # x = XSPF.new(playlist_document)
133 # pl = XSPF::Playlist.new(x)
134 # tl = XSPF::Tracklist.new(pl)
135 #
136 # puts "XML version: #{x.version}"
137 # puts "XML encoding: #{ x.encoding}"
138 # puts "XSPF version: #{pl.version}"
139 # puts "Namespace: #{pl.xmlns}"
140 # puts "Playlist title: #{pl.title}"
141 # puts "Playlist creator: #{pl.creator}"
142 # puts "Playlist annotation: #{pl.annotation}"
143 # puts "Playlist info: #{pl.info}"
144 # puts "Playlist identifier: #{pl.identifier}"
145 # puts "Playlist attribution: #{pl.attribution}"
146 # puts "Tracklist: #{pl.tracklist}"
147 # tl.tracks.each do |t|
148 # puts "Track identifier: #{t.identifier}"
149 # puts "Track title: #{t.title}"
150 # puts "Track creator: #{t.creator}"
151 # puts "Track duration: #{t.duration}"
152 # puts "Track metainformation: link=#{t.meta_rel} content=#{t.meta_content}"
153 # end
154 #
155 # # Convert the XSPF document to SMIL
156 # x.to_smil
157 #
158 # # Convert the XSPF document to HTML
159 # x.to_html
160 #
161 # # Convert the XSPF document in a M3U playlist
162 # x.to_m3u
163 #
164
165 class XSPF
166
167 attr_reader :xspf
168
169 #:stopdoc:
170 ATTRIBUTES = %w{ version encoding }
171 VERSION_RDOC = "Version for the XML document or _nil_ if not defined"
172 ENCODING_RDOC = "Encoding of the XML document or _nil_ if not defined"
173
174 OUTPUT_FORMATS = %w{ m3u html smil soundblox }
175 M3U_RDOC = "Creates a .m3u playlist from the XSPF document. This method makes use of the official XSPF to M3U XSLT transformation by Lucas Gonze."
176 HTML_RDOC = "Outputs the playlist as an HTML page. This method makes use of the official XSPF to HTML XSLT transformation by Lucas Gonze."
177 SMIL_RDOC = "Creates a .smil playlist from the XSPF document. This method makes use of the official XSPF to SMIL XSLT transformation by Lucas Gonze."
178 SOUNDBLOX_RDOC = "Creates a SoundBlox playlist from the XSPF document. This method makes use of the official XSPF to SoundBlox XSLT tranformation by Lucas Gonze."
179
180 ATTRIBUTES.each do |attrib|
181 MetaGen.add_method(self, attrib, "@xspf.#{attrib}", eval(attrib.upcase + '_RDOC').to_s )
182 end
183
184 OUTPUT_FORMATS.each do |format|
185 MetaGen.add_output_format(self, format, @xspf, eval(format.upcase + '_RDOC').to_s )
186 end
187
188 #:startdoc:
189
190 # Creates a XSPF object from a file or string
191 def initialize(source = nil)
192 @xspf = REXML::Document.new(source)
193 end
194
195 # The <playlist> section of the XSPF document
196 def playlist
197 @xspf.root
198 end
199
200 end
201
202 class XSPF::Playlist < XSPF
203
204 attr_reader :playlist
205
206 #:stopdoc:
207 ATTRIBUTES = %w{ xmlns version }
208 ELEMENTS = %w{ title creator annotation info location identifier image date license attribution extension }
209 ATTRIBUTE_AND_ELEMENT = %w{ link meta }
210 attribution_subelements = %w{ location identifier }
211 extension_subelements = %w{ application content }
212 XMLNS_RDOC = "The XML namespace. It must be http://xspf.org/ns/0/ for a valid XSPF document."
213 VERSION_RDOC = "The XSPF version. It may be 0 or 1, although 1 is strongly advised."
214 TITLE_RDOC = "A human-readable title for the playlist. xspf:playlist elements MAY contain exactly one."
215 CREATOR_RDOC = "Human-readable name of the entity (author, authors, group, company, etc) that authored the playlist. XSPF::Playlist objects MAY contain exactly one."
216 ANNOTATION_RDOC = "A human-readable comment on the playlist. This is character data, not HTML, and it may not contain markup. XSPF::Playlist objects elements MAY contain exactly one."
217 INFO_RDOC = "URL of a web page to find out more about this playlist. Likely to be homepage of the author, and would be used to find out more about the author and to find more playlists by the author. XSPF::Playlist objects MAY contain exactly one."
218 LOCATION_RDOC = "Source URL for this playlist. XSPF::Playlist objects MAY contain exactly one."
219 IDENTIFIER_RDOC = "Canonical ID for this playlist. Likely to be a hash or other location-independent name. MUST be a legal URN. XSPF::Playlist objects MAY contain exactly one."
220 IMAGE_RDOC = "URL of an image to display if XSPF::Playlist#image return nil. XSPF::Playlist objects MAY contain exactly one."
221 DATE_RDOC = "Creation date (not last-modified date) of the playlist, formatted as a XML schema dateTime. XSPF::Playlist objects MAY contain exactly one."
222 LICENSE_RDOC = "URL of a resource that describes the license under which this playlist was released. XSPF::Playlist objects MAY contain zero or one license element."
223 ATTRIBUTION_RDOC = "An ordered list of URIs. The purpose is to satisfy licenses allowing modification but requiring attribution. If you modify such a playlist, move its XSPF::Playlist#location or XSPF::Playlist#identifier element to the top of the items in the XSPF::Playlist#attribution element. XSPF::Playlist objects MAY contain exactly one attribution element. Please note that currently XSPF for Ruby does not parse the contents of XSPF::Playlist#attribution."
224 EXTENSION_RDOC = "The extension element allows non-XSPF XML to be included in XSPF documents without breaking XSPF validation. The purpose is to allow nested XML, which the meta and link elements do not. XSPF::Playlist objects MAY contain zero or more extension elements but currently XSPF for Ruby returns only the first one."
225 LINK_REL_RDOC = "The link element allows non-XSPF web resources to be included in XSPF documents without breaking XSPF validation. A valid 'link' element has a 'rel' attribute and a 'content' element, obtained with XSPF::Playlist#link_rel and XSPF::Playlist#link_content respectively. XSPF::Playlist objects MAY contain zero or more link elements, but currently XSPF for Ruby returns only the first one."
226 LINK_CONTENT_RDOC = "The link element allows non-XSPF web resources to be included in XSPF documents without breaking XSPF validation. A valid 'link' element has a 'rel' attribute and a 'content' element, obtained with XSPF::Playlist#link_rel and XSPF::Playlist#link_content respectively. XSPF::Playlist objects MAY contain zero or more meta elements, but currently XSPF for Ruby returns only the first one."
227 META_REL_RDOC = "The meta element allows non-XSPF metadata to be included in XSPF documents without breaking XSPF validation. A valid 'meta' element has a 'rel' attribute and a 'content' element, obtained with XSPF::Playlist#meta_rel and XSPF::Playlist#meta_content respectively. XSPF::Playlist objects MAY contain zero or more meta elements, but currently XSPF for Ruby returns only the first one."
228 META_CONTENT_RDOC = "The meta element allows non-XSPF metadata to be included in XSPF documents without breaking XSPF validation. A valid 'meta' element has a 'rel' attribute and a 'content' element, obtained with XSPF::Playlist#meta_rel and XSPF::Playlist#meta_content respectively. XSPF::Playlist objects MAY contain zero or more meta elements, but currently XSPF for Ruby returns only the first one."
229
230 # FIXME Currently we only return the first "link"
231 # FIXME Currently we only return the first "meta"
232 # FIXME Currently we only return the first "extension"
233 # TODO Parse "attribution"
234 # TODO Parse "extension"
235
236 # Returns the value of the attribute or nil if the attribute is not present
237 ATTRIBUTES.each do |attrib|
238 MetaGen.add_method( self, attrib, "@playlist.root.attributes['#{attrib}']", eval(attrib.upcase + '_RDOC').to_s )
239 end
240
241 ELEMENTS.each do |element|
242 MetaGen.add_method( self, element, "@playlist.elements['#{element}'].text", eval(element.upcase + '_RDOC').to_s )
243 end
244
245 ATTRIBUTE_AND_ELEMENT.each do |ae|
246 MetaGen.add_method( self, "#{ae}_content", "@playlist.elements['#{ae}'].text", eval(ae.upcase + '_CONTENT_RDOC').to_s )
247 MetaGen.add_method( self, "#{ae}_rel", "@playlist.elements['#{ae}'].attributes['rel']", eval(ae.upcase + '_REL_RDOC').to_s )
248 end
249
250 #:startdoc:
251
252 # Creates a XSPF::Playlist from a XSPF document
253 def initialize(source)
254 @playlist = source.playlist
255 end
256
257 # The <trackList> section of the XSPF document
258 def tracklist
259 @playlist.elements['trackList'] || nil
260 end
261
262 end
263
264 class XSPF::Tracklist < XSPF::Playlist
265
266 attr_reader :tracklist
267
268 # Creates a XSPF::Tracklist from a XSPF::Playlist
269 def initialize(playlist)
270 @tracklist = playlist.tracklist
271 end
272
273 # An array XSPF::Track objects, one for each track returned by XSPF::Playlist#tracklist
274 def tracks
275 @tracklist.elements.collect { |track| XSPF::Track.new(track) }
276 end
277
278 end
279
280 class XSPF::Track
281
282 attr_reader :track
283
284 #:stopdoc:
285 ELEMENTS = %w{ location identifier title creator annotation info image album trackNum duration extension }
286 ATTRIBUTE_AND_ELEMENT = %w{ link meta }
287 LOCATION_RDOC = "URL of resource to be rendered. Probably an audio resource, but MAY be any type of resource with a well-known duration, such as video, a SMIL document, or an XSPF document. The duration of the resource defined in this element defines the duration of rendering. XSPF::Track objects MAY contain zero or more location elements, but a user-agent MUST NOT render more than one of the named resources. Currently, XSPF for Ruby returns only the first location."
288 IDENTIFIER_RDOC = "Canonical ID for this resource. Likely to be a hash or other location-independent name, such as a MusicBrainz identifier or isbn URN (if there existed isbn numbers for audio). MUST be a legal URN. XSPF::Track objects elements MAY contain zero or more identifier elements, but currently XSPF for Ruby returns only the first one."
289 TITLE_RDOC = "Human-readable name of the track that authored the resource which defines the duration of track rendering. This value is primarily for fuzzy lookups, though a user-agent may display it. XSPF::Track objects MAY contain exactly one."
290 CREATOR_RDOC = "Human-readable name of the entity (author, authors, group, company, etc) that authored the resource which defines the duration of track rendering. This value is primarily for fuzzy lookups, though a user-agent may display it. XSPF::Track objects MAY contain exactly one."
291 ANNOTATION_RDOC = "A human-readable comment on the track. This is character data, not HTML, and it may not contain markup. XSPF::Track objects MAY contain exactly one."
292 INFO_RDOC = "URL of a place where this resource can be bought or more info can be found."
293 IMAGE_RDOC = "URL of an image to display for the duration of the track. XSPF::Track objects MAY contain exactly one."
294 ALBUM_RDOC = "Human-readable name of the collection from which the resource which defines the duration of track rendering comes. For a song originally published as a part of a CD or LP, this would be the title of the original release. This value is primarily for fuzzy lookups, though a user-agent may display it. XSPF::Track objects MAY contain exactly one."
295 TRACKNUM_RDOC = "Integer with value greater than zero giving the ordinal position of the media on the XSPF::Track#album. This value is primarily for fuzzy lookups, though a user-agent may display it. XSPF::Track objects MAY contain exactly one. It MUST be a valid XML Schema nonNegativeInteger."
296 DURATION_RDOC = "The time to render a resource, in milliseconds. It MUST be a valid XML Schema nonNegativeInteger. This value is only a hint -- different XSPF generators will generate slightly different values. A user-agent MUST NOT use this value to determine the rendering duration, since the data will likely be low quality. XSPF::Track objects MAY contain exactly one duration element."
297 EXTENSION_RDOC = "The extension element allows non-XSPF XML to be included in XSPF documents without breaking XSPF validation. The purpose is to allow nested XML, which the meta and link elements do not. XSPF::Track objects MAY contain zero or more extension elements, but currently XSPF for Ruby returns only the first one."
298 LINK_REL_RDOC = "The link element allows non-XSPF web resources to be included in XSPF documents without breaking XSPF validation. A valid 'link' element has a 'rel' attribute and a 'content' element, obtained with XSPF::Track#link_rel and XSPF::Track#link_content respectively. XSPF::Track objects MAY contain zero or more link elements, but currently XSPF for Ruby returns only the first one."
299 LINK_CONTENT_RDOC = "The link element allows non-XSPF web resources to be included in XSPF documents without breaking XSPF validation. A valid 'link' element has a 'rel' attribute and a 'content' element, obtained with XSPF::Track#link_rel and XSPF::Track#link_content respectively. XSPF::Track objects MAY contain zero or more meta elements, but currently XSPF for Ruby returns only the first one."
300 META_REL_RDOC = "The meta element allows non-XSPF metadata to be included in XSPF documents without breaking XSPF validation. A valid 'meta' element has a 'rel' attribute and a 'content' element, obtained with XSPF::Track#meta_rel and XSPF::Track#meta_content respectively. XSPF::Track objects MAY contain zero or more meta elements, but currently XSPF for Ruby returns only the first one."
301 META_CONTENT_RDOC = "The meta element allows non-XSPF metadata to be included in XSPF documents without breaking XSPF validation. A valid 'meta' element has a 'rel' attribute and a 'content' element, obtained with XSPF::Track#meta_rel and XSPF::Track#meta_content respectively. XSPF::Track objects MAY contain zero or more meta elements, but currently XSPF for Ruby returns only the first one."
302
303 ELEMENTS.each do |element|
304 MetaGen.add_method( self, element, "@track.elements['#{element}'].text", eval(element.upcase + '_RDOC').to_s )
305 end
306
307 ATTRIBUTE_AND_ELEMENT.each do |ae|
308 MetaGen.add_method( self, "#{ae}_content", "@track.elements['#{ae}'].text", eval(ae.upcase + '_CONTENT_RDOC').to_s )
309 MetaGen.add_method( self, "#{ae}_rel", "@track.elements['#{ae}'].attributes['rel']", eval(ae.upcase + '_REL_RDOC').to_s )
310 end
311
312 # :startdoc:
313
314 # Creates a XSPF::Track object from a <track> section of the XSPF document
315 def initialize(tr)
316 @track = tr
317 end
318
319 end
Generated using the rcov code coverage analysis tool for Ruby version 0.7.0.