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 : ItemPropertyGroupingBase, IBuildPropertyGroup, IEnumerable {
40 XmlElement propertyGroup;
41 XmlAttribute condition;
42 string importedFromFilename;
44 GroupingCollection parentCollection;
45 Project parentProject;
47 IDictionary propertiesByName;
49 public BuildPropertyGroup ()
54 internal BuildPropertyGroup (bool forXml, Project project)
56 this.propertyGroup = null;
57 this.condition = null;
58 this.importedFromFilename = null;
59 this.isImported = false;
60 this.parentCollection = null;
61 this.parentProject = project;
63 this.properties = new ArrayList ();
65 this.propertiesByName = CollectionsUtil.CreateCaseInsensitiveHashtable ();
68 public BuildProperty AddNewProperty (string propertyName,
71 return AddNewProperty (propertyName, propertyValue,
75 internal BuildProperty AddNewProperty (string propertyName,
77 PropertyType propertyType)
79 BuildProperty added, existing;
81 added = new BuildProperty (propertyName, propertyValue);
82 added.PropertyType = propertyType;
83 if (properties != null) {
84 properties.Add (added);
85 } else if (propertiesByName != null) {
86 if (propertiesByName.Contains (propertyName) == true) {
87 existing = (BuildProperty) propertiesByName [added.Name];
88 if (added.PropertyType <= existing.PropertyType) {
89 propertiesByName.Remove (added.Name);
90 propertiesByName.Add (added.Name, added);
93 propertiesByName.Add (added.Name, added);
96 throw new Exception ("PropertyGroup is not initialized.");
100 internal void AddFromExistingProperty (BuildProperty buildProperty)
102 BuildProperty added, existing;
104 added = buildProperty.Clone (false);
105 if (propertiesByName.Contains (added.Name) == true) {
106 existing = (BuildProperty) propertiesByName [added.Name];
107 if (added.PropertyType <= existing.PropertyType) {
108 propertiesByName.Remove (added.Name);
109 propertiesByName.Add (added.Name, added);
112 propertiesByName.Add (added.Name, added);
119 public BuildPropertyGroup Clone (bool deepClone)
124 // FIXME: what it is doing?
125 public override void Evaluate (BuildPropertyGroup evaluatedPropertyBag,
126 bool ignoreCondition,
128 Hashtable conditionedPropertiesTable,
133 public IEnumerator GetEnumerator ()
135 if (properties != null)
136 foreach (BuildProperty bp in properties)
138 else if (propertiesByName != null)
139 foreach (DictionaryEntry de in propertiesByName)
140 yield return (BuildProperty) de.Value;
142 throw new Exception ("PropertyGroup is not initialized.");
145 public void GetStringArraysForAllProperties (out string[] propertyNames,
146 out string[] propertyValues,
147 out string[] propertyFinalValues)
149 propertyNames = null;
150 propertyValues = null;
151 propertyFinalValues = null;
153 if (properties != null) {
154 foreach (BuildProperty bp in properties) {
155 propertyNames [i] = bp.Name;
156 propertyValues [i] = bp.Value;
157 propertyFinalValues [i] = bp.FinalValue;
160 } else if (propertiesByName != null) {
161 foreach (DictionaryEntry de in propertiesByName) {
162 propertyNames [i] = ((BuildProperty) de.Value).Name;
163 propertyValues [i] = ((BuildProperty) de.Value).Value;
164 propertyFinalValues [i] = ((BuildProperty) de.Value).FinalValue;
170 public void RemoveProperty (BuildProperty propertyToRemove)
172 if (properties == null)
173 throw new Exception ("PropertyGroup is not initialized.");
174 properties.Remove (propertyToRemove);
177 public void RemovePropertyByName (string propertyNameToRemove)
179 if (propertiesByName == null)
180 throw new Exception ("PropertyGroup is not initialized.");
181 propertiesByName.Remove (propertyNameToRemove);
184 public void SetProperty (string propertyName,
185 string propertyValue)
187 if (propertiesByName.Contains (propertyName) == false) {
188 AddNewProperty (propertyName, propertyValue);
190 ((BuildProperty) propertiesByName [propertyName]).Value = propertyValue;
193 internal void BindToXml (XmlElement propertyGroupElement)
195 if (propertyGroupElement == null)
196 throw new ArgumentNullException ();
197 this.properties = new ArrayList ();
198 this.propertyGroup = propertyGroupElement;
199 this.condition = propertyGroupElement.GetAttributeNode ("Condition");
200 this.importedFromFilename = null;
201 this.isImported = false;
202 foreach (XmlElement xe in propertyGroupElement.ChildNodes) {
203 BuildProperty bp = AddNewProperty(xe.Name, xe.InnerText);
204 bp.PropertyType = PropertyType.Normal;
206 Expression finalValue = new Expression (parentProject, bp.Value);
207 bp.FinalValue = (string) finalValue.ToNonArray (typeof (string));
208 parentProject.EvaluatedProperties.AddFromExistingProperty (bp);
212 public string Condition {
214 if (condition == null)
217 return condition.Value;
220 if (condition != null)
221 condition.Value = value;
227 if (properties != null)
228 return properties.Count;
229 else if (propertiesByName != null)
230 return propertiesByName.Count;
232 throw new Exception ("PropertyGroup is not initialized.");
236 public string ImportedFromFilename {
238 return importedFromFilename;
242 public bool IsImported {
248 public BuildProperty this[string propertyName] {
250 if (propertiesByName.Contains (propertyName)) {
251 return (BuildProperty) propertiesByName [propertyName];
257 propertiesByName [propertyName] = value;
261 internal GroupingCollection GroupingCollection {
262 get { return parentCollection; }
263 set { parentCollection = value; }