forked from dotnet/java-interop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnumMappings.cs
304 lines (256 loc) · 10.1 KB
/
EnumMappings.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using Mono.Options;
using Xamarin.Android.Tools;
namespace MonoDroid.Generation {
partial class EnumMappings {
public class EnumDescription {
public Dictionary<string, string> Members = new Dictionary<string, string> ();
public Dictionary<string, string> JniNames = new Dictionary<string, string> ();
public bool BitField;
public bool FieldsRemoved;
}
internal Dictionary<string, EnumDescription> ParseXmlFieldMappings (string csv, int filter_version, IList<KeyValuePair<string, string>> remove_nodes)
{
return ParseFieldMappings (FieldXmlToCsv (csv), null, filter_version, remove_nodes);
}
internal Dictionary<string, EnumDescription> ParseFieldMappings (string csv, string flagsFile, int filter_version, IList<KeyValuePair<string, string>> remove_nodes)
{
if (csv == null)
return new Dictionary<string, EnumDescription> ();
using (var csr = new StreamReader (csv))
return ParseFieldMappings (csr, flagsFile != null ? File.ReadAllLines (flagsFile) : null, filter_version, remove_nodes);
}
// key: Enum name
// value: Dictionary:
// key: enum element name
// value: enum element value
internal Dictionary<string, EnumDescription> ParseFieldMappings (TextReader source, string [] enumFlags, int filter_version, IList<KeyValuePair<string, string>> remove_nodes)
{
var enums = new Dictionary<string, EnumDescription> ();
if (source == null)
return enums;
bool transient = false;
string s;
string last_enum = null;
EnumDescription enumDescription = null;
while ((s = source.ReadLine ()) != null) {
try {
if (string.IsNullOrEmpty (s) || s.StartsWith ("//"))
continue;
if (s == "- ENTER TRANSIENT MODE -") {
transient = true;
continue;
}
string[] pieces = s.Split (',');
string verstr = pieces[0].Trim ();
string enu = pieces[1].Trim ();
string member = pieces[2].Trim ();
string java_name = pieces[3].Trim ();
string value = pieces[4].Trim ();
if (filter_version > 0 && filter_version < int.Parse (verstr))
continue;
if (!string.IsNullOrEmpty (java_name.Trim ()))
remove_nodes.Add (new KeyValuePair<string, string> (java_name.Trim (), transient ? enu : null));
// This is a line that only deletes a const, not maps it to an enum
if (string.IsNullOrEmpty (enu))
continue;
// If this is a new enum, add the old one and reset things
if (last_enum != enu) {
if (last_enum != null)
enums.Add (last_enum, enumDescription);
last_enum = enu;
enumDescription = new EnumDescription () { FieldsRemoved = !transient };
}
if (pieces.Length > 5)
enumDescription.BitField = pieces [5].Trim () == "Flags";
if (enumFlags != null && enumFlags.Contains (enu))
enumDescription.BitField = true;
// Add this member to the enum
enumDescription.Members.Add (member, value);
enumDescription.JniNames.Add (member, java_name);
} catch (Exception ex) {
Report.Error (Report.ErrorEnumMapping + 0, "ERROR at parsing enum " + s, ex);
throw;
}
}
// Make sure the last enum gets added to the list
if (last_enum != null)
enums.Add (last_enum, enumDescription);
return enums;
}
internal void ParseJniMember (string jniMember, out string package, out string type, out string member)
{
try {
DoParseJniMember (jniMember, out package, out type, out member);
} catch (Exception ex) {
throw new Exception (string.Format ("failed to parse enum mapping: JNI member: {0}", jniMember, ex));
}
}
static void DoParseJniMember (string jniMember, out string package, out string type, out string member)
{
int endPackage = jniMember.LastIndexOf ('/');
int endClass = jniMember.LastIndexOf ('.');
package = jniMember.Substring (0, endPackage).Replace ('/', '.');
if (package.StartsWith ("I:"))
package = package.Substring (2);
if (endClass >= 0) {
type = jniMember.Substring (endPackage + 1, endClass - endPackage - 1).Replace ('$', '.');
member = jniMember.Substring (endClass + 1);
} else {
type = jniMember.Substring (endPackage + 1).Replace ('$', '.');
member = "";
}
}
internal List<string> WriteEnumerations (string output_dir, Dictionary<string, EnumDescription> enums, GenBase [] gens, bool useShortFileNames)
{
if (!Directory.Exists (output_dir))
Directory.CreateDirectory (output_dir);
var files = new List<string> ();
foreach (var enu in enums) {
var path = Path.Combine (output_dir, GetFileName (enu.Key, useShortFileNames) + ".cs");
files.Add (path);
using (StreamWriter sw = new StreamWriter (path, append: false)) {
string ns = enu.Key.Substring (0, enu.Key.LastIndexOf ('.')).Trim ();
string enoom = enu.Key.Substring (enu.Key.LastIndexOf ('.') + 1).Trim ();
sw.WriteLine ("namespace {0} {{", ns);
if (enu.Value.BitField)
sw.WriteLine (" [System.Flags]");
sw.WriteLine (" public enum {0} {{", enoom);
foreach (var member in enu.Value.Members) {
var managedMember = FindManagedMember (ns, enoom, enu.Value, member.Key, gens);
if (managedMember != null)
sw.WriteLine (" [global::Android.Runtime.IntDefinition (\"" + managedMember + "\")]");
sw.WriteLine (" {0} = {1},", member.Key.Trim (), member.Value.Trim ());
}
sw.WriteLine (" }");
sw.WriteLine ("}");
}
}
return files;
}
string FindManagedMember (string ns, string enumName, EnumDescription desc, string enumFieldName, IEnumerable<GenBase> gens)
{
if (desc.FieldsRemoved)
return null;
var jniMember = desc.JniNames [enumFieldName];
if (string.IsNullOrWhiteSpace (jniMember)) {
// enum values like "None" falls here.
return null;
}
return FindManagedMember (jniMember, gens);
}
WeakReference cache_found_class;
string FindManagedMember (string jniMember, IEnumerable<GenBase> gens)
{
string package, type, member;
ParseJniMember (jniMember, out package, out type, out member);
var fullJavaType = (string.IsNullOrEmpty (package) ? "" : package + ".") + type;
var cls = cache_found_class != null ? cache_found_class.Target as GenBase : null;
if (cls == null || cls.JniName != fullJavaType) {
cls = gens.FirstOrDefault (g => g.JavaName == fullJavaType);
if (cls == null) {
// The class was not found e.g. removed by metadata fixup.
return null;
}
cache_found_class = new WeakReference (cls);
}
var fld = cls.Fields.FirstOrDefault (f => f.JavaName == member);
if (fld == null) {
// The field was not found e.g. removed by metadata fixup.
return null;
}
return cls.FullName + "." + fld.Name;
}
Dictionary<string,string> file_name_map = new Dictionary<string, string> ();
string GetFileName (string file, bool useShortFileNames)
{
if (!useShortFileNames)
return file;
string s;
if (file_name_map.TryGetValue (file, out s))
return s;
s = file_name_map.Count.ToString ();
file_name_map [file] = s;
return s;
}
internal void WriteEnumerationRegistrations (StreamWriter sw, Dictionary<string, EnumDescription> enums)
{
foreach (var enu in enums)
sw.WriteLine (" <add-node path=\"/api\"><enum name=\"{0}\" /></add-node>", enu.Key);
}
StringReader MethodXmlToCsv (string file)
{
if (file == null)
return null;
var sw = new StringWriter ();
var doc = new XmlDocument ();
doc.Load (file);
foreach (XmlElement e in doc.SelectNodes ("/enum-method-mappings/mapping")) {
string version = e.XGetAttribute ("api-level");
string package = null;
string java_class = null;
if (e.HasAttribute ("jni-class")) {
string c = e.XGetAttribute ("jni-class");
int s = c.LastIndexOf ('/');
package = c.Substring (0, s).Replace ('/', '.');
java_class = c.Substring (s+1).Replace ('$', '.');
} else if (e.HasAttribute ("jni-interface")) {
string c = e.XGetAttribute ("jni-interface");
int s = c.LastIndexOf ('/');
package = c.Substring (0, s).Replace ('/', '.');
java_class = "[Interface]" + c.Substring (s+1).Replace ('$', '.');
} else {
throw new InvalidOperationException (string.Format ("Missing mandatory attribute 'jni-class' or 'jni-interface' on a mapping element: {0}", e.OuterXml));
}
foreach (XmlElement m in e.SelectNodes ("method")) {
string method = GetMandatoryAttribute (m, "jni-name");
string parameter = GetMandatoryAttribute (m, "parameter");
string enum_type = GetMandatoryAttribute (m, "clr-enum-type");
sw.WriteLine ("{0}, {1}, {2}, {3}, {4}, {5}", String.IsNullOrEmpty (version) ? "0" : version, package, java_class, method, parameter, enum_type);
}
}
return new StringReader (sw.ToString ());
}
internal List<ApiTransform> ParseXmlMethodMappings (string file, int filter_version)
{
return ParseMethodMappings (MethodXmlToCsv (file), filter_version);
}
internal List<ApiTransform> ParseMethodMappings (string file, int filter_version)
{
if (file == null)
return new List<ApiTransform> ();
using (StreamReader sr = new StreamReader (file))
return ParseMethodMappings (sr, filter_version);
}
internal List<ApiTransform> ParseMethodMappings (TextReader source, int filter_version)
{
var list = new List<ApiTransform> ();
if (source == null)
return list;
string s;
bool preserveTypeMode = false;
while ((s = source.ReadLine ()) != null) {
if (s.Trim () == "---- PRESERVE TYPE MODE ----")
preserveTypeMode = true;
if (s.Length == 0 || s.StartsWith ("//", StringComparison.Ordinal))
continue;
var items = s.Split (',');
int ver;
if (filter_version > 0 && int.TryParse (items [0], out ver) && filter_version < ver)
continue;
try {
list.Add (new ApiTransform (preserveTypeMode, items));
} catch (Exception ex) {
Report.Error (Report.ErrorEnumMapping + 0, "Failed to process enum mapping. Text line: " + s, ex);
throw;
}
}
return list;
}
}
}