1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
|
//
// BuildPropertyGroup.cs: Represents a group of properties
//
// Author:
// Marek Sieradzki (marek.sieradzki@gmail.com)
//
// (C) 2005 Marek Sieradzki
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#if NET_2_0
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using System.Xml;
namespace Microsoft.Build.BuildEngine {
public class BuildPropertyGroup : IEnumerable {
XmlElement propertyGroup;
bool isImported;
GroupingCollection parentCollection;
Project parentProject;
List <BuildProperty> properties;
Dictionary <string, BuildProperty> propertiesByName;
public BuildPropertyGroup ()
: this (null, null)
{
}
internal BuildPropertyGroup (XmlElement xmlElement, Project project)
{
this.isImported = false;
this.parentCollection = null;
this.parentProject = project;
this.isImported = false;
this.propertyGroup = xmlElement;
if (FromXml) {
this.properties = new List <BuildProperty> ();
foreach (XmlNode xn in propertyGroup.ChildNodes) {
if (xn is XmlElement == false)
continue;
XmlElement xe = (XmlElement) xn;
BuildProperty bp = new BuildProperty (parentProject, xe);
AddProperty (bp);
}
} else
this.propertiesByName = new Dictionary <string, BuildProperty> (StringComparer.InvariantCultureIgnoreCase);
}
public BuildProperty AddNewProperty (string propertyName,
string propertyValue)
{
return AddNewProperty (propertyName, propertyValue, false);
}
public BuildProperty AddNewProperty (string propertyName,
string propertyValue,
bool treatPropertyValueAsLiteral)
{
BuildProperty prop;
if (FromXml) {
XmlElement xe;
xe = propertyGroup.OwnerDocument.CreateElement (propertyName);
propertyGroup.AppendChild (xe);
if (treatPropertyValueAsLiteral)
xe.InnerText = Utilities.Escape (propertyValue);
else
xe.InnerText = propertyValue;
prop = new BuildProperty (parentProject, xe);
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.ContainsKey (property.Name)) {
BuildProperty existing = propertiesByName [property.Name];
if (property.PropertyType <= existing.PropertyType) {
propertiesByName.Remove (property.Name);
propertiesByName.Add (property.Name, property);
}
} else {
propertiesByName.Add (property.Name, property);
}
}
}
[MonoTODO]
public void Clear ()
{
if (FromXml)
properties = new List <BuildProperty> ();
else
propertiesByName = new Dictionary <string, BuildProperty> ();
}
[MonoTODO]
public BuildPropertyGroup Clone (bool deepClone)
{
throw new NotImplementedException ();
}
public IEnumerator GetEnumerator ()
{
if (properties != null) {
foreach (BuildProperty bp in properties)
yield return bp;
} else if (propertiesByName != null) {
foreach (KeyValuePair <string, BuildProperty> kvp in propertiesByName)
yield return kvp.Value;
} else
throw new Exception ("PropertyGroup is not initialized.");
}
public void RemoveProperty (BuildProperty propertyToRemove)
{
if (properties == null)
throw new Exception ("PropertyGroup is not initialized.");
properties.Remove (propertyToRemove);
}
public void RemoveProperty (string propertyName)
{
if (propertiesByName == null)
throw new Exception ("PropertyGroup is not initialized.");
propertiesByName.Remove (propertyName);
}
public void SetProperty (string propertyName,
string propertyValue)
{
SetProperty (propertyName, propertyValue, false);
}
// FIXME: use treatPropertyValueAsLiteral
[MonoTODO]
public void SetProperty (string propertyName,
string propertyValue,
bool treatPropertyValueAsLiteral)
{
if (!propertiesByName.ContainsKey (propertyName)) {
BuildProperty bp = new BuildProperty (propertyName, propertyValue);
AddProperty (bp);
}
propertiesByName [propertyName].Value = propertyValue;
}
internal void Evaluate ()
{
if (!FromXml) {
throw new InvalidOperationException ();
}
foreach (BuildProperty bp in properties) {
if (bp.Condition == String.Empty)
bp.Evaluate ();
else {
ConditionExpression ce = ConditionParser.ParseCondition (bp.Condition);
if (ce.BoolEvaluate (parentProject))
bp.Evaluate ();
}
}
}
public string Condition {
get {
if (!FromXml)
return String.Empty;
return propertyGroup.GetAttribute ("Condition");
}
set {
if (!FromXml)
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)
return properties.Count;
else if (propertiesByName != null)
return propertiesByName.Count;
else
throw new Exception ("PropertyGroup is not initialized.");
}
}
public bool IsImported {
get {
return isImported;
}
}
internal bool FromXml {
get {
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;
}
}
internal GroupingCollection GroupingCollection {
get { return parentCollection; }
set { parentCollection = value; }
}
}
}
#endif
|