| | | 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 | | /// <inheritdoc cref="IMatcher.MatchBehaviour"/> |
| | 4 | 21 | | public MatchBehaviour MatchBehaviour { get; } |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Initializes a new instance of the <see cref="XPathMatcher"/> class. |
| | | 25 | | /// </summary> |
| | | 26 | | /// <param name="patterns">The patterns.</param> |
| | 5 | 27 | | public XPathMatcher([NotNull] params string[] patterns) : this(MatchBehaviour.AcceptOnMatch, patterns) |
| | 5 | 28 | | { |
| | 5 | 29 | | } |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Initializes a new instance of the <see cref="XPathMatcher"/> class. |
| | | 33 | | /// </summary> |
| | | 34 | | /// <param name="matchBehaviour">The match behaviour.</param> |
| | | 35 | | /// <param name="patterns">The patterns.</param> |
| | 6 | 36 | | public XPathMatcher(MatchBehaviour matchBehaviour, [NotNull] params string[] patterns) |
| | 6 | 37 | | { |
| | 6 | 38 | | Check.NotNull(patterns, nameof(patterns)); |
| | | 39 | | |
| | 6 | 40 | | MatchBehaviour = matchBehaviour; |
| | 6 | 41 | | _patterns = patterns; |
| | 6 | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <inheritdoc cref="IStringMatcher.IsMatch"/> |
| | | 45 | | public double IsMatch(string input) |
| | 4 | 46 | | { |
| | 4 | 47 | | double match = MatchScores.Mismatch; |
| | 4 | 48 | | if (input != null) |
| | 4 | 49 | | { |
| | | 50 | | try |
| | 4 | 51 | | { |
| | 4 | 52 | | var nav = new XmlDocument { InnerXml = input }.CreateNavigator(); |
| | | 53 | | #if NETSTANDARD1_3 |
| | | 54 | | match = MatchScores.ToScore(_patterns.Select(p => true.Equals(nav.Evaluate($"boolean({p})")))); |
| | | 55 | | #else |
| | 12 | 56 | | match = MatchScores.ToScore(_patterns.Select(p => true.Equals(nav.XPath2Evaluate($"boolean({p})")))) |
| | | 57 | | #endif |
| | 4 | 58 | | } |
| | 0 | 59 | | catch (Exception) |
| | 0 | 60 | | { |
| | | 61 | | // just ignore exception |
| | 0 | 62 | | } |
| | 4 | 63 | | } |
| | | 64 | | |
| | 4 | 65 | | return MatchBehaviourHelper.Convert(MatchBehaviour, match); |
| | 4 | 66 | | } |
| | | 67 | | |
| | | 68 | | /// <inheritdoc cref="IStringMatcher.GetPatterns"/> |
| | | 69 | | public string[] GetPatterns() |
| | 1 | 70 | | { |
| | 1 | 71 | | return _patterns; |
| | 1 | 72 | | } |
| | | 73 | | |
| | | 74 | | /// <inheritdoc cref="IMatcher.Name"/> |
| | 1 | 75 | | public string Name => "XPathMatcher"; |
| | | 76 | | } |
| | | 77 | | } |