File: ActivityWithResultConverter.cs

package info (click to toggle)
mono 6.8.0.105%2Bdfsg-3.3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,284,512 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 (198 lines) | stat: -rw-r--r-- 7,771 bytes parent folder | download | duplicates (7)
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
//-----------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------------------------

namespace System.Activities.XamlIntegration
{
    using Microsoft.VisualBasic.Activities;
    using System;
    using System.Activities.Expressions;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Reflection;
    using System.Runtime;
    using System.Text.RegularExpressions;
    using System.Windows.Markup;
    using System.Xaml;
    using System.Xaml.Schema;
    using System.Xml.Linq;
    using Microsoft.VisualBasic.Activities.XamlIntegration;

    public sealed class ActivityWithResultConverter : TypeConverterBase
    {
        public ActivityWithResultConverter()
            : base(typeof(Activity<>), typeof(ExpressionConverterHelper<>))
        {
        }

        public ActivityWithResultConverter(Type type)
            : base(type, typeof(Activity<>), typeof(ExpressionConverterHelper<>))
        {
        }

        internal static object GetRootTemplatedActivity(IServiceProvider serviceProvider)
        {
            // For now, we only support references to the root Activity when we're inside an Activity.Body
            // Note that in the case of nested activity bodies, this gives us the outer activity
            IRootObjectProvider rootProvider =
                serviceProvider.GetService(typeof(IRootObjectProvider)) as IRootObjectProvider;
            if (rootProvider == null)
            {
                return null;
            }
            IAmbientProvider ambientProvider =
                serviceProvider.GetService(typeof(IAmbientProvider)) as IAmbientProvider;
            if (ambientProvider == null)
            {
                return null;
            }
            IXamlSchemaContextProvider schemaContextProvider =
                serviceProvider.GetService(typeof(IXamlSchemaContextProvider)) as IXamlSchemaContextProvider;
            if (schemaContextProvider == null)
            {
                return null;
            }
            XamlMember activityBody = GetXamlMember(schemaContextProvider.SchemaContext, typeof(Activity), "Implementation");
            XamlMember dynamicActivityBody = GetXamlMember(schemaContextProvider.SchemaContext, typeof(DynamicActivity), "Implementation");
            if (activityBody == null || dynamicActivityBody == null)
            {
                return null;
            }
            if (ambientProvider.GetFirstAmbientValue(null, activityBody, dynamicActivityBody) == null)
            {
                return null;
            }
            object rootActivity = rootProvider.RootObject as Activity;
            return rootActivity;
        }

        static XamlMember GetXamlMember(XamlSchemaContext schemaContext, Type type, string memberName)
        {
            XamlType xamlType = schemaContext.GetXamlType(type);
            if (xamlType == null)
            {
                return null;
            }
            XamlMember xamlMember = xamlType.GetMember(memberName);
            return xamlMember;
        }

        internal sealed class ExpressionConverterHelper<T> : TypeConverterHelper<Activity<T>>
        {
            static Regex LiteralEscapeRegex = new Regex(@"^(%+\[)");
            static Type LocationHelperType = typeof(LocationHelper<>);

            TypeConverter baseConverter;
            Type valueType;
            LocationHelper locationHelper; // true if we're dealing with a Location

            public ExpressionConverterHelper()
                : this(TypeHelper.AreTypesCompatible(typeof(T), typeof(Location)))
            {
            }

            public ExpressionConverterHelper(bool isLocationType)
            {
                this.valueType = typeof(T);

                if (isLocationType)
                {
                    Fx.Assert(this.valueType.IsGenericType && this.valueType.GetGenericArguments().Length == 1, "Should only get Location<T> here");
                    this.valueType = this.valueType.GetGenericArguments()[0];
                    Type concreteHelperType = LocationHelperType.MakeGenericType(typeof(T), this.valueType);
                    this.locationHelper = (LocationHelper)Activator.CreateInstance(concreteHelperType);
                }
            }

            TypeConverter BaseConverter
            {
                get
                {
                    if (this.baseConverter == null)
                    {
                        this.baseConverter = TypeDescriptor.GetConverter(this.valueType);
                    }

                    return this.baseConverter;
                }
            }

            public override Activity<T> ConvertFromString(string text, ITypeDescriptorContext context)
            {
                if (IsExpression(text))
                {
                    // Expression.  Use the expression parser.
                    string expressionText = text.Substring(1, text.Length - 2);

                    if (this.locationHelper != null)
                    {
                        // 
                        return (Activity<T>)this.locationHelper.CreateExpression(expressionText);
                    }
                    else
                    {
                        // 
                        return new VisualBasicValue<T>()
                        {
                            ExpressionText = expressionText
                        };
                    }
                }
                else
                {
                    if (this.locationHelper != null)
                    {
                        throw FxTrace.Exception.AsError(new InvalidOperationException(SR.InvalidLocationExpression));
                    }

                    // look for "%[....]" escape pattern
                    if (text.EndsWith("]", StringComparison.Ordinal) && LiteralEscapeRegex.IsMatch(text))
                    {
                        // strip off the very front-most '%' from the original string
                        text = text.Substring(1, text.Length - 1);
                    }

                    T literalValue;
                    if (text is T)
                    {
                        literalValue = (T)(object)text;
                    }
                    else if (text == string.Empty) // workaround for System.Runtime.Xaml bug
                    {
                        literalValue = default(T);
                    }
                    else
                    {
                        // Literal value.  Invoke the base type converter.
                        literalValue = (T)BaseConverter.ConvertFromString(context, text);
                    }

                    return new Literal<T> { Value = literalValue };
                }
            }

            static bool IsExpression(string text)
            {
                return (text.StartsWith("[", StringComparison.Ordinal) && text.EndsWith("]", StringComparison.Ordinal));
            }            

            // to perform the generics dance around Locations we need these helpers
            abstract class LocationHelper
            {
                public abstract Activity CreateExpression(string expressionText);
            }

            class LocationHelper<TLocationValue> : LocationHelper
            {
                public override Activity CreateExpression(string expressionText)
                {
                    return new VisualBasicReference<TLocationValue>()
                    {
                        ExpressionText = expressionText
                    };
                }
            }
        } 
    }
}