2 // ProjectElementContainer.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.
29 using System.Collections.Generic;
32 using Microsoft.Build.Exceptions;
33 using Microsoft.Build.Internal;
35 namespace Microsoft.Build.Construction
37 public abstract class ProjectElementContainer : ProjectElement
39 internal ProjectElementContainer () {}
40 LinkedList<ProjectElement> children = new LinkedList<ProjectElement> ();
42 public IEnumerable<ProjectElement> AllChildren {
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;
54 public ICollection<ProjectElement> Children {
55 get { return new CollectionFromEnumerable<ProjectElement> (
56 children.Where (p => !(p is ProjectCommentElement))); }
59 public ICollection<ProjectElement> ChildrenReversed {
60 get { return new CollectionFromEnumerable<ProjectElement> (
61 new ReverseEnumerable<ProjectElement> (children)); }
65 get { return children.Count; }
67 public ProjectElement FirstChild {
68 get { return children.First == null ? null : children.First.Value; }
71 public ProjectElement LastChild {
72 get { return children.Last == null ? null: children.Last.Value; }
76 public void AppendChild (ProjectElement child)
78 children.AddLast (child.LinkedListNode);
82 public void InsertAfterChild (ProjectElement child, ProjectElement reference)
84 if (reference == null) {
88 children.AddAfter (reference.LinkedListNode, child.LinkedListNode);
92 public void InsertBeforeChild (ProjectElement child, ProjectElement reference)
94 if (reference == null) {
98 children.AddBefore (reference.LinkedListNode, child.LinkedListNode);
102 public void PrependChild (ProjectElement child)
104 children.AddFirst (child.LinkedListNode);
108 public void RemoveAllChildren ()
110 foreach (var child in children)
114 public void RemoveChild (ProjectElement child)
117 children.Remove (child.LinkedListNode);
120 internal override void SaveValue (XmlWriter writer)
122 base.SaveValue (writer);
123 foreach (var child in children)
127 internal override void Load (XmlReader reader)
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);
141 internal override void LoadValue (XmlReader reader)
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);
155 internal abstract ProjectElement LoadChildElement (XmlReader reader);