This repository has been archived by the owner on Feb 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 180
/
Copy pathmonodroid-schema-gen.cs
executable file
·197 lines (167 loc) · 6.18 KB
/
monodroid-schema-gen.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Xml;
using System.Xml.Schema;
namespace Commons.AndroidSchemaGen
{
public class Driver
{
const string android_ns = "http://schemas.android.com/apk/res/android";
public static void Main (string [] args)
{
new Driver ().Run (args);
}
List<Assembly> asses = new List<Assembly> ();
List<Type> views = new List<Type> ();
Type view;
XmlDocument doc = new XmlDocument ();
List<string> java_views = new List<string> ();
Dictionary<string,string> java_base_types = new Dictionary<string,string> ();
Dictionary<string,List<string>> attributes;
// look for Android.Views.View
public void Run (string [] args)
{
LoadXml ();
foreach (string arg in args)
asses.Add (Assembly.ReflectionOnlyLoadFrom (arg));
foreach (var ass_ in asses)
foreach (var type in ass_.GetTypes ())
if (type.Name == "View" && type.FullName == "Android.Views.View") {
view = type;
break;
}
foreach (var ass_ in asses)
foreach (var type in ass_.GetTypes ())
if (IsView (type))
views.Add (type);
var xs = new XmlSchema ();
xs.Namespaces.Add ("xs", XmlSchema.Namespace);
xs.Namespaces.Add ("android", android_ns);
var xsimp = new XmlSchemaImport () {SchemaLocation = "schemas.android.com.apk.res.android.xsd", Namespace = android_ns};
xs.Includes.Add (xsimp);
var choice = new XmlSchemaChoice ();
var xsg = new XmlSchemaGroup () { Name = "any-view" };
xsg.Particle = choice;
xs.Items.Add (xsg);
var xseView = new XmlSchemaElement () { Name = "View", SchemaTypeName = new XmlQualifiedName ("View") };
xs.Items.Add (xseView);
var xsctView = new XmlSchemaComplexType () { Name = "View" };
xs.Items.Add (xsctView);
foreach (var att in CreateAttributes ("View"))
xsctView.Attributes.Add (att);
foreach (var type in views) {
if (type == view) // we define individually.
continue;
var nameString = StripGenericName (type);
if (!java_views.Contains (nameString)) {
Console.Error.WriteLine ("Skipped " + nameString);
continue;
}
// FIXME: should not be special case.
if (type.IsGenericType && nameString == "AdapterView")
continue;
// <xs:element name="FooBarView">
// <xs:complexType>
// <xs:complexContent>
// <xs:extension base="FooBarBaseView">
// <xs:group ref="any-view" minOccurs=0 maxOccurs="unbounded"> <!-- if it is ViewGroup (and can have children) -->
// </xs:extension>
var xse = new XmlSchemaElement () { Name = nameString, SchemaTypeName = new XmlQualifiedName (nameString) };
AddAnnotation (xse, "Runtime Type: " + type.FullName);
xs.Items.Add (xse);
var xsct = new XmlSchemaComplexType () { Name = nameString };
xs.Items.Add (xsct);
var xscm = new XmlSchemaComplexContent ();
xsct.ContentModel = xscm;
var xsce = new XmlSchemaComplexContentExtension () { BaseTypeName = new XmlQualifiedName (StripGenericName (type.BaseType)) };
xscm.Content = xsce;
// expand attributes
foreach (var att in CreateAttributes (xse.Name))
xsce.Attributes.Add (att);
// add ref-to-this-element element to "any-view" group which could be used for children's applicable group.
choice.Items.Add (new XmlSchemaElement () { RefName = new XmlQualifiedName (nameString) });
if (IsDirectViewManagerImplementor (type) && !IsDirectViewManagerImplementor (type.BaseType))
xsce.Particle = new XmlSchemaGroupRef () { RefName = new XmlQualifiedName ("any-view"), MinOccurs = 0, MaxOccursString = "unbounded" };
}
using (var xw = XmlWriter.Create ("android-layout-xml.xsd", new XmlWriterSettings { Indent = true }))
xs.Write (xw);
}
IEnumerable<XmlSchemaAttribute> CreateAttributes (string elem)
{
var lp = attributes.FirstOrDefault (p => p.Key == elem);
if (lp.Value != null)
foreach (var attr in lp.Value)
yield return new XmlSchemaAttribute () { RefName = new XmlQualifiedName (attr, android_ns) };
}
string StripGenericName (Type type)
{
return type.IsGenericType ? type.Name.Substring (0, type.Name.IndexOf ('`')) : type.Name;
}
void AddAnnotation (XmlSchemaAnnotated a, string text)
{
a.Annotation = a.Annotation ?? new XmlSchemaAnnotation ();
AddAnnotation (a.Annotation, text);
}
void AddAnnotation (XmlSchemaAnnotation a, string text)
{
a.Items.Add (new XmlSchemaDocumentation () { Markup = new XmlNode [] {doc.CreateTextNode (text) } });
}
bool IsDirectViewManagerImplementor (Type type)
{
return IsViewManager (type) && !IsViewManager (type.BaseType);
}
bool IsViewManager (Type type)
{
return type.GetInterfaces ().Any (t => t.FullName == "Android.Views.IViewManager");
}
bool IsView (Type type)
{
if (type == null)
return false;
if (type.BaseType == view)
return true;
if (views.Contains (type.BaseType))
return true;
if (!asses.Contains (type.Assembly))
return false;
return IsView (type.BaseType);
}
void LoadXml ()
{
var dic = new Dictionary<string,List<string>> ();
attributes = dic;
doc.Load ("all-known-attributes.xml");
foreach (XmlElement cls in doc.SelectNodes ("/android-attribute-defs/class")) {
var l = new List<string> ();
string name = cls.GetAttribute ("name");
// Attributes in nested type X_Y (e.g. LinearLayout_Layout) will be moved to X type.
if (name.IndexOf ('_') > 0) {
string subst = "View";//name.Substring (0, name.IndexOf ('_'));
Console.Error.WriteLine ("Merging {0} into {1}", name, subst);
name = subst;
}
if (dic.ContainsKey (name))
l = dic [name];
else
dic [name] = l;
foreach (XmlElement a in cls.SelectNodes ("a")) {
var an = a.InnerText;
string av = an.Substring (an.IndexOf (':') + 1);
if (!l.Contains (av))
l.Add (av);
}
}
doc.Load ("type-hierarchy.xml");
foreach (XmlElement cls in doc.SelectNodes ("/android-hierarchy/class")) {
string name = cls.GetAttribute ("name");
string bs = cls.GetAttribute ("base");
bs = bs.Substring (bs.LastIndexOf ('.') + 1);
java_views.Add (name);
java_base_types.Add (name, bs);
}
}
}
}