public void TestTrivial()
 {
     Assert.False(PatternMatchUtils.SimpleMatch((string)null, string.Empty));
     Assert.False(PatternMatchUtils.SimpleMatch("1", null));
     DoTest("*", "123", true);
     DoTest("123", "123", true);
 }
    public object Invoke(IMethodInvocation invocation)
    {
        if (invocation.Method.ReturnType != typeof(string[]))
        {
            throw new NotSupportedException(
                      "StringArrayFilterAdvice must be applied on methods " +
                      "that return an array of string.");
        }

        string[] stringArray = (string[])invocation.Proceed();

        if (_pattern.Length > 0)
        {
            List <string> strings = new List <string>();
            foreach (string item in stringArray)
            {
                if (PatternMatchUtils.SimpleMatch(_pattern, item))
                {
                    strings.Add(item);
                }
            }

            return(strings.ToArray());
        }

        return(stringArray);
    }
        /// <summary>
        /// Does the supplied <paramref name="datum"/> satisfy the criteria encapsulated by
        /// this instance?
        /// </summary>
        /// <param name="datum">The datum to be checked by this criteria instance.</param>
        /// <returns>
        /// True if the supplied <paramref name="datum"/> satisfies the criteria encapsulated
        /// by this instance; false if not or the supplied <paramref name="datum"/> is null.
        /// </returns>
        public bool IsSatisfied(object datum)
        {
            bool       satisfied = false;
            MethodBase method    = datum as MethodBase;

            if (method != null)
            {
                satisfied = PatternMatchUtils.SimpleMatch(pattern.ToLower(), method.Name.ToLower());
            }
            return(satisfied);
        }
            public bool MatchHeader(string headerName)
            {
                var header = headerName.ToLower();

                if (PatternMatchUtils.SimpleMatch(Pattern, header))
                {
                    return(true);
                }

                return(false);
            }
            public bool MatchHeader(string headerName)
            {
                var header = headerName.ToLower();

                foreach (var pattern in Patterns)
                {
                    if (PatternMatchUtils.SimpleMatch(pattern, header))
                    {
                        return(true);
                    }
                }

                return(false);
            }
 /// <summary>
 /// Return if the given object name matches one of the object names specified.
 /// </summary>
 /// <remarks>
 /// <p>
 /// The default implementation checks for "xxx*", "*xxx" and "*xxx*" matches,
 /// as well as direct equality. Can be overridden in subclasses.
 /// </p>
 /// </remarks>
 /// <param name="objectName">the object name to check</param>
 /// <returns>if the names match</returns>
 protected virtual bool IsObjectNameMatch(string objectName)
 {
     if (objectNames != null)
     {
         for (int i = 0; i < objectNames.Count; i++)
         {
             string mappedName = String.Copy((string)objectNames[i]);
             if (PatternMatchUtils.SimpleMatch(mappedName, objectName))
             {
                 return(true);
             }
         }
     }
     return(false);
 }
示例#7
0
        protected List <string> GetMatchingHeaderNames(string pattern, IDictionary <string, object> headers)
        {
            var matchingHeaderNames = new List <string>();

            if (headers != null)
            {
                foreach (var header in headers)
                {
                    if (PatternMatchUtils.SimpleMatch(pattern, header.Key))
                    {
                        matchingHeaderNames.Add(header.Key);
                    }
                }
            }

            return(matchingHeaderNames);
        }
示例#8
0
        public virtual IMessageBuilder FilterAndCopyHeadersIfAbsent(IDictionary <string, object> headersToCopy, params string[] headerPatternsToFilter)
        {
            IDictionary <string, object> headers = new Dictionary <string, object>(headersToCopy);

            if (headerPatternsToFilter?.Length > 0)
            {
                foreach (var entry in headersToCopy)
                {
                    if (PatternMatchUtils.SimpleMatch(headerPatternsToFilter, entry.Key))
                    {
                        headers.Remove(entry.Key);
                    }
                }
            }

            return(CopyHeadersIfAbsent(headers));
        }
        /// <summary>
        /// Match a method against the given pattern.
        /// </summary>
        /// <param name="pattern">the pattern to match against.</param>
        /// <param name="method">the method to match.</param>
        /// <returns>
        /// <see lang="true"/> if the method matches the given pattern; otherwise <see lang="false"/>.
        /// </returns>
        /// <exception cref="System.ArgumentException">
        /// If the supplied <paramref name="pattern"/> is invalid.
        /// </exception>
        public static bool MethodMatch(string pattern, MethodInfo method)
        {
            Match m = methodMatchRegex.Match(pattern);

            if (!m.Success)
            {
                throw new ArgumentException(String.Format("The pattern [{0}] is not well-formed.", pattern));
            }

            // Check method name
            string methodNamePattern = m.Groups["methodName"].Value;

            if (!PatternMatchUtils.SimpleMatch(methodNamePattern, method.Name))
            {
                return(false);
            }

            if (m.Groups["parameters"].Value.Length > 0)
            {
                // Check parameter types
                string   parameters = m.Groups["parameterTypes"].Value;
                string[] paramTypes =
                    (parameters.Length == 0)
                    ? new string[0]
                    : StringUtils.DelimitedListToStringArray(parameters, ",");
                ParameterInfo[] paramInfos = method.GetParameters();

                // Verify parameter count
                if (paramTypes.Length != paramInfos.Length)
                {
                    return(false);
                }

                // Match parameter types
                for (int i = 0; i < paramInfos.Length; i++)
                {
                    if (paramInfos[i].ParameterType != TypeResolutionUtils.ResolveType(paramTypes[i]))
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
示例#10
0
        private List <string> GetMatchingHeaderNames(string pattern, IDictionary <string, object> headers)
        {
            if (headers == null)
            {
                return(new List <string>());
            }

            var matchingHeaderNames = new List <string>();

            foreach (var key in headers.Keys)
            {
                if (PatternMatchUtils.SimpleMatch(pattern, key))
                {
                    matchingHeaderNames.Add(key);
                }
            }

            return(matchingHeaderNames);
        }
示例#11
0
        /// <summary>
        ///     Match a method against the given pattern.
        /// </summary>
        /// <param name="pattern">the pattern to match against.</param>
        /// <param name="method">the method to match.</param>
        /// <returns>
        ///     <see lang="true" /> if the method matches the given pattern; otherwise <see lang="false" />.
        /// </returns>
        /// <exception cref="System.ArgumentException">
        ///     If the supplied <paramref name="pattern" /> is invalid.
        /// </exception>
        public static bool MethodMatch(string pattern, MethodInfo method)
        {
            var m = _methodMatchRegex.Match(pattern);

            if (!m.Success)
            {
                throw new ArgumentException(String.Format("The pattern [{0}] is not well-formed.", pattern));
            }

            // Check method name
            var methodNamePattern = m.Groups["methodName"].Value;

            if (!PatternMatchUtils.SimpleMatch(methodNamePattern, method.Name))
            {
                return(false);
            }

            if (m.Groups["parameters"].Value.Length > 0)
            {
                // Check parameter types
                var parameters = m.Groups["parameterTypes"].Value;
                var paramTypes =
                    (parameters.Length == 0)
                                                ? new string[0]
                                                : StringUtils.DelimitedListToStringArray(parameters, ",");
                var paramInfos = method.GetParameters();

                // Verify parameter count
                if (paramTypes.Length != paramInfos.Length)
                {
                    return(false);
                }

                // Match parameter types
                var result = !paramInfos.Where((t, i) => t.ParameterType != ResolveType(paramTypes[i])).Any();
                return(result);
            }

            return(true);
        }
 /// <summary>
 /// Does the supplied <paramref name="methodName"/> match the supplied <paramref name="mappedName"/>?
 /// </summary>
 /// <remarks>
 /// <p>
 /// The default implementation checks for "xxx*", "*xxx" and "*xxx*" matches,
 /// as well as direct equality. Can be overridden in subclasses.
 /// </p>
 /// </remarks>
 /// <param name="methodName">
 /// The method name of the class.
 /// </param>
 /// <param name="mappedName">
 /// The name in the descriptor.
 /// </param>
 /// <returns>
 /// <b>True</b> if the names match.
 /// </returns>
 protected virtual bool IsMatch(string methodName, string mappedName)
 {
     return(PatternMatchUtils.SimpleMatch(mappedName, methodName));
 }
 private void DoTest(string pattern, string str, bool shouldMatch)
 {
     Assert.Equal(shouldMatch, PatternMatchUtils.SimpleMatch(pattern, str));
 }
 /// <summary>
 /// Does the supplied type's <see cref="Type.FullName"/> match any of the <see cref="TypeNamePatterns"/>?
 /// </summary>
 /// <param name="type">
 /// The candidate <see cref="System.Type"/>.
 /// </param>
 /// <returns>
 /// <see langword="true"/> if the <paramref name="type"/> matches any of the <see cref="TypeNamePatterns"/>.
 /// </returns>
 public bool Matches(Type type)
 {
     return(PatternMatchUtils.SimpleMatch(_typeNamePatterns, type.FullName));
 }