FileClassifier.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / wpf / src / BuildTasks / Microsoft / Build / Tasks / Windows / FileClassifier.cs / 1305600 / FileClassifier.cs

                            //----------------------------------------------------------------------------
//
// 
//    Copyright (C) Microsoft Corporation.  All rights reserved. 
// 
// 
// Description: An MSBuild task that classify the input files to different 
//              categories based on the input item's attributes.
// 
// Spec:        http://avalon/app/Compilation/Avalon-MSBUILD%20Targets.doc
//
// History:
//  06/20/03: weibz   rewrite and moved over to WCP tree 
//
//--------------------------------------------------------------------------- 
 
using System;
using System.IO; 
using System.Collections.Generic;

using System.Globalization;
using System.Diagnostics; 
using System.Reflection;
using System.Resources; 
using System.Runtime.InteropServices; 

 
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;

using MS.Utility; 
using MS.Internal.Tasks;
 
// Since we disable PreSharp warnings in this file, PreSharp warning is unknown to C# compiler. 
// We first need to disable warnings about unknown message numbers and unknown pragmas.
#pragma warning disable 1634, 1691 

namespace Microsoft.Build.Tasks.Windows
{
    ///  
    /// The File Classification task puts all the input baml files, image files into different
    /// output resource groups, such as Resources for Main assembly and Resources for satellite 
    /// assembly. 
    /// 
    public sealed class FileClassifier : Task 
    {
        //-----------------------------------------------------
        //
        //  Constructors 
        //
        //----------------------------------------------------- 
 
        #region Constructors
 
        /// 
        /// Constrcutor
        /// 
        public FileClassifier() 
            : base(SR.ResourceManager)
        { 
            // set default values for some non-required input items 

            _culture = null; 
        }

        #endregion Constructors
 
        //------------------------------------------------------
        // 
        //  Public Methods 
        //
        //----------------------------------------------------- 

        #region Public Methods

        ///  
        /// ITask Execute method
        ///  
        ///  
        public override bool Execute()
        { 
            bool ret = false;
            var mainEmbeddedList = new List();
            var satelliteEmbeddedList = new List();
            var clrEmbeddedResourceList = new List(); 
            var clrSatelliteEmbeddedResourceList = new List();
 
            try 
            {
                TaskHelper.DisplayLogo(Log, SR.Get(SRID.FileClassifierTask)); 

                ret = VerifyTaskInputs();

                if (ret != false) 
                {
                    // Do the real work to classify input files. 
                    Classify(SourceFiles, mainEmbeddedList, satelliteEmbeddedList); 

                    if (CLRResourceFiles != null) 
                    {
                        // Generate the output CLR embedded resource list.
                        Classify(CLRResourceFiles, clrEmbeddedResourceList, clrSatelliteEmbeddedResourceList);
                    } 

                    // move the arraylist to the TaskItem array. 
                    MainEmbeddedFiles = mainEmbeddedList.ToArray(); 
                    SatelliteEmbeddedFiles = satelliteEmbeddedList.ToArray();
                    CLREmbeddedResource = clrEmbeddedResourceList.ToArray(); 
                    CLRSatelliteEmbeddedResource = clrSatelliteEmbeddedResourceList.ToArray();
                }
            }
            catch (Exception e) 
            {
                // PreSharp Complaint 6500 - do not handle null-ref or SEH exceptions. 
                if (e is NullReferenceException || e is SEHException) 
                {
                    throw; 
                }
                else
                {
                    string message; 
                    string errorId;
 
                    errorId = Log.ExtractMessageCode(e.Message, out message); 

                    if (String.IsNullOrEmpty(errorId)) 
                    {
                        errorId = UnknownErrorID;
                        message = SR.Get(SRID.UnknownBuildError, message);
                    } 

                    Log.LogError(null, errorId, null, null, 0, 0, 0, 0, message, null); 
                } 

                return false; 
            }
#pragma warning disable 6500
            catch // Non-CLS compliant errors
            { 
                Log.LogErrorWithCodeFromResources(SRID.NonClsError);
                return false; 
            } 
#pragma warning restore 6500
 

            return ret;
        }
 
        #endregion Public Methods
 
        //------------------------------------------------------ 
        //
        //  Public Properties 
        //
        //------------------------------------------------------

        #region Public Properties 

        ///  
        /// SourceFiles: List of Items thatare to be classified 
        /// 
        [Required] 
        public ITaskItem [] SourceFiles { get; set; }

        /// 
        /// Can have values (exe, or dll) 
        /// 
        [Required] 
        public string OutputType { get; set; } 

        ///  
        /// Culture of the build. Can be null if the build is non-localizable
        /// 
        public string Culture
        { 
            get { return _culture != null ? _culture.ToLower(CultureInfo.InvariantCulture) : null; }
            set { _culture = value; } 
        } 

 
        /// 
        /// The CLR resource file list.
        /// In Project file, those files will be define by type CLRResource.
        /// such as:   
        /// 
        ///  
        public ITaskItem[] CLRResourceFiles { get; set; } 

        ///  
        /// Output Item list for the CLR resources that will be saved in
        /// the main assembly.
        /// 
        ///  
        [Output]
        public ITaskItem[] CLREmbeddedResource { get; set; } 
 
        /// 
        /// Output Item list for the CLR resources that will be saved in 
        /// the satellite assembly.
        /// 
        /// 
        [Output] 
        public ITaskItem[] CLRSatelliteEmbeddedResource { get; set; }
 
        ///  
        /// MainEmbeddedFiles
        /// 
        /// Non-localizable resources which will be embedded into the Main assembly.
        /// 
        [Output]
        public ITaskItem [] MainEmbeddedFiles { get; set; } 

        ///  
        /// SatelliteEmbeddedFiles 
        ///
        /// Localizable files which are embedded to the Satellite assembly for the 
        /// culture which is set in Culture property..
        /// 
        [Output]
        public ITaskItem [] SatelliteEmbeddedFiles { get; set; } 

        #endregion Public Properties 
 
        //-----------------------------------------------------
        // 
        //  Public Events
        //
        //------------------------------------------------------
 
        //-----------------------------------------------------
        // 
        //  Protected Methods 
        //
        //----------------------------------------------------- 


        //-----------------------------------------------------
        // 
        //  Internal Methods
        // 
        //------------------------------------------------------ 

        //----------------------------------------------------- 
        //
        //  Internal Properties
        //
        //------------------------------------------------------ 

        //------------------------------------------------------ 
        // 
        //  Internal Events
        // 
        //-----------------------------------------------------


        //------------------------------------------------------ 
        //
        //  Private Methods 
        // 
        //-----------------------------------------------------
 
        #region Private Methods

        //
        // Verify all the propety values set from project file. 
        // If any input value is set wrongly, report appropriate build error
        // and return false. 
        // 
        private bool VerifyTaskInputs()
        { 
            bool bValidInput = true;

            //
            // Verify different property values 
            //
            // OutputType is marked as Required, it should never be null or emtpy. 
            // otherwise, the task should have been failed before the code goes here. 

            string targetType = OutputType.ToLowerInvariant( ); 
            switch (targetType)
            {
                case SharedStrings.Library :
                case SharedStrings.Module  : 
                case SharedStrings.WinExe  :
                case SharedStrings.Exe     : 
                    break; 
                default :
                    Log.LogErrorWithCodeFromResources(SRID.TargetIsNotSupported, targetType); 
                    bValidInput = false;
                    break;
            }
 
            // SourceFiles property is marked as Required.
            // MSBUILD Engine should have checked the setting for this property 
            // so don't need to recheck here. 

            if (TaskHelper.IsValidCultureName(Culture) == false) 
            {
                Log.LogErrorWithCodeFromResources(SRID.InvalidCulture, Culture);
                bValidInput = false;
            } 

            return bValidInput; 
        } 

        private void Classify(IEnumerable inputItems, List mainList, List satelliteList) 
        {
            foreach (ITaskItem inputItem in inputItems)
            {
                ITaskItem outputItem = new TaskItem 
                {
                    ItemSpec = inputItem.ItemSpec, 
                }; 

                // Selectively copy metadata over. 
                outputItem.SetMetadata(SharedStrings.Link, inputItem.GetMetadata(SharedStrings.Link));

                if (IsItemLocalizable(inputItem))
                { 
                    satelliteList.Add(outputItem);
                } 
                else 
                {
                    mainList.Add(outputItem); 
                }
            }
        }
 
        // 
        // Check if the item is localizable or not. 
        //  
        // 
        //  
        private bool IsItemLocalizable(ITaskItem fileItem)
        {
            bool isLocalizable = false;
 
            // if the default culture is not set, by default all
            // the items are not localizable. 
 
            if (Culture != null && Culture.Equals("") == false)
            { 
                string localizableString;

                // Default culture is set, by default the item is localizable
                // unless it is set as false in the Localizable attribute. 

                isLocalizable = true; 
 
                localizableString = fileItem.GetMetadata(SharedStrings.Localizable);
 
                if (localizableString != null && String.Compare(localizableString, "false", StringComparison.OrdinalIgnoreCase) ==0 )
                {
                    isLocalizable = false;
                } 
            }
 
            return isLocalizable; 
        }
 
        #endregion Private Methods

        //-----------------------------------------------------
        // 
        //  Private Properties
        // 
        //----------------------------------------------------- 

 
        //------------------------------------------------------
        //
        //  Private Fields
        // 
        //-----------------------------------------------------
 
        #region Private Fields 

        private string _culture; 

        private const string UnknownErrorID = "FC1000";

        #endregion Private Fields 

    } 
} 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
 
//----------------------------------------------------------------------------
//
// 
//    Copyright (C) Microsoft Corporation.  All rights reserved. 
// 
// 
// Description: An MSBuild task that classify the input files to different 
//              categories based on the input item's attributes.
// 
// Spec:        http://avalon/app/Compilation/Avalon-MSBUILD%20Targets.doc
//
// History:
//  06/20/03: weibz   rewrite and moved over to WCP tree 
//
//--------------------------------------------------------------------------- 
 
using System;
using System.IO; 
using System.Collections.Generic;

using System.Globalization;
using System.Diagnostics; 
using System.Reflection;
using System.Resources; 
using System.Runtime.InteropServices; 

 
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;

using MS.Utility; 
using MS.Internal.Tasks;
 
// Since we disable PreSharp warnings in this file, PreSharp warning is unknown to C# compiler. 
// We first need to disable warnings about unknown message numbers and unknown pragmas.
#pragma warning disable 1634, 1691 

namespace Microsoft.Build.Tasks.Windows
{
    ///  
    /// The File Classification task puts all the input baml files, image files into different
    /// output resource groups, such as Resources for Main assembly and Resources for satellite 
    /// assembly. 
    /// 
    public sealed class FileClassifier : Task 
    {
        //-----------------------------------------------------
        //
        //  Constructors 
        //
        //----------------------------------------------------- 
 
        #region Constructors
 
        /// 
        /// Constrcutor
        /// 
        public FileClassifier() 
            : base(SR.ResourceManager)
        { 
            // set default values for some non-required input items 

            _culture = null; 
        }

        #endregion Constructors
 
        //------------------------------------------------------
        // 
        //  Public Methods 
        //
        //----------------------------------------------------- 

        #region Public Methods

        ///  
        /// ITask Execute method
        ///  
        ///  
        public override bool Execute()
        { 
            bool ret = false;
            var mainEmbeddedList = new List();
            var satelliteEmbeddedList = new List();
            var clrEmbeddedResourceList = new List(); 
            var clrSatelliteEmbeddedResourceList = new List();
 
            try 
            {
                TaskHelper.DisplayLogo(Log, SR.Get(SRID.FileClassifierTask)); 

                ret = VerifyTaskInputs();

                if (ret != false) 
                {
                    // Do the real work to classify input files. 
                    Classify(SourceFiles, mainEmbeddedList, satelliteEmbeddedList); 

                    if (CLRResourceFiles != null) 
                    {
                        // Generate the output CLR embedded resource list.
                        Classify(CLRResourceFiles, clrEmbeddedResourceList, clrSatelliteEmbeddedResourceList);
                    } 

                    // move the arraylist to the TaskItem array. 
                    MainEmbeddedFiles = mainEmbeddedList.ToArray(); 
                    SatelliteEmbeddedFiles = satelliteEmbeddedList.ToArray();
                    CLREmbeddedResource = clrEmbeddedResourceList.ToArray(); 
                    CLRSatelliteEmbeddedResource = clrSatelliteEmbeddedResourceList.ToArray();
                }
            }
            catch (Exception e) 
            {
                // PreSharp Complaint 6500 - do not handle null-ref or SEH exceptions. 
                if (e is NullReferenceException || e is SEHException) 
                {
                    throw; 
                }
                else
                {
                    string message; 
                    string errorId;
 
                    errorId = Log.ExtractMessageCode(e.Message, out message); 

                    if (String.IsNullOrEmpty(errorId)) 
                    {
                        errorId = UnknownErrorID;
                        message = SR.Get(SRID.UnknownBuildError, message);
                    } 

                    Log.LogError(null, errorId, null, null, 0, 0, 0, 0, message, null); 
                } 

                return false; 
            }
#pragma warning disable 6500
            catch // Non-CLS compliant errors
            { 
                Log.LogErrorWithCodeFromResources(SRID.NonClsError);
                return false; 
            } 
#pragma warning restore 6500
 

            return ret;
        }
 
        #endregion Public Methods
 
        //------------------------------------------------------ 
        //
        //  Public Properties 
        //
        //------------------------------------------------------

        #region Public Properties 

        ///  
        /// SourceFiles: List of Items thatare to be classified 
        /// 
        [Required] 
        public ITaskItem [] SourceFiles { get; set; }

        /// 
        /// Can have values (exe, or dll) 
        /// 
        [Required] 
        public string OutputType { get; set; } 

        ///  
        /// Culture of the build. Can be null if the build is non-localizable
        /// 
        public string Culture
        { 
            get { return _culture != null ? _culture.ToLower(CultureInfo.InvariantCulture) : null; }
            set { _culture = value; } 
        } 

 
        /// 
        /// The CLR resource file list.
        /// In Project file, those files will be define by type CLRResource.
        /// such as:   
        /// 
        ///  
        public ITaskItem[] CLRResourceFiles { get; set; } 

        ///  
        /// Output Item list for the CLR resources that will be saved in
        /// the main assembly.
        /// 
        ///  
        [Output]
        public ITaskItem[] CLREmbeddedResource { get; set; } 
 
        /// 
        /// Output Item list for the CLR resources that will be saved in 
        /// the satellite assembly.
        /// 
        /// 
        [Output] 
        public ITaskItem[] CLRSatelliteEmbeddedResource { get; set; }
 
        ///  
        /// MainEmbeddedFiles
        /// 
        /// Non-localizable resources which will be embedded into the Main assembly.
        /// 
        [Output]
        public ITaskItem [] MainEmbeddedFiles { get; set; } 

        ///  
        /// SatelliteEmbeddedFiles 
        ///
        /// Localizable files which are embedded to the Satellite assembly for the 
        /// culture which is set in Culture property..
        /// 
        [Output]
        public ITaskItem [] SatelliteEmbeddedFiles { get; set; } 

        #endregion Public Properties 
 
        //-----------------------------------------------------
        // 
        //  Public Events
        //
        //------------------------------------------------------
 
        //-----------------------------------------------------
        // 
        //  Protected Methods 
        //
        //----------------------------------------------------- 


        //-----------------------------------------------------
        // 
        //  Internal Methods
        // 
        //------------------------------------------------------ 

        //----------------------------------------------------- 
        //
        //  Internal Properties
        //
        //------------------------------------------------------ 

        //------------------------------------------------------ 
        // 
        //  Internal Events
        // 
        //-----------------------------------------------------


        //------------------------------------------------------ 
        //
        //  Private Methods 
        // 
        //-----------------------------------------------------
 
        #region Private Methods

        //
        // Verify all the propety values set from project file. 
        // If any input value is set wrongly, report appropriate build error
        // and return false. 
        // 
        private bool VerifyTaskInputs()
        { 
            bool bValidInput = true;

            //
            // Verify different property values 
            //
            // OutputType is marked as Required, it should never be null or emtpy. 
            // otherwise, the task should have been failed before the code goes here. 

            string targetType = OutputType.ToLowerInvariant( ); 
            switch (targetType)
            {
                case SharedStrings.Library :
                case SharedStrings.Module  : 
                case SharedStrings.WinExe  :
                case SharedStrings.Exe     : 
                    break; 
                default :
                    Log.LogErrorWithCodeFromResources(SRID.TargetIsNotSupported, targetType); 
                    bValidInput = false;
                    break;
            }
 
            // SourceFiles property is marked as Required.
            // MSBUILD Engine should have checked the setting for this property 
            // so don't need to recheck here. 

            if (TaskHelper.IsValidCultureName(Culture) == false) 
            {
                Log.LogErrorWithCodeFromResources(SRID.InvalidCulture, Culture);
                bValidInput = false;
            } 

            return bValidInput; 
        } 

        private void Classify(IEnumerable inputItems, List mainList, List satelliteList) 
        {
            foreach (ITaskItem inputItem in inputItems)
            {
                ITaskItem outputItem = new TaskItem 
                {
                    ItemSpec = inputItem.ItemSpec, 
                }; 

                // Selectively copy metadata over. 
                outputItem.SetMetadata(SharedStrings.Link, inputItem.GetMetadata(SharedStrings.Link));

                if (IsItemLocalizable(inputItem))
                { 
                    satelliteList.Add(outputItem);
                } 
                else 
                {
                    mainList.Add(outputItem); 
                }
            }
        }
 
        // 
        // Check if the item is localizable or not. 
        //  
        // 
        //  
        private bool IsItemLocalizable(ITaskItem fileItem)
        {
            bool isLocalizable = false;
 
            // if the default culture is not set, by default all
            // the items are not localizable. 
 
            if (Culture != null && Culture.Equals("") == false)
            { 
                string localizableString;

                // Default culture is set, by default the item is localizable
                // unless it is set as false in the Localizable attribute. 

                isLocalizable = true; 
 
                localizableString = fileItem.GetMetadata(SharedStrings.Localizable);
 
                if (localizableString != null && String.Compare(localizableString, "false", StringComparison.OrdinalIgnoreCase) ==0 )
                {
                    isLocalizable = false;
                } 
            }
 
            return isLocalizable; 
        }
 
        #endregion Private Methods

        //-----------------------------------------------------
        // 
        //  Private Properties
        // 
        //----------------------------------------------------- 

 
        //------------------------------------------------------
        //
        //  Private Fields
        // 
        //-----------------------------------------------------
 
        #region Private Fields 

        private string _culture; 

        private const string UnknownErrorID = "FC1000";

        #endregion Private Fields 

    } 
} 

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

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