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.
29 using System.Xml.Linq;
31 namespace Xamarin.ApiDiff {
33 public static class Helper {
35 public static bool IsTrue (this XElement self, string name)
37 return (self.GetAttribute (name) == "true");
40 public static string GetAttribute (this XElement self, string name)
42 var n = self.Attribute (name);
48 // null == no obsolete, String.Empty == no description
49 public static string GetObsoleteMessage (this XElement self)
51 var cattrs = self.Element ("attributes");
55 foreach (var ca in cattrs.Elements ("attribute")) {
56 if (ca.GetAttribute ("name") != "System.ObsoleteAttribute")
58 var props = ca.Element ("properties");
60 return String.Empty; // no description
61 foreach (var p in props.Elements ("property")) {
62 if (p.GetAttribute ("name") != "Message")
64 return p.GetAttribute ("value");
70 // make it beautiful (.NET -> C#)
71 public static string GetTypeName (this XElement self, string name)
73 string type = self.GetAttribute (name);
77 StringBuilder sb = null;
78 bool is_nullable = false;
79 if (type.StartsWith ("System.Nullable`1[", StringComparison.Ordinal)) {
81 sb = new StringBuilder (type, 18, type.Length - 19, 1024);
83 sb = new StringBuilder (type);
86 bool is_ref = (sb [sb.Length - 1] == '&');
88 sb.Remove (sb.Length - 1, 1);
91 while ((sb [sb.Length - 1] == ']') && (sb [sb.Length - 2] == '[')) {
92 sb.Remove (sb.Length - 2, 2);
96 bool is_pointer = (sb [sb.Length - 1] == '*');
98 sb.Remove (sb.Length - 1, 1);
100 type = GetTypeName (sb.Replace ('+', '.').ToString ());
103 sb.Append (self.GetAttribute ("direction")).Append (' ');
113 return sb.ToString ();
116 static string GetTypeName (string type)
118 int pos = type.IndexOf ('`');
120 int end = type.LastIndexOf (']');
121 string subtype = type.Substring (pos + 3, end - pos - 3);
122 return type.Substring (0, pos) + "<" + GetTypeName (subtype) + ">";
126 case "System.String":
130 case "System.UInt32":
134 case "System.UInt64":
138 case "System.Boolean":
140 case "System.Object":
142 case "System.Single":
144 case "System.Double":
152 case "System.UInt16":
157 if (type.StartsWith (State.Namespace, StringComparison.Ordinal))
158 type = type.Substring (State.Namespace.Length + 1);