PageRenderTime 20ms CodeModel.GetById 12ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/QDFeedParser.Silverlight.Tests/Utils/SilverlightTestFileLoader.cs

#
C# | 75 lines | 62 code | 13 blank | 0 comment | 3 complexity | 04ebdd37c74a8dda9fd184efc6e73c70 MD5 | raw file
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using System.IO;
 5using System.IO.IsolatedStorage;
 6using System.Text;
 7
 8namespace QDFeedParser.Silverlight.Tests
 9{
10    public class SilverlightTestFileLoader
11    {
12        public static IList<string> SampleRssFeeds()
13        {
14            return new[] { "Aaronontheweb-RSS.xml", "del.icio.us-RSS.xml" };
15        }
16
17        public static IList<string> SampleAtomFeeds()
18        {
19            return new[] { "ScottGu-Atom.xml", "GoogleNews-Atom.xml" };
20        }
21
22        public static string ReadFeedContents(string path)
23        {
24            using(var stream = typeof(SilverlightTestFileLoader).Assembly.GetManifestResourceStream(string.Format("QDFeedParser.Silverlight.Tests.TestFiles.{0}", path)))
25            {
26                using(var streamreader = new StreamReader(stream))
27                {
28                    return streamreader.ReadToEnd();
29                }
30            }
31        }
32
33        public static void WriteFeedToIsolatedStorage(string feedxml, Uri feedUri)
34        {
35            using(var storage = IsolatedStorageFile.GetUserStoreForApplication())
36            {
37                CreateDirectoryTree(feedUri, storage);
38
39                using(var filestream = new IsolatedStorageFileStream(feedUri.OriginalString, FileMode.Create, FileAccess.Write, storage))
40                {
41                    var data = Encoding.UTF8.GetBytes(feedxml);
42
43                    filestream.Write(data, 0, data.Count());
44                }
45            }
46        }
47
48        public static void CreateDirectoryTree(Uri feedUri)
49        {
50            using(var storage = IsolatedStorageFile.GetUserStoreForApplication())
51            {
52                CreateDirectoryTree(feedUri, storage);
53            }
54        }
55
56        public static void CreateDirectoryTree(Uri feedUri, IsolatedStorageFile storage)
57        {
58            if (!feedUri.OriginalString.Contains('\\')) return;
59
60            var directory = GetDirectoryPath(feedUri);
61
62            if (!storage.DirectoryExists(directory)) 
63                storage.CreateDirectory(directory);
64        }
65
66        public static string GetDirectoryPath(Uri feedUri)
67        {
68            if (!feedUri.OriginalString.Contains('\\')) return string.Empty;
69
70            var directoryPos = feedUri.OriginalString.LastIndexOf('\\');
71            return feedUri.OriginalString.Substring(0, directoryPos);
72        }
73    }
74
75}