-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathXmlImporter.cs
More file actions
237 lines (190 loc) · 8.53 KB
/
XmlImporter.cs
File metadata and controls
237 lines (190 loc) · 8.53 KB
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using Java.Interop.Tools.JavaCallableWrappers.CallableWrapperMembers;
using Java.Interop.Tools.JavaCallableWrappers.Extensions;
using Java.Interop.Tools.TypeNameMappings;
namespace Java.Interop.Tools.JavaCallableWrappers.Adapters;
public static class XmlImporter
{
public static List<CallableWrapperType> Import (string filename)
{
using (var sr = new StreamReader (filename, Encoding.UTF8))
return Import (sr);
}
public static List<CallableWrapperType> Import (TextReader sr)
{
using (var xml = XmlReader.Create (sr))
return Import (xml);
}
public static List<CallableWrapperType> Import (XmlReader xml)
{
var doc = XDocument.Load (xml);
var types = new List<CallableWrapperType> ();
foreach (var type in doc.Root.Elements ("type"))
types.Add (ImportType (type));
return types;
}
public static List<CallableWrapperType> Import (XElement xml)
{
var types = new List<CallableWrapperType> ();
foreach (var type in xml.Elements ("type"))
types.Add (ImportType (type));
return types;
}
public static CallableWrapperType ImportType (XElement xml)
{
var name = xml.GetRequiredAttribute ("name");
var package = xml.GetAttributeOrDefault ("package", (string?) "");
var partial_assembly_qualified_name = xml.GetRequiredAttribute ("partial_assembly_qualified_name");
// If package is empty, generate a crc64 package name from the assembly qualified name
if (string.IsNullOrEmpty (package))
package = GeneratePackageFromAssemblyQualifiedName (partial_assembly_qualified_name);
var type = new CallableWrapperType (name, package ?? "", partial_assembly_qualified_name) {
ApplicationJavaClass = xml.GetAttributeOrDefault ("application_java_class", (string?) null),
ExtendsType = xml.GetAttributeOrDefault ("extends_type", (string?) null),
GenerateOnCreateOverrides = xml.GetAttributeOrDefault ("generate_on_create_overrides", false),
HasExport = xml.GetAttributeOrDefault ("has_export", false),
IsAbstract = xml.GetAttributeOrDefault ("is_abstract", false),
IsApplication = xml.GetAttributeOrDefault ("is_application", false),
IsInstrumentation = xml.GetAttributeOrDefault ("is_instrumentation", false),
MonoRuntimeInitialization = xml.GetAttributeOrDefault ("mono_runtime_initialization", (string?) null),
};
if (xml.GetAttributeOrDefault ("application_constructor", (string?) null) is string applicationConstructor)
type.ApplicationConstructor = new CallableWrapperApplicationConstructor (applicationConstructor);
ImportAnnotations (type.Annotations, xml.Element ("annotations"));
ImportImplementedInterfaces (type, xml.Element ("implemented_interfaces"));
ImportConstructors (type, xml.Element ("constructors"));
ImportMethods (type, xml.Element ("methods"));
ImportFields (type, xml.Element ("fields"));
foreach (var nestedType in xml.Elements ("nested_type"))
type.NestedTypes.Add (ImportType (nestedType));
return type;
}
static void ImportAnnotations (List<CallableWrapperTypeAnnotation> annotations, XElement? xml)
{
foreach (var annotation in xml?.Elements ("annotation") ?? []) {
var a = ImportAnnotation (annotation);
annotations.Add (a);
}
}
static CallableWrapperTypeAnnotation ImportAnnotation (XElement xml)
{
var name = xml.GetRequiredAttribute ("name");
var annotation = new CallableWrapperTypeAnnotation (name);
foreach (var property in xml.Elements ("property")) {
var p = ImportAnnotationProperty (property);
annotation.Properties.Add (p);
}
return annotation;
}
static KeyValuePair<string, string> ImportAnnotationProperty (XElement xml)
{
var name = xml.GetRequiredAttribute ("name");
var value = xml.GetRequiredAttribute ("value");
return new KeyValuePair<string, string> (name, value);
}
static void ImportImplementedInterfaces (CallableWrapperType type, XElement? xml)
{
foreach (var iface in xml?.Elements ("interface") ?? []) {
var name = iface.GetRequiredAttribute ("name");
type.ImplementedInterfaces.Add (name);
}
}
static void ImportConstructors (CallableWrapperType type, XElement? xml)
{
foreach (var ctor in xml?.Elements ("constructor") ?? []) {
var c = ImportConstructor (type, ctor);
type.Constructors.Add (c);
}
}
static void ImportMethods (CallableWrapperType type, XElement? xml)
{
foreach (var method in xml?.Elements ("method") ?? []) {
var m = ImportMethod (type, method);
type.Methods.Add (m);
}
}
static CallableWrapperConstructor ImportConstructor (CallableWrapperType type, XElement xml)
{
var name = xml.GetRequiredAttribute ("name");
var method = xml.GetRequiredAttribute ("method");
var jniSig = xml.GetRequiredAttribute ("jni_signature");
var ctor = new CallableWrapperConstructor (type, name, method, jniSig);
FillInMethodDetails (ctor, xml);
return ctor;
}
static CallableWrapperMethod ImportMethod (CallableWrapperType type, XElement xml)
{
var name = xml.GetRequiredAttribute ("name");
var method = xml.GetRequiredAttribute ("method");
var jniSig = xml.GetRequiredAttribute ("jni_signature");
var m = new CallableWrapperMethod (type, name, method, jniSig);
FillInMethodDetails (m, xml);
return m;
}
static void FillInMethodDetails (CallableWrapperMethod method, XElement xml)
{
// Common between constructors and methods
method.ManagedParameters = xml.GetAttributeOrDefault ("managed_parameters", (string?) null);
method.JavaNameOverride = xml.GetAttributeOrDefault ("java_name_override", (string?) null);
method.Params = xml.GetAttributeOrDefault ("params", (string?) null);
method.Retval = xml.GetAttributeOrDefault ("retval", (string?) null);
method.JavaAccess = xml.GetAttributeOrDefault ("java_access", (string?) null);
method.IsExport = xml.GetAttributeOrDefault ("is_export", false);
method.IsStatic = xml.GetAttributeOrDefault ("is_static", false);
method.IsDynamicallyRegistered = xml.GetAttributeOrDefault ("is_dynamically_registered", false);
method.SuperCall = xml.GetAttributeOrDefault ("super_call", (string?) null);
method.ActivateCall = xml.GetAttributeOrDefault ("activate_call", (string?) null);
if (xml.GetAttributeOrDefault ("thrown_type_names", (string?) null) is string thrownTypeNames)
method.ThrownTypeNames = thrownTypeNames.Split (new [] { ',' }, StringSplitOptions.RemoveEmptyEntries);
ImportAnnotations (method.Annotations, xml.Element ("annotations"));
}
static void ImportFields (CallableWrapperType type, XElement? xml)
{
foreach (var field in xml?.Elements ("field") ?? []) {
var f = ImportField (field);
type.Fields.Add (f);
}
}
static CallableWrapperField ImportField (XElement xml)
{
var name = xml.GetRequiredAttribute ("name");
var type = xml.GetRequiredAttribute ("type");
var visibility = xml.GetRequiredAttribute ("visibility");
var initializer_name = xml.GetRequiredAttribute ("initializer_name");
var field = new CallableWrapperField (name, type, visibility, initializer_name) {
IsStatic = xml.GetAttributeOrDefault ("is_static", false),
};
ImportAnnotations (field.Annotations, xml.Element ("annotations"));
return field;
}
/// <summary>
/// Generates a package name from a partial assembly qualified name.
/// This is used when the package attribute is missing or empty in the XML.
/// </summary>
/// <param name="partialAssemblyQualifiedName">The partial assembly qualified name in format "Namespace.TypeName, AssemblyName"</param>
/// <returns>A package name based on the current <see cref="JavaNativeTypeManager.PackageNamingPolicy"/></returns>
static string GeneratePackageFromAssemblyQualifiedName (string partialAssemblyQualifiedName)
{
// Format: "Namespace.TypeName, AssemblyName" or "Namespace.TypeName+NestedType, AssemblyName"
var commaIndex = partialAssemblyQualifiedName.IndexOf (',');
if (commaIndex < 0)
return "";
var fullTypeName = partialAssemblyQualifiedName.Substring (0, commaIndex).Trim ();
var assemblyName = partialAssemblyQualifiedName.Substring (commaIndex + 1).Trim ();
// Extract namespace from full type name
// Handle nested types by using '+' as separator
var plusIndex = fullTypeName.IndexOf ('+');
var typeNameWithoutNested = plusIndex >= 0 ? fullTypeName.Substring (0, plusIndex) : fullTypeName;
var lastDotIndex = typeNameWithoutNested.LastIndexOf ('.');
var ns = lastDotIndex >= 0 ? typeNameWithoutNested.Substring (0, lastDotIndex) : "";
// If no namespace, return empty (will use default package)
if (string.IsNullOrEmpty (ns))
return "";
return JavaNativeTypeManager.GetPackageName (ns, assemblyName);
}
}