3 // Sebastien Pouliot <sebastien@xamarin.com>
5 // Copyright 2013 Xamarin Inc. http://www.xamarin.com
7 // Permission is hereby granted, free of charge, to any person obtaining
8 // a copy of this software and associated documentation files (the
9 // "Software"), to deal in the Software without restriction, including
10 // without limitation the rights to use, copy, modify, merge, publish,
11 // distribute, sublicense, and/or sell copies of the Software, and to
12 // permit persons to whom the Software is furnished to do so, subject to
13 // the following conditions:
15 // The above copyright notice and this permission notice shall be
16 // included in all copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 using System.Collections.Generic;
31 using System.Xml.Linq;
33 namespace Xamarin.ApiDiff {
35 public abstract class MemberComparer : Comparer {
37 public abstract string GroupName { get; }
38 public abstract string ElementName { get; }
40 public void Compare (XElement source, XElement target)
42 var s = source.Element (GroupName);
43 var t = target.Element (GroupName);
44 if (XNode.DeepEquals (s, t))
49 foreach (var item in t.Elements (ElementName))
52 } else if (t == null) {
54 foreach (var item in s.Elements (ElementName))
58 Compare (s.Elements (ElementName), t.Elements (ElementName));
62 public override void SetContext (XElement current)
66 public XElement Source { get; set; }
68 public virtual bool Find (XElement e)
70 return e.GetAttribute ("name") == Source.GetAttribute ("name");
73 public override void Compare (IEnumerable<XElement> source, IEnumerable<XElement> target)
77 foreach (var s in source) {
80 var t = target.SingleOrDefault (Find);
82 // not in target, it was removed
91 // still in target so will be part of Added
96 // delayed, that way we show "Modified", "Added" and then "Removed"
98 foreach (var item in removed) {
108 // remaining == newly added in target
110 foreach (var item in target) {
112 if (State.IgnoreAdded.Contains (GetDescription (item)))
124 public abstract string GetDescription (XElement e);
126 protected StringBuilder GetObsoleteMessage (XElement e)
128 var sb = new StringBuilder ();
129 string o = e.GetObsoleteMessage ();
131 sb.Append ("[Obsolete (\"").Append (o).AppendLine ("\")]");
132 for (int i = 0; i < State.Indent + 1; i++)
138 public override bool Equals (XElement source, XElement target)
140 if (base.Equals (source, target))
143 return GetDescription (source) == GetDescription (target);
146 public virtual void BeforeAdding ()
148 Output.WriteLine ("<p>Added {0}:</p><pre>", GroupName);
151 public override void Added (XElement target)
153 Indent ().WriteLine ("\t{0}", GetDescription (target));
156 public virtual void AfterAdding ()
158 Output.WriteLine ("</pre>");
161 public override void Modified (XElement source, XElement target)
165 public virtual void BeforeRemoving ()
167 Output.WriteLine ("<p>Removed {0}:</p><pre>", GroupName);
170 public override void Removed (XElement source)
172 Indent ().WriteLine ("\t{0}", GetDescription (source));
175 public virtual void AfterRemoving ()
177 Output.WriteLine ("</pre>");