Merge pull request #799 from kebby/master
[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                         return GetTypeName (type.Replace ('+', '.'));
78                 }
79
80                 static string GetTypeName (string type)
81                 {
82                         if (type.StartsWith ("System.Nullable`1[", StringComparison.Ordinal))
83                                 return type.Substring (18, type.Length - 19) + "?";
84
85                         int pos = type.IndexOf ('`');
86                         if (pos >= 0) {
87                                 int end = type.LastIndexOf (']');
88                                 string subtype = type.Substring (pos + 3, end - pos - 3);
89                                 return type.Substring (0, pos) + "&lt;" + GetTypeName (subtype) + "&gt;";
90                         }
91
92                         switch (type) {
93                         case "System.String":
94                                 return "string";
95                         case "System.Int32":
96                                 return "int";
97                         case "System.UInt32":
98                                 return "uint";
99                         case "System.Int64":
100                                 return "long";
101                         case "System.UInt64":
102                                 return "ulong";
103                         case "System.Void":
104                                 return "void";
105                         case "System.Boolean":
106                                 return "bool";
107                         case "System.Object":
108                                 return "object";
109                         case "System.Single":
110                                 return "float";
111                         case "System.Double":
112                                 return "double";
113                         case "System.Byte":
114                                 return "byte";
115                         case "System.SByte":
116                                 return "sbyte";
117                         case "System.Int16":
118                                 return "short";
119                         case "System.UInt16":
120                                 return "ushort";
121                         case "System.Char":
122                                 return "char";
123                         default:
124                                 if (type.StartsWith (State.Namespace, StringComparison.Ordinal))
125                                         type = type.Substring (State.Namespace.Length + 1);
126                                 return type;
127                         }
128                 }
129         }
130 }