2 // ProjectItemElementa.cs
5 // Leszek Ciesielski (skolima@gmail.com)
7 // (C) 2011 Leszek Ciesielski
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.
30 using System.Collections.Generic;
32 using Microsoft.Build.Internal;
34 using Microsoft.Build.Exceptions;
36 namespace Microsoft.Build.Construction
38 [System.Diagnostics.DebuggerDisplayAttribute ("{ItemType} Include={Include} Exclude={Exclude} "
39 + "#Metadata={Count} Condition={Condition}")]
40 public class ProjectItemElement : ProjectElementContainer
42 internal ProjectItemElement (string itemType, ProjectRootElement containingProject)
45 ContainingProject = containingProject;
48 public string Exclude { get { return exclude ?? String.Empty; } set { exclude = value; } }
49 public bool HasMetadata {
51 var metadata = Metadata.FirstOrDefault ();
52 return metadata != null;
56 public string Include { get { return include ?? String.Empty; } set { include = value; } }
58 public string ItemType { get { return itemType ?? String.Empty; } set { itemType = value; } }
59 public ICollection<ProjectMetadataElement> Metadata {
60 get { return new CollectionFromEnumerable<ProjectMetadataElement> (
61 new FilteredEnumerable<ProjectMetadataElement> (Children)); }
64 public string Remove { get { return @remove ?? String.Empty; } set { @remove = value; } }
65 string keepDuplicates;
67 string removeMetadata;
73 string KeepDuplicates { get { return keepDuplicates ?? String.Empty; } set { keepDuplicates = value; } }
79 string KeepMetadata { get { return keepMetadata ?? String.Empty; } set { keepMetadata = value; } }
85 string RemoveMetadata { get { return removeMetadata ?? String.Empty; } set { removeMetadata = value; } }
87 public ProjectMetadataElement AddMetadata (string name, string unevaluatedValue)
89 var metadata = ContainingProject.CreateMetadataElement (name, unevaluatedValue);
90 AppendChild (metadata);
93 internal override string XmlName {
94 get { return ItemType; }
96 internal override void SaveValue (XmlWriter writer)
98 SaveAttribute (writer, "Include", Include);
99 SaveAttribute (writer, "Exclude", Exclude);
100 SaveAttribute (writer, "KeepDuplicates", KeepDuplicates);
101 SaveAttribute (writer, "KeepMetadata", KeepMetadata);
102 SaveAttribute (writer, "RemoveMetadata", RemoveMetadata);
103 SaveAttribute (writer, "Remove", Remove);
104 base.SaveValue (writer);
107 internal override void LoadAttribute (string name, string value)
116 case "KeepDuplicates":
117 KeepDuplicates = value;
120 KeepMetadata = value;
122 case "RemoveMetadata":
123 RemoveMetadata = value;
129 base.LoadAttribute (name, value);
133 internal override void LoadValue (XmlReader reader)
135 if (string.IsNullOrWhiteSpace (Include) && string.IsNullOrEmpty (Remove))
136 throw new InvalidProjectFileException (Location, null, string.Format ("Both Include and Remove attribute are null or empty on '{0}' item", ItemType));
137 base.LoadValue (reader);
139 internal override ProjectElement LoadChildElement (XmlReader reader)
141 var metadata = ContainingProject.CreateMetadataElement (reader.LocalName);
142 AppendChild (metadata);
146 public ElementLocation ExcludeLocation { get; private set; }
147 public ElementLocation IncludeLocation { get; private set; }
148 public ElementLocation KeepDuplicatesLocation { get; private set; }
149 public ElementLocation RemoveLocation { get; private set; }
150 public ElementLocation RemoveMetadataLocation { get; private set; }
152 ElementLocation ExcludeLocation { get; set; }
153 ElementLocation IncludeLocation { get; set; }
154 ElementLocation KeepDuplicatesLocation { get; set; }
155 ElementLocation RemoveLocation { get; set; }
156 ElementLocation RemoveMetadataLocation { get; set; }