forked from dotnet/java-interop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApiFixup.cs
220 lines (202 loc) · 7.35 KB
/
ApiFixup.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using System.Xml.XPath;
using Xamarin.Android.Tools;
namespace MonoDroid.Generation
{
public class ApiFixup
{
XmlDocument api_doc;
string apiSource = "";
public string ApiSource { get { return apiSource; } }
public ApiFixup (XmlDocument apiDoc)
{
api_doc = apiDoc;
var api = api_doc.DocumentElement;
if (api != null)
apiSource = api.XGetAttribute ("api-source");
}
public void Process (IEnumerable<XmlDocument> metaDocs, string apiLevel, int productVersion)
{
foreach (var metaDoc in metaDocs)
Process (metaDoc, apiLevel, productVersion);
}
bool ShouldSkip (XPathNavigator node, int apiLevel, int productVersion)
{
if (apiLevel > 0) {
string apiSince = node.XGetAttribute ("api-since", "");
string apiUntil = node.XGetAttribute ("api-until", "");
if (!string.IsNullOrEmpty (apiSince) && int.Parse (apiSince) > apiLevel)
return true;
if (!string.IsNullOrEmpty (apiUntil) && int.Parse (apiUntil) < apiLevel)
return true;
}
if (productVersion > 0) {
var product_version = node.XGetAttribute ("product-version", "");
if (!string.IsNullOrEmpty (product_version) && int.Parse (product_version) > productVersion)
return true;
}
return false;
}
bool ShouldApply (XPathNavigator node)
{
if (!string.IsNullOrEmpty (apiSource)) {
var targetsource = node.XGetAttribute ("api-source", "");
if (string.IsNullOrEmpty (targetsource))
return true;
return targetsource == apiSource;
}
return true;
}
void Process (XmlDocument meta_doc, string apiLevelString, int productVersion)
{
XPathNavigator api_nav = api_doc.CreateNavigator ();
XPathNavigator meta_nav = meta_doc.CreateNavigator ();
int apiLevel = 0;
int.TryParse (apiLevelString, out apiLevel);
XPathNodeIterator metadata = meta_nav.Select ("/metadata/*");
string prev_path = null;
XPathNavigator attr_last_cache = null;
while (metadata.MoveNext ()) {
var metanav = metadata.Current;
if (ShouldSkip (metanav, apiLevel, productVersion))
continue;
if (!ShouldApply (metanav))
continue;
string path = metanav.XGetAttribute ("path", "");
if (path != prev_path)
attr_last_cache = null;
prev_path = path;
switch (metanav.LocalName) {
case "remove-node":
try {
XPathNodeIterator api_iter = api_nav.Select (path);
List<XmlElement> matches = new List<XmlElement> ();
while (api_iter.MoveNext ())
matches.Add (((IHasXmlNode)api_iter.Current).GetNode () as XmlElement);
foreach (XmlElement api_node in matches)
api_node.ParentNode.RemoveChild (api_node);
if (matches.Count == 0)
// BG8A00
Report.Warning (0, Report.WarningApiFixup + 0, "<remove-node path=\"{0}\"/> matched no nodes.", path);
} catch (XPathException e) {
// BG4A01
Report.Error (Report.ErrorApiFixup + 1, e, "Invalid XPath specification: {0}", path);
}
break;
case "add-node":
try {
XPathNodeIterator api_iter = api_nav.Select (path);
bool matched = false;
while (api_iter.MoveNext ()) {
XmlElement api_node = ((IHasXmlNode)api_iter.Current).GetNode () as XmlElement;
foreach (XmlNode child in ((IHasXmlNode)metanav).GetNode().ChildNodes)
api_node.AppendChild (api_doc.ImportNode (child, true));
matched = true;
}
if (!matched)
// BG8A01
Report.Warning (0, Report.WarningApiFixup + 1, "<add-node path=\"{0}\"/> matched no nodes.", path);
} catch (XPathException e) {
// BG4A02
Report.Error (Report.ErrorApiFixup + 2, e, "Invalid XPath specification: {0}", path);
}
break;
case "change-node":
try {
XPathNodeIterator api_iter = api_nav.Select (path);
bool matched = false;
while (api_iter.MoveNext ()) {
XmlElement node = ( (IHasXmlNode) api_iter.Current).GetNode () as XmlElement;
XmlElement parent = node.ParentNode as XmlElement;
XmlElement new_node = api_doc.CreateElement (metanav.Value);
foreach (XmlNode child in node.ChildNodes)
new_node.AppendChild (child.Clone ());
foreach (XmlAttribute attribute in node.Attributes)
new_node.Attributes.Append ( (XmlAttribute) attribute.Clone ());
parent.ReplaceChild (new_node, node);
matched = true;
}
if (!matched)
// BG8A03
Report.Warning (0, Report.WarningApiFixup + 3, "<change-node-type path=\"{0}\"/> matched no nodes.", path);
} catch (XPathException e) {
// BG4A03
Report.Error (Report.ErrorApiFixup + 3, e, "Invalid XPath specification: {0}", path);
}
break;
case "attr":
try {
string attr_name = metanav.XGetAttribute ("name", "");
if (string.IsNullOrEmpty (attr_name))
// BG4A07
Report.Error (Report.ErrorApiFixup + 7, "Target attribute name is not specified for path: {0}", path);
var nodes = attr_last_cache != null ?
(IEnumerable<XPathNavigator>) new XPathNavigator [] {attr_last_cache} :
api_nav.Select (path).OfType<XPathNavigator> ();
int attr_matched = 0;
foreach (var n in nodes) {
XmlElement node = ((IHasXmlNode) n).GetNode () as XmlElement;
node.SetAttribute (attr_name, metanav.Value);
//attr_last_cache = n;
attr_matched++;
}
if (attr_matched == 0)
// BG8A04
Report.Warning (0, Report.WarningApiFixup + 4, "<attr path=\"{0}\"/> matched no nodes.", path);
if (attr_matched != 1)
attr_last_cache = null;
} catch (XPathException e) {
// BG4A04
Report.Error (Report.ErrorApiFixup + 4, e, "Invalid XPath specification: {0}", path);
}
break;
case "move-node":
try {
XPathExpression expr = api_nav.Compile (path);
string parent = metanav.Value;
XPathNodeIterator parent_iter = api_nav.Select (parent);
bool matched = false;
while (parent_iter.MoveNext ()) {
XmlNode parent_node = ((IHasXmlNode)parent_iter.Current).GetNode ();
XPathNodeIterator path_iter = parent_iter.Current.Clone ().Select (expr);
while (path_iter.MoveNext ()) {
XmlNode node = ((IHasXmlNode)path_iter.Current).GetNode ();
parent_node.AppendChild (node.Clone ());
node.ParentNode.RemoveChild (node);
}
matched = true;
}
if (!matched)
// BG8A05
Report.Warning (0, Report.WarningApiFixup + 5, "<move-node path=\"{0}\"/> matched no nodes.", path);
} catch (XPathException e) {
// BG4A05
Report.Error (Report.ErrorApiFixup + 5, e, "Invalid XPath specification: {0}", path);
}
break;
case "remove-attr":
try {
string name = metanav.XGetAttribute ("name", "");
XPathNodeIterator api_iter = api_nav.Select (path);
bool matched = false;
while (api_iter.MoveNext ()) {
XmlElement node = ( (IHasXmlNode) api_iter.Current).GetNode () as XmlElement;
node.RemoveAttribute (name);
matched = true;
}
if (!matched)
// BG8A06
Report.Warning (0, Report.WarningApiFixup + 6, "<remove-attr path=\"{0}\"/> matched no nodes.", path);
} catch (XPathException e) {
// BG4A06
Report.Error (Report.ErrorApiFixup + 6, e, "Invalid XPath specification: {0}", path);
}
break;
}
}
}
}
}