2007-02-03 Marek Sieradzki <marek.sieradzki@gmail.com>
[mono.git] / mcs / class / Microsoft.Build.Engine / Microsoft.Build.BuildEngine / TargetCollection.cs
index b8f4529063fda654859793dea6ca1fcc2c077098..940ff9181daf2a03df0ab9d64b07bf73de16e40a 100644 (file)
@@ -31,28 +31,42 @@ using System;
 using System.Collections;
 using System.Collections.Generic;
 using System.Reflection;
+using System.Xml;
 
 namespace Microsoft.Build.BuildEngine {
        public class TargetCollection : ICollection, IEnumerable {
                
                Dictionary <string, Target>     targetsByName;
-               //Project                               parentProject;
+               Project                         parentProject;
        
                internal TargetCollection (Project project)
                {
-                       this.targetsByName = new Dictionary <string, Target> ();
-                       //this.parentProject = project;
+                       this.targetsByName = new Dictionary <string, Target> (StringComparer.InvariantCultureIgnoreCase);
+                       this.parentProject = project;
                }
 
-               // This must create a new xml element and stuff.
                [MonoTODO]
                public Target AddNewTarget (string targetName)
                {
-                       throw new NotImplementedException ();
+                       if (targetName == null)
+                               throw new InvalidProjectFileException (
+                                       "The required attribute \"Name\" is missing from element <Target>.");
+               
+                       XmlElement targetElement = parentProject.XmlDocument.CreateElement ("Target", Project.XmlNamespace);
+                       parentProject.XmlDocument.DocumentElement.AppendChild (targetElement);
+                       targetElement.SetAttribute ("Name", targetName);
+                       
+                       Target t = new Target (targetElement, parentProject, null);
+                       
+                       AddTarget (t);
+                       
+                       return t;
                }
 
                internal void AddTarget (Target target)
                {
+                       if (targetsByName.ContainsKey (target.Name))
+                               targetsByName.Remove (target.Name);
                        targetsByName.Add (target.Name, target);
                }
 
@@ -68,13 +82,15 @@ namespace Microsoft.Build.BuildEngine {
 
                public IEnumerator GetEnumerator ()
                {
-                       foreach (KeyValuePair <string, Target> kvp in targetsByName) {
+                       foreach (KeyValuePair <string, Target> kvp in targetsByName)
                                yield return kvp.Value;
-                       }
                }
 
                public void RemoveTarget (Target targetToRemove)
                {
+                       if (targetToRemove == null)
+                               throw new ArgumentNullException ();
+                       
                        targetsByName.Remove (targetToRemove.Name);
                }