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 XmlElement propertyGroup;
42 GroupingCollection parentCollection;
43 Project parentProject;
44 List <BuildProperty> properties;
45 Dictionary <string, BuildProperty> propertiesByName;
47 public BuildPropertyGroup ()
52 internal BuildPropertyGroup (XmlElement xmlElement, Project project)
54 this.isImported = false;
55 this.parentCollection = null;
56 this.parentProject = project;
57 this.isImported = false;
58 this.propertyGroup = xmlElement;
61 this.properties = new List <BuildProperty> ();
62 foreach (XmlNode xn in propertyGroup.ChildNodes) {
63 if (xn is XmlElement == false)
66 XmlElement xe = (XmlElement) xn;
67 BuildProperty bp = new BuildProperty (parentProject, xe);
71 this.propertiesByName = new Dictionary <string, BuildProperty> (StringComparer.InvariantCultureIgnoreCase);
74 public BuildProperty AddNewProperty (string propertyName,
77 return AddNewProperty (propertyName, propertyValue, false);
80 public BuildProperty AddNewProperty (string propertyName,
82 bool treatPropertyValueAsLiteral)
89 xe = propertyGroup.OwnerDocument.CreateElement (propertyName);
90 propertyGroup.AppendChild (xe);
92 if (treatPropertyValueAsLiteral)
93 xe.InnerText = Utilities.Escape (propertyValue);
95 xe.InnerText = propertyValue;
97 prop = new BuildProperty (parentProject, xe);
99 prop = new BuildProperty (propertyName, propertyValue);
105 internal void AddProperty (BuildProperty property)
108 properties.Add (property);
110 if (propertiesByName.ContainsKey (property.Name)) {
111 BuildProperty existing = propertiesByName [property.Name];
112 if (property.PropertyType <= existing.PropertyType) {
113 propertiesByName.Remove (property.Name);
114 propertiesByName.Add (property.Name, property);
117 propertiesByName.Add (property.Name, property);
125 throw new NotImplementedException ();
129 public BuildPropertyGroup Clone (bool deepClone)
131 throw new NotImplementedException ();
134 public IEnumerator GetEnumerator ()
136 if (properties != null) {
137 foreach (BuildProperty bp in properties)
139 } else if (propertiesByName != null) {
140 foreach (KeyValuePair <string, BuildProperty> kvp in propertiesByName)
141 yield return kvp.Value;
143 throw new Exception ("PropertyGroup is not initialized.");
146 public void RemoveProperty (BuildProperty propertyToRemove)
148 if (properties == null)
149 throw new Exception ("PropertyGroup is not initialized.");
150 properties.Remove (propertyToRemove);
153 public void RemoveProperty (string propertyName)
155 if (propertiesByName == null)
156 throw new Exception ("PropertyGroup is not initialized.");
157 propertiesByName.Remove (propertyName);
160 public void SetProperty (string propertyName,
161 string propertyValue)
163 SetProperty (propertyName, propertyValue, false);
166 // FIXME: use treatPropertyValueAsLiteral
168 public void SetProperty (string propertyName,
169 string propertyValue,
170 bool treatPropertyValueAsLiteral)
172 if (!propertiesByName.ContainsKey (propertyName)) {
173 AddNewProperty (propertyName, propertyValue);
175 propertiesByName [propertyName].Value = propertyValue;
178 internal void Evaluate ()
181 throw new InvalidOperationException ();
183 foreach (BuildProperty bp in properties) {
184 if (bp.Condition == String.Empty)
187 ConditionExpression ce = ConditionParser.ParseCondition (bp.Condition);
188 if (ce.BoolEvaluate (parentProject))
194 public string Condition {
198 return propertyGroup.GetAttribute ("Condition");
202 throw new InvalidOperationException (
203 "Cannot set a condition on an object not represented by an XML element in the project file.");
204 propertyGroup.SetAttribute ("Condition", value);
210 if (properties != null)
211 return properties.Count;
212 else if (propertiesByName != null)
213 return propertiesByName.Count;
215 throw new Exception ("PropertyGroup is not initialized.");
219 public bool IsImported {
225 internal bool FromXml {
227 return propertyGroup != null;
231 public BuildProperty this [string propertyName] {
234 throw new InvalidOperationException ("Properties in persisted property groups cannot be accessed by name.");
236 if (propertiesByName.ContainsKey (propertyName)) {
237 return propertiesByName [propertyName];
243 propertiesByName [propertyName] = value;
247 internal GroupingCollection GroupingCollection {
248 get { return parentCollection; }
249 set { parentCollection = value; }