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.Specialized;
33 using System.Reflection;
37 namespace Microsoft.Build.BuildEngine {
38 public class BuildPropertyGroup : IEnumerable {
40 XmlElement propertyGroup;
42 GroupingCollection parentCollection;
43 Project parentProject;
45 IDictionary propertiesByName;
47 internal bool FromXml {
49 return propertyGroup != null;
53 public BuildPropertyGroup ()
58 internal BuildPropertyGroup (XmlElement xmlElement, Project project)
60 this.isImported = false;
61 this.parentCollection = null;
62 this.parentProject = project;
63 this.isImported = false;
64 this.propertyGroup = xmlElement;
67 this.properties = new ArrayList ();
68 foreach (XmlElement xe in propertyGroup.ChildNodes) {
69 BuildProperty bp = new BuildProperty (parentProject, xe);
73 this.propertiesByName = CollectionsUtil.CreateCaseInsensitiveHashtable ();
76 public BuildProperty AddNewProperty (string propertyName,
79 return AddNewProperty (propertyName, propertyValue, false);
82 public BuildProperty AddNewProperty (string propertyName,
84 bool treatPropertyValueAsLiteral)
91 xe = propertyGroup.OwnerDocument.CreateElement (propertyName);
92 propertyGroup.AppendChild (xe);
93 if (treatPropertyValueAsLiteral) {
94 xe.InnerText = Utilities.Escape (propertyValue);
96 xe.InnerText = propertyValue;
98 prop = new BuildProperty (parentProject, xe);
100 prop = new BuildProperty (propertyName, propertyValue);
106 internal void AddProperty (BuildProperty property)
109 properties.Add (property);
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);
118 propertiesByName.Add (property.Name, property);
127 public BuildPropertyGroup Clone (bool deepClone)
132 public IEnumerator GetEnumerator ()
134 if (properties != null)
135 foreach (BuildProperty bp in properties)
137 else if (propertiesByName != null)
138 foreach (DictionaryEntry de in propertiesByName)
139 yield return (BuildProperty) de.Value;
141 throw new Exception ("PropertyGroup is not initialized.");
144 public void RemoveProperty (BuildProperty propertyToRemove)
146 if (properties == null)
147 throw new Exception ("PropertyGroup is not initialized.");
148 properties.Remove (propertyToRemove);
151 public void RemoveProperty (string propertyName)
153 if (propertiesByName == null)
154 throw new Exception ("PropertyGroup is not initialized.");
155 propertiesByName.Remove (propertyName);
158 public void SetProperty (string propertyName,
159 string propertyValue)
161 SetProperty (propertyName, propertyValue, false);
164 // FIXME: use treatPropertyValueAsLiteral
166 public void SetProperty (string propertyName,
167 string propertyValue,
168 bool treatPropertyValueAsLiteral)
170 if (propertiesByName.Contains (propertyName) == false) {
171 AddNewProperty (propertyName, propertyValue);
173 ((BuildProperty) propertiesByName [propertyName]).Value = propertyValue;
176 internal void Evaluate ()
179 throw new InvalidOperationException ();
181 foreach (BuildProperty bp in properties) {
186 public string Condition {
188 return propertyGroup.GetAttribute ("Condition");
191 propertyGroup.SetAttribute ("Condition", value);
197 if (properties != null)
198 return properties.Count;
199 else if (propertiesByName != null)
200 return propertiesByName.Count;
202 throw new Exception ("PropertyGroup is not initialized.");
206 public bool IsImported {
212 public BuildProperty this[string propertyName] {
214 if (propertiesByName.Contains (propertyName)) {
215 return (BuildProperty) propertiesByName [propertyName];
221 propertiesByName [propertyName] = value;
225 internal GroupingCollection GroupingCollection {
226 get { return parentCollection; }
227 set { parentCollection = value; }