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