Click here to Skip to main content
15,886,518 members
Articles / Programming Languages / XML

XML Schema Reader Writer Plugin for VS 2005/2008

Rate me:
Please Sign up or sign in to vote.
4.43/5 (5 votes)
17 Apr 2009CPOL3 min read 32.9K   716   11  
Schema based XML reader writer implemented as .NET COM generator
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using EnvDTE;

namespace XmlSchemaAddon
{
    /// <summary>
    /// Wraps IDE related code
    /// </summary>
    internal class IDECodeLibrary
    {
        /// <summary>
        /// How the file related to the build and deployment process.
        /// </summary>
        internal enum BuildAction
        {
            None,
            Compile,
            Content,
            EmbeddedResource
        }

        /// <summary>
        /// Project items's property
        /// </summary>
        internal enum ProjectItemProperty
        {
            /// <summary>
            /// The build action of a project item.
            /// </summary>
            BuildAction,

            /// <summary>
            /// The item's full path.
            /// </summary>
            FullPath

        }

        private static List<FileInfo> GetProjectFiles(ProjectItem item, string filter)
        {
            List<FileInfo> list = new List<FileInfo>();
            if (item == null) return list;
            
            for (short i = 0; i < item.FileCount; ++i)
            {
                string fileName = item.get_FileNames(i);
                if (!File.Exists(fileName)) continue;

                if (string.IsNullOrEmpty(filter))
                    list.Add(new FileInfo(fileName));
                else if (Path.GetExtension(fileName) == filter)
                    list.Add(new FileInfo(fileName));
            }

            foreach (ProjectItem subItem in item.ProjectItems)
            {
                if ( subItem != null )
                   list.AddRange( GetProjectFiles(subItem, filter ));                 
            }

            return list;
        }

        /// <summary>
        /// Returns list of all the files in the project
        /// </summary>
        /// <param name="proj">Project to query</param>
        /// <param name="filter">Filter extension such as ".cs, .xsd, .cpp"</param>
        /// <returns>list of all the files in the project</returns>
        internal static List<FileInfo> GetProjectFiles(Project proj, string filter)
        {
            List<FileInfo> list = new List<FileInfo>();
            if( proj == null  ) return list;

            foreach (ProjectItem item in proj.ProjectItems)
            {
                if (proj.ProjectItems.Count > 0)
                   list.AddRange( GetProjectFiles(item, filter) );               
              
            }

            return list;
        }

        /// <summary>
        /// Extracts the default namespace of a project 
        /// </summary>
        /// <param name="proj">the target project</param>
        /// <returns>default namespace of a project or null</returns>
        internal static string GetProjectDefaultNamespace(Project proj)
        {
            foreach (Property prop in proj.Properties)
            {
                if (string.Compare(prop.Name, "DefaultNamespace", true) == 0)
                    return prop.Value.ToString();
            }

            return null;
        }


        /// <summary>
        /// Locates and returns a folder in the project
        /// or creates one if not existed
        /// </summary>
        /// <param name="proj">The project to extract the folder from</param>
        /// <param name="folderName">The name of the folder</param>
        /// <returns>"XmlSchemaObjects" folder</returns>
        internal static ProjectItem GetProjectFolder(Project proj, string folderName)
        {
            foreach (ProjectItem item in proj.ProjectItems)
            {
                if (item.Kind != EnvDTE.Constants.vsProjectItemKindPhysicalFolder && 
                    item.Kind != EnvDTE.Constants.vsProjectItemKindVirtualFolder )
                    continue;

                if ( string.Compare(item.Name,folderName, true) == 0)
                    return item;
            }

            string dirPath = Path.GetDirectoryName(proj.FullName) + Path.DirectorySeparatorChar + folderName;
            if (Directory.Exists(dirPath))
                Directory.Delete(dirPath, true);

            return proj.ProjectItems.AddFolder(folderName, EnvDTE.Constants.vsProjectItemKindPhysicalFolder);
            
        }


        /// <summary>
        /// Moves a file into a project physical folder, and set it as the folder's sub item.
        /// </summary>
        /// <param name="file">The source file to move</param>
        /// <param name="project">The project</param>
        /// <param name="folderName">The name of the folder to move the file to</param>
        /// <returns>The created sub item in the project represent the file.</returns>
        /// <remarks>
        ///  If the folder don't exist, one will be created.
        /// </remarks>
        internal static ProjectItem MoveFileToProjectFolder(FileInfo file,string folderName, EnvDTE.Project project)
        {
            EnvDTE.ProjectItem projFolder = IDECodeLibrary.GetProjectFolder(project, folderName);

            string projFolderPath = Path.GetDirectoryName(project.FullName) +
                                            Path.DirectorySeparatorChar + projFolder.Name;

            if (File.Exists(projFolderPath + Path.DirectorySeparatorChar + file.Name))
                File.Delete(projFolderPath + Path.DirectorySeparatorChar + file.Name);

            file.MoveTo(projFolderPath + Path.DirectorySeparatorChar + file.Name);
            return projFolder.ProjectItems.AddFromFile(file.FullName);
           
        }

        /// <summary>
        /// Sets the build action of the given item
        /// </summary>
        /// <param name="item">Project item (for example a file)</param>
        /// <param name="action">The build action</param>
        internal static void SetItemBuildAction( ProjectItem item, BuildAction action)
        {
            SetProjectItemProperty(item, ProjectItemProperty.BuildAction, (int)action);
        }


        /// <summary>
        /// Returns the value of project item property
        /// </summary>
        /// <param name="item">The item that the property is associated with</param>
        /// <param name="propType">The property type</param>
        /// <returns>The value of the property or null if it wasn't found.</returns>
        internal static object GetProjectItemProperty( ProjectItem item, ProjectItemProperty propType)
        {
            foreach( EnvDTE.Property prop in item.Properties)
            {
                if (string.Compare(prop.Name, propType.ToString(), true) == 0)
                    return prop.Value;
            }

            return null;
        }

        /// <summary>
        /// Sets the value of project item property
        /// </summary>
        /// <param name="item">The item that the property is associated with</param>
        /// <param name="propType">The property type</param>
        /// <param name="value">The new value to set</param>
        /// <returns><value>true</value> for success</returns>
        internal static bool SetProjectItemProperty(ProjectItem item, ProjectItemProperty propType, object value)
        {
            foreach (EnvDTE.Property prop in item.Properties)
            {
                if (string.Compare(prop.Name, propType.ToString(), true) == 0)
                {
                    prop.Value = value;
                    return true;
                }
            }

            return false;
        }


    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader
Israel Israel
Born and raised in Israel, caught the programming virus at the age of 15.
Since than I can't stop coding.

Comments and Discussions