Exemplo n.º 1
0
        internal static Validation ValidateHelper(
            Dictionary <string, ValidationMethodPair> ruleIdToMethodMap,
            string ruleName,
            bool dynamicValidation,
            ref string matchedPattern,
            ref IDictionary <string, string> groups,
            ref string failureLevel,
            ref string fingerprint,
            ref string message,
            out bool pluginCanPerformDynamicAnalysis)
        {
            pluginCanPerformDynamicAnalysis = false;
            fingerprint = null;
            message     = null;

            ValidationMethodPair validationPair = GetValidationMethodPair(ruleName, ruleIdToMethodMap);

            if (validationPair == null)
            {
                return(Validation.ValidatorNotFound);
            }

            Validation result = ValidateStaticHelper(validationPair.IsValidStatic,
                                                     ref matchedPattern,
                                                     ref groups,
                                                     ref failureLevel,
                                                     ref fingerprint,
                                                     ref message);

            pluginCanPerformDynamicAnalysis = validationPair.IsValidDynamic != null;

            return((result != Validation.NoMatch && result != Validation.Expired && dynamicValidation && pluginCanPerformDynamicAnalysis) ?
                   ValidateDynamicHelper(validationPair.IsValidDynamic, ref fingerprint, ref message) :
                   result);
        }
Exemplo n.º 2
0
        private Dictionary <string, ValidationMethodPair> LoadValidationAssemblies(IEnumerable <string> validatorPaths)
        {
            var ruleToMethodMap = new Dictionary <string, ValidationMethodPair>();

            AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);

            foreach (string validatorPath in validatorPaths)
            {
                Assembly assembly = null;

                if (_fileSystem.FileExists(validatorPath))
                {
                    try
                    {
                        assemblyBaseFolder = Path.GetDirectoryName(validatorPath);
                        assembly           = _fileSystem.AssemblyLoadFrom(validatorPath);
                    }
                    catch (ReflectionTypeLoadException)
                    {
                        // TODO log something here.
                    }

                    if (assembly == null)
                    {
                        continue;
                    }

                    foreach (Type type in assembly.GetTypes())
                    {
                        string typeName = type.Name;

                        if (!typeName.EndsWith("Validator") || typeName.Equals("Validator"))
                        {
                            continue;
                        }

                        MethodInfo isValidStatic = type.GetMethod(
                            "IsValidStatic",
                            new[]
                        {
                            typeof(string).MakeByRefType(),                      // Matched pattern.
                            typeof(Dictionary <string, string>).MakeByRefType(), // Regex groups.
                            typeof(string).MakeByRefType(),                      // FailureLevel.
                            typeof(string).MakeByRefType(),                      // Fingerprint.
                            typeof(string).MakeByRefType(),                      // Message.
                        },
                            null);

                        if (isValidStatic == null || isValidStatic?.ReturnType != typeof(string))
                        {
                            continue;
                        }

                        MethodInfo isValidDynamic = type.GetMethod(
                            "IsValidDynamic",
                            new[]
                        {
                            typeof(string).MakeByRefType(),     // Fingerprint.
                            typeof(string).MakeByRefType(),     // Message.
                        },
                            null);

                        if (isValidDynamic?.ReturnType != typeof(string))
                        {
                            isValidDynamic = null;
                        }

                        ruleToMethodMap[typeName] = new ValidationMethodPair
                        {
                            IsValidStatic  = isValidStatic,
                            IsValidDynamic = isValidDynamic,
                        };
                    }
                }
            }

            return(ruleToMethodMap);
        }