2007-01-10 Marek Sieradzki <marek.sieradzki@gmail.com>
[mono.git] / mcs / class / Microsoft.Build.Engine / Microsoft.Build.BuildEngine / BuildPropertyGroup.cs
index 86620a3d32e38643ef4c76f79648ecdef7e1f466..d20027559483678e4edc059062834cfd4f5c8486 100644 (file)
@@ -37,30 +37,31 @@ using System.Xml;
 namespace Microsoft.Build.BuildEngine {
        public class BuildPropertyGroup : IEnumerable {
        
+               bool                    read_only;
+               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, false)
                {
                }
 
-               internal BuildPropertyGroup (XmlElement xmlElement, Project project)
+               internal BuildPropertyGroup (XmlElement xmlElement, Project project, ImportedProject importedProject, bool readOnly)
                {
-                       this.isImported = false;
+                       this.importedProject = importedProject;
                        this.parentCollection = null;
                        this.parentProject = project;
-                       this.isImported = false;
                        this.propertyGroup = xmlElement;
+                       this.read_only = readOnly;
 
                        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;
@@ -86,7 +87,7 @@ namespace Microsoft.Build.BuildEngine {
                        if (FromXml) {
                                XmlElement xe;
                                
-                               xe = propertyGroup.OwnerDocument.CreateElement (propertyName);
+                               xe = propertyGroup.OwnerDocument.CreateElement (propertyName, Project.XmlNamespace);
                                propertyGroup.AppendChild (xe);
                                
                                if (treatPropertyValueAsLiteral)
@@ -95,11 +96,11 @@ namespace Microsoft.Build.BuildEngine {
                                        xe.InnerText = propertyValue;
                                
                                prop = new BuildProperty (parentProject, xe);
-                       } else {
-                               prop = new BuildProperty (propertyName, propertyValue);
-                       }
-                       AddProperty (prop);
-                       return prop;
+                               AddProperty (prop);
+                               parentProject.EvaluatedProperties.AddProperty (prop);
+                               return prop;
+                       } else
+                               throw new InvalidOperationException ("This method is only valid for persisted <System.Object[]> elements.");
                }
 
                internal void AddProperty (BuildProperty property)
@@ -119,10 +120,13 @@ namespace Microsoft.Build.BuildEngine {
                        }
                }
                
-               [MonoTODO]
                public void Clear ()
                {
-                       throw new NotImplementedException ();
+                       if (FromXml) {
+                               propertyGroup.RemoveAll ();
+                               properties = new List <BuildProperty> ();
+                       } else
+                               propertiesByName = new Dictionary <string, BuildProperty> ();
                }
 
                [MonoTODO]
@@ -133,28 +137,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,
@@ -163,16 +176,27 @@ namespace Microsoft.Build.BuildEngine {
                        SetProperty (propertyName, propertyValue, false);
                }
                
-               // FIXME: use treatPropertyValueAsLiteral
-               [MonoTODO]
+               // FIXME: add a test for SetProperty on property from xml
                public void SetProperty (string propertyName,
                                         string propertyValue,
                                         bool treatPropertyValueAsLiteral)
                {
-                       if (!propertiesByName.ContainsKey (propertyName)) {
-                               AddNewProperty (propertyName, propertyValue);
-                       }
-                       propertiesByName [propertyName].Value = propertyValue;
+                       if (read_only)
+                               return;
+                       
+                       if (propertiesByName.ContainsKey (propertyName))
+                               propertiesByName.Remove (propertyName);
+
+                       BuildProperty bp;
+                       if (treatPropertyValueAsLiteral)
+                               bp = new BuildProperty (propertyName, Utilities.Escape (propertyValue));
+                       else
+                               bp = new BuildProperty (propertyName, propertyValue);
+
+                       AddProperty (bp);
+
+                       if (IsGlobal)
+                               parentProject.ProcessXml ();
                }
                
                internal void Evaluate ()
@@ -207,18 +231,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;
                        }
                }
 
@@ -228,16 +250,21 @@ namespace Microsoft.Build.BuildEngine {
                        }
                }
 
+               bool IsGlobal {
+                       get {
+                               return parentProject != null && propertyGroup == null;
+                       }
+               }
+
                public BuildProperty this [string propertyName] {
                        get {
                                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;
@@ -248,6 +275,10 @@ namespace Microsoft.Build.BuildEngine {
                        get { return parentCollection; }
                        set { parentCollection = value; }
                }
+
+               internal XmlElement XmlElement {
+                       get { return propertyGroup; }
+               }
        }
 }