SearchCriteria.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #pragma warning disable CS1591
  2. #pragma warning disable SA1600
  3. using System;
  4. using System.Text.RegularExpressions;
  5. using MediaBrowser.Model.Extensions;
  6. namespace MediaBrowser.Model.Dlna
  7. {
  8. public class SearchCriteria
  9. {
  10. public SearchType SearchType { get; set; }
  11. /// <summary>
  12. /// Splits the specified string.
  13. /// </summary>
  14. /// <param name="str">The string.</param>
  15. /// <param name="term">The term.</param>
  16. /// <param name="limit">The limit.</param>
  17. /// <returns>System.String[].</returns>
  18. private static string[] RegexSplit(string str, string term, int limit)
  19. {
  20. return new Regex(term).Split(str, limit);
  21. }
  22. /// <summary>
  23. /// Splits the specified string.
  24. /// </summary>
  25. /// <param name="str">The string.</param>
  26. /// <param name="term">The term.</param>
  27. /// <returns>System.String[].</returns>
  28. private static string[] RegexSplit(string str, string term)
  29. {
  30. return Regex.Split(str, term, RegexOptions.IgnoreCase);
  31. }
  32. public SearchCriteria(string search)
  33. {
  34. if (string.IsNullOrEmpty(search))
  35. {
  36. throw new ArgumentNullException(nameof(search));
  37. }
  38. SearchType = SearchType.Unknown;
  39. string[] factors = RegexSplit(search, "(and|or)");
  40. foreach (string factor in factors)
  41. {
  42. string[] subFactors = RegexSplit(factor.Trim().Trim('(').Trim(')').Trim(), "\\s", 3);
  43. if (subFactors.Length == 3)
  44. {
  45. if (string.Equals("upnp:class", subFactors[0], StringComparison.OrdinalIgnoreCase) &&
  46. (string.Equals("=", subFactors[1]) || string.Equals("derivedfrom", subFactors[1], StringComparison.OrdinalIgnoreCase)))
  47. {
  48. if (string.Equals("\"object.item.imageItem\"", subFactors[2]) || string.Equals("\"object.item.imageItem.photo\"", subFactors[2], StringComparison.OrdinalIgnoreCase))
  49. {
  50. SearchType = SearchType.Image;
  51. }
  52. else if (string.Equals("\"object.item.videoItem\"", subFactors[2], StringComparison.OrdinalIgnoreCase))
  53. {
  54. SearchType = SearchType.Video;
  55. }
  56. else if (string.Equals("\"object.container.playlistContainer\"", subFactors[2], StringComparison.OrdinalIgnoreCase))
  57. {
  58. SearchType = SearchType.Playlist;
  59. }
  60. else if (string.Equals("\"object.container.album.musicAlbum\"", subFactors[2], StringComparison.OrdinalIgnoreCase))
  61. {
  62. SearchType = SearchType.MusicAlbum;
  63. }
  64. }
  65. }
  66. }
  67. }
  68. }
  69. }