Esempio n. 1
0
        /// <inheritdoc cref="IObjectMatcher.IsMatch"/>
        public double IsMatch(object input)
        {
            bool match = false;

            if (input != null)
            {
                try
                {
                    // Check if JToken or object
                    JToken jtokenInput = input is JToken tokenInput ? tokenInput : JObject.FromObject(input);

                    // Check if JToken or string or object
                    JToken jtokenValue =
                        Value is JToken tokenValue ? tokenValue :
                        Value is string stringValue?JToken.Parse(stringValue) :
                            JObject.FromObject(input);

                    match = JToken.DeepEquals(jtokenValue, jtokenInput);
                }
                catch (JsonException)
                {
                    // just ignore JsonException
                }
            }

            return(MatchBehaviourHelper.Convert(MatchBehaviour, MatchScores.ToScore(match)));
        }
Esempio n. 2
0
        /// <inheritdoc cref="IStringMatcher.IsMatch"/>
        public double IsMatch(string input)
        {
            double match = MatchScores.Mismatch;

            if (input != null)
            {
                try
                {
                    var nav = new XmlDocument {
                        InnerXml = input
                    }.CreateNavigator();
#if NETSTANDARD1_3
                    match = MatchScores.ToScore(_patterns.Select(p => true.Equals(nav.Evaluate($"boolean({p})"))));
#else
                    match = MatchScores.ToScore(_patterns.Select(p => true.Equals(nav.XPath2Evaluate($"boolean({p})"))));
#endif
                }
                catch (Exception)
                {
                    // just ignore exception
                }
            }

            return(MatchBehaviourHelper.Convert(MatchBehaviour, match));
        }
Esempio n. 3
0
        /// <inheritdoc cref="IObjectMatcher.IsMatch"/>
        public double IsMatch(object input)
        {
            JObject value;

            switch (input)
            {
            case JObject valueAsJObject:
                value = valueAsJObject;
                break;

            default:
                value = JObject.FromObject(input);
                break;
            }

            // Convert a single object to a Queryable JObject-list with 1 entry.
            var queryable1 = new[] { value }.AsQueryable();

            // Generate the DynamicLinq select statement.
            string dynamicSelect = JsonUtils.GenerateDynamicLinqStatement(value);

            // Execute DynamicLinq Select statement.
            var queryable2 = queryable1.Select(dynamicSelect);

            // Use the Any(...) method to check if the result matches.
            double match = MatchScores.ToScore(_patterns.Select(pattern => queryable2.Any(pattern)));

            return(MatchBehaviourHelper.Convert(MatchBehaviour, match));
        }
Esempio n. 4
0
        /// <inheritdoc cref="IObjectMatcher.IsMatch"/>
        public double IsMatch(object input)
        {
            bool match = false;

            // When input is null or byte[], return Mismatch.
            if (input != null && !(input is byte[]))
            {
                try
                {
                    var inputAsJToken = ConvertValueToJToken(input);

                    match = IsMatch(
                        _jTokenConverter(_valueAsJToken),
                        _jTokenConverter(inputAsJToken));
                }
                catch (JsonException)
                {
                    if (ThrowException)
                    {
                        throw;
                    }
                }
            }

            return(MatchBehaviourHelper.Convert(MatchBehaviour, MatchScores.ToScore(match)));
        }
Esempio n. 5
0
        /// <inheritdoc cref="IStringMatcher.IsMatch"/>
        public double IsMatch(string input)
        {
            if (_values.Length == 1)
            {
                return(MatchBehaviourHelper.Convert(MatchBehaviour, MatchScores.ToScore(_values[0].GetPattern() == input)));
            }

            return(MatchBehaviourHelper.Convert(MatchBehaviour, MatchScores.ToScore(_values.Select(v => v.GetPattern()).Contains(input))));
        }
Esempio n. 6
0
        /// <inheritdoc cref="IStringMatcher.IsMatch"/>
        public double IsMatch(string input)
        {
            if (_values.Length == 1)
            {
                return(MatchBehaviourHelper.Convert(MatchBehaviour, MatchScores.ToScore(_values[0] == input)));
            }

            return(MatchBehaviourHelper.Convert(MatchBehaviour, MatchScores.ToScore(_values.Contains(input))));
        }
Esempio n. 7
0
        /// <inheritdoc cref="RegexMatcher.IsMatch"/>
        public override double IsMatch(string input)
        {
            if (string.IsNullOrEmpty(input) || !MediaTypeHeaderValue.TryParse(input, out MediaTypeHeaderValue contentType))
            {
                return(MatchBehaviourHelper.Convert(MatchBehaviour, MatchScores.Mismatch));
            }

            return(base.IsMatch(contentType.MediaType));
        }
        /// <inheritdoc cref="IStringMatcher.IsMatch"/>
        public double IsMatch(string input)
        {
            // Convert a single input string to a Queryable string-list with 1 entry.
            IQueryable queryable = new[] { input }.AsQueryable();

            // Use the Any(...) method to check if the result matches
            double match = MatchScores.ToScore(_patterns.Select(pattern => queryable.Any(pattern)));

            return(MatchBehaviourHelper.Convert(MatchBehaviour, match));
        }
        public double IsMatchInternal(object input)
        {
            double match = MatchScores.Mismatch;

            if (input != null)
            {
                match = MatchScores.ToScore(_patterns.Select(pattern => IsMatch(input, pattern)));
            }

            return(MatchBehaviourHelper.Convert(MatchBehaviour, match));
        }
Esempio n. 10
0
        /// <inheritdoc cref="IObjectMatcher.IsMatch"/>
        public double IsMatch(object input)
        {
            double match = MatchScores.Mismatch;

            // When input is null or byte[], return Mismatch.
            if (input != null && !(input is byte[]))
            {
                string inputAsString = JsonConvert.SerializeObject(input);
                return(IsMatch(inputAsString));
            }

            return(MatchBehaviourHelper.Convert(MatchBehaviour, match));
        }
Esempio n. 11
0
        /// <inheritdoc cref="IStringMatcher.IsMatch"/>
        public double IsMatch(string input)
        {
            double match = MatchScores.Mismatch;

            if (input != null)
            {
                try
                {
                    match = MatchScores.ToScore(_expressions.Select(e => e.IsMatch(input)));
                }
                catch (Exception)
                {
                    // just ignore exception
                }
            }

            return(MatchBehaviourHelper.Convert(MatchBehaviour, match));
        }
Esempio n. 12
0
        /// <inheritdoc cref="IStringMatcher.IsMatch"/>
        public double IsMatch(string input)
        {
            double match = MatchScores.Mismatch;

            if (input != null)
            {
                try
                {
                    match = MatchScores.ToScore(_patterns.Select(pattern => bool.Parse(new JmesPath().Transform(input, pattern))));
                }
                catch (JsonException)
                {
                    // just ignore JsonException
                }
            }

            return(MatchBehaviourHelper.Convert(MatchBehaviour, match));
        }
Esempio n. 13
0
        /// <inheritdoc cref="IStringMatcher.IsMatch"/>
        public double IsMatch(string input)
        {
            double match = MatchScores.Mismatch;

            if (input != null)
            {
                try
                {
                    var jtoken = JToken.Parse(input);
                    match = IsMatch(jtoken);
                }
                catch (JsonException)
                {
                    // just ignore JsonException
                }
            }

            return(MatchBehaviourHelper.Convert(MatchBehaviour, match));
        }
Esempio n. 14
0
        /// <inheritdoc cref="IObjectMatcher.IsMatch"/>
        public double IsMatch(object input)
        {
            bool match = false;

            // When input is null or byte[], return Mismatch.
            if (input != null && !(input is byte[]))
            {
                try
                {
                    // Check if JToken or object
                    JToken jtokenInput = input is JToken tokenInput ? tokenInput : JObject.FromObject(input);

                    // Check if JToken, string, IEnumerable or object
                    JToken jtokenValue;
                    switch (Value)
                    {
                    case JToken tokenValue:
                        jtokenValue = tokenValue;
                        break;

                    case string stringValue:
                        jtokenValue = JsonUtils.Parse(stringValue);
                        break;

                    case IEnumerable enumerableValue:
                        jtokenValue = JArray.FromObject(enumerableValue);
                        break;

                    default:
                        jtokenValue = JObject.FromObject(Value);
                        break;
                    }

                    match = DeepEquals(jtokenValue, jtokenInput);
                }
                catch (JsonException)
                {
                    // just ignore JsonException
                }
            }

            return(MatchBehaviourHelper.Convert(MatchBehaviour, MatchScores.ToScore(match)));
        }
Esempio n. 15
0
        /// <inheritdoc cref="IObjectMatcher.IsMatch"/>
        public double IsMatch(object input)
        {
            double match = MatchScores.Mismatch;

            if (input != null)
            {
                try
                {
                    // Check if JToken or object
                    JToken jtoken = input is JToken token ? token : JObject.FromObject(input);
                    match = IsMatch(jtoken);
                }
                catch (JsonException)
                {
                    // just ignore JsonException
                }
            }

            return(MatchBehaviourHelper.Convert(MatchBehaviour, match));
        }
Esempio n. 16
0
        /// <inheritdoc cref="IObjectMatcher.IsMatch"/>
        public double IsMatch(object input)
        {
            bool match = false;

            if (input != null)
            {
                try
                {
                    // Check if JToken or object
                    JToken jtoken = input is JToken token ? token : JObject.FromObject(input);

                    match = JToken.DeepEquals(JToken.Parse(_value), jtoken);
                }
                catch (JsonException)
                {
                    // just ignore JsonException
                }
            }

            return(MatchBehaviourHelper.Convert(MatchBehaviour, MatchScores.ToScore(match)));
        }
Esempio n. 17
0
        /// <inheritdoc cref="IObjectMatcher.IsMatch"/>
        public double IsMatch(object input)
        {
            bool match;

            switch (input)
            {
            case string @string:
                match = !string.IsNullOrEmpty(@string);
                break;

            case byte[] bytes:
                match = bytes != null && bytes.Any();
                break;

            default:
                match = input != null;
                break;
            }

            return(MatchBehaviourHelper.Convert(MatchBehaviour, MatchScores.ToScore(match)));
        }
Esempio n. 18
0
        /// <inheritdoc cref="IStringMatcher.IsMatch"/>
        public virtual double IsMatch(string input)
        {
            double match = MatchScores.Mismatch;

            if (input != null)
            {
                try
                {
                    match = MatchScores.ToScore(_expressions.Select(e => e.IsMatch(input)));
                }
                catch (Exception)
                {
                    if (ThrowException)
                    {
                        throw;
                    }
                }
            }

            return(MatchBehaviourHelper.Convert(MatchBehaviour, match));
        }
Esempio n. 19
0
        /// <inheritdoc cref="IStringMatcher.IsMatch"/>
        public double IsMatch(string input)
        {
            double match = MatchScores.Mismatch;

            // Convert a single input string to a Queryable string-list with 1 entry.
            IQueryable queryable = new[] { input }.AsQueryable();

            try
            {
                // Use the Any(...) method to check if the result matches
                match = MatchScores.ToScore(_patterns.Select(pattern => queryable.Any(pattern)));

                return(MatchBehaviourHelper.Convert(MatchBehaviour, match));
            }
            catch
            {
                // just ignore exception
                // TODO add logging?
            }

            return(MatchBehaviourHelper.Convert(MatchBehaviour, match));
        }
Esempio n. 20
0
        /// <inheritdoc cref="IObjectMatcher.IsMatch"/>
        public double IsMatch(object input)
        {
            double match = MatchScores.Mismatch;

            // When input is null or byte[], return Mismatch.
            if (input != null && !(input is byte[]))
            {
                try
                {
                    // Check if JToken or object
                    JToken jtoken = input is JToken token ? token : JObject.FromObject(input);
                    match = IsMatch(jtoken);
                }
                catch (JsonException)
                {
                    if (ThrowException)
                    {
                        throw;
                    }
                }
            }

            return(MatchBehaviourHelper.Convert(MatchBehaviour, match));
        }
Esempio n. 21
0
        /// <inheritdoc cref="IStringMatcher.IsMatch"/>
        public double IsMatch(string input)
        {
            IStringMetric m = GetStringMetricType();

            return(MatchBehaviourHelper.Convert(MatchBehaviour, MatchScores.ToScore(_patterns.Select(p => m.GetSimilarity(p, input)))));
        }
        /// <inheritdoc cref="IStringMatcher.IsMatch"/>
        public double IsMatch(string input)
        {
            var match = !string.IsNullOrEmpty(input);

            return(MatchBehaviourHelper.Convert(MatchBehaviour, MatchScores.ToScore(match)));
        }
Esempio n. 23
0
        /// <inheritdoc cref="IObjectMatcher.IsMatch"/>
        public double IsMatch(object input)
        {
            bool equals = ValueAsObject != null?Equals(ValueAsObject, input) : ValueAsBytes.SequenceEqual((byte[])input);

            return(MatchBehaviourHelper.Convert(MatchBehaviour, MatchScores.ToScore(equals)));
        }
Esempio n. 24
0
 /// <inheritdoc cref="IStringMatcher.IsMatch"/>
 public double IsMatch(string input)
 {
     return(MatchBehaviourHelper.Convert(MatchBehaviour, MatchScores.ToScore(_values.Select(value => value.Equals(input)))));
 }