XAMLParseException.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / Orcas / NetFXw7 / wpf / src / Framework / System / Windows / Markup / XAMLParseException.cs / 1 / XAMLParseException.cs

                            //---------------------------------------------------------------------------- 
//
// File: XamlParseException.cs
//
// Description: 
//   Parser exceptions
// 
// 
// History:
//    6/06/01:    rogerg        Created 
//    5/28/03:    peterost      Ported to wcp
//
// Copyright (C) 2003 by Microsoft Corporation.  All rights reserved.
// 
//---------------------------------------------------------------------------
 
using System; 
using System.Xml;
using System.IO; 
using System.Collections;
using System.Diagnostics;
using System.Reflection;
using System.Text; 
using System.Globalization;
using System.ComponentModel; 
using System.Security; 
using System.Runtime.Serialization;
using System.Security.Permissions; 
using MS.Utility;
using MS.Internal;

#if PBTCOMPILER 
namespace MS.Internal.Markup
#else 
 
using System.Windows;
using System.Windows.Threading; 


namespace System.Windows.Markup
#endif 
{
    ///Exception class for parser specific exceptions 
    [Serializable] 
#if PBTCOMPILER
    internal class XamlParseException : SystemException 
#else
    public class XamlParseException : SystemException
#endif
    { 

        #region Public 
 
        #region Constructors
 
        ///
        /// Constructor
        ///
        public XamlParseException() : base () 
        {
        } 
 
        ///
        /// Constructor 
        ///
        ///
        /// Exception message
        /// 
        public XamlParseException(string message) : base (message)
        { 
        } 

        /// 
        /// Constructor
        ///
        ///Exception message
        ///exception occured 
        public XamlParseException(string message, Exception innerException) : base(message, innerException)
        { 
        } 

 
        ///
        /// Constructor
        ///
        /// 
        /// Exception message
        /// 
        /// 
        /// lineNumber the exception occured at
        /// 
        ///
        /// LinePosition the Exception occured at.
        ///
        ///  
        public XamlParseException(string message,int lineNumber, int linePosition) : this (message)
        { 
            _lineNumber = lineNumber; 
            _linePosition = linePosition;
        } 


        ///
        /// Constructor 
        ///
        /// 
        /// Exception message 
        ///
        /// 
        /// lineNumber the exception occured at
        ///
        ///
        /// LinePosition the Exception occured at. 
        ///
        /// 
        /// original Exception that was thrown. 
        ///
        public XamlParseException(string message,int lineNumber, int linePosition,Exception innerException) 
                        : this(message, innerException)
        {
            _lineNumber = lineNumber;
            _linePosition = linePosition; 
        }
 
        #endregion Constructors 

        #region Properties 

        ///
        /// LineNumber that the exception occured on.
        /// 
        public int LineNumber
        { 
            get { return _lineNumber; } 
        }
 

        ///
        /// LinePosition that the exception occured on.
        /// 
        public int LinePosition
        { 
            get { return _linePosition; } 
        }
 
        ///
        /// If this is set, it indicates that the Xaml exception occurred
        /// in the context of a dictionary item, and this was the Xaml Key
        /// value of that item. 
        ///
 
        public object KeyContext 
        {
#if PBTCOMPILER 
            set { _keyContext= value; }
#else
            get { return _keyContext; }
            internal set { _keyContext= value; } 
#endif
        } 
 
        ///
        /// If this is set, it indicates that the Xaml exception occurred 
        /// in the context of an object with a Xaml Uid set, and this was the
        /// value of that Uid.
        ///
        public string UidContext 
        {
#if PBTCOMPILER 
            set { _uidContext = value; } 
#else
            get { return _uidContext; } 
            internal set { _uidContext = value; }
#endif
        }
 
        ///
        /// If this is set, it indicates that the Xaml exception occurred 
        /// in the context of an object with a Xaml Name set, and this was the 
        /// value of that name.
        /// 
        public string NameContext
        {
#if PBTCOMPILER
            set { _nameContext = value; } 
#else
            get { return _nameContext; } 
            internal set { _nameContext = value; } 
#endif
 
        }

        ///
        /// The BaseUri in effect at the point of the exception. 
        ///
        public Uri BaseUri 
        { 
#if PBTCOMPILER
            set { _baseUri = value; } 
#else
            get { return _baseUri; }
            internal set { _baseUri = value; }
#endif 
        }
 
        #endregion Properties 

        #endregion Public 

        #region Private

        #region Serialization 

        ///  
        /// Internal constructor used for serialization when marshalling an 
        /// exception of this type across and AppDomain or machine boundary.
        ///  
        /// 
        /// Contains all the information needed to serialize or deserialize
        /// the object.
        ///  
        /// 
        /// Describes the source and destination of a given serialized stream, 
        /// as well as a means for serialization to retain that context and an 
        /// additional caller-defined context.
        ///  
        //CASRemoval:[SecurityPermissionAttribute(SecurityAction.Demand,SerializationFormatter=true)]
        protected  XamlParseException(
            SerializationInfo info,
            StreamingContext context 
            )
            : base(info, context) 
        { 
            _lineNumber = info.GetInt32("Line");
            _linePosition = info.GetInt32("Position"); 
        }

        /// 
        /// Populates a SerializationInfo with the data needed to serialize the target object. 
        /// 
        ///  
        /// The SerializationInfo to populate with data. 
        /// 
        ///  
        /// The destination for this serialization.
        /// 
        ///
        /// 
        ///     Critical: calls Exception.GetObjectData which LinkDemands
        ///     TreatAsSafe: We make the corresponding demand here, to ensure that it becomes a full 
        ///         demand. 
        ///
        [SecurityCritical, SecurityTreatAsSafe] 

#if ! PBTCOMPILER
        [SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.SerializationFormatter)]
#else 
        [SecurityPermissionAttribute(SecurityAction.Demand,SerializationFormatter=true)]
#endif 
        public override void GetObjectData(SerializationInfo info, StreamingContext context) 
        {
            base.GetObjectData(info, context); 
            info.AddValue("Line", (Int32)_lineNumber);
            info.AddValue("Position", (Int32)_linePosition);
        }
 
        #endregion Serialization
 
        #region internal helper methods 

 
#if ! PBTCOMPILER
        //
        // Return the relative file path for current markup stream or file.
        // If the stream comes from assembly resource with .baml extension, this method 
        // still reports .xaml.
        // The purpose of this method is to help developer to debug a failed baml stream. 
        // 
        internal static string GetMarkupFilePath(Uri resourceUri)
        { 
            string bamlFilePath = string.Empty;
            string xamlFilePath = string.Empty;

            if (resourceUri != null) 
            {
                if (resourceUri.IsAbsoluteUri) 
                { 
                    bamlFilePath = resourceUri.GetComponents(UriComponents.Path, UriFormat.Unescaped);
                } 
                else
                {
                    bamlFilePath = resourceUri.OriginalString;
                } 

                // Replace the .baml with .xaml file extension. 
                xamlFilePath = bamlFilePath.Replace(BamlExt, XamlExt); 

                // Confirm the result has a .xaml file extension. 
                if (-1 == xamlFilePath.LastIndexOf(XamlExt, StringComparison.Ordinal))
                    xamlFilePath = string.Empty;
            }
 
            return xamlFilePath;
        } 
 
        //
        // Get context information at the point of the exception, such as 
        // names and Uids.
        //
        internal static void GetObjectContext(
                                        BamlRecordReader bamlRecordReader, 
                                        XamlObjectIds currentXamlObjectIds,
                                        XamlObjectIds contextXamlObjectIds, 
                                        out Type objectType) 
        {
            object parentObject = null; 

            GetObjectContext( bamlRecordReader, currentXamlObjectIds, contextXamlObjectIds, out objectType,
                              out parentObject );
        } 

        internal static void GetObjectContext( 
                                        BamlRecordReader bamlRecordReader, 
                                        out object parentObject )
        { 
            XamlObjectIds currentXamlObjectIds = new XamlObjectIds();
            Type objectType;
            GetObjectContext( bamlRecordReader, currentXamlObjectIds, null, out objectType, out parentObject );
        } 

        internal static void GetObjectContext( 
                                        BamlRecordReader bamlRecordReader, 
                                        XamlObjectIds currentXamlObjectIds,
                                        XamlObjectIds contextXamlObjectIds, 
                                        out Type objectType,
                                        out object parentObject)
        {
            ParserStack contextStack; 
            ReaderContextStackData readerContextStackData;
            parentObject = null; 
 
            objectType = null;
 
            // We get context from the reader, so there's nothing to do if we don't
            // have one.

            if (bamlRecordReader == null) 
            {
                return; 
            } 

            contextStack = bamlRecordReader.ContextStack; 
            if( contextStack.Count == 0 )
            {
                return;
            } 

            // Walk the reader's context stack and look for context information 
            // (x:Name, x:Key, and x:Uid). 

            bool currentObjectElementFound = (currentXamlObjectIds == null); // (See below for usage and comment) 
            bool parentObjectElementFound = false;

            for( int i = contextStack.Count-1; i >= 0; i-- )
            { 
                readerContextStackData = contextStack[i] as ReaderContextStackData;
 
                // First, see if we can fill in the currentXamlObjectIds. 
                //
                // The context stack data has entries for both objects and properties 
                // E.g. if we're trying to parse "Foo" under 

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