SearchCriteria.cs 2.9 KB

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