| | | 1 | | using System; |
| | | 2 | | using System.Linq; |
| | | 3 | | using System.Xml; |
| | | 4 | | using JetBrains.Annotations; |
| | | 5 | | using WireMock.Validation; |
| | | 6 | | #if !NETSTANDARD1_3 |
| | | 7 | | using Wmhelp.XPath2; |
| | | 8 | | #endif |
| | | 9 | | |
| | | 10 | | namespace WireMock.Matchers |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// XPath2Matcher |
| | | 14 | | /// </summary> |
| | | 15 | | /// <seealso cref="IStringMatcher" /> |
| | | 16 | | public class XPathMatcher : IStringMatcher |
| | | 17 | | { |
| | | 18 | | private readonly string[] _patterns; |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Initializes a new instance of the <see cref="XPathMatcher"/> class. |
| | | 22 | | /// </summary> |
| | | 23 | | /// <param name="patterns">The patterns.</param> |
| | 4 | 24 | | public XPathMatcher([NotNull] params string[] patterns) |
| | 4 | 25 | | { |
| | 4 | 26 | | Check.NotNull(patterns, nameof(patterns)); |
| | | 27 | | |
| | 4 | 28 | | _patterns = patterns; |
| | 4 | 29 | | } |
| | | 30 | | |
| | | 31 | | /// <inheritdoc cref="IStringMatcher.IsMatch"/> |
| | | 32 | | public double IsMatch(string input) |
| | 2 | 33 | | { |
| | 2 | 34 | | if (input == null) |
| | 0 | 35 | | { |
| | 0 | 36 | | return MatchScores.Mismatch; |
| | | 37 | | } |
| | | 38 | | |
| | | 39 | | try |
| | 2 | 40 | | { |
| | 2 | 41 | | var nav = new XmlDocument { InnerXml = input }.CreateNavigator(); |
| | | 42 | | #if NETSTANDARD1_3 |
| | | 43 | | return MatchScores.ToScore(_patterns.Select(p => true.Equals(nav.Evaluate($"boolean({p})")))); |
| | | 44 | | #else |
| | 6 | 45 | | return MatchScores.ToScore(_patterns.Select(p => true.Equals(nav.XPath2Evaluate($"boolean({p})")))); |
| | | 46 | | #endif |
| | | 47 | | } |
| | 0 | 48 | | catch (Exception) |
| | 0 | 49 | | { |
| | 0 | 50 | | return MatchScores.Mismatch; |
| | | 51 | | } |
| | 2 | 52 | | } |
| | | 53 | | |
| | | 54 | | /// <inheritdoc cref="IStringMatcher.GetPatterns"/> |
| | | 55 | | public string[] GetPatterns() |
| | 1 | 56 | | { |
| | 1 | 57 | | return _patterns; |
| | 1 | 58 | | } |
| | | 59 | | |
| | | 60 | | /// <inheritdoc cref="IMatcher.GetName"/> |
| | | 61 | | public string GetName() |
| | 1 | 62 | | { |
| | 1 | 63 | | return "XPathMatcher"; |
| | 1 | 64 | | } |
| | | 65 | | } |
| | | 66 | | } |