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; } }
66 string keepDuplicates;
67 public string KeepDuplicates { get { return keepDuplicates ?? String.Empty; } set { keepDuplicates = value; } }
69 public string KeepMetadata { get { return keepMetadata ?? String.Empty; } set { keepMetadata = value; } }
70 string removeMetadata;
71 public string RemoveMetadata { get { return removeMetadata ?? String.Empty; } set { removeMetadata = value; } }
73 public ProjectMetadataElement AddMetadata (string name, string unevaluatedValue)
75 var metadata = ContainingProject.CreateMetadataElement (name, unevaluatedValue);
76 AppendChild (metadata);
79 internal override string XmlName {
80 get { return ItemType; }
82 internal override void SaveValue (XmlWriter writer)
84 SaveAttribute (writer, "Include", Include);
85 SaveAttribute (writer, "Exclude", Exclude);
87 SaveAttribute (writer, "KeepDuplicates", KeepDuplicates);
88 SaveAttribute (writer, "KeepMetadata", KeepMetadata);
89 SaveAttribute (writer, "RemoveMetadata", RemoveMetadata);
91 SaveAttribute (writer, "Remove", Remove);
92 base.SaveValue (writer);
95 internal override void LoadAttribute (string name, string value)
105 case "KeepDuplicates":
106 KeepDuplicates = value;
109 KeepMetadata = value;
111 case "RemoveMetadata":
112 RemoveMetadata = value;
119 base.LoadAttribute (name, value);
123 internal override void LoadValue (XmlReader reader)
125 if (string.IsNullOrWhiteSpace (Include) && string.IsNullOrEmpty (Remove))
126 throw new InvalidProjectFileException (Location, null, string.Format ("Both Include and Remove attribute are null or empty on '{0}' item", ItemType));
127 base.LoadValue (reader);
129 internal override ProjectElement LoadChildElement (XmlReader reader)
131 var metadata = ContainingProject.CreateMetadataElement (reader.LocalName);
132 AppendChild (metadata);
136 public ElementLocation ExcludeLocation { get; private set; }
137 public ElementLocation IncludeLocation { get; private set; }
138 public ElementLocation KeepDuplicatesLocation { get; private set; }
139 public ElementLocation RemoveLocation { get; private set; }
140 public ElementLocation RemoveMetadataLocation { get; private set; }
142 ElementLocation ExcludeLocation { get; set; }
143 ElementLocation IncludeLocation { get; set; }
144 ElementLocation KeepDuplicatesLocation { get; set; }
145 ElementLocation RemoveLocation { get; set; }
146 ElementLocation RemoveMetadataLocation { get; set; }