Merge pull request #2250 from esdrubal/master
[mono.git] / mcs / class / referencesource / System.Activities.Presentation / System.Activities.Presentation / System / Activities / Presentation / View / ParserContext.cs
1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //----------------------------------------------------------------
4
5 namespace System.Activities.Presentation.View
6 {
7     using Microsoft.VisualBasic.Activities;
8     using System.Activities.Presentation.Model;
9     using System.Activities.Presentation.Hosting;
10     using System.Activities.Presentation.Xaml;
11     using System.Collections.Generic;
12     using System.ComponentModel;
13     using System.Globalization;
14     using System.Linq;
15     using System.Reflection;
16     using System.Runtime;
17     using System.Windows.Markup;
18     using System.Xaml;
19
20
21
22     class ParserContext : LocationReferenceEnvironment, IValueSerializerContext, IXamlNameResolver, INamespacePrefixLookup, IXamlNamespaceResolver
23     {
24         ModelItem baseModelItem;
25         EditingContext context;
26         IDictionary<string, string> namespaceLookup;
27
28         public ParserContext()
29         {
30         }
31
32         public ParserContext(ModelItem modelItem)
33         {
34             this.Initialize(modelItem);
35         }
36
37         public IContainer Container
38         {
39             get { return null; }
40         }
41
42         public object Instance
43         {
44             get;
45             internal set;
46         }
47
48         public PropertyDescriptor PropertyDescriptor
49         {
50             get;
51             internal set;
52         }
53
54         public override Activity Root
55         {
56             get { return null; }
57         }
58
59         IDictionary<string, string> NamespaceLookup
60         {
61             get
62             {
63                 if (this.namespaceLookup == null)
64                 {
65                     this.namespaceLookup = new Dictionary<string, string>();
66                 }
67                 return this.namespaceLookup;
68             }
69         }
70
71         public bool Initialize(ModelItem modelItem)
72         {
73             this.baseModelItem = modelItem;
74             if (null != modelItem)
75             {
76                 this.context = modelItem.GetEditingContext();
77             }
78             return (null != this.baseModelItem);
79         }
80
81         public override bool IsVisible(LocationReference reference)
82         {
83             object other = this.Resolve(reference.Name);
84             
85             return object.ReferenceEquals(other, reference);
86         }
87
88         public override bool TryGetLocationReference(string name, out LocationReference result)
89         {
90             result = (LocationReference)this.Resolve(name);
91             return result != null;
92         }
93
94
95         public string GetNamespace(string prefix)
96         {
97             var nameSpace = this.NamespaceLookup
98                 .Where(p => string.Equals(p.Value, prefix))
99                 .Select(p => p.Key)
100                 .FirstOrDefault();
101
102             return nameSpace;
103         }
104         
105         public IEnumerable<NamespaceDeclaration> GetNamespacePrefixes()
106         {
107             List<NamespaceDeclaration> namespacePrefixes = new List<NamespaceDeclaration>();
108             LoadNameSpace(namespacePrefixes);
109             return namespacePrefixes;
110         }
111
112         public override IEnumerable<LocationReference> GetLocationReferences()
113         {
114             List<LocationReference> toReturn = new List<LocationReference>();
115             if (this.baseModelItem != null)
116             {
117                 List<ModelItem> declaredVariables = VisualBasicEditor.GetVariablesInScope(this.baseModelItem);
118
119                 foreach (ModelItem modelItem in declaredVariables)
120                 {
121                     toReturn.Add(modelItem.GetCurrentValue() as LocationReference);
122                 }
123             }
124             return toReturn;
125         }
126
127         public object Resolve(string name)
128         {
129             IEnumerable<LocationReference> variables = this.GetLocationReferences();
130             return variables.FirstOrDefault<LocationReference>(p =>
131             {
132                 return p != null && p.Name != null && p.Name.Equals(name);
133             });
134         }
135         
136         public object Resolve(string name, out bool isFullyInitialized)
137         {
138             object result = Resolve(name);
139             isFullyInitialized = (result != null);
140             return result;
141         }
142
143         public bool IsFixupTokenAvailable
144         {
145             get
146             {
147                 
148                 return false;
149             }
150         }
151
152         internal IEnumerable<string> Namespaces
153         {
154             get
155             {
156                 var namespacesToReturn = new HashSet<string>();
157                 //combine default import namespaces
158                 foreach (var import in VisualBasicSettings.Default.ImportReferences)
159                 {
160                     namespacesToReturn.Add(import.Import);
161                 }
162                 //with custom ones, defined in user provided assemblies
163                 if (null != this.namespaceLookup)
164                 {
165                     foreach (var nameSpace in this.namespaceLookup.Keys)
166                     {
167                         //out of full namespace declaration (i.e. "clr-namespace:<namespace>;assembly=<assembly>"
168                         //get clear namespace name
169                         int startIndex = nameSpace.IndexOf(":", StringComparison.Ordinal);
170                         int endIndex = nameSpace.IndexOf(";", StringComparison.Ordinal);
171                         if (startIndex >= 0 && endIndex >= 0)
172                         {
173                             string clrNamespace = nameSpace.Substring(startIndex + 1, endIndex - startIndex - 1);
174                             namespacesToReturn.Add(clrNamespace);
175                         }
176                     }
177                 }
178
179                 ImportedNamespaceContextItem importedNamespaces = this.context.Items.GetValue<ImportedNamespaceContextItem>();
180                 namespacesToReturn.UnionWith(importedNamespaces.ImportedNamespaces);
181                 //return all namespaces
182                 return namespacesToReturn;
183             }
184         }
185
186         public object GetFixupToken(IEnumerable<string> names)
187         {
188             return null;
189         }
190
191         public object GetFixupToken(IEnumerable<string> names, bool canAssignDirectly)
192         {
193             return null;
194         }
195
196         public object GetService(Type serviceType)
197         {
198             if (serviceType == typeof(IXamlNameResolver)
199                 || serviceType == typeof(INamespacePrefixLookup)
200                 || serviceType == typeof(IXamlNamespaceResolver))
201             {
202                 return this;
203             }
204             else
205             {
206                 return null;
207             }
208         }
209
210         public ValueSerializer GetValueSerializerFor(Type type)
211         {
212             return null;
213         }
214
215         public ValueSerializer GetValueSerializerFor(PropertyDescriptor descriptor)
216         {
217             return null;
218         }
219
220         public string LookupPrefix(string ns)
221         {
222             //get reference to namespace lookup dictionary (create one if necessary)
223             var lookupTable = this.NamespaceLookup;
224             string prefix;
225             //check if given namespace is already registered
226             if (!lookupTable.TryGetValue(ns, out prefix))
227             {
228                 //no, create a unique prefix
229                 prefix = string.Format(CultureInfo.InvariantCulture, "__{0}", Guid.NewGuid().ToString().Replace("-", "").Substring(0, 5));
230                 //and store value in the dictionary
231                 lookupTable[ns] = prefix;
232             }
233             //return prefix
234             return prefix;
235         }
236
237         public void OnComponentChanged()
238         {
239             throw FxTrace.Exception.AsError(new NotSupportedException());
240         }
241
242         public bool OnComponentChanging()
243         {
244             throw FxTrace.Exception.AsError(new NotSupportedException());
245         }
246
247         void LoadNameSpace(List<NamespaceDeclaration> result)
248         {
249             if (null == this.context)
250             {
251                 Fx.Assert("EditingContext is null");
252                 return;
253             }
254             AssemblyContextControlItem assemblyContext = this.context.Items.GetValue<AssemblyContextControlItem>();
255             if (null == assemblyContext)
256             {
257                 Fx.Assert("AssemblyContextControlItem not defined in EditingContext.Items");
258                 return;
259             }
260             if (null != assemblyContext.LocalAssemblyName)
261             {
262                 result.Add(GetEntry(assemblyContext.LocalAssemblyName));
263             }
264             if (null != assemblyContext.ReferencedAssemblyNames)
265             {
266                 foreach (AssemblyName name in assemblyContext.ReferencedAssemblyNames)
267                 {
268                     result.Add(GetEntry(name));
269                 }
270             }
271         }
272
273         NamespaceDeclaration GetEntry(AssemblyName name)
274         {
275             string ns =
276                 string.Format(CultureInfo.InvariantCulture, "clr-namespace:{0};assembly={1}",
277                 Guid.NewGuid().ToString().Replace('-', '_'), name.Name);
278             return new NamespaceDeclaration(ns, Guid.NewGuid().ToString());
279         }     
280         
281         IEnumerable<KeyValuePair<string, object>> IXamlNameResolver.GetAllNamesAndValuesInScope()
282         {
283             return null;
284         }
285         
286         event EventHandler IXamlNameResolver.OnNameScopeInitializationComplete
287         {
288             add { }
289             remove { }
290         }
291
292     }
293
294
295 }