2 // BuildPropertyGroup.cs: Represents a group of properties
5 // Marek Sieradzki (marek.sieradzki@gmail.com)
7 // (C) 2005 Marek Sieradzki
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
31 using System.Collections;
32 using System.Collections.Generic;
33 using System.Reflection;
37 namespace Microsoft.Build.BuildEngine {
38 public class BuildPropertyGroup : IEnumerable {
40 ImportedProject importedProject;
41 XmlElement propertyGroup;
42 GroupingCollection parentCollection;
43 Project parentProject;
44 List <BuildProperty> properties;
45 Dictionary <string, BuildProperty> propertiesByName;
47 public BuildPropertyGroup ()
48 : this (null, null, null)
52 internal BuildPropertyGroup (XmlElement xmlElement, Project project, ImportedProject importedProject)
54 this.importedProject = importedProject;
55 this.parentCollection = null;
56 this.parentProject = project;
57 this.propertyGroup = xmlElement;
60 this.properties = new List <BuildProperty> ();
61 foreach (XmlNode xn in propertyGroup.ChildNodes) {
62 if (!(xn is XmlElement))
65 XmlElement xe = (XmlElement) xn;
66 BuildProperty bp = new BuildProperty (parentProject, xe);
70 this.propertiesByName = new Dictionary <string, BuildProperty> (StringComparer.InvariantCultureIgnoreCase);
73 public BuildProperty AddNewProperty (string propertyName,
76 return AddNewProperty (propertyName, propertyValue, false);
79 public BuildProperty AddNewProperty (string propertyName,
81 bool treatPropertyValueAsLiteral)
88 xe = propertyGroup.OwnerDocument.CreateElement (propertyName);
89 propertyGroup.AppendChild (xe);
91 if (treatPropertyValueAsLiteral)
92 xe.InnerText = Utilities.Escape (propertyValue);
94 xe.InnerText = propertyValue;
96 prop = new BuildProperty (parentProject, xe);
100 throw new InvalidOperationException ("This method is only valid for persisted <System.Object[]> elements.");
103 internal void AddProperty (BuildProperty property)
106 properties.Add (property);
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);
115 propertiesByName.Add (property.Name, property);
123 properties = new List <BuildProperty> ();
125 propertiesByName = new Dictionary <string, BuildProperty> ();
129 public BuildPropertyGroup Clone (bool deepClone)
131 throw new NotImplementedException ();
134 public IEnumerator GetEnumerator ()
137 foreach (BuildProperty bp in properties)
140 foreach (KeyValuePair <string, BuildProperty> kvp in propertiesByName)
141 yield return kvp.Value;
144 public void RemoveProperty (BuildProperty propertyToRemove)
147 properties.Remove (propertyToRemove);
149 foreach (KeyValuePair <string, BuildProperty> kvp in propertiesByName)
150 if (kvp.Value == propertyToRemove) {
151 propertiesByName.Remove (kvp.Key);
157 public void RemoveProperty (string propertyName)
160 foreach (BuildProperty bp in properties)
161 if (bp.Name == propertyName) {
162 properties.Remove (bp);
166 propertiesByName.Remove (propertyName);
169 public void SetProperty (string propertyName,
170 string propertyValue)
172 SetProperty (propertyName, propertyValue, false);
175 public void SetProperty (string propertyName,
176 string propertyValue,
177 bool treatPropertyValueAsLiteral)
179 if (!propertiesByName.ContainsKey (propertyName)) {
180 BuildProperty bp = new BuildProperty (propertyName, propertyValue);
184 if (treatPropertyValueAsLiteral)
185 propertiesByName [propertyName].Value = Utilities.Escape (propertyValue);
187 propertiesByName [propertyName].Value = propertyValue;
190 internal void Evaluate ()
193 throw new InvalidOperationException ();
195 foreach (BuildProperty bp in properties) {
196 if (bp.Condition == String.Empty)
199 ConditionExpression ce = ConditionParser.ParseCondition (bp.Condition);
200 if (ce.BoolEvaluate (parentProject))
206 public string Condition {
210 return propertyGroup.GetAttribute ("Condition");
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);
223 return properties.Count;
225 return propertiesByName.Count;
229 public bool IsImported {
231 return importedProject != null;
235 internal bool FromXml {
237 return propertyGroup != null;
241 public BuildProperty this [string propertyName] {
244 throw new InvalidOperationException ("Properties in persisted property groups cannot be accessed by name.");
246 if (propertiesByName.ContainsKey (propertyName))
247 return propertiesByName [propertyName];
252 propertiesByName [propertyName] = value;
256 internal GroupingCollection GroupingCollection {
257 get { return parentCollection; }
258 set { parentCollection = value; }