Merge pull request #1203 from esdrubal/protect
[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                 string keepDuplicates;
66                 string keepMetadata;
67                 string removeMetadata;
68                 #if NET_4_5
69                 public
70                 #else
71                 internal
72                 #endif
73                 string KeepDuplicates { get { return keepDuplicates ?? String.Empty; } set { keepDuplicates = value; } }
74                 #if NET_4_5
75                 public
76                 #else
77                 internal
78                 #endif
79                 string KeepMetadata { get { return keepMetadata ?? String.Empty; } set { keepMetadata = value; } }
80                 #if NET_4_5
81                 public
82                 #else
83                 internal
84                 #endif
85                 string RemoveMetadata { get { return removeMetadata ?? String.Empty; } set { removeMetadata = value; } }
86                 
87                 public ProjectMetadataElement AddMetadata (string name, string unevaluatedValue)
88                 {
89                         var metadata = ContainingProject.CreateMetadataElement (name, unevaluatedValue);
90                         AppendChild (metadata);
91                         return metadata;
92                 }
93                 internal override string XmlName {
94                         get { return ItemType; }
95                 }
96                 internal override void SaveValue (XmlWriter writer)
97                 {
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);
105                 }
106
107                 internal override void LoadAttribute (string name, string value)
108                 {
109                         switch (name) {
110                         case "Include":
111                                 Include = value;
112                                 break;
113                         case "Exclude":
114                                 Exclude = value;
115                                 break;
116                         case "KeepDuplicates":
117                                 KeepDuplicates = value;
118                                 break;
119                         case "KeepMetadata":
120                                 KeepMetadata = value;
121                                 break;
122                         case "RemoveMetadata":
123                                 RemoveMetadata = value;
124                                 break;
125                         case "Remove":
126                                 Remove = value;
127                                 break;
128                         default:
129                                 base.LoadAttribute (name, value);
130                                 break;
131                         }
132                 }
133                 internal override void LoadValue (XmlReader reader)
134                 {
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);
138                 }
139                 internal override ProjectElement LoadChildElement (XmlReader reader)
140                 {
141                         var metadata = ContainingProject.CreateMetadataElement (reader.LocalName);
142                         AppendChild (metadata);
143                         return metadata;
144                 }
145 #if NET_4_5
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; }
151 #else
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; }
157 #endif
158         }
159 }