SearchCriteria.cs 2.8 KB

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