Merge pull request #820 from brendanzagaeski/master
[mono.git] / mcs / class / Microsoft.Build / Microsoft.Build.Construction / ProjectItemElement.cs
1 //
2 // ProjectItemElementa.cs
3 //
4 // Author:
5 //   Leszek Ciesielski (skolima@gmail.com)
6 //
7 // (C) 2011 Leszek Ciesielski
8 //
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:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
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.
27 //
28
29 using System;
30 using System.Collections.Generic;
31 using System.Linq;
32 using Microsoft.Build.Internal;
33 using System.Xml;
34 using Microsoft.Build.Exceptions;
35
36 namespace Microsoft.Build.Construction
37 {
38         [System.Diagnostics.DebuggerDisplayAttribute ("{ItemType} Include={Include} Exclude={Exclude} "
39                                                       + "#Metadata={Count} Condition={Condition}")]
40         public class ProjectItemElement : ProjectElementContainer
41         {
42                 internal ProjectItemElement (string itemType, ProjectRootElement containingProject)
43                 {
44                         ItemType = itemType;
45                         ContainingProject = containingProject;
46                 }
47                 string exclude;
48                 public string Exclude { get { return exclude ?? String.Empty; } set { exclude = value; } }
49                 public bool HasMetadata {
50                         get {
51                                 var metadata = Metadata.FirstOrDefault ();
52                                 return metadata != null;
53                         }
54                 }
55                 string include;
56                 public string Include { get { return include ?? String.Empty; } set { include = value; } }
57                 string itemType;
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)); }
62                 }
63                 string @remove;
64                 public string Remove { get { return @remove ?? String.Empty; } set { @remove = value; } }
65                 #if NET_4_5
66                 string keepDuplicates;
67                 public string KeepDuplicates { get { return keepDuplicates ?? String.Empty; } set { keepDuplicates = value; } }
68                 string keepMetadata;
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; } }
72                 #endif
73                 public ProjectMetadataElement AddMetadata (string name, string unevaluatedValue)
74                 {
75                         var metadata = ContainingProject.CreateMetadataElement (name, unevaluatedValue);
76                         AppendChild (metadata);
77                         return metadata;
78                 }
79                 internal override string XmlName {
80                         get { return ItemType; }
81                 }
82                 internal override void SaveValue (XmlWriter writer)
83                 {
84                         SaveAttribute (writer, "Include", Include);
85                         SaveAttribute (writer, "Exclude", Exclude);
86 #if NET_4_5
87                         SaveAttribute (writer, "KeepDuplicates", KeepDuplicates);
88                         SaveAttribute (writer, "KeepMetadata", KeepMetadata);
89                         SaveAttribute (writer, "RemoveMetadata", RemoveMetadata);
90 #endif
91                         SaveAttribute (writer, "Remove", Remove);
92                         base.SaveValue (writer);
93                 }
94
95                 internal override void LoadAttribute (string name, string value)
96                 {
97                         switch (name) {
98                         case "Include":
99                                 Include = value;
100                                 break;
101                         case "Exclude":
102                                 Exclude = value;
103                                 break;
104 #if NET_4_5
105                         case "KeepDuplicates":
106                                 KeepDuplicates = value;
107                                 break;
108                         case "KeepMetadata":
109                                 KeepMetadata = value;
110                                 break;
111                         case "RemoveMetadata":
112                                 RemoveMetadata = value;
113                                 break;
114 #endif
115                         case "Remove":
116                                 Remove = value;
117                                 break;
118                         default:
119                                 base.LoadAttribute (name, value);
120                                 break;
121                         }
122                 }
123                 internal override void LoadValue (XmlReader reader)
124                 {
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);
128                 }
129                 internal override ProjectElement LoadChildElement (XmlReader reader)
130                 {
131                         var metadata = ContainingProject.CreateMetadataElement (reader.LocalName);
132                         AppendChild (metadata);
133                         return metadata;
134                 }
135 #if NET_4_5
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; }
141 #else
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; }
147 #endif
148         }
149 }