* support-test-*.cs: Rename from test-*-p2.cs.
[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.Specialized;
33 using System.Reflection;
34 using System.Text;
35 using System.Xml;
36
37 namespace Microsoft.Build.BuildEngine {
38         public class BuildPropertyGroup : ItemPropertyGroupingBase, IBuildPropertyGroup, IEnumerable {
39         
40                 XmlElement              propertyGroup;
41                 XmlAttribute            condition;
42                 string                  importedFromFilename;
43                 bool                    isImported;
44                 GroupingCollection      parentCollection;
45                 Project                 parentProject;
46                 IList                   properties;
47                 IDictionary             propertiesByName;
48         
49                 public BuildPropertyGroup ()
50                         : this (true, null)
51                 {
52                 }
53                 
54                 internal BuildPropertyGroup (bool forXml, Project project)
55                 {
56                         this.propertyGroup = null;
57                         this.condition = null;
58                         this.importedFromFilename = null;
59                         this.isImported = false;
60                         this.parentCollection = null;
61                         this.parentProject = project;
62                         if (forXml == true)
63                                 this.properties = new ArrayList ();
64                         else
65                                 this.propertiesByName = CollectionsUtil.CreateCaseInsensitiveHashtable ();
66                 }
67
68                 public BuildProperty AddNewProperty (string propertyName,
69                                                      string propertyValue)
70                 {
71                         return AddNewProperty (propertyName, propertyValue,
72                                 PropertyType.Normal);
73                 }
74                 
75                 internal BuildProperty AddNewProperty (string propertyName,
76                                                        string propertyValue,
77                                                        PropertyType propertyType)
78                 {
79                         BuildProperty added, existing;
80                         
81                         added = new BuildProperty (propertyName, propertyValue);
82                         added.PropertyType = propertyType;
83                         if (properties != null) {
84                                 properties.Add (added);
85                         } else if (propertiesByName != null) {
86                                 if (propertiesByName.Contains (propertyName) == true) {
87                                         existing = (BuildProperty) propertiesByName [added.Name];
88                                         if (added.PropertyType <= existing.PropertyType) {
89                                                 propertiesByName.Remove (added.Name);
90                                                 propertiesByName.Add (added.Name, added);
91                                         }
92                                 } else {
93                                         propertiesByName.Add (added.Name, added);
94                                 }
95                         } else
96                                 throw new Exception ("PropertyGroup is not initialized.");
97                         return added;
98                 }
99                 
100                 internal void AddFromExistingProperty (BuildProperty buildProperty)
101                 {
102                         BuildProperty added, existing;
103                         
104                         added = buildProperty.Clone (false);
105                         if (propertiesByName.Contains (added.Name) == true) {
106                                 existing = (BuildProperty) propertiesByName [added.Name];
107                                 if (added.PropertyType <= existing.PropertyType) {
108                                         propertiesByName.Remove (added.Name);
109                                         propertiesByName.Add (added.Name, added);
110                                 }
111                         } else
112                                 propertiesByName.Add (added.Name, added);
113                 }
114                 
115                 public void Clear ()
116                 {
117                 }
118
119                 public BuildPropertyGroup Clone (bool deepClone)
120                 {
121                         return null;
122                 }
123
124                 // FIXME: what it is doing?
125                 public override void Evaluate (BuildPropertyGroup evaluatedPropertyBag,
126                                                bool ignoreCondition,
127                                                bool honorCondition,
128                                                Hashtable conditionedPropertiesTable,
129                                                ProcessingPass pass)
130                 {
131                 }
132                 
133                 public IEnumerator GetEnumerator ()
134                 {
135                         if (properties != null)
136                                 foreach (BuildProperty bp in properties)
137                                         yield return bp;
138                         else if (propertiesByName != null)
139                                 foreach (DictionaryEntry de in propertiesByName)
140                                         yield return (BuildProperty) de.Value;
141                         else
142                                 throw new Exception ("PropertyGroup is not initialized.");
143                 }
144
145                 public void GetStringArraysForAllProperties (out string[] propertyNames,
146                                                              out string[] propertyValues,
147                                                              out string[] propertyFinalValues)
148                 {
149                         propertyNames = null;
150                         propertyValues = null;
151                         propertyFinalValues = null;
152                         int i = 0;
153                         if (properties != null) {
154                                 foreach (BuildProperty bp in properties) {
155                                         propertyNames [i] = bp.Name;
156                                         propertyValues [i] = bp.Value;
157                                         propertyFinalValues [i] = bp.FinalValue;
158                                         i++;
159                                 }
160                         } else if (propertiesByName != null) {
161                                 foreach (DictionaryEntry de in propertiesByName) {
162                                         propertyNames [i] = ((BuildProperty) de.Value).Name;
163                                         propertyValues [i] = ((BuildProperty) de.Value).Value;
164                                         propertyFinalValues [i] = ((BuildProperty) de.Value).FinalValue;
165                                         i++;
166                                 }
167                         }
168                 }
169
170                 public void RemoveProperty (BuildProperty propertyToRemove)
171                 {
172                         if (properties == null)
173                                 throw new Exception ("PropertyGroup is not initialized.");
174                         properties.Remove (propertyToRemove);
175                 }
176
177                 public void RemovePropertyByName (string propertyNameToRemove)
178                 {
179                         if (propertiesByName == null)
180                                 throw new Exception ("PropertyGroup is not initialized.");
181                         propertiesByName.Remove (propertyNameToRemove);
182                 }
183
184                 public void SetProperty (string propertyName,
185                                          string propertyValue)
186                 {
187                         if (propertiesByName.Contains (propertyName) == false) {
188                                 AddNewProperty (propertyName, propertyValue);
189                         }
190                         ((BuildProperty) propertiesByName [propertyName]).Value = propertyValue;
191                 }
192                 
193                 internal void BindToXml (XmlElement propertyGroupElement)
194                 {
195                         if (propertyGroupElement == null)
196                                 throw new ArgumentNullException ();
197                         this.properties = new ArrayList ();
198                         this.propertyGroup = propertyGroupElement;
199                         this.condition = propertyGroupElement.GetAttributeNode ("Condition");
200                         this.importedFromFilename = null;
201                         this.isImported = false;
202                         foreach (XmlElement xe in propertyGroupElement.ChildNodes) {
203                                 BuildProperty bp = AddNewProperty(xe.Name, xe.InnerText);
204                                 bp.PropertyType = PropertyType.Normal;
205                                 bp.BindToXml (xe);
206                                 Expression finalValue = new Expression (parentProject, bp.Value);
207                                 bp.FinalValue = (string) finalValue.ToNonArray (typeof (string));
208                                 parentProject.EvaluatedProperties.AddFromExistingProperty (bp);
209                         } 
210                 }
211                 
212                 public string Condition {
213                         get {
214                                 if (condition == null)
215                                         return null;
216                                 else
217                                         return condition.Value;
218                         }
219                         set {
220                                 if (condition != null)
221                                         condition.Value = value;
222                         }
223                 }
224
225                 public int Count {
226                         get {
227                                 if (properties != null)
228                                         return properties.Count;
229                                 else if (propertiesByName != null)
230                                         return propertiesByName.Count;
231                                 else
232                                         throw new Exception ("PropertyGroup is not initialized.");
233                         }
234                 }
235
236                 public string ImportedFromFilename {
237                         get {
238                                 return importedFromFilename;
239                         }
240                 }
241
242                 public bool IsImported {
243                         get {
244                                 return isImported;
245                         }
246                 }
247
248                 public BuildProperty this[string propertyName] {
249                         get {
250                                 if (propertiesByName.Contains (propertyName)) {
251                                         return (BuildProperty) propertiesByName [propertyName];
252                                 } else {
253                                         return null;
254                                 }
255                         }
256                         set {
257                                 propertiesByName [propertyName] = value;
258                         }
259                 }
260                 
261                 internal GroupingCollection GroupingCollection {
262                         get { return parentCollection; }
263                         set { parentCollection = value; }
264                 }
265         }
266 }
267
268 #endif