Merge pull request #1398 from jwestfall69/dgv-first_row_index
[mono.git] / mcs / class / Microsoft.Build / Microsoft.Build.Evaluation / ProjectItem.cs
index b5e09e24a87f687e38433e89f39cab6c33b657c5..cc187f9b94c0b8e5f1f26d67bad8af188f55e205 100644 (file)
@@ -3,8 +3,11 @@
 //
 // Author:
 //   Leszek Ciesielski (skolima@gmail.com)
+//   Rolf Bjarne Kvinge (rolf@xamarin.com)
+//   Atsushi Enomoto (atsushi@xamarin.com)
 //
 // (C) 2011 Leszek Ciesielski
+// Copyright (C) 2011,2013 Xamarin Inc.
 //
 // Permission is hereby granted, free of charge, to any person obtaining
 // a copy of this software and associated documentation files (the
 //
 
 using System;
+using System.Collections.Generic;
 using System.Diagnostics;
+using System.Linq;
 using Microsoft.Build.Construction;
+using System.IO;
+using Microsoft.Build.Framework;
 
 namespace Microsoft.Build.Evaluation
 {
-        [DebuggerDisplay("{ItemType}={EvaluatedInclude} [{UnevaluatedInclude}] #DirectMetadata={DirectMetadataCount}")]
-        public class ProjectItem
-        {
-                internal ProjectItem (ProjectItemElement xml)
-                {
-                        Xml = xml;
-                }
-
-                public ProjectItemElement Xml { get; private set; }
-        }
+       [DebuggerDisplay ("{ItemType}={EvaluatedInclude} [{UnevaluatedInclude}] #DirectMetadata={DirectMetadataCount}")]
+       public class ProjectItem
+       {
+               internal ProjectItem (Project project, ProjectItemElement xml, string evaluatedInclude)
+               {
+                       this.project = project;
+                       this.xml = xml;
+                       if (project.ItemDefinitions.ContainsKey (ItemType))
+                               foreach (var md in project.ItemDefinitions [ItemType].Metadata)
+                                       metadata.Add (md);
+                       foreach (var md in xml.Metadata)
+                               metadata.Add (new ProjectMetadata (project, ItemType, metadata, m => metadata.Remove (m), md));
+                       this.evaluated_include = evaluatedInclude;
+                       is_imported = project.ProjectCollection.OngoingImports.Any ();                  
+               }
+               
+               readonly Project project;
+               readonly ProjectItemElement xml;
+               readonly List<ProjectMetadata> metadata = new List<ProjectMetadata> ();
+               readonly bool is_imported;
+               readonly string evaluated_include;
+               
+               internal string RecursiveDir { get; set; }
+
+               public ProjectMetadata GetMetadata (string name)
+               {
+                       return metadata.FirstOrDefault (m => m.Name == name);
+               }
+
+               public string GetMetadataValue (string name)
+               {
+                       if (name == null)
+                               throw new ArgumentNullException ("name");
+                       var wk = ProjectCollection.GetWellKnownMetadata (name, EvaluatedInclude, project.GetFullPath, RecursiveDir);
+                       if (wk != null)
+                               return wk;
+                       var m = GetMetadata (name);
+                       return m != null ? m.EvaluatedValue : string.Empty;
+               }
+
+               public bool HasMetadata (string name)
+               {
+                       return GetMetadata (name) != null;
+               }
+
+               public bool RemoveMetadata (string name)
+               {
+                       var m = GetMetadata (name);
+                       if (m == null)
+                               return false;
+                       return metadata.Remove (m);
+               }
+
+               public void Rename (string name)
+               {
+                       throw new NotImplementedException ();
+               }
+
+               public ProjectMetadata SetMetadataValue (string name, string unevaluatedValue)
+               {
+                       // This has to do several tasks:
+                       // - it cannot directly change Xml.Metadata because the ProjectItemElement might be shared
+                       //   among multiple ProjectItems.
+                       // - hence it has to create another ProjectItemElement instance and add it to the project
+                       //   XML construction, with specific Include value that is assigned to this instance, and
+                       //   metadata values that are assigned to this instance.
+                       throw new NotImplementedException ();
+               }
+
+               public IEnumerable<ProjectMetadata> DirectMetadata {
+                       get {
+                               var list = new List<ProjectMetadata> ();
+                               foreach (var xm in xml.Metadata)
+                                       yield return new ProjectMetadata (project, ItemType, list, p => list.Remove (p), xm);
+                       }
+               }
+
+               public int DirectMetadataCount {
+                       get { return xml.Metadata.Count; }
+               }
+
+               public string EvaluatedInclude {
+                       get { return evaluated_include; }
+               }
+
+               public bool IsImported {
+                       get { return is_imported; }
+               }
+
+               public string ItemType {
+                       get { return Xml.ItemType; }
+                       set { Xml.ItemType = value; }
+               }
+
+               public ICollection<ProjectMetadata> Metadata {
+                       get { return metadata; }
+               }
+
+               public int MetadataCount {
+                       get { return metadata.Count; }
+               }
+
+               public Project Project {
+                       get { return project; }
+               }
+
+               public string UnevaluatedInclude {
+                       get { return Xml.Include; }
+                       set { Xml.Include = value; }
+               }
+
+               public ProjectItemElement Xml {
+                       get { return xml; }
+               }               
+       }
 }