3 // Sebastien Pouliot <sebastien@xamarin.com>
5 // Copyright 2013-2014 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;
30 using System.Reflection;
32 using System.Xml.Linq;
34 namespace Xamarin.ApiDiff {
36 public static class Helper {
37 public static bool IsTrue (this XElement self, string name)
39 return (self.GetAttribute (name) == "true");
42 public static string GetAttribute (this XElement self, string name)
44 var n = self.Attribute (name);
50 // null == no obsolete, String.Empty == no description
51 public static string GetObsoleteMessage (this XElement self)
53 var cattrs = self.Element ("attributes");
57 foreach (var ca in cattrs.Elements ("attribute")) {
58 if (ca.GetAttribute ("name") != "System.ObsoleteAttribute")
60 var props = ca.Element ("properties");
62 return String.Empty; // no description
63 foreach (var p in props.Elements ("property")) {
64 if (p.GetAttribute ("name") != "Message")
66 return p.GetAttribute ("value");
72 public static IEnumerable<XElement> Descendants (this XElement self, params string[] names)
78 for (int i = 0; i < names.Length - 1; i++) {
79 el = el.Element (names [i]);
83 return el.Elements (names [names.Length - 1]);
86 public static List<XElement> DescendantList (this XElement self, params string[] names)
88 var descendants = self.Descendants (names);
89 if (descendants == null)
91 return descendants.ToList ();
94 // make it beautiful (.NET -> C#)
95 public static string GetTypeName (this XElement self, string name)
97 string type = self.GetAttribute (name);
101 StringBuilder sb = null;
102 bool is_nullable = false;
103 if (type.StartsWith ("System.Nullable`1[", StringComparison.Ordinal)) {
105 sb = new StringBuilder (type, 18, type.Length - 19, 1024);
107 sb = new StringBuilder (type);
110 bool is_ref = (sb [sb.Length - 1] == '&');
112 sb.Remove (sb.Length - 1, 1);
115 while ((sb [sb.Length - 1] == ']') && (sb [sb.Length - 2] == '[')) {
116 sb.Remove (sb.Length - 2, 2);
120 bool is_pointer = (sb [sb.Length - 1] == '*');
122 sb.Remove (sb.Length - 1, 1);
124 type = GetTypeName (sb.Replace ('+', '.').ToString ());
127 sb.Append (self.GetAttribute ("direction")).Append (' ');
137 return sb.ToString ();
140 static string GetTypeName (string type)
142 int pos = type.IndexOf ('`');
144 int end = type.LastIndexOf (']');
145 string subtype = type.Substring (pos + 3, end - pos - 3);
146 return type.Substring (0, pos) + "<" + GetTypeName (subtype) + ">";
150 case "System.String":
154 case "System.UInt32":
158 case "System.UInt64":
162 case "System.Boolean":
164 case "System.Object":
166 case "System.Single":
168 case "System.Double":
176 case "System.UInt16":
184 case "System.nfloat":
186 case "System.IntPtr":
189 if (type.StartsWith (State.Namespace, StringComparison.Ordinal))
190 type = type.Substring (State.Namespace.Length + 1);
195 public static MethodAttributes GetMethodAttributes (this XElement element)
197 var srcAttribs = element.Attribute ("attrib");
198 return (MethodAttributes) (srcAttribs != null ? Int32.Parse (srcAttribs.Value) : 0);
201 public static FieldAttributes GetFieldAttributes (this XElement element)
203 var srcAttribs = element.Attribute ("attrib");
204 return (FieldAttributes) (srcAttribs != null ? Int32.Parse (srcAttribs.Value) : 0);