Merge pull request #4540 from kumpera/android-changes-part1
[mono.git] / mcs / class / Microsoft.Build / Microsoft.Build.Construction / ProjectElementContainer.cs
1 //
2 // ProjectElementContainer.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.Collections.Generic;
30 using System.Linq;
31 using System.Xml;
32 using Microsoft.Build.Exceptions;
33 using Microsoft.Build.Internal;
34
35 namespace Microsoft.Build.Construction
36 {
37         public abstract class ProjectElementContainer : ProjectElement
38         {
39                 internal ProjectElementContainer () {}
40                 LinkedList<ProjectElement> children = new LinkedList<ProjectElement> ();
41
42                 public IEnumerable<ProjectElement> AllChildren {
43                         get {
44                                 foreach (var child in Children) {
45                                         var container = child as ProjectElementContainer;
46                                         if (container != null)
47                                                 foreach (var containersChild in container.AllChildren)
48                                                         yield return containersChild;
49                                         yield return child;
50                                 }
51                         }
52                 }
53
54                 public ICollection<ProjectElement> Children {
55                         get { return new CollectionFromEnumerable<ProjectElement> (
56                                 children.Where (p => !(p is ProjectCommentElement))); }
57                 }
58
59                 public ICollection<ProjectElement> ChildrenReversed {
60                         get { return new CollectionFromEnumerable<ProjectElement> (
61                                 new ReverseEnumerable<ProjectElement> (children)); }
62                 }
63
64                 public int Count {
65                         get { return children.Count; }
66                 }
67                 public ProjectElement FirstChild {
68                         get { return children.First == null ? null : children.First.Value; }
69                         private set { }
70                 }
71                 public ProjectElement LastChild {
72                         get { return children.Last == null ? null: children.Last.Value; }
73                         private set { }
74                 }
75
76                 public void AppendChild (ProjectElement child)
77                 {
78                         children.AddLast (child.LinkedListNode);
79                         child.Parent = this;
80                 }
81
82                 public void InsertAfterChild (ProjectElement child, ProjectElement reference)
83                 {
84                         if (reference == null) {
85                                 PrependChild (child);
86                         } else {
87                                 child.Parent = this;
88                                 children.AddAfter (reference.LinkedListNode, child.LinkedListNode);
89                         }
90                 }
91
92                 public void InsertBeforeChild (ProjectElement child, ProjectElement reference)
93                 {
94                         if (reference == null) {
95                                 AppendChild (child);
96                         } else {
97                                 child.Parent = this;
98                                 children.AddBefore (reference.LinkedListNode, child.LinkedListNode);
99                         }
100                 }
101
102                 public void PrependChild (ProjectElement child)
103                 {
104                         children.AddFirst (child.LinkedListNode);
105                         child.Parent = this;
106                 }
107
108                 public void RemoveAllChildren ()
109                 {
110                         foreach (var child in children)
111                                 RemoveChild (child);
112                 }
113
114                 public void RemoveChild (ProjectElement child)
115                 {
116                         child.Parent = null;
117                         children.Remove (child.LinkedListNode);
118                 }
119
120                 internal override void SaveValue (XmlWriter writer)
121                 {
122                         base.SaveValue (writer);
123                         foreach (var child in children)
124                                 child.Save (writer);
125                 }
126
127                 internal override void Load (XmlReader reader)
128                 {
129                         reader.Read ();
130                         reader.MoveToContent ();
131                         FillLocation (reader);
132                         if (reader.LocalName != XmlName || reader.NamespaceURI != MSBuildNamespace)
133                                 throw CreateError (reader, string.Format ("Unexpected XML {0} \"{1}\" in namespace \"{2}\" appeared, while \"{3}\" in namespace \"{4}\" is expected.",
134                                                 reader.NodeType, reader.LocalName, reader.NamespaceURI, XmlName, MSBuildNamespace), -1);
135                         while (reader.MoveToNextAttribute ()) {
136                                 LoadAttribute (reader.Name, reader.Value);
137                         }
138                         LoadValue (reader);
139                 }
140
141                 internal override void LoadValue (XmlReader reader)
142                 {
143                         while (reader.Read ()) {
144                                 if (reader.NodeType == XmlNodeType.Element) {
145                                         var child = LoadChildElement (reader);
146                                         child.Load (reader.ReadSubtree ());
147                                 } else if (reader.NodeType == XmlNodeType.Comment) {
148                                         var commentElement = new ProjectCommentElement (ContainingProject);
149                                         commentElement.Load (reader);
150                                         AppendChild (commentElement);
151                                 }
152                         }
153                 }
154
155                 internal abstract ProjectElement LoadChildElement (XmlReader reader);
156         }
157 }