43d92822f2261ad984926749df4ee4373745f1c5
[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 : IEnumerable {
39         
40                 XmlElement              propertyGroup;
41                 bool                    isImported;
42                 GroupingCollection      parentCollection;
43                 Project                 parentProject;
44                 IList                   properties;
45                 IDictionary             propertiesByName;
46
47                 internal bool FromXml {
48                         get {
49                                 return propertyGroup != null;
50                         }
51                 }
52         
53                 public BuildPropertyGroup ()
54                         : this (null, null)
55                 {
56                 }
57
58                 internal BuildPropertyGroup (XmlElement xmlElement, Project project)
59                 {
60                         this.isImported = false;
61                         this.parentCollection = null;
62                         this.parentProject = project;
63                         this.isImported = false;
64                         this.propertyGroup = xmlElement;
65
66                         if (FromXml) {
67                                 this.properties = new ArrayList ();
68                                 foreach (XmlElement xe in propertyGroup.ChildNodes) {
69                                         BuildProperty bp = new BuildProperty (parentProject, xe);
70                                         AddProperty (bp);
71                                 } 
72                         } else
73                                 this.propertiesByName = CollectionsUtil.CreateCaseInsensitiveHashtable ();
74                 }
75
76                 public BuildProperty AddNewProperty (string propertyName,
77                                                      string propertyValue)
78                 {
79                         return AddNewProperty (propertyName, propertyValue, false);
80                 }
81                 
82                 public BuildProperty AddNewProperty (string propertyName,
83                                                      string propertyValue,
84                                                      bool treatPropertyValueAsLiteral)
85                 {
86                         BuildProperty prop;
87
88                         if (FromXml) {
89                                 XmlElement xe;
90                                 
91                                 xe = propertyGroup.OwnerDocument.CreateElement (propertyName);
92                                 propertyGroup.AppendChild (xe);
93                                 if (treatPropertyValueAsLiteral) {
94                                         xe.InnerText = Utilities.Escape (propertyValue);
95                                 } else {
96                                         xe.InnerText = propertyValue;
97                                 }
98                                 prop = new BuildProperty (parentProject, xe);
99                         } else {
100                                 prop = new BuildProperty (propertyName, propertyValue);
101                         }
102                         AddProperty (prop);
103                         return prop;
104                 }
105
106                 internal void AddProperty (BuildProperty property)
107                 {
108                         if (FromXml) {
109                                 properties.Add (property);
110                         } else {
111                                 if (propertiesByName.Contains (property.Name) == true) {
112                                         BuildProperty existing = (BuildProperty) propertiesByName [property.Name];
113                                         if (property.PropertyType <= existing.PropertyType) {
114                                                 propertiesByName.Remove (property.Name);
115                                                 propertiesByName.Add (property.Name, property);
116                                         }
117                                 } else {
118                                         propertiesByName.Add (property.Name, property);
119                                 }
120                         }
121                 }
122                 
123                 public void Clear ()
124                 {
125                 }
126
127                 public BuildPropertyGroup Clone (bool deepClone)
128                 {
129                         return null;
130                 }
131
132                 public IEnumerator GetEnumerator ()
133                 {
134                         if (properties != null)
135                                 foreach (BuildProperty bp in properties)
136                                         yield return bp;
137                         else if (propertiesByName != null)
138                                 foreach (DictionaryEntry de in propertiesByName)
139                                         yield return (BuildProperty) de.Value;
140                         else
141                                 throw new Exception ("PropertyGroup is not initialized.");
142                 }
143
144                 public void RemoveProperty (BuildProperty propertyToRemove)
145                 {
146                         if (properties == null)
147                                 throw new Exception ("PropertyGroup is not initialized.");
148                         properties.Remove (propertyToRemove);
149                 }
150
151                 public void RemoveProperty (string propertyName)
152                 {
153                         if (propertiesByName == null)
154                                 throw new Exception ("PropertyGroup is not initialized.");
155                         propertiesByName.Remove (propertyName);
156                 }
157
158                 public void SetProperty (string propertyName,
159                                          string propertyValue)
160                 {
161                         SetProperty (propertyName, propertyValue, false);
162                 }
163                 
164                 // FIXME: use treatPropertyValueAsLiteral
165                 [MonoTODO]
166                 public void SetProperty (string propertyName,
167                                          string propertyValue,
168                                          bool treatPropertyValueAsLiteral)
169                 {
170                         if (propertiesByName.Contains (propertyName) == false) {
171                                 AddNewProperty (propertyName, propertyValue);
172                         }
173                         ((BuildProperty) propertiesByName [propertyName]).Value = propertyValue;
174                 }
175                 
176                 internal void Evaluate ()
177                 {
178                         if (!FromXml) {
179                                 throw new InvalidOperationException ();
180                         }
181                         foreach (BuildProperty bp in properties) {
182                                 bp.Evaluate ();
183                         }
184                 }
185                 
186                 public string Condition {
187                         get {
188                                 return propertyGroup.GetAttribute ("Condition");
189                         }
190                         set {
191                                 propertyGroup.SetAttribute ("Condition", value);
192                         }
193                 }
194
195                 public int Count {
196                         get {
197                                 if (properties != null)
198                                         return properties.Count;
199                                 else if (propertiesByName != null)
200                                         return propertiesByName.Count;
201                                 else
202                                         throw new Exception ("PropertyGroup is not initialized.");
203                         }
204                 }
205
206                 public bool IsImported {
207                         get {
208                                 return isImported;
209                         }
210                 }
211
212                 public BuildProperty this[string propertyName] {
213                         get {
214                                 if (propertiesByName.Contains (propertyName)) {
215                                         return (BuildProperty) propertiesByName [propertyName];
216                                 } else {
217                                         return null;
218                                 }
219                         }
220                         set {
221                                 propertiesByName [propertyName] = value;
222                         }
223                 }
224                 
225                 internal GroupingCollection GroupingCollection {
226                         get { return parentCollection; }
227                         set { parentCollection = value; }
228                 }
229         }
230 }
231
232 #endif