1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 //----------------------------------------------------------------
5 namespace System.Activities.Presentation.View
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;
15 using System.Reflection;
17 using System.Windows.Markup;
22 class ParserContext : LocationReferenceEnvironment, IValueSerializerContext, IXamlNameResolver, INamespacePrefixLookup, IXamlNamespaceResolver
24 ModelItem baseModelItem;
25 EditingContext context;
26 IDictionary<string, string> namespaceLookup;
28 public ParserContext()
32 public ParserContext(ModelItem modelItem)
34 this.Initialize(modelItem);
37 public IContainer Container
42 public object Instance
48 public PropertyDescriptor PropertyDescriptor
54 public override Activity Root
59 IDictionary<string, string> NamespaceLookup
63 if (this.namespaceLookup == null)
65 this.namespaceLookup = new Dictionary<string, string>();
67 return this.namespaceLookup;
71 public bool Initialize(ModelItem modelItem)
73 this.baseModelItem = modelItem;
74 if (null != modelItem)
76 this.context = modelItem.GetEditingContext();
78 return (null != this.baseModelItem);
81 public override bool IsVisible(LocationReference reference)
83 object other = this.Resolve(reference.Name);
85 return object.ReferenceEquals(other, reference);
88 public override bool TryGetLocationReference(string name, out LocationReference result)
90 result = (LocationReference)this.Resolve(name);
91 return result != null;
95 public string GetNamespace(string prefix)
97 var nameSpace = this.NamespaceLookup
98 .Where(p => string.Equals(p.Value, prefix))
105 public IEnumerable<NamespaceDeclaration> GetNamespacePrefixes()
107 List<NamespaceDeclaration> namespacePrefixes = new List<NamespaceDeclaration>();
108 LoadNameSpace(namespacePrefixes);
109 return namespacePrefixes;
112 public override IEnumerable<LocationReference> GetLocationReferences()
114 List<LocationReference> toReturn = new List<LocationReference>();
115 if (this.baseModelItem != null)
117 List<ModelItem> declaredVariables = VisualBasicEditor.GetVariablesInScope(this.baseModelItem);
119 foreach (ModelItem modelItem in declaredVariables)
121 toReturn.Add(modelItem.GetCurrentValue() as LocationReference);
127 public object Resolve(string name)
129 IEnumerable<LocationReference> variables = this.GetLocationReferences();
130 return variables.FirstOrDefault<LocationReference>(p =>
132 return p != null && p.Name != null && p.Name.Equals(name);
136 public object Resolve(string name, out bool isFullyInitialized)
138 object result = Resolve(name);
139 isFullyInitialized = (result != null);
143 public bool IsFixupTokenAvailable
152 internal IEnumerable<string> Namespaces
156 var namespacesToReturn = new HashSet<string>();
157 //combine default import namespaces
158 foreach (var import in VisualBasicSettings.Default.ImportReferences)
160 namespacesToReturn.Add(import.Import);
162 //with custom ones, defined in user provided assemblies
163 if (null != this.namespaceLookup)
165 foreach (var nameSpace in this.namespaceLookup.Keys)
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)
173 string clrNamespace = nameSpace.Substring(startIndex + 1, endIndex - startIndex - 1);
174 namespacesToReturn.Add(clrNamespace);
179 ImportedNamespaceContextItem importedNamespaces = this.context.Items.GetValue<ImportedNamespaceContextItem>();
180 namespacesToReturn.UnionWith(importedNamespaces.ImportedNamespaces);
181 //return all namespaces
182 return namespacesToReturn;
186 public object GetFixupToken(IEnumerable<string> names)
191 public object GetFixupToken(IEnumerable<string> names, bool canAssignDirectly)
196 public object GetService(Type serviceType)
198 if (serviceType == typeof(IXamlNameResolver)
199 || serviceType == typeof(INamespacePrefixLookup)
200 || serviceType == typeof(IXamlNamespaceResolver))
210 public ValueSerializer GetValueSerializerFor(Type type)
215 public ValueSerializer GetValueSerializerFor(PropertyDescriptor descriptor)
220 public string LookupPrefix(string ns)
222 //get reference to namespace lookup dictionary (create one if necessary)
223 var lookupTable = this.NamespaceLookup;
225 //check if given namespace is already registered
226 if (!lookupTable.TryGetValue(ns, out prefix))
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;
237 public void OnComponentChanged()
239 throw FxTrace.Exception.AsError(new NotSupportedException());
242 public bool OnComponentChanging()
244 throw FxTrace.Exception.AsError(new NotSupportedException());
247 void LoadNameSpace(List<NamespaceDeclaration> result)
249 if (null == this.context)
251 Fx.Assert("EditingContext is null");
254 AssemblyContextControlItem assemblyContext = this.context.Items.GetValue<AssemblyContextControlItem>();
255 if (null == assemblyContext)
257 Fx.Assert("AssemblyContextControlItem not defined in EditingContext.Items");
260 if (null != assemblyContext.LocalAssemblyName)
262 result.Add(GetEntry(assemblyContext.LocalAssemblyName));
264 if (null != assemblyContext.ReferencedAssemblyNames)
266 foreach (AssemblyName name in assemblyContext.ReferencedAssemblyNames)
268 result.Add(GetEntry(name));
273 NamespaceDeclaration GetEntry(AssemblyName name)
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());
281 IEnumerable<KeyValuePair<string, object>> IXamlNameResolver.GetAllNamesAndValuesInScope()
286 event EventHandler IXamlNameResolver.OnNameScopeInitializationComplete