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.Exceptions;
34 namespace Microsoft.Build.Construction
36 public abstract class ProjectElement
38 internal const string MSBuildNamespace = "http://schemas.microsoft.com/developer/msbuild/2003";
40 internal ProjectElement ()
42 linkedListNode = new LinkedListNode<ProjectElement> (this);
44 public ProjectRootElement ContainingProject { get; internal set; }
45 public ProjectElement PreviousSibling {
46 get { return LinkedListNode.Previous == null ? null : LinkedListNode.Previous.Value; }
49 public ProjectElementContainer Parent { get; internal set; }
50 public ProjectElement NextSibling {
51 get { return LinkedListNode.Next == null ? null : LinkedListNode.Next.Value; }
55 public string Label { get { return label ?? String.Empty; } set { label = value; } }
57 public virtual string Condition { get { return condition ?? String.Empty; } set { condition = value; } }
58 public IEnumerable<ProjectElementContainer> AllParents {
61 while (parent != null) {
63 parent = parent.Parent;
67 readonly LinkedListNode<ProjectElement> linkedListNode;
68 internal LinkedListNode<ProjectElement> LinkedListNode { get { return linkedListNode; } }
69 internal virtual void Load (XmlReader reader)
71 reader.ReadToFollowing (XmlName);
72 FillLocation (reader);
73 while (reader.MoveToNextAttribute ()) {
74 LoadAttribute (reader.Name, reader.Value);
76 reader.MoveToElement ();
79 internal virtual void LoadAttribute (string name, string value)
91 throw new InvalidProjectFileException (Location, null, string.Format (
92 "Attribute \"{0}\" is not known on node \"{1}\" [type {2}].", name, XmlName,
96 internal virtual void LoadValue (XmlReader reader)
99 internal abstract string XmlName { get; }
100 internal virtual void Save (XmlWriter writer)
102 writer.WriteStartElement (XmlName);
104 writer.WriteEndElement ();
106 internal virtual void SaveValue (XmlWriter writer)
108 SaveAttribute (writer, "Label", Label);
109 SaveAttribute (writer, "Condition", Condition);
111 internal void SaveAttribute (XmlWriter writer, string attributeName, string attributeValue)
113 if (!string.IsNullOrWhiteSpace (attributeValue))
114 writer.WriteAttributeString (attributeName, attributeValue);
118 public ElementLocation Location { get; private set; }
119 public ElementLocation LabelLocation { get; private set; }
120 public ElementLocation ConditionLocation { get; private set; }
122 internal ElementLocation Location { get; private set; }
123 internal ElementLocation LabelLocation { get; private set; }
124 internal ElementLocation ConditionLocation { get; private set; }
127 internal void FillLocation (XmlReader reader)
129 var l = reader as IXmlLineInfo;
130 if (l != null && l.HasLineInfo ())
131 Location = new ProjectElementLocation (reader.BaseURI, l);
132 if (reader.MoveToAttribute ("Condition") && l.HasLineInfo ())
133 ConditionLocation = new ProjectElementLocation (reader.BaseURI, l);
134 if (reader.MoveToAttribute ("Label") && l.HasLineInfo ())
135 LabelLocation = new ProjectElementLocation (reader.BaseURI, l);
136 reader.MoveToElement ();
139 class ProjectElementLocation : ElementLocation
141 public ProjectElementLocation (string file, IXmlLineInfo li)
144 this.line = li.LineNumber;
145 this.column = li.LinePosition;
152 public override string File { get { return file; } }
153 public override int Line { get { return line; } }
154 public override int Column { get { return column; } }
157 internal InvalidProjectFileException CreateError (XmlReader reader, string message, int columnOffset = 0)
159 var li = reader as IXmlLineInfo;
160 bool valid = li != null && li.HasLineInfo ();
161 throw new InvalidProjectFileException (reader.BaseURI, valid ? li.LineNumber : 0, valid ? li.LinePosition + columnOffset : 0, 0, 0, message, null, null, null);