ParserContext.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / NetFx40 / Tools / System.Activities.Presentation / System / Activities / Presentation / View / ParserContext.cs / 1305376 / ParserContext.cs

                            //---------------------------------------------------------------- 
// Copyright (c) Microsoft Corporation.  All rights reserved.
//---------------------------------------------------------------

namespace System.Activities.Presentation.View 
{
    using Microsoft.VisualBasic.Activities; 
    using System.Activities.Presentation.Model; 
    using System.Activities.Presentation.Hosting;
    using System.Activities.Presentation.Xaml; 
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Globalization;
    using System.Linq; 
    using System.Reflection;
    using System.Runtime; 
    using System.Windows.Markup; 
    using System.Xaml;
 


    class ParserContext : LocationReferenceEnvironment, IValueSerializerContext, IXamlNameResolver, INamespacePrefixLookup, IXamlNamespaceResolver
    { 
        ModelItem baseModelItem;
        EditingContext context; 
        IDictionary namespaceLookup; 

        public ParserContext() 
        {
        }

        public ParserContext(ModelItem modelItem) 
        {
            this.Initialize(modelItem); 
        } 

        public IContainer Container 
        {
            get { return null; }
        }
 
        public object Instance
        { 
            get; 
            internal set;
        } 

        public PropertyDescriptor PropertyDescriptor
        {
            get; 
            internal set;
        } 
 
        public override Activity Root
        { 
            get { return null; }
        }

        IDictionary NamespaceLookup 
        {
            get 
            { 
                if (this.namespaceLookup == null)
                { 
                    this.namespaceLookup = new Dictionary();
                }
                return this.namespaceLookup;
            } 
        }
 
        public bool Initialize(ModelItem modelItem) 
        {
            this.baseModelItem = modelItem; 
            if (null != modelItem)
            {
                this.context = modelItem.GetEditingContext();
            } 
            return (null != this.baseModelItem);
        } 
 
        public override bool IsVisible(LocationReference reference)
        { 
            object other = this.Resolve(reference.Name);

            return object.ReferenceEquals(other, reference);
        } 

        public override bool TryGetLocationReference(string name, out LocationReference result) 
        { 
            result = (LocationReference)this.Resolve(name);
            return result != null; 
        }


        public string GetNamespace(string prefix) 
        {
            var nameSpace = this.NamespaceLookup. 
                Where(p => string.Equals(p.Value, prefix)). 
                Select(p => p.Key).
                FirstOrDefault(); 

            return nameSpace;
        }
 
        public IEnumerable GetNamespacePrefixes()
        { 
            List namespacePrefixes = new List(); 
            LoadNameSpace(namespacePrefixes);
            return namespacePrefixes; 
        }

        public override IEnumerable GetLocationReferences()
        { 
            List toReturn = new List();
 
            if (baseModelItem != null) 
            {
                //store the reference to the root model item 
                ModelItem rootModelItem = baseModelItem.Root;

                //Get the activityModelItem
                while (baseModelItem != null && !typeof(Activity).IsAssignableFrom(baseModelItem.ItemType)) 
                {
                    baseModelItem = baseModelItem.Parent; 
                } 

                //try to get activity out of model item 
                Activity activity = (null != baseModelItem ? baseModelItem.GetCurrentValue() : null) as Activity;
                if (activity != null)
                {
                    IEnumerable variableCollection = VariableHelper.GetVariableCollection(baseModelItem); 
                    if (null != variableCollection)
                    { 
                        foreach (ModelItem item in variableCollection) 
                        {
                            toReturn.Add((LocationReference)item.GetCurrentValue()); 
                        }
                    }

                    // Now, check if activity contains action handlers which defined that variable 
                    IEnumerable variablesInActionHandlers = VariableHelper.FindActionHandlerVariables(baseModelItem);
                    if (null != variablesInActionHandlers) 
                    { 
                        foreach (ModelItem variableModelItem in variablesInActionHandlers)
                        { 
                            toReturn.Add((LocationReference)variableModelItem.GetCurrentValue());
                        }
                    }
 
                    // Now try all variables in Scope.
                    IEnumerable allVariablesInScope = VariableHelper.FindVariablesInScope(baseModelItem); 
                    if (null != allVariablesInScope) 
                    {
                        foreach (ModelItem variableModelItem in allVariablesInScope) 
                        {
                            toReturn.Add((LocationReference)variableModelItem.GetCurrentValue());
                        }
                    } 
                }
                // As a final search , try too see if the user is trying to bind to the Arguments in 
                // the  custom type designer. 
                if (ActivityBuilderHelper.IsActivityBuilderType(rootModelItem))
                { 
                    foreach (Variable variable in ActivityBuilderHelper.GetVariables(rootModelItem))
                    {
                        toReturn.Add(variable);
                    } 
                }
            } 
            return toReturn; 
        }
 
        public object Resolve(string name)
        {
            Object retVal = null;
            if (baseModelItem != null && retVal == null) 
            {
                //store the reference to the root model item 
                ModelItem rootModelItem = baseModelItem.Root; 

                //Get the activityModelItem 
                while (baseModelItem != null && !typeof(Activity).IsAssignableFrom(baseModelItem.ItemType))
                {
                    baseModelItem = baseModelItem.Parent;
                } 

                //try to get activity out of model item 
                Activity activity = (null != baseModelItem ? baseModelItem.GetCurrentValue() : null) as Activity; 
                if (activity != null)
                { 
                    IEnumerable variableCollection = VariableHelper.GetVariableCollection(baseModelItem);
                    if (null != variableCollection)
                    {
                        retVal = variableCollection. 
                            Where(p => string.Equals(name, p.Properties["Name"].ComputedValue)).
                            Select(p => p.GetCurrentValue()). 
                            FirstOrDefault(); 
                    }
 
                    // Now, check if activity contains action handlers which defined that variable
                    if (retVal == null)
                    {
                        IEnumerable variablesInActionHandlers = VariableHelper.FindActionHandlerVariables(baseModelItem); 
                        foreach (ModelItem variableModelItem in variablesInActionHandlers)
                        { 
                            Variable variable = (Variable)variableModelItem.GetCurrentValue(); 
                            if (variable.Name.Equals(name))
                            { 
                                retVal = variable;
                                break;
                            }
                        } 
                    }
 
                    // Now try all variables in Scope. 
                    if (retVal == null)
                    { 
                        IEnumerable allVariablesInScope = VariableHelper.FindVariablesInScope(baseModelItem);
                        foreach (ModelItem variableModelItem in allVariablesInScope)
                        {
                            Variable variable = (Variable)variableModelItem.GetCurrentValue(); 
                            if (variable != null)
                            { 
                                if (variable.Name.Equals(name)) 
                                {
                                    retVal = variable; 
                                    break;
                                }
                            }
                        } 
                    }
                } 
                // As a final search , try too see if the user is trying to bind to the Arguments in 
                // the  custom type designer.
                if (retVal == null && ActivityBuilderHelper.IsActivityBuilderType(rootModelItem)) 
                {
                    foreach (Variable variable in ActivityBuilderHelper.GetVariables(rootModelItem))
                    {
                        if (variable.Name.Equals(name)) 
                        {
                            retVal = variable; 
                            break; 
                        }
                    } 
                }
            }
            return retVal;
        } 

        public object Resolve(string name, out bool isFullyInitialized) 
        { 
            object result = Resolve(name);
            isFullyInitialized = (result != null); 
            return result;
        }

        public bool IsFixupTokenAvailable 
        {
            get 
            { 

                return false; 
            }
        }

        internal IEnumerable Namespaces 
        {
            get 
            { 
                var namespacesToReturn = new HashSet();
                //combine default import namespaces 
                foreach (var import in VisualBasicSettings.Default.ImportReferences)
                {
                    namespacesToReturn.Add(import.Import);
                } 
                //with custom ones, defined in user provided assemblies
                if (null != this.namespaceLookup) 
                { 
                    foreach (var nameSpace in this.namespaceLookup.Keys)
                    { 
                        //out of full namespace declaration (i.e. "clr-namespace:;assembly="
                        //get clear namespace name
                        int startIndex = nameSpace.IndexOf(":", StringComparison.Ordinal);
                        int endIndex = nameSpace.IndexOf(";", StringComparison.Ordinal); 
                        if (startIndex >= 0 && endIndex >= 0)
                        { 
                            string clrNamespace = nameSpace.Substring(startIndex + 1, endIndex - startIndex - 1); 
                            namespacesToReturn.Add(clrNamespace);
                        } 
                    }
                }

                ImportedNamespaceContextItem importedNamespaces = this.context.Items.GetValue(); 
                namespacesToReturn.UnionWith(importedNamespaces.ImportedNamespaces);
                //return all namespaces 
                return namespacesToReturn; 
            }
        } 

        public object GetFixupToken(IEnumerable names)
        {
            return null; 
        }
 
        public object GetFixupToken(IEnumerable names, bool canAssignDirectly) 
        {
            return null; 
        }

        public object GetService(Type serviceType)
        { 
            if (serviceType == typeof(IXamlNameResolver)
                || serviceType == typeof(INamespacePrefixLookup) 
                || serviceType == typeof(IXamlNamespaceResolver)) 
            {
                return this; 
            }
            else
            {
                return null; 
            }
        } 
 
        public ValueSerializer GetValueSerializerFor(Type type)
        { 
            return null;
        }

        public ValueSerializer GetValueSerializerFor(PropertyDescriptor descriptor) 
        {
            return null; 
        } 

        public string LookupPrefix(string ns) 
        {
            //get reference to namespace lookup dictionary (create one if necessary)
            var lookupTable = this.NamespaceLookup;
            string prefix; 
            //check if given namespace is already registered
            if (!lookupTable.TryGetValue(ns, out prefix)) 
            { 
                //no, create a unique prefix
                prefix = string.Format(CultureInfo.InvariantCulture, "__{0}", Guid.NewGuid().ToString().Replace("-", "").Substring(0, 5)); 
                //and store value in the dictionary
                lookupTable[ns] = prefix;
            }
            //return prefix 
            return prefix;
        } 
 
        public void OnComponentChanged()
        { 
            throw FxTrace.Exception.AsError(new NotSupportedException());
        }

        public bool OnComponentChanging() 
        {
            throw FxTrace.Exception.AsError(new NotSupportedException()); 
        } 

        void LoadNameSpace(List result) 
        {
            if (null == this.context)
            {
                Fx.Assert("EditingContext is null"); 
                return;
            } 
            AssemblyContextControlItem assemblyContext = this.context.Items.GetValue(); 
            if (null == assemblyContext)
            { 
                Fx.Assert("AssemblyContextControlItem not defined in EditingContext.Items");
                return;
            }
            if (null != assemblyContext.LocalAssemblyName) 
            {
                result.Add(GetEntry(assemblyContext.LocalAssemblyName)); 
            } 
            if (null != assemblyContext.ReferencedAssemblyNames)
            { 
                foreach (AssemblyName name in assemblyContext.ReferencedAssemblyNames)
                {
                    result.Add(GetEntry(name));
                } 
            }
        } 
 
        NamespaceDeclaration GetEntry(AssemblyName name)
        { 
            string ns =
                string.Format(CultureInfo.InvariantCulture, "clr-namespace:{0};assembly={1}",
                Guid.NewGuid().ToString().Replace('-', '_'), name.Name);
            return new NamespaceDeclaration(ns, Guid.NewGuid().ToString()); 
        }
 
        IEnumerable> IXamlNameResolver.GetAllNamesAndValuesInScope() 
        {
            return null; 
        }

        event EventHandler IXamlNameResolver.OnNameScopeInitializationComplete
        { 
            add { }
            remove { } 
        } 

    } 


}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//---------------------------------------------------------------- 
// Copyright (c) Microsoft Corporation.  All rights reserved.
//---------------------------------------------------------------

namespace System.Activities.Presentation.View 
{
    using Microsoft.VisualBasic.Activities; 
    using System.Activities.Presentation.Model; 
    using System.Activities.Presentation.Hosting;
    using System.Activities.Presentation.Xaml; 
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Globalization;
    using System.Linq; 
    using System.Reflection;
    using System.Runtime; 
    using System.Windows.Markup; 
    using System.Xaml;
 


    class ParserContext : LocationReferenceEnvironment, IValueSerializerContext, IXamlNameResolver, INamespacePrefixLookup, IXamlNamespaceResolver
    { 
        ModelItem baseModelItem;
        EditingContext context; 
        IDictionary namespaceLookup; 

        public ParserContext() 
        {
        }

        public ParserContext(ModelItem modelItem) 
        {
            this.Initialize(modelItem); 
        } 

        public IContainer Container 
        {
            get { return null; }
        }
 
        public object Instance
        { 
            get; 
            internal set;
        } 

        public PropertyDescriptor PropertyDescriptor
        {
            get; 
            internal set;
        } 
 
        public override Activity Root
        { 
            get { return null; }
        }

        IDictionary NamespaceLookup 
        {
            get 
            { 
                if (this.namespaceLookup == null)
                { 
                    this.namespaceLookup = new Dictionary();
                }
                return this.namespaceLookup;
            } 
        }
 
        public bool Initialize(ModelItem modelItem) 
        {
            this.baseModelItem = modelItem; 
            if (null != modelItem)
            {
                this.context = modelItem.GetEditingContext();
            } 
            return (null != this.baseModelItem);
        } 
 
        public override bool IsVisible(LocationReference reference)
        { 
            object other = this.Resolve(reference.Name);

            return object.ReferenceEquals(other, reference);
        } 

        public override bool TryGetLocationReference(string name, out LocationReference result) 
        { 
            result = (LocationReference)this.Resolve(name);
            return result != null; 
        }


        public string GetNamespace(string prefix) 
        {
            var nameSpace = this.NamespaceLookup. 
                Where(p => string.Equals(p.Value, prefix)). 
                Select(p => p.Key).
                FirstOrDefault(); 

            return nameSpace;
        }
 
        public IEnumerable GetNamespacePrefixes()
        { 
            List namespacePrefixes = new List(); 
            LoadNameSpace(namespacePrefixes);
            return namespacePrefixes; 
        }

        public override IEnumerable GetLocationReferences()
        { 
            List toReturn = new List();
 
            if (baseModelItem != null) 
            {
                //store the reference to the root model item 
                ModelItem rootModelItem = baseModelItem.Root;

                //Get the activityModelItem
                while (baseModelItem != null && !typeof(Activity).IsAssignableFrom(baseModelItem.ItemType)) 
                {
                    baseModelItem = baseModelItem.Parent; 
                } 

                //try to get activity out of model item 
                Activity activity = (null != baseModelItem ? baseModelItem.GetCurrentValue() : null) as Activity;
                if (activity != null)
                {
                    IEnumerable variableCollection = VariableHelper.GetVariableCollection(baseModelItem); 
                    if (null != variableCollection)
                    { 
                        foreach (ModelItem item in variableCollection) 
                        {
                            toReturn.Add((LocationReference)item.GetCurrentValue()); 
                        }
                    }

                    // Now, check if activity contains action handlers which defined that variable 
                    IEnumerable variablesInActionHandlers = VariableHelper.FindActionHandlerVariables(baseModelItem);
                    if (null != variablesInActionHandlers) 
                    { 
                        foreach (ModelItem variableModelItem in variablesInActionHandlers)
                        { 
                            toReturn.Add((LocationReference)variableModelItem.GetCurrentValue());
                        }
                    }
 
                    // Now try all variables in Scope.
                    IEnumerable allVariablesInScope = VariableHelper.FindVariablesInScope(baseModelItem); 
                    if (null != allVariablesInScope) 
                    {
                        foreach (ModelItem variableModelItem in allVariablesInScope) 
                        {
                            toReturn.Add((LocationReference)variableModelItem.GetCurrentValue());
                        }
                    } 
                }
                // As a final search , try too see if the user is trying to bind to the Arguments in 
                // the  custom type designer. 
                if (ActivityBuilderHelper.IsActivityBuilderType(rootModelItem))
                { 
                    foreach (Variable variable in ActivityBuilderHelper.GetVariables(rootModelItem))
                    {
                        toReturn.Add(variable);
                    } 
                }
            } 
            return toReturn; 
        }
 
        public object Resolve(string name)
        {
            Object retVal = null;
            if (baseModelItem != null && retVal == null) 
            {
                //store the reference to the root model item 
                ModelItem rootModelItem = baseModelItem.Root; 

                //Get the activityModelItem 
                while (baseModelItem != null && !typeof(Activity).IsAssignableFrom(baseModelItem.ItemType))
                {
                    baseModelItem = baseModelItem.Parent;
                } 

                //try to get activity out of model item 
                Activity activity = (null != baseModelItem ? baseModelItem.GetCurrentValue() : null) as Activity; 
                if (activity != null)
                { 
                    IEnumerable variableCollection = VariableHelper.GetVariableCollection(baseModelItem);
                    if (null != variableCollection)
                    {
                        retVal = variableCollection. 
                            Where(p => string.Equals(name, p.Properties["Name"].ComputedValue)).
                            Select(p => p.GetCurrentValue()). 
                            FirstOrDefault(); 
                    }
 
                    // Now, check if activity contains action handlers which defined that variable
                    if (retVal == null)
                    {
                        IEnumerable variablesInActionHandlers = VariableHelper.FindActionHandlerVariables(baseModelItem); 
                        foreach (ModelItem variableModelItem in variablesInActionHandlers)
                        { 
                            Variable variable = (Variable)variableModelItem.GetCurrentValue(); 
                            if (variable.Name.Equals(name))
                            { 
                                retVal = variable;
                                break;
                            }
                        } 
                    }
 
                    // Now try all variables in Scope. 
                    if (retVal == null)
                    { 
                        IEnumerable allVariablesInScope = VariableHelper.FindVariablesInScope(baseModelItem);
                        foreach (ModelItem variableModelItem in allVariablesInScope)
                        {
                            Variable variable = (Variable)variableModelItem.GetCurrentValue(); 
                            if (variable != null)
                            { 
                                if (variable.Name.Equals(name)) 
                                {
                                    retVal = variable; 
                                    break;
                                }
                            }
                        } 
                    }
                } 
                // As a final search , try too see if the user is trying to bind to the Arguments in 
                // the  custom type designer.
                if (retVal == null && ActivityBuilderHelper.IsActivityBuilderType(rootModelItem)) 
                {
                    foreach (Variable variable in ActivityBuilderHelper.GetVariables(rootModelItem))
                    {
                        if (variable.Name.Equals(name)) 
                        {
                            retVal = variable; 
                            break; 
                        }
                    } 
                }
            }
            return retVal;
        } 

        public object Resolve(string name, out bool isFullyInitialized) 
        { 
            object result = Resolve(name);
            isFullyInitialized = (result != null); 
            return result;
        }

        public bool IsFixupTokenAvailable 
        {
            get 
            { 

                return false; 
            }
        }

        internal IEnumerable Namespaces 
        {
            get 
            { 
                var namespacesToReturn = new HashSet();
                //combine default import namespaces 
                foreach (var import in VisualBasicSettings.Default.ImportReferences)
                {
                    namespacesToReturn.Add(import.Import);
                } 
                //with custom ones, defined in user provided assemblies
                if (null != this.namespaceLookup) 
                { 
                    foreach (var nameSpace in this.namespaceLookup.Keys)
                    { 
                        //out of full namespace declaration (i.e. "clr-namespace:;assembly="
                        //get clear namespace name
                        int startIndex = nameSpace.IndexOf(":", StringComparison.Ordinal);
                        int endIndex = nameSpace.IndexOf(";", StringComparison.Ordinal); 
                        if (startIndex >= 0 && endIndex >= 0)
                        { 
                            string clrNamespace = nameSpace.Substring(startIndex + 1, endIndex - startIndex - 1); 
                            namespacesToReturn.Add(clrNamespace);
                        } 
                    }
                }

                ImportedNamespaceContextItem importedNamespaces = this.context.Items.GetValue(); 
                namespacesToReturn.UnionWith(importedNamespaces.ImportedNamespaces);
                //return all namespaces 
                return namespacesToReturn; 
            }
        } 

        public object GetFixupToken(IEnumerable names)
        {
            return null; 
        }
 
        public object GetFixupToken(IEnumerable names, bool canAssignDirectly) 
        {
            return null; 
        }

        public object GetService(Type serviceType)
        { 
            if (serviceType == typeof(IXamlNameResolver)
                || serviceType == typeof(INamespacePrefixLookup) 
                || serviceType == typeof(IXamlNamespaceResolver)) 
            {
                return this; 
            }
            else
            {
                return null; 
            }
        } 
 
        public ValueSerializer GetValueSerializerFor(Type type)
        { 
            return null;
        }

        public ValueSerializer GetValueSerializerFor(PropertyDescriptor descriptor) 
        {
            return null; 
        } 

        public string LookupPrefix(string ns) 
        {
            //get reference to namespace lookup dictionary (create one if necessary)
            var lookupTable = this.NamespaceLookup;
            string prefix; 
            //check if given namespace is already registered
            if (!lookupTable.TryGetValue(ns, out prefix)) 
            { 
                //no, create a unique prefix
                prefix = string.Format(CultureInfo.InvariantCulture, "__{0}", Guid.NewGuid().ToString().Replace("-", "").Substring(0, 5)); 
                //and store value in the dictionary
                lookupTable[ns] = prefix;
            }
            //return prefix 
            return prefix;
        } 
 
        public void OnComponentChanged()
        { 
            throw FxTrace.Exception.AsError(new NotSupportedException());
        }

        public bool OnComponentChanging() 
        {
            throw FxTrace.Exception.AsError(new NotSupportedException()); 
        } 

        void LoadNameSpace(List result) 
        {
            if (null == this.context)
            {
                Fx.Assert("EditingContext is null"); 
                return;
            } 
            AssemblyContextControlItem assemblyContext = this.context.Items.GetValue(); 
            if (null == assemblyContext)
            { 
                Fx.Assert("AssemblyContextControlItem not defined in EditingContext.Items");
                return;
            }
            if (null != assemblyContext.LocalAssemblyName) 
            {
                result.Add(GetEntry(assemblyContext.LocalAssemblyName)); 
            } 
            if (null != assemblyContext.ReferencedAssemblyNames)
            { 
                foreach (AssemblyName name in assemblyContext.ReferencedAssemblyNames)
                {
                    result.Add(GetEntry(name));
                } 
            }
        } 
 
        NamespaceDeclaration GetEntry(AssemblyName name)
        { 
            string ns =
                string.Format(CultureInfo.InvariantCulture, "clr-namespace:{0};assembly={1}",
                Guid.NewGuid().ToString().Replace('-', '_'), name.Name);
            return new NamespaceDeclaration(ns, Guid.NewGuid().ToString()); 
        }
 
        IEnumerable> IXamlNameResolver.GetAllNamesAndValuesInScope() 
        {
            return null; 
        }

        event EventHandler IXamlNameResolver.OnNameScopeInitializationComplete
        { 
            add { }
            remove { } 
        } 

    } 


}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK