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 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 ArrayList ();
62 foreach (XmlElement xe in propertyGroup.ChildNodes) {
63 BuildProperty bp = new BuildProperty (parentProject, xe);
67 this.propertiesByName = CollectionsUtil.CreateCaseInsensitiveHashtable ();
70 public BuildProperty AddNewProperty (string propertyName,
73 return AddNewProperty (propertyName, propertyValue, false);
76 public BuildProperty AddNewProperty (string propertyName,
78 bool treatPropertyValueAsLiteral)
85 xe = propertyGroup.OwnerDocument.CreateElement (propertyName);
86 propertyGroup.AppendChild (xe);
87 if (treatPropertyValueAsLiteral) {
88 xe.InnerText = Utilities.Escape (propertyValue);
90 xe.InnerText = propertyValue;
92 prop = new BuildProperty (parentProject, xe);
94 prop = new BuildProperty (propertyName, propertyValue);
100 internal void AddProperty (BuildProperty property)
103 properties.Add (property);
105 if (propertiesByName.Contains (property.Name)) {
106 BuildProperty existing = (BuildProperty) propertiesByName [property.Name];
107 if (property.PropertyType <= existing.PropertyType) {
108 propertiesByName.Remove (property.Name);
109 propertiesByName.Add (property.Name, property);
112 propertiesByName.Add (property.Name, property);
120 throw new NotImplementedException ();
124 public BuildPropertyGroup Clone (bool deepClone)
126 throw new NotImplementedException ();
129 public IEnumerator GetEnumerator ()
131 if (properties != null) {
132 foreach (BuildProperty bp in properties)
134 } else if (propertiesByName != null) {
135 foreach (DictionaryEntry de in propertiesByName)
136 yield return (BuildProperty) de.Value;
138 throw new Exception ("PropertyGroup is not initialized.");
141 public void RemoveProperty (BuildProperty propertyToRemove)
143 if (properties == null)
144 throw new Exception ("PropertyGroup is not initialized.");
145 properties.Remove (propertyToRemove);
148 public void RemoveProperty (string propertyName)
150 if (propertiesByName == null)
151 throw new Exception ("PropertyGroup is not initialized.");
152 propertiesByName.Remove (propertyName);
155 public void SetProperty (string propertyName,
156 string propertyValue)
158 SetProperty (propertyName, propertyValue, false);
161 // FIXME: use treatPropertyValueAsLiteral
163 public void SetProperty (string propertyName,
164 string propertyValue,
165 bool treatPropertyValueAsLiteral)
167 if (propertiesByName.Contains (propertyName) == false) {
168 AddNewProperty (propertyName, propertyValue);
170 ((BuildProperty) propertiesByName [propertyName]).Value = propertyValue;
173 internal void Evaluate ()
176 throw new InvalidOperationException ();
178 foreach (BuildProperty bp in properties) {
183 public string Condition {
187 return propertyGroup.GetAttribute ("Condition");
191 throw new InvalidOperationException (
192 "Cannot set a condition on an object not represented by an XML element in the project file.");
193 propertyGroup.SetAttribute ("Condition", value);
199 if (properties != null)
200 return properties.Count;
201 else if (propertiesByName != null)
202 return propertiesByName.Count;
204 throw new Exception ("PropertyGroup is not initialized.");
208 public bool IsImported {
214 internal bool FromXml {
216 return propertyGroup != null;
220 public BuildProperty this [string propertyName] {
223 throw new InvalidOperationException ("Properties in persisted property groups cannot be accessed by name.");
225 if (propertiesByName.Contains (propertyName)) {
226 return (BuildProperty) propertiesByName [propertyName];
232 propertiesByName [propertyName] = value;
236 internal GroupingCollection GroupingCollection {
237 get { return parentCollection; }
238 set { parentCollection = value; }