[banshee] AsxPlaylistFormat: Add support for external ASX playlists (bgo#664424)



commit dcae3a33f9031a88df7f804656053205464fe522
Author: Roderich Schupp <roderich schupp googlemail com>
Date:   Wed Nov 30 21:50:57 2011 +0100

    AsxPlaylistFormat: Add support for external ASX playlists (bgo#664424)
    
    Handle the entryref tag, which references an external ASX file. Add the
    elements from that secondary playlists as part of the main playlist.
    
    Add a unit test for an external ASX playlist.
    
    Signed-off-by: Bertrand Lorentz <bertrand lorentz gmail com>

 .../Banshee.Playlists.Formats/AsxPlaylistFormat.cs |  105 +++++++++++++-------
 .../Tests/PlaylistFormatTests.cs                   |   11 ++
 tests/data/playlist-data/entryref.asx              |   11 ++
 3 files changed, 92 insertions(+), 35 deletions(-)
---
diff --git a/src/Core/Banshee.Services/Banshee.Playlists.Formats/AsxPlaylistFormat.cs b/src/Core/Banshee.Services/Banshee.Playlists.Formats/AsxPlaylistFormat.cs
index de0d008..6346170 100644
--- a/src/Core/Banshee.Services/Banshee.Playlists.Formats/AsxPlaylistFormat.cs
+++ b/src/Core/Banshee.Services/Banshee.Playlists.Formats/AsxPlaylistFormat.cs
@@ -33,6 +33,8 @@ using System.Collections.Generic;
 
 using Mono.Unix;
 
+using Hyena;
+
 using Banshee.Base;
 using Banshee.Sources;
 
@@ -82,48 +84,81 @@ namespace Banshee.Playlists.Formats
                 throw new InvalidPlaylistException();
             }
 
-            while(xml_reader.Read()) {
-                if(xml_reader.NodeType != XmlNodeType.Element || xml_reader.Depth != 1
-                    || String.Compare(xml_reader.LocalName, "entry", true) != 0) {
+            while (xml_reader.Read ()) {
+                if (xml_reader.NodeType != XmlNodeType.Element || xml_reader.Depth != 1) {
                     continue;
                 }
 
-                Dictionary<string, object> element = AddElement();
+                switch (xml_reader.LocalName.ToLower ()) {
+                    case "title":
+                        try {
+                            xml_reader.Read ();
+                            Title = xml_reader.Value;
+                        }
+                        catch {
+                        }
+                        break;
+
+                    case "entry":
+                        LoadEntry (xml_reader);
+                        break;
+
+                    case "entryref":
+                        string href = xml_reader["HREF"] ?? xml_reader["href"];
+                        if (href != null) {
+                            PlaylistParser secondary = new PlaylistParser ();
+                            if (secondary.Parse (new SafeUri (ResolveUri (href)))) {
+                                // splice in Elements of secondary
+                                foreach (Dictionary<string, object> e in secondary.Elements) {
+                                    Elements.Add (e);
+                                }
+                            }
+                        }
+                        break;
+                }
+            }
+        }
 
-                do {
-                    try {
-                        xml_reader.Read();
-                    } catch {
-                        xml_reader.Skip();
-                    }
+        private void LoadEntry (XmlTextReader xml_reader)
+        {
+            Dictionary<string, object> element = AddElement ();
 
-                    if(xml_reader.NodeType != XmlNodeType.Element) {
-                        continue;
-                    }
+            do {
+                try {
+                    xml_reader.Read ();
+                } catch {
+                    xml_reader.Skip ();
+                }
 
-                    switch(xml_reader.LocalName.ToLower()) {
-                        case "title":
-                            xml_reader.Read();
-                            element["title"] = xml_reader.Value;
-                            break;
-                        case "ref":
-                            // asf links say they are http, but are mmsh instead
-                            string uri_aux = xml_reader["HREF"] ?? xml_reader["href"];
-                            if (uri_aux.StartsWith("http", StringComparison.CurrentCultureIgnoreCase))
-                                uri_aux = "mmsh" + uri_aux.Substring(4);
-
-                            element["uri"] = ResolveUri(uri_aux);
-                            break;
-                        case "duration":
-                            try {
-                                xml_reader.Read();
-                                element["duration"] = TimeSpan.Parse(xml_reader.Value);
-                            } catch {
-                            }
-                            break;
+                if (xml_reader.NodeType != XmlNodeType.Element) {
+                    continue;
+                }
+
+                switch (xml_reader.LocalName.ToLower ()) {
+                    case "title":
+                        xml_reader.Read ();
+                        element["title"] = xml_reader.Value;
+                        break;
+
+                    case "ref":
+                        // asf links say they are http, but are mmsh instead
+                        string uri_aux = xml_reader["HREF"] ?? xml_reader["href"];
+                        if (uri_aux.StartsWith ("http", StringComparison.CurrentCultureIgnoreCase)) {
+                            uri_aux = "mmsh" + uri_aux.Substring (4);
                     }
-                } while(!xml_reader.EOF && xml_reader.Depth > 1);
-            }
+
+                        element["uri"] = ResolveUri (uri_aux);
+                        break;
+
+                    case "duration":
+                        try {
+                            xml_reader.Read ();
+                            element["duration"] = TimeSpan.Parse (xml_reader.Value);
+                        } catch {
+                        }
+                        break;
+                }
+            } while (!xml_reader.EOF && xml_reader.Depth > 1);
         }
 
         public override void Save(Stream stream, ITrackModelSource source)
diff --git a/src/Core/Banshee.Services/Banshee.Playlists.Formats/Tests/PlaylistFormatTests.cs b/src/Core/Banshee.Services/Banshee.Playlists.Formats/Tests/PlaylistFormatTests.cs
index 698f553..25bb977 100644
--- a/src/Core/Banshee.Services/Banshee.Playlists.Formats/Tests/PlaylistFormatTests.cs
+++ b/src/Core/Banshee.Services/Banshee.Playlists.Formats/Tests/PlaylistFormatTests.cs
@@ -94,6 +94,17 @@ namespace Banshee.Playlists.Formats.Tests
         }
 
         [Test]
+        public void ReadAsxEntryRef ()
+        {
+            PlaylistParser parser = new PlaylistParser ();
+            parser.BaseUri = BaseUri;
+
+            parser.Parse (new SafeUri ("http://download.banshee.fm/test/remote.asx";));
+            IPlaylistFormat plref = LoadPlaylist (new AsxPlaylistFormat (), "entryref.asx");
+            Assert.AreEqual (parser.Elements, plref.Elements);
+        }
+
+        [Test]
         public void ReadM3uSimple ()
         {
             LoadTest (new M3uPlaylistFormat (), "simple.m3u", false);
diff --git a/tests/data/playlist-data/entryref.asx b/tests/data/playlist-data/entryref.asx
new file mode 100644
index 0000000..d05d8a1
--- /dev/null
+++ b/tests/data/playlist-data/entryref.asx
@@ -0,0 +1,11 @@
+<asx version="3.0">
+	<title>Reference to another ASX</title>
+	<entryref href="http://download.banshee.fm/test/remote.asx"; />
+</asx>
+
+
+
+
+
+	
+	



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]