PartialClassGenerationTaskInternal.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 / XamlBuildTask / Microsoft / Build / Tasks / Xaml / PartialClassGenerationTaskInternal.cs / 1606072 / PartialClassGenerationTaskInternal.cs

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

namespace Microsoft.Build.Tasks.Xaml 
{
 
    using System; 
    using System.CodeDom;
    using System.CodeDom.Compiler; 
    using System.Collections.Generic;
    using System.IO;
    using System.Runtime;
    using System.Xaml; 
    using System.Xaml.Schema;
    using System.Xml; 
    using System.Reflection; 
    using System.Globalization;
 
    internal class PartialClassGenerationTaskInternal : MarshalByRefObject, IExceptionLogger
    {
        const string UnknownExceptionErrorCode = "XC1000";
 
        IList applicationMarkup;
        IList generatedResources; 
        IList generatedCodeFiles; 
        IList references;
        IList sourceCodeFiles; 
        HashSet sourceFilePaths;
        IList logData;
        IList assemblyNames;
        XamlSchemaContext schemaContext; 
        HashSet markupFileNames;
 
        public IList ApplicationMarkup 
        {
            get 
            {
                if (this.applicationMarkup == null)
                {
                    this.applicationMarkup = new List(); 
                }
                return this.applicationMarkup; 
            } 
            set
            { 
                this.applicationMarkup = value;
            }
        }
 
        public string AssemblyName
        { get; set; } 
 
        public IList GeneratedResources
        { 
            get
            {
                if (this.generatedResources == null)
                { 
                    this.generatedResources = new List();
                } 
                return this.generatedResources; 
            }
        } 

        public IList GeneratedCodeFiles
        {
            get 
            {
                if (this.generatedCodeFiles == null) 
                { 
                    this.generatedCodeFiles = new List();
                } 
                return generatedCodeFiles;
            }
        }
 
        public string GeneratedSourceExtension
        { get; set; } 
 
        public string Language
        { get; set; } 

        public IList LogData
        {
            get 
            {
                if (this.logData == null) 
                { 
                    this.logData = new List();
                } 
                return this.logData;
            }
        }
 
        public string OutputPath
        { get; set; } 
 
        public IList References
        { 
            get
            {
                if (this.references == null)
                { 
                    this.references = new List();
                } 
                return this.references; 
            }
            set 
            {
                this.references = value;
            }
        } 

        public IList LoadedAssemblyList 
        { 
            get
            { 
                if (this.assemblyNames == null)
                {
                    if (IsInProcessXamlMarkupCompile)
                    { 
                        this.assemblyNames = cachedAssemblyList;
                    } 
                    else 
                    {
                        this.assemblyNames = new List(); 
                    }
                }
                return this.assemblyNames;
            } 
            set
            { 
                this.assemblyNames = value; 
                if (IsInProcessXamlMarkupCompile)
                { 
                    cachedAssemblyList = value;
                }
            }
        } 
        private static IList cachedAssemblyList = null;
 
        public bool IsInProcessXamlMarkupCompile 
        { get; set; }
 
        public string RootNamespace
        { get; set; }

        public IList SourceCodeFiles 
        {
            get 
            { 
                if (this.sourceCodeFiles == null)
                { 
                    this.sourceCodeFiles = new List();
                }
                return this.sourceCodeFiles;
            } 
            set
            { 
                this.sourceCodeFiles = value; 
            }
        } 

        public bool RequiresCompilationPass2
        { get; set; }
 
        HashSet SourceFilePaths
        { 
            get 
            {
                if (sourceFilePaths == null) 
                {
                    sourceFilePaths = new HashSet();
                    if (SourceCodeFiles != null)
                    { 
                        foreach (string sourceCodeFile in SourceCodeFiles)
                        { 
                            sourceFilePaths.Add(sourceCodeFile); 
                        }
                    } 
                }
                return sourceFilePaths;
            }
            set 
            {
                this.sourceFilePaths = value; 
            } 
        }
 
        public XamlSchemaContext SchemaContext
        {
            get
            { 
                if (schemaContext == null)
                { 
                    if (LoadedAssemblyList.Count > 0) 
                    {
                        schemaContext = new XamlSchemaContext(LoadedAssemblyList); 
                    }
                    else
                    {
                        schemaContext = new XamlSchemaContext(); 
                    }
                } 
                return schemaContext; 
            }
        } 

        public string HelperClassFullName
        { get; set; }
 
        public bool Execute()
        { 
            try 
            {
                if (this.ApplicationMarkup == null || this.ApplicationMarkup.Count == 0) 
                {
                    return true;
                }
                if (!CodeDomProvider.IsDefinedLanguage(this.Language)) 
                {
                    throw FxTrace.Exception.Argument("Language", SR.UnrecognizedLanguage(this.Language)); 
                } 

                AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve += new ResolveEventHandler(XamlBuildTaskServices.ReflectionOnlyAssemblyResolve); 
                bool retVal = true;
                // We load the assemblies for the real builds
                // For intellisense builds, we load them the first time only
                if (!IsInProcessXamlMarkupCompile || this.LoadedAssemblyList == null) 
                {
                    if (this.References != null) 
                    { 
                        try
                        { 
                            this.LoadedAssemblyList = XamlBuildTaskServices.Load(this.References);
                        }
                        catch (FileNotFoundException e)
                        { 
                            LogException(e, e.FileName);
                            retVal = false; 
                        } 
                    }
                } 

                CodeDomProvider codeDomProvider = CodeDomProvider.CreateProvider(this.Language);

                ProcessHelperClassGeneration(codeDomProvider); 
                foreach (string app in ApplicationMarkup)
                { 
                    try 
                    {
                        retVal &= ProcessMarkupItem(app, codeDomProvider); 
                    }
                    catch (FileLoadException e)
                    {
                        if (Fx.IsFatal(e)) 
                        {
                            throw; 
                        } 

                        // Temporary fix for CSDMain 114201. 
                        // We need to reset the XamlSchemaContext if we fail to resolve an assembly. That way we can get
                        // the correct error message for every file in ApplicationMarkup list of Xaml files.
                        ResetSchemaContext();
 
                        LogException(new InvalidOperationException(SR.AssemblyCannotBeResolved(XamlBuildTaskServices.FileNotLoaded)), app);
                        retVal = false; 
                    } 
                    catch (Exception e)
                    { 
                        if (Fx.IsFatal(e))
                        {
                            throw;
                        } 
                        LogException(e, app);
                        retVal = false; 
                    } 
                }
 
                return retVal;
            }
            catch (Exception e)
            { 
                if (Fx.IsFatal(e))
                { 
                    throw; 
                }
 
                // Log unknown errors that do not originate from the task.
                // Assumes that all known errors are logged when the exception is thrown.
                if (!(e is LoggedException))
                { 
                    LogException(e);
                } 
                return false; 
            }
        } 

        void ResetSchemaContext()
        {
            this.schemaContext = null; 
        }
 
        void ProcessHelperClassGeneration(CodeDomProvider codeDomProvider) 
        {
            string codeFileName = "_" + this.AssemblyName + GetGeneratedSourceExtension(codeDomProvider); 
            codeFileName = Path.Combine(this.OutputPath, codeFileName);


            string namespaceName = "XamlStaticHelperNamespace"; 
            string className = "_XamlStaticHelper";
 
            // Generate code file 
            CodeCompileUnit codeUnit = new ClassGenerator(this, codeDomProvider).GenerateHelperClass(namespaceName, className, this.LoadedAssemblyList);
            WriteCode(codeDomProvider, codeUnit, codeFileName); 
            this.GeneratedCodeFiles.Add(codeFileName);

            this.HelperClassFullName = namespaceName + "." + className;
        } 

        bool ProcessMarkupItem(string markupItem, CodeDomProvider codeDomProvider) 
        { 
            XamlBuildTaskServices.PopulateModifiers(codeDomProvider);
 
            XamlNodeList xamlNodes = ReadXamlNodes(markupItem);
            if (xamlNodes == null)
            {
                return false; 
            }
 
            ClassData classData = ReadClassData(xamlNodes, markupItem); 

            string outputFileName = GetFileName(markupItem); 
            string codeFileName = Path.ChangeExtension(outputFileName, GetGeneratedSourceExtension(codeDomProvider));
            string markupFileName = Path.ChangeExtension(outputFileName, GeneratedSourceExtension + XamlBuildTaskServices.XamlExtension);
            classData.MarkupFileName = Path.GetFileName(markupFileName);
            classData.HelperClassFullName = this.HelperClassFullName; 

            // Check if code file with partial class exists 
            classData.SourceFileExists = UserProvidedFileExists(markupItem, codeDomProvider); 

            // Generate code file 
            CodeCompileUnit codeUnit = new ClassGenerator(this, codeDomProvider).Generate(classData);
            WriteCode(codeDomProvider, codeUnit, codeFileName);
            this.GeneratedCodeFiles.Add(codeFileName);
 
            // Generate resource file
            if (!string.IsNullOrEmpty(this.AssemblyName)) 
            { 
                // Generate xaml "implementation" file
                XmlWriterSettings xmlSettings = new XmlWriterSettings { Indent = true, IndentChars = "  ", CloseOutput = true }; 
                using (XmlWriter xmlWriter = XmlWriter.Create(File.Open(markupFileName, FileMode.Create), xmlSettings))
                {
                    XamlXmlWriterSettings xamlSettings = new XamlXmlWriterSettings() { CloseOutput = true };
                    using (XamlReader reader = classData.StrippedXaml.GetReader()) 
                    {
                        using (XamlXmlWriter xamlWriter = new XamlXmlWriter(xmlWriter, reader.SchemaContext, xamlSettings)) 
                        { 
                            XamlServices.Transform(reader, xamlWriter);
                        } 
                    }
                }
                this.GeneratedResources.Add(markupFileName);
            } 

            if (classData.RequiresCompilationPass2) 
            { 
                this.RequiresCompilationPass2 = true;
            } 
            else
            {
                if (!ValidateXaml(xamlNodes, markupItem))
                { 
                    this.RequiresCompilationPass2 = true;
                } 
            } 
            return true;
        } 

        string GetFileName(string markupItem)
        {
            if (markupFileNames == null) 
            {
                markupFileNames = new HashSet(); 
            } 

            string originalMarkupItemName = Path.GetFileNameWithoutExtension(markupItem); 
            string markupItemName = originalMarkupItemName;
            int i = 0;
            while (this.markupFileNames.Contains(markupItemName))
            { 
                markupItemName = originalMarkupItemName + "." + (++i).ToString(CultureInfo.InvariantCulture);
            } 
            this.markupFileNames.Add(markupItemName); 
            markupItemName = markupItemName + Path.GetExtension(markupItem);
            if (this.OutputPath == null) 
            {
                throw FxTrace.Exception.AsError(
                        this.LogException(
                        new InvalidOperationException(SR.OutputPathCannotBeNull))); 
            }
            return Path.Combine(this.OutputPath, markupItemName); 
        } 

        XamlNodeList ReadXamlNodes(string xamlFileName) 
        {
            XamlNodeList nodeList = new XamlNodeList(this.SchemaContext);

            try 
            {
                XamlXmlReaderSettings settings = new XamlXmlReaderSettings 
                { 
                    AllowProtectedMembersOnRoot = true,
                    ProvideLineInfo = true 
                };

                using (StreamReader streamReader = new StreamReader(xamlFileName))
                { 
                    XamlReader reader = new XamlXmlReader(XmlReader.Create(streamReader), this.SchemaContext, settings);
                    XamlServices.Transform(reader, nodeList.Writer); 
                } 
            }
            catch (XmlException e) 
            {
                LogException(e, xamlFileName, e.LineNumber, e.LinePosition);
                return null;
            } 
            catch (XamlException e)
            { 
                LogException(e, xamlFileName, e.LineNumber, e.LinePosition); 
                return null;
            } 

            if (nodeList.Count > 0)
            {
                return nodeList; 
            }
            else 
            { 
                return null;
            } 
        }

        ClassData ReadClassData(XamlNodeList xamlNodes, string xamlFileName)
        { 
            ClassImporter importer = new ClassImporter(this, xamlFileName, this.AssemblyName, this.Language.Equals("VB") ? this.RootNamespace : null);
            ClassData classData = importer.ReadFromXaml(xamlNodes); 
            return classData; 
        }
 
        bool ValidateXaml(XamlNodeList xamlNodeList, string xamlFileName)
        {
            using(XamlReader xamlReader = xamlNodeList.GetReader())
            { 
                IList validationErrors = null;
                ClassValidator validator = new ClassValidator(this, xamlFileName, null, null); 
                return validator.ValidateXaml(xamlReader, true, this.AssemblyName, out validationErrors); 
            }
        } 

        void WriteCode(CodeDomProvider provider, CodeCompileUnit codeUnit, string fileName)
        {
            using (StreamWriter fileStream = new StreamWriter(fileName)) 
            {
                using (IndentedTextWriter tw = new IndentedTextWriter(fileStream)) 
                { 
                    provider.GenerateCodeFromCompileUnit(codeUnit, tw, new CodeGeneratorOptions());
                } 
            }
        }

        string GetGeneratedSourceExtension(CodeDomProvider codeDomProvider) 
        {
            string result = null; 
            if (!string.IsNullOrEmpty(this.GeneratedSourceExtension)) 
            {
                result = this.GeneratedSourceExtension; 
                if (!result.StartsWith(".", StringComparison.Ordinal))
                {
                    result = "." + result;
                } 
            }
            return result + "." + codeDomProvider.FileExtension; 
        } 

        bool UserProvidedFileExists(string markupItemPath, CodeDomProvider codeDomProvider) 
        {
            string desiredSourceFilePath = Path.ChangeExtension(markupItemPath, "xaml." + codeDomProvider.FileExtension);
            return SourceFilePaths.Contains(desiredSourceFilePath);
        } 

        public Exception LogException(Exception exception, string fileName) 
        { 
            return LogException(exception, fileName, 0, 0);
        } 

        public Exception LogException(Exception exception, string fileName, int lineNumber, int linePosition)
        {
            this.LogData.Add(new LogData() 
                {
                    Message = exception.Message, 
                    FileName = fileName, 
                    LineNumber = lineNumber,
                    LinePosition = linePosition 
                });
            return new LoggedException(exception);
        }
 
        public Exception LogException(Exception exception)
        { 
            this.LogData.Add(new LogData() 
                {
                    Message = exception.Message, 
                    LineNumber = 0,
                    LinePosition = 0
                });
            return new LoggedException(exception); 
        }
    } 
} 

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

namespace Microsoft.Build.Tasks.Xaml 
{
 
    using System; 
    using System.CodeDom;
    using System.CodeDom.Compiler; 
    using System.Collections.Generic;
    using System.IO;
    using System.Runtime;
    using System.Xaml; 
    using System.Xaml.Schema;
    using System.Xml; 
    using System.Reflection; 
    using System.Globalization;
 
    internal class PartialClassGenerationTaskInternal : MarshalByRefObject, IExceptionLogger
    {
        const string UnknownExceptionErrorCode = "XC1000";
 
        IList applicationMarkup;
        IList generatedResources; 
        IList generatedCodeFiles; 
        IList references;
        IList sourceCodeFiles; 
        HashSet sourceFilePaths;
        IList logData;
        IList assemblyNames;
        XamlSchemaContext schemaContext; 
        HashSet markupFileNames;
 
        public IList ApplicationMarkup 
        {
            get 
            {
                if (this.applicationMarkup == null)
                {
                    this.applicationMarkup = new List(); 
                }
                return this.applicationMarkup; 
            } 
            set
            { 
                this.applicationMarkup = value;
            }
        }
 
        public string AssemblyName
        { get; set; } 
 
        public IList GeneratedResources
        { 
            get
            {
                if (this.generatedResources == null)
                { 
                    this.generatedResources = new List();
                } 
                return this.generatedResources; 
            }
        } 

        public IList GeneratedCodeFiles
        {
            get 
            {
                if (this.generatedCodeFiles == null) 
                { 
                    this.generatedCodeFiles = new List();
                } 
                return generatedCodeFiles;
            }
        }
 
        public string GeneratedSourceExtension
        { get; set; } 
 
        public string Language
        { get; set; } 

        public IList LogData
        {
            get 
            {
                if (this.logData == null) 
                { 
                    this.logData = new List();
                } 
                return this.logData;
            }
        }
 
        public string OutputPath
        { get; set; } 
 
        public IList References
        { 
            get
            {
                if (this.references == null)
                { 
                    this.references = new List();
                } 
                return this.references; 
            }
            set 
            {
                this.references = value;
            }
        } 

        public IList LoadedAssemblyList 
        { 
            get
            { 
                if (this.assemblyNames == null)
                {
                    if (IsInProcessXamlMarkupCompile)
                    { 
                        this.assemblyNames = cachedAssemblyList;
                    } 
                    else 
                    {
                        this.assemblyNames = new List(); 
                    }
                }
                return this.assemblyNames;
            } 
            set
            { 
                this.assemblyNames = value; 
                if (IsInProcessXamlMarkupCompile)
                { 
                    cachedAssemblyList = value;
                }
            }
        } 
        private static IList cachedAssemblyList = null;
 
        public bool IsInProcessXamlMarkupCompile 
        { get; set; }
 
        public string RootNamespace
        { get; set; }

        public IList SourceCodeFiles 
        {
            get 
            { 
                if (this.sourceCodeFiles == null)
                { 
                    this.sourceCodeFiles = new List();
                }
                return this.sourceCodeFiles;
            } 
            set
            { 
                this.sourceCodeFiles = value; 
            }
        } 

        public bool RequiresCompilationPass2
        { get; set; }
 
        HashSet SourceFilePaths
        { 
            get 
            {
                if (sourceFilePaths == null) 
                {
                    sourceFilePaths = new HashSet();
                    if (SourceCodeFiles != null)
                    { 
                        foreach (string sourceCodeFile in SourceCodeFiles)
                        { 
                            sourceFilePaths.Add(sourceCodeFile); 
                        }
                    } 
                }
                return sourceFilePaths;
            }
            set 
            {
                this.sourceFilePaths = value; 
            } 
        }
 
        public XamlSchemaContext SchemaContext
        {
            get
            { 
                if (schemaContext == null)
                { 
                    if (LoadedAssemblyList.Count > 0) 
                    {
                        schemaContext = new XamlSchemaContext(LoadedAssemblyList); 
                    }
                    else
                    {
                        schemaContext = new XamlSchemaContext(); 
                    }
                } 
                return schemaContext; 
            }
        } 

        public string HelperClassFullName
        { get; set; }
 
        public bool Execute()
        { 
            try 
            {
                if (this.ApplicationMarkup == null || this.ApplicationMarkup.Count == 0) 
                {
                    return true;
                }
                if (!CodeDomProvider.IsDefinedLanguage(this.Language)) 
                {
                    throw FxTrace.Exception.Argument("Language", SR.UnrecognizedLanguage(this.Language)); 
                } 

                AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve += new ResolveEventHandler(XamlBuildTaskServices.ReflectionOnlyAssemblyResolve); 
                bool retVal = true;
                // We load the assemblies for the real builds
                // For intellisense builds, we load them the first time only
                if (!IsInProcessXamlMarkupCompile || this.LoadedAssemblyList == null) 
                {
                    if (this.References != null) 
                    { 
                        try
                        { 
                            this.LoadedAssemblyList = XamlBuildTaskServices.Load(this.References);
                        }
                        catch (FileNotFoundException e)
                        { 
                            LogException(e, e.FileName);
                            retVal = false; 
                        } 
                    }
                } 

                CodeDomProvider codeDomProvider = CodeDomProvider.CreateProvider(this.Language);

                ProcessHelperClassGeneration(codeDomProvider); 
                foreach (string app in ApplicationMarkup)
                { 
                    try 
                    {
                        retVal &= ProcessMarkupItem(app, codeDomProvider); 
                    }
                    catch (FileLoadException e)
                    {
                        if (Fx.IsFatal(e)) 
                        {
                            throw; 
                        } 

                        // Temporary fix for CSDMain 114201. 
                        // We need to reset the XamlSchemaContext if we fail to resolve an assembly. That way we can get
                        // the correct error message for every file in ApplicationMarkup list of Xaml files.
                        ResetSchemaContext();
 
                        LogException(new InvalidOperationException(SR.AssemblyCannotBeResolved(XamlBuildTaskServices.FileNotLoaded)), app);
                        retVal = false; 
                    } 
                    catch (Exception e)
                    { 
                        if (Fx.IsFatal(e))
                        {
                            throw;
                        } 
                        LogException(e, app);
                        retVal = false; 
                    } 
                }
 
                return retVal;
            }
            catch (Exception e)
            { 
                if (Fx.IsFatal(e))
                { 
                    throw; 
                }
 
                // Log unknown errors that do not originate from the task.
                // Assumes that all known errors are logged when the exception is thrown.
                if (!(e is LoggedException))
                { 
                    LogException(e);
                } 
                return false; 
            }
        } 

        void ResetSchemaContext()
        {
            this.schemaContext = null; 
        }
 
        void ProcessHelperClassGeneration(CodeDomProvider codeDomProvider) 
        {
            string codeFileName = "_" + this.AssemblyName + GetGeneratedSourceExtension(codeDomProvider); 
            codeFileName = Path.Combine(this.OutputPath, codeFileName);


            string namespaceName = "XamlStaticHelperNamespace"; 
            string className = "_XamlStaticHelper";
 
            // Generate code file 
            CodeCompileUnit codeUnit = new ClassGenerator(this, codeDomProvider).GenerateHelperClass(namespaceName, className, this.LoadedAssemblyList);
            WriteCode(codeDomProvider, codeUnit, codeFileName); 
            this.GeneratedCodeFiles.Add(codeFileName);

            this.HelperClassFullName = namespaceName + "." + className;
        } 

        bool ProcessMarkupItem(string markupItem, CodeDomProvider codeDomProvider) 
        { 
            XamlBuildTaskServices.PopulateModifiers(codeDomProvider);
 
            XamlNodeList xamlNodes = ReadXamlNodes(markupItem);
            if (xamlNodes == null)
            {
                return false; 
            }
 
            ClassData classData = ReadClassData(xamlNodes, markupItem); 

            string outputFileName = GetFileName(markupItem); 
            string codeFileName = Path.ChangeExtension(outputFileName, GetGeneratedSourceExtension(codeDomProvider));
            string markupFileName = Path.ChangeExtension(outputFileName, GeneratedSourceExtension + XamlBuildTaskServices.XamlExtension);
            classData.MarkupFileName = Path.GetFileName(markupFileName);
            classData.HelperClassFullName = this.HelperClassFullName; 

            // Check if code file with partial class exists 
            classData.SourceFileExists = UserProvidedFileExists(markupItem, codeDomProvider); 

            // Generate code file 
            CodeCompileUnit codeUnit = new ClassGenerator(this, codeDomProvider).Generate(classData);
            WriteCode(codeDomProvider, codeUnit, codeFileName);
            this.GeneratedCodeFiles.Add(codeFileName);
 
            // Generate resource file
            if (!string.IsNullOrEmpty(this.AssemblyName)) 
            { 
                // Generate xaml "implementation" file
                XmlWriterSettings xmlSettings = new XmlWriterSettings { Indent = true, IndentChars = "  ", CloseOutput = true }; 
                using (XmlWriter xmlWriter = XmlWriter.Create(File.Open(markupFileName, FileMode.Create), xmlSettings))
                {
                    XamlXmlWriterSettings xamlSettings = new XamlXmlWriterSettings() { CloseOutput = true };
                    using (XamlReader reader = classData.StrippedXaml.GetReader()) 
                    {
                        using (XamlXmlWriter xamlWriter = new XamlXmlWriter(xmlWriter, reader.SchemaContext, xamlSettings)) 
                        { 
                            XamlServices.Transform(reader, xamlWriter);
                        } 
                    }
                }
                this.GeneratedResources.Add(markupFileName);
            } 

            if (classData.RequiresCompilationPass2) 
            { 
                this.RequiresCompilationPass2 = true;
            } 
            else
            {
                if (!ValidateXaml(xamlNodes, markupItem))
                { 
                    this.RequiresCompilationPass2 = true;
                } 
            } 
            return true;
        } 

        string GetFileName(string markupItem)
        {
            if (markupFileNames == null) 
            {
                markupFileNames = new HashSet(); 
            } 

            string originalMarkupItemName = Path.GetFileNameWithoutExtension(markupItem); 
            string markupItemName = originalMarkupItemName;
            int i = 0;
            while (this.markupFileNames.Contains(markupItemName))
            { 
                markupItemName = originalMarkupItemName + "." + (++i).ToString(CultureInfo.InvariantCulture);
            } 
            this.markupFileNames.Add(markupItemName); 
            markupItemName = markupItemName + Path.GetExtension(markupItem);
            if (this.OutputPath == null) 
            {
                throw FxTrace.Exception.AsError(
                        this.LogException(
                        new InvalidOperationException(SR.OutputPathCannotBeNull))); 
            }
            return Path.Combine(this.OutputPath, markupItemName); 
        } 

        XamlNodeList ReadXamlNodes(string xamlFileName) 
        {
            XamlNodeList nodeList = new XamlNodeList(this.SchemaContext);

            try 
            {
                XamlXmlReaderSettings settings = new XamlXmlReaderSettings 
                { 
                    AllowProtectedMembersOnRoot = true,
                    ProvideLineInfo = true 
                };

                using (StreamReader streamReader = new StreamReader(xamlFileName))
                { 
                    XamlReader reader = new XamlXmlReader(XmlReader.Create(streamReader), this.SchemaContext, settings);
                    XamlServices.Transform(reader, nodeList.Writer); 
                } 
            }
            catch (XmlException e) 
            {
                LogException(e, xamlFileName, e.LineNumber, e.LinePosition);
                return null;
            } 
            catch (XamlException e)
            { 
                LogException(e, xamlFileName, e.LineNumber, e.LinePosition); 
                return null;
            } 

            if (nodeList.Count > 0)
            {
                return nodeList; 
            }
            else 
            { 
                return null;
            } 
        }

        ClassData ReadClassData(XamlNodeList xamlNodes, string xamlFileName)
        { 
            ClassImporter importer = new ClassImporter(this, xamlFileName, this.AssemblyName, this.Language.Equals("VB") ? this.RootNamespace : null);
            ClassData classData = importer.ReadFromXaml(xamlNodes); 
            return classData; 
        }
 
        bool ValidateXaml(XamlNodeList xamlNodeList, string xamlFileName)
        {
            using(XamlReader xamlReader = xamlNodeList.GetReader())
            { 
                IList validationErrors = null;
                ClassValidator validator = new ClassValidator(this, xamlFileName, null, null); 
                return validator.ValidateXaml(xamlReader, true, this.AssemblyName, out validationErrors); 
            }
        } 

        void WriteCode(CodeDomProvider provider, CodeCompileUnit codeUnit, string fileName)
        {
            using (StreamWriter fileStream = new StreamWriter(fileName)) 
            {
                using (IndentedTextWriter tw = new IndentedTextWriter(fileStream)) 
                { 
                    provider.GenerateCodeFromCompileUnit(codeUnit, tw, new CodeGeneratorOptions());
                } 
            }
        }

        string GetGeneratedSourceExtension(CodeDomProvider codeDomProvider) 
        {
            string result = null; 
            if (!string.IsNullOrEmpty(this.GeneratedSourceExtension)) 
            {
                result = this.GeneratedSourceExtension; 
                if (!result.StartsWith(".", StringComparison.Ordinal))
                {
                    result = "." + result;
                } 
            }
            return result + "." + codeDomProvider.FileExtension; 
        } 

        bool UserProvidedFileExists(string markupItemPath, CodeDomProvider codeDomProvider) 
        {
            string desiredSourceFilePath = Path.ChangeExtension(markupItemPath, "xaml." + codeDomProvider.FileExtension);
            return SourceFilePaths.Contains(desiredSourceFilePath);
        } 

        public Exception LogException(Exception exception, string fileName) 
        { 
            return LogException(exception, fileName, 0, 0);
        } 

        public Exception LogException(Exception exception, string fileName, int lineNumber, int linePosition)
        {
            this.LogData.Add(new LogData() 
                {
                    Message = exception.Message, 
                    FileName = fileName, 
                    LineNumber = lineNumber,
                    LinePosition = linePosition 
                });
            return new LoggedException(exception);
        }
 
        public Exception LogException(Exception exception)
        { 
            this.LogData.Add(new LogData() 
                {
                    Message = exception.Message, 
                    LineNumber = 0,
                    LinePosition = 0
                });
            return new LoggedException(exception); 
        }
    } 
} 

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