2006-12-16 Marek Sieradzki <marek.sieradzki@gmail.com>
[mono.git] / mcs / class / Microsoft.Build.Engine / Microsoft.Build.BuildEngine / BuildPropertyGroup.cs
index f3f79a52a67b6b150df49d478fa2f53de7249bdf..fce679d43449af45a7c3f218be9c95f6c383ff78 100644 (file)
@@ -37,30 +37,29 @@ using System.Xml;
 namespace Microsoft.Build.BuildEngine {
        public class BuildPropertyGroup : IEnumerable {
        
+               ImportedProject         importedProject;
                XmlElement              propertyGroup;
-               bool                    isImported;
                GroupingCollection      parentCollection;
                Project                 parentProject;
                List <BuildProperty>    properties;
                Dictionary <string, BuildProperty>      propertiesByName;
 
                public BuildPropertyGroup ()
-                       : this (null, null)
+                       : this (null, null, null)
                {
                }
 
-               internal BuildPropertyGroup (XmlElement xmlElement, Project project)
+               internal BuildPropertyGroup (XmlElement xmlElement, Project project, ImportedProject importedProject)
                {
-                       this.isImported = false;
+                       this.importedProject = importedProject;
                        this.parentCollection = null;
                        this.parentProject = project;
-                       this.isImported = false;
                        this.propertyGroup = xmlElement;
 
                        if (FromXml) {
                                this.properties = new List <BuildProperty> ();
                                foreach (XmlNode xn in propertyGroup.ChildNodes) {
-                                       if (xn is XmlElement == false)
+                                       if (!(xn is XmlElement))
                                                continue;
                                        
                                        XmlElement xe = (XmlElement) xn;
@@ -88,17 +87,17 @@ namespace Microsoft.Build.BuildEngine {
                                
                                xe = propertyGroup.OwnerDocument.CreateElement (propertyName);
                                propertyGroup.AppendChild (xe);
-                               if (treatPropertyValueAsLiteral) {
+                               
+                               if (treatPropertyValueAsLiteral)
                                        xe.InnerText = Utilities.Escape (propertyValue);
-                               } else {
+                               else
                                        xe.InnerText = propertyValue;
-                               }
+                               
                                prop = new BuildProperty (parentProject, xe);
-                       } else {
-                               prop = new BuildProperty (propertyName, propertyValue);
-                       }
-                       AddProperty (prop);
-                       return prop;
+                               AddProperty (prop);
+                               return prop;
+                       } else
+                               throw new InvalidOperationException ("This method is only valid for persisted <System.Object[]> elements.");
                }
 
                internal void AddProperty (BuildProperty property)
@@ -118,10 +117,12 @@ namespace Microsoft.Build.BuildEngine {
                        }
                }
                
-               [MonoTODO]
                public void Clear ()
                {
-                       throw new NotImplementedException ();
+                       if (FromXml)
+                               properties = new List <BuildProperty> ();
+                       else
+                               propertiesByName = new Dictionary <string, BuildProperty> ();
                }
 
                [MonoTODO]
@@ -132,28 +133,37 @@ namespace Microsoft.Build.BuildEngine {
 
                public IEnumerator GetEnumerator ()
                {
-                       if (properties != null) {
+                       if (FromXml)
                                foreach (BuildProperty bp in properties)
                                        yield return bp;
-                       } else if (propertiesByName != null) {
+                       else 
                                foreach (KeyValuePair <string, BuildProperty> kvp in propertiesByName)
                                        yield return kvp.Value;
-                       } else
-                               throw new Exception ("PropertyGroup is not initialized.");
                }
 
                public void RemoveProperty (BuildProperty propertyToRemove)
                {
-                       if (properties == null)
-                               throw new Exception ("PropertyGroup is not initialized.");
-                       properties.Remove (propertyToRemove);
+                       if (FromXml)
+                               properties.Remove (propertyToRemove);
+                       else {
+                               foreach (KeyValuePair <string, BuildProperty> kvp in propertiesByName)
+                                       if (kvp.Value == propertyToRemove) {
+                                               propertiesByName.Remove (kvp.Key);
+                                               break;
+                                       }
+                       }
                }
 
                public void RemoveProperty (string propertyName)
                {
-                       if (propertiesByName == null)
-                               throw new Exception ("PropertyGroup is not initialized.");
-                       propertiesByName.Remove (propertyName);
+                       if (FromXml) {
+                               foreach (BuildProperty bp in properties)
+                                       if (bp.Name == propertyName) {
+                                               properties.Remove (bp);
+                                               break;
+                                       }
+                       } else
+                               propertiesByName.Remove (propertyName);
                }
 
                public void SetProperty (string propertyName,
@@ -162,16 +172,19 @@ namespace Microsoft.Build.BuildEngine {
                        SetProperty (propertyName, propertyValue, false);
                }
                
-               // FIXME: use treatPropertyValueAsLiteral
-               [MonoTODO]
                public void SetProperty (string propertyName,
                                         string propertyValue,
                                         bool treatPropertyValueAsLiteral)
                {
                        if (!propertiesByName.ContainsKey (propertyName)) {
-                               AddNewProperty (propertyName, propertyValue);
+                               BuildProperty bp = new BuildProperty (propertyName, propertyValue);
+                               AddProperty (bp);
                        }
-                       propertiesByName [propertyName].Value = propertyValue;
+
+                       if (treatPropertyValueAsLiteral)
+                               propertiesByName [propertyName].Value = Utilities.Escape (propertyValue);
+                       else
+                               propertiesByName [propertyName].Value = propertyValue;
                }
                
                internal void Evaluate ()
@@ -206,18 +219,16 @@ namespace Microsoft.Build.BuildEngine {
 
                public int Count {
                        get {
-                               if (properties != null)
+                               if (FromXml)
                                        return properties.Count;
-                               else if (propertiesByName != null)
-                                       return propertiesByName.Count;
                                else
-                                       throw new Exception ("PropertyGroup is not initialized.");
+                                       return propertiesByName.Count;
                        }
                }
 
                public bool IsImported {
                        get {
-                               return isImported;
+                               return importedProject != null;
                        }
                }
 
@@ -232,11 +243,10 @@ namespace Microsoft.Build.BuildEngine {
                                if (FromXml)
                                        throw new InvalidOperationException ("Properties in persisted property groups cannot be accessed by name.");
                                
-                               if (propertiesByName.ContainsKey (propertyName)) {
+                               if (propertiesByName.ContainsKey (propertyName))
                                        return propertiesByName [propertyName];
-                               } else {
+                               else
                                        return null;
-                               }
                        }
                        set {
                                propertiesByName [propertyName] = value;