forked from dotnet/java-interop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnumMap.cs
98 lines (85 loc) · 3.38 KB
/
EnumMap.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Xml;
using Xamarin.Android;
namespace MonoDroid.Generation
{
partial class EnumMappings
{
private string output_dir;
private string output_metadata;
private List<KeyValuePair<string, string>> remove_nodes;
private int version;
private bool fix_constants_instead_of_removing;
public EnumMappings (string outputDir, string outputMetadata, string version, bool fixConstantsInsteadOfRemove)
{
output_dir = outputDir;
output_metadata = outputMetadata;
this.version = version == null ? 0 : int.Parse (version);
fix_constants_instead_of_removing = fixConstantsInsteadOfRemove;
}
internal Dictionary<string,EnumDescription> Process (string fieldMap, string flagsFile, string methodMap)
{
remove_nodes = new List<KeyValuePair<string,string>> ();
var enums = (fieldMap ?? "").EndsWith (".csv")
? ParseFieldMappings (fieldMap, flagsFile, version, remove_nodes)
: ParseXmlFieldMappings (fieldMap, version, remove_nodes);
var methods = (methodMap ?? "").EndsWith (".csv")
? ParseMethodMappings (methodMap, version)
: ParseXmlMethodMappings (methodMap, version);
// Create output metadata file
using (StreamWriter sw = new StreamWriter (output_metadata, false)) {
sw.WriteLine ("<metadata>");
WriteEnumerationRegistrations (sw, enums);
foreach (var m in methods)
m.WriteTransform (sw);
if (fix_constants_instead_of_removing)
FixOldConstants (sw);
else
RemoveOldConstants (sw);
sw.WriteLine ("</metadata>");
}
return enums;
}
// <remove-node path="/api/package[@name='java.lang']/class[@name='Float']/field[@name='MAX_VALUE']" />
void RemoveOldConstants (StreamWriter sw)
{
foreach (var e in remove_nodes) {
string enu = e.Key;
string package, type, member;
ParseJniMember (enu, out package, out type, out member);
try {
sw.WriteLine (" <remove-node path=\"/api/package[@name='{0}']/{3}[@name='{1}']/field[@name='{2}']\" />",
package, type, member, enu.StartsWith ("I:") ? "interface" : "class");
} catch (Exception ex) {
Report.Error (Report.ErrorEnumMap + 0, "ERROR: failed to remove old comments: " + enu, ex);
throw;
}
}
}
void FixOldConstants (StreamWriter sw)
{
foreach (var pair in remove_nodes) {
var enu = pair.Key;
string package, type, member;
ParseJniMember (enu, out package, out type, out member);
if (pair.Value != null) {
sw.WriteLine (" <attr path=\"/api/package[@name='{0}']/{3}[@name='{1}']/field[@name='{2}']\" name=\"type\">{4}</attr>",
package, type, member, enu.StartsWith ("I:") ? "interface" : "class", pair.Value);
sw.WriteLine (" <attr path=\"/api/package[@name='{0}']/{3}[@name='{1}']/field[@name='{2}']\" name=\"deprecated\">This constant will be removed in the future version. Use {4} enum directly instead of this field.</attr>",
package, type, member, enu.StartsWith ("I:") ? "interface" : "class", pair.Value);
continue;
}
try {
sw.WriteLine (" <remove-node path=\"/api/package[@name='{0}']/{3}[@name='{1}']/field[@name='{2}']\" />",
package, type, member, enu.StartsWith ("I:") ? "interface" : "class");
} catch (Exception) {
Console.Error.WriteLine ("ERROR: failed to remove old comments: " + enu);
throw;
}
}
}
}
}