using System;
using System.Collections;
-using System.Collections.Specialized;
+using System.Collections.Generic;
using System.Reflection;
using System.Text;
using System.Xml;
namespace Microsoft.Build.BuildEngine {
public class BuildPropertyGroup : IEnumerable {
+ ImportedProject importedProject;
XmlElement propertyGroup;
- bool isImported;
GroupingCollection parentCollection;
Project parentProject;
- IList properties;
- IDictionary propertiesByName;
+ List <BuildProperty> properties;
+ Dictionary <string, BuildProperty> propertiesByName;
- internal bool FromXml {
- get {
- return propertyGroup != null;
- }
- }
-
public BuildPropertyGroup ()
- : this (null, null)
+ : this (null, null, null)
{
}
- internal BuildPropertyGroup (XmlElement xmlElement, Project project)
+ internal BuildPropertyGroup (XmlElement xmlElement, Project project, ImportedProject importedProject)
{
- this.isImported = false;
+ this.importedProject = importedProject;
this.parentCollection = null;
this.parentProject = project;
- this.isImported = false;
this.propertyGroup = xmlElement;
if (FromXml) {
- this.properties = new ArrayList ();
- foreach (XmlElement xe in propertyGroup.ChildNodes) {
+ this.properties = new List <BuildProperty> ();
+ foreach (XmlNode xn in propertyGroup.ChildNodes) {
+ if (!(xn is XmlElement))
+ continue;
+
+ XmlElement xe = (XmlElement) xn;
BuildProperty bp = new BuildProperty (parentProject, xe);
AddProperty (bp);
}
} else
- this.propertiesByName = CollectionsUtil.CreateCaseInsensitiveHashtable ();
+ this.propertiesByName = new Dictionary <string, BuildProperty> (StringComparer.InvariantCultureIgnoreCase);
}
public BuildProperty AddNewProperty (string propertyName,
xe = propertyGroup.OwnerDocument.CreateElement (propertyName);
propertyGroup.AppendChild (xe);
- if (treatPropertyValueAsLiteral) {
+
+ if (treatPropertyValueAsLiteral)
xe.InnerText = Utilities.Escape (propertyValue);
- } else {
+ else
xe.InnerText = propertyValue;
- }
+
prop = new BuildProperty (parentProject, xe);
- } else {
- prop = new BuildProperty (propertyName, propertyValue);
- }
- AddProperty (prop);
- return prop;
+ AddProperty (prop);
+ return prop;
+ } else
+ throw new InvalidOperationException ("This method is only valid for persisted <System.Object[]> elements.");
}
internal void AddProperty (BuildProperty property)
if (FromXml) {
properties.Add (property);
} else {
- if (propertiesByName.Contains (property.Name) == true) {
- BuildProperty existing = (BuildProperty) propertiesByName [property.Name];
+ if (propertiesByName.ContainsKey (property.Name)) {
+ BuildProperty existing = propertiesByName [property.Name];
if (property.PropertyType <= existing.PropertyType) {
propertiesByName.Remove (property.Name);
propertiesByName.Add (property.Name, property);
public void Clear ()
{
+ if (FromXml)
+ properties = new List <BuildProperty> ();
+ else
+ propertiesByName = new Dictionary <string, BuildProperty> ();
}
+ [MonoTODO]
public BuildPropertyGroup Clone (bool deepClone)
{
- return null;
+ throw new NotImplementedException ();
}
public IEnumerator GetEnumerator ()
{
- if (properties != null)
+ if (FromXml)
foreach (BuildProperty bp in properties)
yield return bp;
- else if (propertiesByName != null)
- foreach (DictionaryEntry de in propertiesByName)
- yield return (BuildProperty) de.Value;
- else
- throw new Exception ("PropertyGroup is not initialized.");
+ else
+ foreach (KeyValuePair <string, BuildProperty> kvp in propertiesByName)
+ yield return kvp.Value;
}
public void RemoveProperty (BuildProperty propertyToRemove)
{
- if (properties == null)
- throw new Exception ("PropertyGroup is not initialized.");
- properties.Remove (propertyToRemove);
+ if (FromXml)
+ properties.Remove (propertyToRemove);
+ else {
+ foreach (KeyValuePair <string, BuildProperty> kvp in propertiesByName)
+ if (kvp.Value == propertyToRemove) {
+ propertiesByName.Remove (kvp.Key);
+ break;
+ }
+ }
}
public void RemoveProperty (string propertyName)
{
- if (propertiesByName == null)
- throw new Exception ("PropertyGroup is not initialized.");
- propertiesByName.Remove (propertyName);
+ if (FromXml) {
+ foreach (BuildProperty bp in properties)
+ if (bp.Name == propertyName) {
+ properties.Remove (bp);
+ break;
+ }
+ } else
+ propertiesByName.Remove (propertyName);
}
public void SetProperty (string propertyName,
SetProperty (propertyName, propertyValue, false);
}
- // FIXME: use treatPropertyValueAsLiteral
- [MonoTODO]
public void SetProperty (string propertyName,
string propertyValue,
bool treatPropertyValueAsLiteral)
{
- if (propertiesByName.Contains (propertyName) == false) {
- AddNewProperty (propertyName, propertyValue);
+ if (!propertiesByName.ContainsKey (propertyName)) {
+ BuildProperty bp = new BuildProperty (propertyName, propertyValue);
+ AddProperty (bp);
}
- ((BuildProperty) propertiesByName [propertyName]).Value = propertyValue;
+
+ if (treatPropertyValueAsLiteral)
+ propertiesByName [propertyName].Value = Utilities.Escape (propertyValue);
+ else
+ propertiesByName [propertyName].Value = propertyValue;
}
internal void Evaluate ()
throw new InvalidOperationException ();
}
foreach (BuildProperty bp in properties) {
- bp.Evaluate ();
+ if (bp.Condition == String.Empty)
+ bp.Evaluate ();
+ else {
+ ConditionExpression ce = ConditionParser.ParseCondition (bp.Condition);
+ if (ce.BoolEvaluate (parentProject))
+ bp.Evaluate ();
+ }
}
}
}
set {
if (!FromXml)
- throw new InvalidOperationException ("Can only set condition on xml elements.");
+ throw new InvalidOperationException (
+ "Cannot set a condition on an object not represented by an XML element in the project file.");
propertyGroup.SetAttribute ("Condition", value);
}
}
public int Count {
get {
- if (properties != null)
+ if (FromXml)
return properties.Count;
- else if (propertiesByName != null)
- return propertiesByName.Count;
else
- throw new Exception ("PropertyGroup is not initialized.");
+ return propertiesByName.Count;
}
}
public bool IsImported {
get {
- return isImported;
+ return importedProject != null;
}
}
- public BuildProperty this[string propertyName] {
+ internal bool FromXml {
get {
- if (propertiesByName.Contains (propertyName)) {
- return (BuildProperty) propertiesByName [propertyName];
- } else {
+ return propertyGroup != null;
+ }
+ }
+
+ public BuildProperty this [string propertyName] {
+ get {
+ if (FromXml)
+ throw new InvalidOperationException ("Properties in persisted property groups cannot be accessed by name.");
+
+ if (propertiesByName.ContainsKey (propertyName))
+ return propertiesByName [propertyName];
+ else
return null;
- }
}
set {
propertiesByName [propertyName] = value;