Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ValidationHelpers.cs « Common « System.Workflow.Activities « referencesource « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b0ada5fd4c64d8836ee3d6e263873b50ef616105 (plain)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// Copyright (c) Microsoft Corporation. All rights reserved. 
//  
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
// WHETHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
// WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. 
// THE ENTIRE RISK OF USE OR RESULTS IN CONNECTION WITH THE USE OF THIS CODE 
// AND INFORMATION REMAINS WITH THE USER. 
//  

/*********************************************************************
 * NOTE: A copy of this file exists at: WF\Common\Shared
 * The two files must be kept in [....].  Any change made here must also
 * be made to WF\Common\Shared\ValidationHelpers.cs
*********************************************************************/
namespace System.Workflow.Activities.Common
{
    #region Imports

    using System;
    using System.Diagnostics;
    using System.Collections;
    using System.Collections.Generic;
    using System.Reflection;
    using System.CodeDom.Compiler;
    using System.Workflow.ComponentModel.Design;
    using System.Workflow.ComponentModel.Compiler;
    using System.Collections.Specialized;
    using System.ComponentModel.Design.Serialization;
    using System.Workflow.ComponentModel;
    #endregion

    internal static class ValidationHelpers
    {
        #region Validation & helpers for ID and Type

        internal static void ValidateIdentifier(IServiceProvider serviceProvider, string identifier)
        {
            if (serviceProvider == null)
                throw new ArgumentNullException("serviceProvider");

            SupportedLanguages language = CompilerHelpers.GetSupportedLanguage(serviceProvider);
            CodeDomProvider provider = CompilerHelpers.GetCodeDomProvider(language);
            if (language == SupportedLanguages.CSharp && identifier.StartsWith("@", StringComparison.Ordinal) ||
                language == SupportedLanguages.VB && identifier.StartsWith("[", StringComparison.Ordinal) && identifier.EndsWith("]", StringComparison.Ordinal) ||
                !provider.IsValidIdentifier(identifier))
            {
                throw new Exception(SR.GetString(SR.Error_InvalidLanguageIdentifier, identifier));
            }
        }

        internal static ValidationError ValidateIdentifier(string propName, IServiceProvider context, string identifier)
        {
            if (context == null)
                throw new ArgumentNullException("context");

            ValidationError error = null;
            if (identifier == null || identifier.Length == 0)
                error = new ValidationError(SR.GetString(SR.Error_PropertyNotSet, propName), ErrorNumbers.Error_PropertyNotSet);
            else
            {
                try
                {
                    ValidationHelpers.ValidateIdentifier(context, identifier);
                }
                catch (Exception e)
                {
                    error = new ValidationError(SR.GetString(SR.Error_InvalidIdentifier, propName, e.Message), ErrorNumbers.Error_InvalidIdentifier);
                }
            }
            if (error != null)
                error.PropertyName = propName;
            return error;
        }

        internal static ValidationError ValidateNameProperty(string propName, IServiceProvider context, string identifier)
        {
            if (context == null)
                throw new ArgumentNullException("context");

            ValidationError error = null;
            if (identifier == null || identifier.Length == 0)
                error = new ValidationError(SR.GetString(SR.Error_PropertyNotSet, propName), ErrorNumbers.Error_PropertyNotSet);
            else
            {
                SupportedLanguages language = CompilerHelpers.GetSupportedLanguage(context);
                CodeDomProvider provider = CompilerHelpers.GetCodeDomProvider(language);

                if (language == SupportedLanguages.CSharp && identifier.StartsWith("@", StringComparison.Ordinal) ||
                    language == SupportedLanguages.VB && identifier.StartsWith("[", StringComparison.Ordinal) && identifier.EndsWith("]", StringComparison.Ordinal) ||
                    !provider.IsValidIdentifier(provider.CreateEscapedIdentifier(identifier)))
                {
                    error = new ValidationError(SR.GetString(SR.Error_InvalidIdentifier, propName, SR.GetString(SR.Error_InvalidLanguageIdentifier, identifier)), ErrorNumbers.Error_InvalidIdentifier);
                }
            }
            if (error != null)
                error.PropertyName = propName;
            return error;
        }

        internal static ValidationErrorCollection ValidateUniqueIdentifiers(Activity rootActivity)
        {
            if (rootActivity == null)
                throw new ArgumentNullException("rootActivity");

            Hashtable identifiers = new Hashtable();
            ValidationErrorCollection validationErrors = new ValidationErrorCollection();
            Queue activities = new Queue();
            activities.Enqueue(rootActivity);
            while (activities.Count > 0)
            {
                Activity activity = (Activity)activities.Dequeue();
                if (activity.Enabled)
                {
                    if (identifiers.ContainsKey(activity.QualifiedName))
                    {
                        ValidationError duplicateError = new ValidationError(SR.GetString(SR.Error_DuplicatedActivityID, activity.QualifiedName), ErrorNumbers.Error_DuplicatedActivityID);
                        duplicateError.PropertyName = "Name";
                        duplicateError.UserData[typeof(Activity)] = activity;
                        validationErrors.Add(duplicateError);
                    }
                    else
                        identifiers.Add(activity.QualifiedName, activity);

                    if (activity is CompositeActivity && ((activity.Parent == null) || !Helpers.IsCustomActivity(activity as CompositeActivity)))
                    {
                        foreach (Activity child in Helpers.GetAllEnabledActivities((CompositeActivity)activity))
                            activities.Enqueue(child);
                    }
                }
            }

            return validationErrors;

        }
        #endregion

        #region Validation for Activity Ref order

        internal static bool IsActivitySourceInOrder(Activity request, Activity response)
        {
            if (request.Parent == null)
                return true;
            List<Activity> responsePath = new List<Activity>();
            responsePath.Add(response);
            Activity responseParent = response is CompositeActivity ? (CompositeActivity)response : response.Parent;
            while (responseParent != null)
            {
                responsePath.Add(responseParent);
                responseParent = responseParent.Parent;
            }

            Activity requestChild = request;
            CompositeActivity requestParent = request is CompositeActivity ? (CompositeActivity)request : request.Parent;
            while (requestParent != null && !responsePath.Contains(requestParent))
            {
                requestChild = requestParent;
                requestParent = requestParent.Parent;
            }

            if (requestParent == requestChild)
                return true;

            bool incorrectOrder = false;
            int index = (responsePath.IndexOf(requestParent) - 1);
            index = (index < 0) ? 0 : index; //sometimes parent gets added to the collection twice which causes index to be -1
            Activity responseChild = responsePath[index];

            if (requestParent == null || Helpers.IsAlternateFlowActivity(requestChild) || Helpers.IsAlternateFlowActivity(responseChild))
                incorrectOrder = true;
            else
            {
                for (int i = 0; i < requestParent.EnabledActivities.Count; i++)
                {
                    if (requestParent.EnabledActivities[i] == requestChild)
                        break;
                    else if (requestParent.EnabledActivities[i] == responseChild)
                    {
                        incorrectOrder = true;
                        break;
                    }
                }
            }
            return !incorrectOrder;
        }

        #endregion

        internal static ValidationErrorCollection ValidateObject(ValidationManager manager, object obj)
        {
            ValidationErrorCollection errors = new ValidationErrorCollection();
            if (obj == null)
                return errors;

            Type objType = obj.GetType();
            if (!objType.IsPrimitive && (objType != typeof(string)))
            {
                bool removeValidatedObjectCollection = false;
                Dictionary<int, object> validatedObjects = manager.Context[typeof(Dictionary<int, object>)] as Dictionary<int, object>;
                if (validatedObjects == null)
                {
                    validatedObjects = new Dictionary<int, object>();
                    manager.Context.Push(validatedObjects);
                    removeValidatedObjectCollection = true;
                }

                try
                {
                    if (!validatedObjects.ContainsKey(obj.GetHashCode()))
                    {
                        validatedObjects.Add(obj.GetHashCode(), obj);
                        try
                        {
                            Validator[] validators = manager.GetValidators(objType);
                            foreach (Validator validator in validators)
                                errors.AddRange(validator.Validate(manager, obj));
                        }
                        finally
                        {
                            validatedObjects.Remove(obj.GetHashCode());
                        }
                    }
                }
                finally
                {
                    if (removeValidatedObjectCollection)
                        manager.Context.Pop();
                }
            }

            return errors;
        }

        internal static ValidationErrorCollection ValidateActivity(ValidationManager manager, Activity activity)
        {
            ValidationErrorCollection errors = ValidationHelpers.ValidateObject(manager, activity);

            foreach (ValidationError error in errors)
            {
                if (!error.UserData.Contains(typeof(Activity)))
                    error.UserData[typeof(Activity)] = activity;
            }
            return errors;
        }

        internal static ValidationErrorCollection ValidateProperty(ValidationManager manager, Activity activity, object obj, PropertyValidationContext propertyValidationContext)
        {
            return ValidateProperty(manager, activity, obj, propertyValidationContext, null);
        }

        internal static ValidationErrorCollection ValidateProperty(ValidationManager manager, Activity activity, object obj, PropertyValidationContext propertyValidationContext, object extendedPropertyContext)
        {
            if (manager == null)
                throw new ArgumentNullException("manager");
            if (obj == null)
                throw new ArgumentNullException("obj");
            if (propertyValidationContext == null)
                throw new ArgumentNullException("propertyValidationContext");

            ValidationErrorCollection errors = new ValidationErrorCollection();

            manager.Context.Push(activity);
            manager.Context.Push(propertyValidationContext);
            if (extendedPropertyContext != null)
                manager.Context.Push(extendedPropertyContext);
            try
            {
                errors.AddRange(ValidationHelpers.ValidateObject(manager, obj));
            }
            finally
            {
                manager.Context.Pop();
                manager.Context.Pop();
                if (extendedPropertyContext != null)
                    manager.Context.Pop();
            }

            return errors;
        }
    }
}