Example #1
0
        // Push context data onto the reader context stack 
        internal void PushContext( 
            ReaderFlags contextFlags,
            object      contextData, 
            Type        expectedType,
            short       expectedTypeId,
            bool        createUsingTypeConverter)
        { 
            ReaderContextStackData d;
 
            lock(_stackDataFactoryCache) 
            {
                if (_stackDataFactoryCache.Count == 0) 
                {
                    d = new ReaderContextStackData();
                }
                else 
                {
                    // Get StackData from the factory cache 
                    d = _stackDataFactoryCache[_stackDataFactoryCache.Count-1]; 
                    _stackDataFactoryCache.RemoveAt(_stackDataFactoryCache.Count-1);
                } 
            }

            d.ContextFlags = contextFlags;
            d.ObjectData = contextData; 
            d.ExpectedType = expectedType;
            d.ExpectedTypeId = expectedTypeId; 
            d.CreateUsingTypeConverter = createUsingTypeConverter; 
            ReaderContextStack.Push(d);
            ParserContext.PushScope(); 

            // If the context data is an object that implements INameScope, make it
            // the new Name scope for registering Names
 
            INameScope nameScope = NameScope.NameScopeFromObject(contextData);
 
            if (nameScope != null) 
            {
                ParserContext.NameScopeStack.Push(nameScope); 
            }

        }
Example #2
0
        // Modify the passed flags to set the NeedToAddToTree flag based on the current 
        // context.  This is needed by subparsers in some cases.
        internal static void CheckForTreeAdd(ref ReaderFlags flags, ReaderContextStackData context) 
        {
            // An element doesn't need to be added to a tree if in the context of constructor
            // paramters or deferred content
 
            if (context == null ||
                (context.ContextType != ReaderFlags.ConstructorParams && 
                 context.ContextType != ReaderFlags.RealizeDeferContent)) 
            {
                flags |= ReaderFlags.NeedToAddToTree; 
            }
        }
Example #3
0
        // Sets the "key" arg as the key on "context".  "parentContext" is the context
        // one level above "context".  Verification is done to make sure that the 
        // "parentContext" is a valid Dictionary.  An error is thrown if it is not.
        private void SetKeyOnContext( 
            object key, 
            string attributeName,
            ReaderContextStackData context, 
            ReaderContextStackData parentContext)
        {
            try
            { 
                // make sure parent is a dictionary
                GetDictionaryFromContext(parentContext, true /*toInsert*/); 
            } 
            catch (XamlParseException e)
            { 
                // rethrow with a better error message
                if (parentContext.CheckFlag(ReaderFlags.CollectionHolder))
                {
                    BamlCollectionHolder holder = (BamlCollectionHolder)parentContext.ObjectData; 
                    object element = context.ObjectData;
 
                    if (element != null && element == holder.Dictionary) 
                    {
                        ThrowExceptionWithLine(SR.Get(SRID.ParserKeyOnExplicitDictionary, attributeName, 
                                       element.GetType().ToString(), holder.PropertyDefinition.Name), e);
                    }
                }
 
                ThrowExceptionWithLine(SR.Get(SRID.ParserNoMatchingIDictionary, attributeName), e);
            } 
 
            // set key on context
            context.Key = key; 
        }
Example #4
0
        private object GetObjectDataFromContext(ReaderContextStackData context) 
        {
            if (null == context.ObjectData && 
                null != context.ExpectedType)
            {
                Debug.Assert(!context.CreateUsingTypeConverter,
                    "We had expected to use a TypeConverter for this " + context.ExpectedType.FullName + 
                    " but somebody is trying to create one now without using a TypeConverter.  If TypeConverter is the correct way, fix the code calling this method.  If not, fix the 'should we use a TypeConverter?' logic in XamlReaderHelper.");
 
                context.ObjectData = CreateInstanceFromType(context.ExpectedType, 
                                                 context.ExpectedTypeId, true);
                if (null == context.ObjectData) 
                {
                    ThrowException(SRID.ParserCantCreateInstanceType, context.ExpectedType.FullName);
                }
 
                // Finish set up of the newly created element.
                context.ExpectedType = null; // Don't want to receive any other values 
                ElementInitialize(context.ObjectData, null /*unknown name*/); 
            }
            return context.ObjectData; 
        }
Example #5
0
        private IAddChild GetIAddChildFromContext(ReaderContextStackData context)
        { 
            IAddChild result = null;

            if (context != null)
            { 
                if (context.CheckFlag(ReaderFlags.IAddChild))
                { 
                    result = BamlRecordManager.AsIAddChild(context.ObjectData); 
                }
                else if (context.ContextType == ReaderFlags.PropertyIAddChild) 
                {
                    BamlCollectionHolder holder = GetCollectionHolderFromContext(context, false /*toInsert*/);

                    result = BamlRecordManager.AsIAddChild(holder.Collection); 
                }
            } 
 
            return result;
        } 
Example #6
0
        private ArrayExtension GetArrayExtensionFromContext(ReaderContextStackData context)
        {
            ArrayExtension result = null; 

            if (context != null) 
            { 
                result = context.ObjectData as ArrayExtension;
 
                if (context.CheckFlag(ReaderFlags.ArrayExt))
                {
                    result = (ArrayExtension)context.ObjectData;
                } 
                else if (context.ContextType == ReaderFlags.PropertyArray)
                { 
                    BamlCollectionHolder holder = GetCollectionHolderFromContext(context, true /*toInsert*/); 

                    result = holder.ArrayExt; 
                }
            }

            return result; 
        }
Example #7
0
        private IList GetListFromContext(ReaderContextStackData context)
        { 
            IList result = null; 

            if (context != null) 
            {
                if (context.CheckFlag(ReaderFlags.IList))
                {
                    result = (IList)GetObjectDataFromContext(context); 
                }
                else if (context.ContextType == ReaderFlags.PropertyIList) 
                { 
                    BamlCollectionHolder holder = GetCollectionHolderFromContext(context, true /*toInsert*/);
 
                    result = holder.List;
                }
            }
 
            return result;
        } 
Example #8
0
        protected IDictionary GetDictionaryFromContext(ReaderContextStackData context, bool toInsert)
        {
            IDictionary result = null; 

            if (context != null) 
            { 
                if (context.CheckFlag(ReaderFlags.IDictionary))
                { 
                    result = (IDictionary)GetObjectDataFromContext(context);
                }
                else if (context.ContextType == ReaderFlags.PropertyIDictionary)
                { 
                    BamlCollectionHolder holder = GetCollectionHolderFromContext(context, toInsert);
 
                    result = holder.Dictionary; 
                }
            } 

            return result;
        }
Example #9
0
        private BamlCollectionHolder GetCollectionHolderFromContext(ReaderContextStackData context, bool toInsert)
        { 
            BamlCollectionHolder holder = (BamlCollectionHolder)context.ObjectData;

            // If we don't have a collection yet, then create one now.
            if (holder.Collection == null && toInsert) 
            {
                // if this collection holder has not yet been used, then initialize its collection 
                InitPropertyCollection(holder, context); 
            }
 
            if (toInsert && holder.IsClosed)
            {
                // if an explicit collection was under a collection property and following its end tag an element
                // was placed, then throw an exception. 
                ThrowException(SRID.ParserPropertyCollectionClosed, holder.PropertyDefinition.Name);
            } 
 
            return holder;
        } 
Example #10
0
        // takes a BamlCollectionHolder and its context and sets the value of the holder's collection to be
        // either the default collection instantiated by the holder's constructor or a newly created
        // collection created based on the expected type. 
        // *** Used when we do not have an explicit tag.
        private void InitPropertyCollection(BamlCollectionHolder holder, ReaderContextStackData context) 
        { 
            // this method should only be called to initialize the collection
            Debug.Assert (holder.Collection == null); 

            if (context.ContextType == ReaderFlags.PropertyArray)
            {
                // arrays are a little different than other collections, because we wrap them in an array extension. 
                // Here we create an array extension and assign the element type based on the property.
 
                ArrayExtension arrayExt = new ArrayExtension(); 
                arrayExt.Type = context.ExpectedType.GetElementType();
                holder.Collection = arrayExt; 
            }
            else if (holder.DefaultCollection != null)
            {
                // if we the property getter returned a default value, then we use that collection to insert 
                // as the property's collection.
 
                holder.Collection = holder.DefaultCollection; 
            }
            else 
            {
                ThrowException(SRID.ParserNullPropertyCollection, holder.PropertyDefinition.Name);
            }
 
            context.ExpectedType = null; // Don't want to receive any other values
        } 
Example #11
0
        private void SetCollectionPropertyValue(ReaderContextStackData context) 
        { 
            BamlCollectionHolder holder = (BamlCollectionHolder)context.ObjectData;
 
            if (holder.Collection == null)
            {
                // If we don't have a collection yet, then create one now.  This can
                // happen if the IDictionary property is read/write, but does not 
                // contain a value and there was no IDictionary object under the property
 
                InitPropertyCollection(holder, context); 
            }
 
            if (!holder.ReadOnly && holder.Collection != holder.DefaultCollection)
            {
                // If we had the default collection is not the same as the collection in the holder,
                // then either we have a RW property with a null default value or a RW property with an 
                // explicit element value.  In either case, set the property's value to be the collection
 
                holder.SetPropertyValue(); 
            }
        }