Fix potential NRE and give better output for fields and some types
[mono.git] / mcs / tools / corcompare / mono-api-html / Helpers.cs
1 // 
2 // Authors
3 //    Sebastien Pouliot  <sebastien@xamarin.com>
4 //
5 // Copyright 2013 Xamarin Inc. http://www.xamarin.com
6 // 
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:
14 //
15 // The above copyright notice and this permission notice shall be
16 // included in all copies or substantial portions of the Software.
17 //
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.
25 //
26
27 using System;
28 using System.Xml.Linq;
29
30 namespace Xamarin.ApiDiff {
31
32         public static class Helper {
33
34                 public static bool IsTrue (this XElement self, string name)
35                 {
36                         return (self.GetAttribute (name) == "true");
37                 }
38
39                 public static string GetAttribute (this XElement self, string name)
40                 {
41                         var n = self.Attribute (name);
42                         if (n == null)
43                                 return null;
44                         return n.Value;
45                 }
46
47                 // null == no obsolete, String.Empty == no description
48                 public static string GetObsoleteMessage (this XElement self)
49                 {
50                         var cattrs = self.Element ("attributes");
51                         if (cattrs == null)
52                                 return null;
53
54                         foreach (var ca in cattrs.Elements ("attribute")) {
55                                 if (ca.GetAttribute ("name") != "System.ObsoleteAttribute")
56                                         continue;
57                                 var props = ca.Element ("properties");
58                                 if (props == null)
59                                         return String.Empty; // no description
60                                 foreach (var p in props.Elements ("property")) {
61                                         if (p.GetAttribute ("name") != "Message")
62                                                 continue;
63                                         return p.GetAttribute ("value");
64                                 }
65                         }
66                         return null;
67                 }
68
69                 // make it beautiful (.NET -> C#)
70                 public static string GetTypeName (this XElement self, string name)
71                 {
72                         string type = self.GetAttribute (name);
73                         if (type == null)
74                                 return null;
75
76                         // inner types
77                         type = type.Replace ('+', '.');
78
79                         if (type.StartsWith ("System.Nullable`1[", StringComparison.Ordinal))
80                                 return type.Substring (18, type.Length - 19) + "?";
81
82                         int pos = type.IndexOf ('`');
83                         if (pos >= 0)
84                                 return type.Substring (0, pos) + "&lt;" + type.Substring (pos + 3).Replace ("]", "&gt;");
85
86                         switch (type) {
87                         case "System.String":
88                                 return "string";
89                         case "System.Int32":
90                                 return "int";
91                         case "System.UInt32":
92                                 return "uint";
93                         case "System.Int64":
94                                 return "long";
95                         case "System.UInt64":
96                                 return "ulong";
97                         case "System.Void":
98                                 return "void";
99                         case "System.Boolean":
100                                 return "bool";
101                         case "System.Object":
102                                 return "object";
103                         case "System.Single":
104                                 return "float";
105                         case "System.Double":
106                                 return "double";
107                         case "System.Byte":
108                                 return "byte";
109                         case "System.SByte":
110                                 return "sbyte";
111                         case "System.Int16":
112                                 return "short";
113                         case "System.UInt16":
114                                 return "ushort";
115                         default:
116                                 if (type.StartsWith (State.Namespace, StringComparison.Ordinal))
117                                         type = type.Substring (State.Namespace.Length + 1);
118                                 return type;
119                         }
120                 }
121         }
122 }