Forgot this in changelog
[mono.git] / mcs / class / Microsoft.Build.Engine / Microsoft.Build.BuildEngine / BuildPropertyGroup.cs
1 //
2 // BuildPropertyGroup.cs: Represents a group of properties
3 //
4 // Author:
5 //   Marek Sieradzki (marek.sieradzki@gmail.com)
6 // 
7 // (C) 2005 Marek Sieradzki
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
28 #if NET_2_0
29
30 using System;
31 using System.Collections;
32 using System.Collections.Generic;
33 using System.Reflection;
34 using System.Text;
35 using System.Xml;
36
37 namespace Microsoft.Build.BuildEngine {
38         public class BuildPropertyGroup : IEnumerable {
39         
40                 ImportedProject         importedProject;
41                 XmlElement              propertyGroup;
42                 GroupingCollection      parentCollection;
43                 Project                 parentProject;
44                 List <BuildProperty>    properties;
45                 Dictionary <string, BuildProperty>      propertiesByName;
46
47                 public BuildPropertyGroup ()
48                         : this (null, null, null)
49                 {
50                 }
51
52                 internal BuildPropertyGroup (XmlElement xmlElement, Project project, ImportedProject importedProject)
53                 {
54                         this.importedProject = importedProject;
55                         this.parentCollection = null;
56                         this.parentProject = project;
57                         this.propertyGroup = xmlElement;
58
59                         if (FromXml) {
60                                 this.properties = new List <BuildProperty> ();
61                                 foreach (XmlNode xn in propertyGroup.ChildNodes) {
62                                         if (!(xn is XmlElement))
63                                                 continue;
64                                         
65                                         XmlElement xe = (XmlElement) xn;
66                                         BuildProperty bp = new BuildProperty (parentProject, xe);
67                                         AddProperty (bp);
68                                 } 
69                         } else
70                                 this.propertiesByName = new Dictionary <string, BuildProperty> (StringComparer.InvariantCultureIgnoreCase);
71                 }
72
73                 public BuildProperty AddNewProperty (string propertyName,
74                                                      string propertyValue)
75                 {
76                         return AddNewProperty (propertyName, propertyValue, false);
77                 }
78                 
79                 public BuildProperty AddNewProperty (string propertyName,
80                                                      string propertyValue,
81                                                      bool treatPropertyValueAsLiteral)
82                 {
83                         BuildProperty prop;
84
85                         if (FromXml) {
86                                 XmlElement xe;
87                                 
88                                 xe = propertyGroup.OwnerDocument.CreateElement (propertyName);
89                                 propertyGroup.AppendChild (xe);
90                                 
91                                 if (treatPropertyValueAsLiteral)
92                                         xe.InnerText = Utilities.Escape (propertyValue);
93                                 else
94                                         xe.InnerText = propertyValue;
95                                 
96                                 prop = new BuildProperty (parentProject, xe);
97                                 AddProperty (prop);
98                                 return prop;
99                         } else
100                                 throw new InvalidOperationException ("This method is only valid for persisted <System.Object[]> elements.");
101                 }
102
103                 internal void AddProperty (BuildProperty property)
104                 {
105                         if (FromXml) {
106                                 properties.Add (property);
107                         } else {
108                                 if (propertiesByName.ContainsKey (property.Name)) {
109                                         BuildProperty existing = propertiesByName [property.Name];
110                                         if (property.PropertyType <= existing.PropertyType) {
111                                                 propertiesByName.Remove (property.Name);
112                                                 propertiesByName.Add (property.Name, property);
113                                         }
114                                 } else {
115                                         propertiesByName.Add (property.Name, property);
116                                 }
117                         }
118                 }
119                 
120                 public void Clear ()
121                 {
122                         if (FromXml)
123                                 properties = new List <BuildProperty> ();
124                         else
125                                 propertiesByName = new Dictionary <string, BuildProperty> ();
126                 }
127
128                 [MonoTODO]
129                 public BuildPropertyGroup Clone (bool deepClone)
130                 {
131                         throw new NotImplementedException ();
132                 }
133
134                 public IEnumerator GetEnumerator ()
135                 {
136                         if (FromXml)
137                                 foreach (BuildProperty bp in properties)
138                                         yield return bp;
139                         else 
140                                 foreach (KeyValuePair <string, BuildProperty> kvp in propertiesByName)
141                                         yield return kvp.Value;
142                 }
143
144                 public void RemoveProperty (BuildProperty propertyToRemove)
145                 {
146                         if (FromXml)
147                                 properties.Remove (propertyToRemove);
148                         else {
149                                 foreach (KeyValuePair <string, BuildProperty> kvp in propertiesByName)
150                                         if (kvp.Value == propertyToRemove) {
151                                                 propertiesByName.Remove (kvp.Key);
152                                                 break;
153                                         }
154                         }
155                 }
156
157                 public void RemoveProperty (string propertyName)
158                 {
159                         if (FromXml) {
160                                 foreach (BuildProperty bp in properties)
161                                         if (bp.Name == propertyName) {
162                                                 properties.Remove (bp);
163                                                 break;
164                                         }
165                         } else
166                                 propertiesByName.Remove (propertyName);
167                 }
168
169                 public void SetProperty (string propertyName,
170                                          string propertyValue)
171                 {
172                         SetProperty (propertyName, propertyValue, false);
173                 }
174                 
175                 public void SetProperty (string propertyName,
176                                          string propertyValue,
177                                          bool treatPropertyValueAsLiteral)
178                 {
179                         if (!propertiesByName.ContainsKey (propertyName)) {
180                                 BuildProperty bp = new BuildProperty (propertyName, propertyValue);
181                                 AddProperty (bp);
182                         }
183
184                         if (treatPropertyValueAsLiteral)
185                                 propertiesByName [propertyName].Value = Utilities.Escape (propertyValue);
186                         else
187                                 propertiesByName [propertyName].Value = propertyValue;
188                 }
189                 
190                 internal void Evaluate ()
191                 {
192                         if (!FromXml) {
193                                 throw new InvalidOperationException ();
194                         }
195                         foreach (BuildProperty bp in properties) {
196                                 if (bp.Condition == String.Empty)
197                                         bp.Evaluate ();
198                                 else {
199                                         ConditionExpression ce = ConditionParser.ParseCondition (bp.Condition);
200                                         if (ce.BoolEvaluate (parentProject))
201                                                 bp.Evaluate ();
202                                 }
203                         }
204                 }
205                 
206                 public string Condition {
207                         get {
208                                 if (!FromXml)
209                                         return String.Empty;
210                                 return propertyGroup.GetAttribute ("Condition");
211                         }
212                         set {
213                                 if (!FromXml)
214                                         throw new InvalidOperationException (
215                                         "Cannot set a condition on an object not represented by an XML element in the project file.");
216                                 propertyGroup.SetAttribute ("Condition", value);
217                         }
218                 }
219
220                 public int Count {
221                         get {
222                                 if (FromXml)
223                                         return properties.Count;
224                                 else
225                                         return propertiesByName.Count;
226                         }
227                 }
228
229                 public bool IsImported {
230                         get {
231                                 return importedProject != null;
232                         }
233                 }
234
235                 internal bool FromXml {
236                         get {
237                                 return propertyGroup != null;
238                         }
239                 }
240
241                 public BuildProperty this [string propertyName] {
242                         get {
243                                 if (FromXml)
244                                         throw new InvalidOperationException ("Properties in persisted property groups cannot be accessed by name.");
245                                 
246                                 if (propertiesByName.ContainsKey (propertyName))
247                                         return propertiesByName [propertyName];
248                                 else
249                                         return null;
250                         }
251                         set {
252                                 propertiesByName [propertyName] = value;
253                         }
254                 }
255                 
256                 internal GroupingCollection GroupingCollection {
257                         get { return parentCollection; }
258                         set { parentCollection = value; }
259                 }
260         }
261 }
262
263 #endif