2006-12-16 Marek Sieradzki <marek.sieradzki@gmail.com>
[mono.git] / mcs / class / Microsoft.Build.Engine / Microsoft.Build.BuildEngine / BuildPropertyGroup.cs
index f20c1e947cb8d436fe516b8a7cbace8399a04f66..fce679d43449af45a7c3f218be9c95f6c383ff78 100644 (file)
@@ -29,7 +29,7 @@
 
 using System;
 using System.Collections;
-using System.Collections.Specialized;
+using System.Collections.Generic;
 using System.Reflection;
 using System.Text;
 using System.Xml;
@@ -37,32 +37,37 @@ using System.Xml;
 namespace Microsoft.Build.BuildEngine {
        public class BuildPropertyGroup : IEnumerable {
        
+               ImportedProject         importedProject;
                XmlElement              propertyGroup;
-               XmlAttribute            condition;
-               string                  importedFromFilename;
-               bool                    isImported;
                GroupingCollection      parentCollection;
                Project                 parentProject;
-               IList                   properties;
-               IDictionary             propertiesByName;
-       
+               List <BuildProperty>    properties;
+               Dictionary <string, BuildProperty>      propertiesByName;
+
                public BuildPropertyGroup ()
-                       : this (true, null)
+                       : this (null, null, null)
                {
                }
-               
-               internal BuildPropertyGroup (bool forXml, Project project)
+
+               internal BuildPropertyGroup (XmlElement xmlElement, Project project, ImportedProject importedProject)
                {
-                       this.propertyGroup = null;
-                       this.condition = null;
-                       this.importedFromFilename = null;
-                       this.isImported = false;
+                       this.importedProject = importedProject;
                        this.parentCollection = null;
                        this.parentProject = project;
-                       if (forXml == true)
-                               this.properties = new ArrayList ();
-                       else
-                               this.propertiesByName = CollectionsUtil.CreateCaseInsensitiveHashtable ();
+                       this.propertyGroup = xmlElement;
+
+                       if (FromXml) {
+                               this.properties = new List <BuildProperty> ();
+                               foreach (XmlNode xn in propertyGroup.ChildNodes) {
+                                       if (!(xn is XmlElement))
+                                               continue;
+                                       
+                                       XmlElement xe = (XmlElement) xn;
+                                       BuildProperty bp = new BuildProperty (parentProject, xe);
+                                       AddProperty (bp);
+                               } 
+                       } else
+                               this.propertiesByName = new Dictionary <string, BuildProperty> (StringComparer.InvariantCultureIgnoreCase);
                }
 
                public BuildProperty AddNewProperty (string propertyName,
@@ -75,94 +80,90 @@ namespace Microsoft.Build.BuildEngine {
                                                     string propertyValue,
                                                     bool treatPropertyValueAsLiteral)
                {
-                       return AddNewProperty (propertyName, propertyValue, treatPropertyValueAsLiteral,
-                               PropertyType.Normal);
+                       BuildProperty prop;
+
+                       if (FromXml) {
+                               XmlElement xe;
+                               
+                               xe = propertyGroup.OwnerDocument.CreateElement (propertyName);
+                               propertyGroup.AppendChild (xe);
+                               
+                               if (treatPropertyValueAsLiteral)
+                                       xe.InnerText = Utilities.Escape (propertyValue);
+                               else
+                                       xe.InnerText = propertyValue;
+                               
+                               prop = new BuildProperty (parentProject, xe);
+                               AddProperty (prop);
+                               return prop;
+                       } else
+                               throw new InvalidOperationException ("This method is only valid for persisted <System.Object[]> elements.");
                }
-               
-               // FIXME: use treatPropertyValueAsLiteral
-               internal BuildProperty AddNewProperty (string propertyName,
-                                                      string propertyValue,
-                                                      bool treatPropertyValueAsLiteral,
-                                                      PropertyType propertyType)
+
+               internal void AddProperty (BuildProperty property)
                {
-                       BuildProperty added, existing;
-                       
-                       added = new BuildProperty (propertyName, propertyValue);
-                       added.PropertyType = propertyType;
-                       if (properties != null) {
-                               properties.Add (added);
-                       } else if (propertiesByName != null) {
-                               if (propertiesByName.Contains (propertyName) == true) {
-                                       existing = (BuildProperty) propertiesByName [added.Name];
-                                       if (added.PropertyType <= existing.PropertyType) {
-                                               propertiesByName.Remove (added.Name);
-                                               propertiesByName.Add (added.Name, added);
+                       if (FromXml) {
+                               properties.Add (property);
+                       } else {
+                               if (propertiesByName.ContainsKey (property.Name)) {
+                                       BuildProperty existing = propertiesByName [property.Name];
+                                       if (property.PropertyType <= existing.PropertyType) {
+                                               propertiesByName.Remove (property.Name);
+                                               propertiesByName.Add (property.Name, property);
                                        }
                                } else {
-                                       propertiesByName.Add (added.Name, added);
+                                       propertiesByName.Add (property.Name, property);
                                }
-                       } else
-                               throw new Exception ("PropertyGroup is not initialized.");
-                       return added;
-               }
-               
-               internal void AddFromExistingProperty (BuildProperty buildProperty)
-               {
-                       BuildProperty added, existing;
-                       
-                       added = buildProperty.Clone (false);
-                       if (propertiesByName.Contains (added.Name) == true) {
-                               existing = (BuildProperty) propertiesByName [added.Name];
-                               if (added.PropertyType <= existing.PropertyType) {
-                                       propertiesByName.Remove (added.Name);
-                                       propertiesByName.Add (added.Name, added);
-                               }
-                       } else
-                               propertiesByName.Add (added.Name, added);
+                       }
                }
                
                public void Clear ()
                {
+                       if (FromXml)
+                               properties = new List <BuildProperty> ();
+                       else
+                               propertiesByName = new Dictionary <string, BuildProperty> ();
                }
 
+               [MonoTODO]
                public BuildPropertyGroup Clone (bool deepClone)
                {
-                       return null;
+                       throw new NotImplementedException ();
                }
 
-               // FIXME: what it is doing?
-               internal void Evaluate (BuildPropertyGroup evaluatedPropertyBag,
-                                              bool ignoreCondition,
-                                              bool honorCondition,
-                                              Hashtable conditionedPropertiesTable,
-                                              ProcessingPass pass)
-               {
-               }
-               
                public IEnumerator GetEnumerator ()
                {
-                       if (properties != null)
+                       if (FromXml)
                                foreach (BuildProperty bp in properties)
                                        yield return bp;
-                       else if (propertiesByName != null)
-                               foreach (DictionaryEntry de in propertiesByName)
-                                       yield return (BuildProperty) de.Value;
-                       else
-                               throw new Exception ("PropertyGroup is not initialized.");
+                       else 
+                               foreach (KeyValuePair <string, BuildProperty> kvp in propertiesByName)
+                                       yield return kvp.Value;
                }
 
                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,
@@ -171,80 +172,81 @@ namespace Microsoft.Build.BuildEngine {
                        SetProperty (propertyName, propertyValue, false);
                }
                
-               // FIXME: use treatPropertyValueAsLiteral
-               [MonoTODO]
                public void SetProperty (string propertyName,
                                         string propertyValue,
                                         bool treatPropertyValueAsLiteral)
                {
-                       if (propertiesByName.Contains (propertyName) == false) {
-                               AddNewProperty (propertyName, propertyValue);
+                       if (!propertiesByName.ContainsKey (propertyName)) {
+                               BuildProperty bp = new BuildProperty (propertyName, propertyValue);
+                               AddProperty (bp);
                        }
-                       ((BuildProperty) propertiesByName [propertyName]).Value = propertyValue;
+
+                       if (treatPropertyValueAsLiteral)
+                               propertiesByName [propertyName].Value = Utilities.Escape (propertyValue);
+                       else
+                               propertiesByName [propertyName].Value = propertyValue;
                }
                
-               internal void BindToXml (XmlElement propertyGroupElement)
+               internal void Evaluate ()
                {
-                       if (propertyGroupElement == null)
-                               throw new ArgumentNullException ();
-                       this.properties = new ArrayList ();
-                       this.propertyGroup = propertyGroupElement;
-                       this.condition = propertyGroupElement.GetAttributeNode ("Condition");
-                       this.importedFromFilename = null;
-                       this.isImported = false;
-                       foreach (XmlElement xe in propertyGroupElement.ChildNodes) {
-                               BuildProperty bp = AddNewProperty(xe.Name, xe.InnerText);
-                               bp.PropertyType = PropertyType.Normal;
-                               bp.BindToXml (xe);
-                               Expression finalValue = new Expression (parentProject, bp.Value);
-                               bp.FinalValue = (string) finalValue.ToNonArray (typeof (string));
-                               parentProject.EvaluatedProperties.AddFromExistingProperty (bp);
-                       } 
+                       if (!FromXml) {
+                               throw new InvalidOperationException ();
+                       }
+                       foreach (BuildProperty bp in properties) {
+                               if (bp.Condition == String.Empty)
+                                       bp.Evaluate ();
+                               else {
+                                       ConditionExpression ce = ConditionParser.ParseCondition (bp.Condition);
+                                       if (ce.BoolEvaluate (parentProject))
+                                               bp.Evaluate ();
+                               }
+                       }
                }
                
                public string Condition {
                        get {
-                               if (condition == null)
-                                       return null;
-                               else
-                                       return condition.Value;
+                               if (!FromXml)
+                                       return String.Empty;
+                               return propertyGroup.GetAttribute ("Condition");
                        }
                        set {
-                               if (condition != null)
-                                       condition.Value = value;
+                               if (!FromXml)
+                                       throw new InvalidOperationException (
+                                       "Cannot set a condition on an object not represented by an XML element in the project file.");
+                               propertyGroup.SetAttribute ("Condition", value);
                        }
                }
 
                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;
                        }
                }
 
-               internal string ImportedFromFilename {
+               public bool IsImported {
                        get {
-                               return importedFromFilename;
+                               return importedProject != null;
                        }
                }
 
-               public bool IsImported {
+               internal bool FromXml {
                        get {
-                               return isImported;
+                               return propertyGroup != null;
                        }
                }
 
-               public BuildProperty this[string propertyName] {
+               public BuildProperty this [string propertyName] {
                        get {
-                               if (propertiesByName.Contains (propertyName)) {
-                                       return (BuildProperty) propertiesByName [propertyName];
-                               } else {
+                               if (FromXml)
+                                       throw new InvalidOperationException ("Properties in persisted property groups cannot be accessed by name.");
+                               
+                               if (propertiesByName.ContainsKey (propertyName))
+                                       return propertiesByName [propertyName];
+                               else
                                        return null;
-                               }
                        }
                        set {
                                propertiesByName [propertyName] = value;
@@ -258,4 +260,4 @@ namespace Microsoft.Build.BuildEngine {
        }
 }
 
-#endif
\ No newline at end of file
+#endif