| | | 1 | | using System; |
| | | 2 | | using System.Linq; |
| | | 3 | | using System.Xml; |
| | | 4 | | using JetBrains.Annotations; |
| | | 5 | | using WireMock.Validation; |
| | | 6 | | #if NET45 |
| | | 7 | | using Wmhelp.XPath2; |
| | | 8 | | #endif |
| | | 9 | | |
| | | 10 | | namespace WireMock.Matchers |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// XPath2Matcher |
| | | 14 | | /// </summary> |
| | | 15 | | /// <seealso cref="WireMock.Matchers.IMatcher" /> |
| | | 16 | | public class XPathMatcher : IMatcher |
| | | 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> |
| | 2 | 24 | | public XPathMatcher([NotNull] params string[] patterns) |
| | 2 | 25 | | { |
| | 2 | 26 | | Check.NotNull(patterns, nameof(patterns)); |
| | | 27 | | |
| | 2 | 28 | | _patterns = patterns; |
| | 2 | 29 | | } |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Determines whether the specified input is match. |
| | | 33 | | /// </summary> |
| | | 34 | | /// <param name="input">The input string</param> |
| | | 35 | | /// <returns>A value between 0.0 - 1.0 of the similarity.</returns> |
| | | 36 | | public double IsMatch(string input) |
| | 2 | 37 | | { |
| | 2 | 38 | | if (input == null) |
| | 0 | 39 | | return MatchScores.Mismatch; |
| | | 40 | | |
| | | 41 | | try |
| | 2 | 42 | | { |
| | 2 | 43 | | var nav = new XmlDocument { InnerXml = input }.CreateNavigator(); |
| | | 44 | | #if NET45 |
| | 4 | 45 | | return MatchScores.ToScore(_patterns.Select(p => true.Equals(nav.XPath2Evaluate($"boolean({p})")))); |
| | | 46 | | #else |
| | | 47 | | return MatchScores.ToScore(_patterns.Select(p => true.Equals(nav.Evaluate($"boolean({p})")))); |
| | | 48 | | #endif |
| | | 49 | | } |
| | 0 | 50 | | catch (Exception) |
| | 0 | 51 | | { |
| | 0 | 52 | | return MatchScores.Mismatch; |
| | | 53 | | } |
| | 2 | 54 | | } |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Gets the patterns. |
| | | 58 | | /// </summary> |
| | | 59 | | /// <returns>Patterns</returns> |
| | | 60 | | public string[] GetPatterns() |
| | 0 | 61 | | { |
| | 0 | 62 | | return _patterns; |
| | 0 | 63 | | } |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// Gets the name. |
| | | 67 | | /// </summary> |
| | | 68 | | /// <returns> |
| | | 69 | | /// Name |
| | | 70 | | /// </returns> |
| | | 71 | | public string GetName() |
| | 0 | 72 | | { |
| | 0 | 73 | | return "XPathMatcher"; |
| | 0 | 74 | | } |
| | | 75 | | } |
| | | 76 | | } |