Manager.cs :  » Profilers » Prof-It » at » jku » ssw » ProfIt » Import » VS2003 » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Profilers » Prof It 
Prof It » at » jku » ssw » ProfIt » Import » VS2003 » Manager.cs
/*----------------------------------------------------------------------
Prof-It for C#
Copyright (c) 2004 Klaus Lehner, University of Linz

This program is free software; you can redistribute it and/or modify it 
under the terms of the GNU General Public License as published by the 
Free Software Foundation; either version 2, or (at your option) any 
later version.

This program is distributed in the hope that it will be useful, but 
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License 
for more details.

You should have received a copy of the GNU General Public License along 
with this program; if not, write to the Free Software Foundation, Inc., 
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
----------------------------------------------------------------------*/

using System;
using System.Collections;
using System.Windows.Forms;
using System.Xml;
using System.Xml.XPath;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;

using at.jku.ssw.ProfIt.Components;

namespace at.jku.ssw.ProfIt.Import.VS2003{


  /// <summary>
  /// This class is responsible for importing projects from Visual Studio .NET
  /// There may be some problems with huge VS.NET projects, this module is 
  /// only at a beta-version
  /// </summary>
  public class ImportManager {

    private static Hashtable foundReferences = new Hashtable();

    private static Solution ReadSolution(string fileName) {
      Solution solution = new Solution(FileUtility.ToShortFileName(fileName));
      solution.RootPath = FileUtility.GetPath(fileName);
      StreamReader sr           = File.OpenText(fileName);
      Regex projectLinePattern  = new Regex("Project\\(.*\\)\\s+=\\s+\"(?<Title>.*)\",\\s*\"(?<Location>.*)\",", RegexOptions.Compiled);
      //ArrayList projects = new ArrayList();
      while (true) {
        string line = sr.ReadLine();
        if (line == null) {
          break;
        }
        Match match = projectLinePattern.Match(line);
        if (match.Success) {
          Project project = new Project(match.Result("${Title}"),solution);
          string location = match.Result("${Location}");
          project.ExternalFileName = solution.RootPath + "\\" + location; 
          if (location.LastIndexOf("\\") > 0) 
            project.SetRelativeDirectory(location.Substring(0, location.LastIndexOf("\\"))); 
          else project.SetRelativeDirectory("");
          if (project.ExternalFileName.EndsWith("csproj"))
            solution.AddProject(project);
        }
      }
      sr.Close();
      return solution;
    }

    public static Solution ImportSolution(string fileName) {
      Solution solution = ReadSolution(fileName);
      Errors.count = 0;

      if (Errors.count == 0) {
        foreach (Project project in solution.Projects) {
          ImportProject(project);
          if (project.OutputType == OutputType.WinExe) solution.StartProject = project;
        }
      }
      if (Errors.count == 0) {
        foreach (Project project in solution.Projects) {
          IncludeReferences(solution, project);
        }
      }

      if (Errors.count == 0) {
        return solution;
      } else {
        return null;
      }
    }

    private static void ImportProject(Project project) {
      try {
        XmlDocument doc = new XmlDocument();
        doc.Load(project.ExternalFileName);
        XPathNavigator nav = doc.CreateNavigator();
        XPathNodeIterator iterator = nav.Select("/VisualStudioProject/CSHARP/Files/Include/File");
        while (iterator.MoveNext()) {
          SourceFile file = new SourceFile(project, iterator.Current.GetAttribute("RelPath", ""));
          switch (iterator.Current.GetAttribute("BuildAction", "")) {
            case "Compile": file.BuildAction = BuildAction.Compile; break;
            case "Content": file.BuildAction = BuildAction.Content; break;
            case "EmbeddedResource": file.BuildAction = BuildAction.EmbeddedResource; break;
            default: file.BuildAction = BuildAction.Content; break;
          }
          
          project.AddFile(file);
        }

        XPathNavigator nav3 = doc.CreateNavigator();
        XPathNodeIterator iterator3 = nav3.Select("/VisualStudioProject/CSHARP");
        while (iterator3.MoveNext()) {
          project.ExternalId = iterator3.Current.GetAttribute("ProjectGuid","");
        }

        iterator = nav.Select("/VisualStudioProject/CSHARP/Build/Settings");
        while (iterator.MoveNext()) {
          switch (iterator.Current.GetAttribute("OutputType", "")) {
            case "Library": project.OutputType = OutputType.Library; break;
            case "WinExe": project.OutputType = OutputType.WinExe; break;
            default: project.OutputType = OutputType.WinExe; break;
          }

        }
      } catch (Exception e){
        Errors.Error(-1,-1, "Error in parsing " + project.ExternalFileName + " due to: " +e.ToString());
      }
    }
    private static void IncludeReferences(Solution solution, Project project) {
      try {
        XmlDocument doc = new XmlDocument();
        doc.Load(project.ExternalFileName);
        XPathNavigator nav = doc.CreateNavigator();
        XPathNodeIterator iterator = nav.Select("/VisualStudioProject/CSHARP/Build/References/Reference");
        while (iterator.MoveNext()) {
          IReference reference = null;
          string hintPath = iterator.Current.GetAttribute("HintPath", "");

          // synchronize the hintpath of the project file with the root path of the project
          // e.g.: hintPath:      ..\..\System.dll
          //       rootPath:      c:\projects\test\
          //       assemblyPath:  c:\System.dll
          
          
          if (hintPath != null && hintPath != "") {
            // remove the last slash
            if (hintPath.StartsWith("..")) {
              string rootPath = project.RootPath.Remove(project.RootPath.Length-1, 1);
              hintPath = hintPath.Replace("/", "\\");
              while (hintPath.StartsWith("..\\")) {
                hintPath = hintPath.Substring(3);
                rootPath = rootPath.Substring(0, rootPath.LastIndexOf("\\"));
              }
              reference = new AssemblyReference(rootPath + "\\" + hintPath);
            }
            else reference = new AssemblyReference(hintPath); /* absolute path */
          } else {
            string projectId = iterator.Current.GetAttribute("Project", "");
            foreach (Project p in solution.Projects) {
              if (p.ExternalId == projectId) {
                reference = new ProjectReference(p);
                break;
              }
            }
            if (reference == null) {
              Errors.Error(-1,-1, "Could not find project with guid " + projectId);
              continue;
            }
          }


          if (!reference.Exists && reference is AssemblyReference) {
            if (foundReferences[reference.FileName] != null) {
              project.AddReference(new AssemblyReference((string)foundReferences[reference.FileName]));
            } else {
              ReferenceCheckerDialog dialog = new ReferenceCheckerDialog();
              dialog.OldFileName = reference.FileName;
              switch (dialog.ShowDialog()) {
                case DialogResult.OK: 
                  project.AddReference(new AssemblyReference(dialog.NewFileName)); 
                  foundReferences.Add(dialog.OldFileName, dialog.NewFileName);
                  break;
                case DialogResult.Ignore: project.AddReference(reference); break;
                case DialogResult.Abort: break;
              }
            }
          } else {
            project.AddReference(reference);
          }
        }
      } catch (Exception e) {
        e.ToString();
        //Errors.Error(-1,-1, "Error in parsing references in " + project.ExternalFileName + " due to " +e.ToString());
      }
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.