PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

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