XamlStackWriter.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / NetFx40 / XamlBuildTask / Microsoft / Build / Tasks / Xaml / XamlStackWriter.cs / 1305376 / XamlStackWriter.cs

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

namespace Microsoft.Build.Tasks.Xaml 
{
    using System; 
    using System.Collections.Generic; 
    using System.Diagnostics;
    using System.Linq; 
    using System.Text;
    using System.Xaml;
    using System.Runtime;
 
    internal enum XamlStackFrameType
    { 
        None, 
        Object,
        GetObject, 
        Member
    }

    internal struct XamlStackFrame 
    {
        private static object s_setSentinel = new object(); 
 
        private object _data;
        private object _isSet; 

        public XamlStackFrameType FrameType { get; private set; }

        public XamlType Type 
        {
            get { return FrameType == XamlStackFrameType.Object ? (XamlType)_data : null; } 
        } 

        public XamlMember Member 
        {
            get { return FrameType == XamlStackFrameType.Member ? (XamlMember)_data : null; }
        }
 
        public bool IsSet()
        { 
            return _isSet != null; 
        }
 
        public bool IsSet(XamlMember member)
        {
            HashSet setMembers = _isSet as HashSet;
            return (setMembers == null) ? false : setMembers.Contains(member); 
        }
 
        internal void Set() 
        {
            if(FrameType != XamlStackFrameType.Member) 
            {
                throw FxTrace.Exception.AsError(new InvalidOperationException(SR.UnexpectedXaml));
            }
            _isSet = s_setSentinel; 
        }
 
        internal void Set(XamlMember member) 
        {
            if(FrameType != XamlStackFrameType.Object && FrameType != XamlStackFrameType.GetObject) 
            {
                throw FxTrace.Exception.AsError(new InvalidOperationException(SR.UnexpectedXaml));
            }
            HashSet setMembers = _isSet as HashSet; 
            if (setMembers == null)
            { 
                setMembers = new HashSet(); 
                _isSet = setMembers;
            } 
            if (!setMembers.Contains(member))
            {
                setMembers.Add(member);
            } 
        }
 
        internal static XamlStackFrame ForObject(XamlType type) 
        {
            XamlStackFrame result = new XamlStackFrame(); 
            result._data = type;
            result.FrameType = XamlStackFrameType.Object;
            return result;
        } 

        internal static XamlStackFrame ForGetObject() 
        { 
            XamlStackFrame result = new XamlStackFrame();
            result.FrameType = XamlStackFrameType.GetObject; 
            return result;
        }

        internal static XamlStackFrame ForMember(XamlMember member) 
        {
            XamlStackFrame result = new XamlStackFrame(); 
            result._data = member; 
            result.FrameType = XamlStackFrameType.Member;
            return result; 
        }
    }

 
    internal class XamlStackWriter : XamlWriter
    { 
        List _stack = new List(); 

        // the stack writer does not care about schema context 
        public override XamlSchemaContext SchemaContext { get { return null; } }

        public int Depth { get { return _stack.Count; } }
 
        public XamlStackFrame TopFrame
        { 
            get 
            {
                Fx.Assert(_stack.Count != 0, "Stack cannot be empty"); 
                return _stack[_stack.Count - 1];
            }
        }
 
        public XamlStackFrame FrameAtDepth(int ndx)
        { 
            return _stack[ndx - 1]; 
        }
 

        public override void WriteEndMember()
        {
            if (TopFrame.FrameType != XamlStackFrameType.Member) 
            {
                throw FxTrace.Exception.AsError(new InvalidOperationException(SR.UnexpectedXaml)); 
            } 
            PopStack();
        } 

        public override void WriteEndObject()
        {
            if (TopFrame.FrameType != XamlStackFrameType.Object && 
                TopFrame.FrameType != XamlStackFrameType.GetObject)
            { 
                throw FxTrace.Exception.AsError(new InvalidOperationException(SR.UnexpectedXaml)); 
            }
            PopStack(); 
        }

        public override void WriteGetObject()
        { 
            SetTopFrame();
            _stack.Add(XamlStackFrame.ForGetObject()); 
        } 

        public override void WriteNamespace(NamespaceDeclaration namespaceDeclaration) 
        {
            throw FxTrace.Exception.AsError(new NotImplementedException());
        }
 
        public override void WriteStartMember(XamlMember property)
        { 
            if (property == null) 
            {
                throw FxTrace.Exception.AsError(new InvalidOperationException(SR.UnexpectedXamlValueNull("property"))); 
            }
            SetTopFrame(property);
            _stack.Add(XamlStackFrame.ForMember(property));
        } 

        public override void WriteStartObject(XamlType type) 
        { 
            if (type == null)
            { 
                throw FxTrace.Exception.AsError(new InvalidOperationException(SR.UnexpectedXamlValueNull("type")));
            }
            SetTopFrame();
            _stack.Add(XamlStackFrame.ForObject(type)); 
        }
 
        public override void WriteValue(object value) 
        {
            SetTopFrame(); 
        }

        private void PopStack()
        { 
            _stack.RemoveAt(_stack.Count - 1);
        } 
 
        private void SetTopFrame()
        { 
            if (Depth > 0 && TopFrame.FrameType == XamlStackFrameType.Member)
            {
                TopFrame.Set();
            } 
        }
 
        private void SetTopFrame(XamlMember member) 
        {
            if (Depth > 0 && 
                (TopFrame.FrameType == XamlStackFrameType.GetObject ||
                 TopFrame.FrameType == XamlStackFrameType.Object))
            {
                TopFrame.Set(member); 
            }
        } 
    } 
}

// 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