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