File: Validator.cs

package info (click to toggle)
mono 6.8.0.105%2Bdfsg-3.3~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,284,488 kB
  • sloc: cs: 11,172,132; xml: 2,850,069; ansic: 671,653; cpp: 122,091; perl: 59,366; javascript: 30,841; asm: 22,168; makefile: 20,093; sh: 15,020; python: 4,827; pascal: 925; sql: 859; sed: 16; php: 1
file content (143 lines) | stat: -rw-r--r-- 6,743 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
namespace System.Workflow.ComponentModel.Compiler
{
    using System;
    using System.Reflection;
    using System.Collections;
    using System.Collections.Generic;

    #region Class Validator
    [Obsolete("The System.Workflow.* types are deprecated.  Instead, please use the new types from System.Activities.*")]
    public class Validator
    {
        public virtual ValidationErrorCollection Validate(ValidationManager manager, object obj)
        {
            if (manager == null)
                throw new ArgumentNullException("manager");
            if (obj == null)
                throw new ArgumentNullException("obj");

            return new ValidationErrorCollection();
        }

        public virtual ValidationError ValidateActivityChange(Activity activity, ActivityChangeAction action)
        {
            if (activity == null)
                throw new ArgumentNullException("activity");
            if (action == null)
                throw new ArgumentNullException("action");

            return null;
        }

        public virtual ValidationErrorCollection ValidateProperties(ValidationManager manager, object obj)
        {
            if (manager == null)
                throw new ArgumentNullException("manager");
            if (obj == null)
                throw new ArgumentNullException("obj");

            ValidationErrorCollection errors = new ValidationErrorCollection();

            Activity activity = manager.Context[typeof(Activity)] as Activity;

            // Validate all members that support validations.
            Walker walker = new Walker(true);
            walker.FoundProperty += delegate(Walker w, WalkerEventArgs args)
            {
                //If we find dynamic property of the same name then we do not invoke the validator associated with the property
                //Attached dependency properties will not be found by FromName().

                // args.CurrentProperty can be null if the property is of type IList.  The walker would go into each item in the
                // list, but we don't need to validate these items.
                if (args.CurrentProperty != null)
                {
                    DependencyProperty dependencyProperty = DependencyProperty.FromName(args.CurrentProperty.Name, args.CurrentProperty.DeclaringType);
                    if (dependencyProperty == null)
                    {
                        object[] validationVisibilityAtrributes = args.CurrentProperty.GetCustomAttributes(typeof(ValidationOptionAttribute), true);
                        ValidationOption validationVisibility = (validationVisibilityAtrributes.Length > 0) ? ((ValidationOptionAttribute)validationVisibilityAtrributes[0]).ValidationOption : ValidationOption.Optional;
                        if (validationVisibility != ValidationOption.None)
                        {
                            errors.AddRange(ValidateProperty(args.CurrentProperty, args.CurrentPropertyOwner, args.CurrentValue, manager));
                            // don't probe into subproperties as validate object inside the ValidateProperties call does it for us
                            args.Action = WalkerAction.Skip;
                        }
                    }
                }
            };

            walker.WalkProperties(activity, obj);

            return errors;
        }

        protected string GetFullPropertyName(ValidationManager manager)
        {
            if (manager == null)
                throw new ArgumentNullException("manager");

            string fullName = string.Empty;

            // iterate the properties in the stack starting with the last one
            int iterator = 0;
            while (manager.Context[iterator] != null)
            {
                if (manager.Context[iterator] is PropertyValidationContext)
                {
                    PropertyValidationContext propertyValidationContext = manager.Context[iterator] as PropertyValidationContext;
                    if (propertyValidationContext.PropertyName == string.Empty)
                        fullName = string.Empty;  // property chain broke... dicard properties after break
                    else if (fullName == string.Empty)
                        fullName = propertyValidationContext.PropertyName;
                    else
                        fullName = propertyValidationContext.PropertyName + "." + fullName;
                }
                iterator++;
            }

            return fullName;
        }

        internal protected ValidationErrorCollection ValidateProperty(PropertyInfo propertyInfo, object propertyOwner, object propertyValue, ValidationManager manager)
        {
            ValidationErrorCollection errors = new ValidationErrorCollection();

            object[] validationVisibilityAtrributes = propertyInfo.GetCustomAttributes(typeof(ValidationOptionAttribute), true);
            ValidationOption validationVisibility = (validationVisibilityAtrributes.Length > 0) ? ((ValidationOptionAttribute)validationVisibilityAtrributes[0]).ValidationOption : ValidationOption.Optional;
            PropertyValidationContext propertyValidationContext = new PropertyValidationContext(propertyOwner, propertyInfo, propertyInfo.Name);
            manager.Context.Push(propertyValidationContext);

            try
            {
                if (propertyValue != null)
                {
                    errors.AddRange(ValidationHelpers.ValidateObject(manager, propertyValue));
                    if (propertyValue is IList)
                    {
                        PropertyValidationContext childContext = new PropertyValidationContext(propertyValue, null, "");
                        manager.Context.Push(childContext);

                        try
                        {
                            foreach (object child in (IList)propertyValue)
                                errors.AddRange(ValidationHelpers.ValidateObject(manager, child));
                        }
                        finally
                        {
                            System.Diagnostics.Debug.Assert(manager.Context.Current == childContext, "Unwinding contextStack: the item that is about to be popped is not the one we pushed.");
                            manager.Context.Pop();
                        }
                    }
                }
            }
            finally
            {
                System.Diagnostics.Debug.Assert(manager.Context.Current == propertyValidationContext, "Unwinding contextStack: the item that is about to be popped is not the one we pushed.");
                manager.Context.Pop();
            }

            return errors;
        }
    }
    #endregion
}