2 // valac --pkg libxml-2.0 --pkg gee-1.0 Gir.vala -o /tmp/Gir
3 public static int main (string[] args) {
5 var g = new Palete.Gir("/usr/share/gir-1.0/Gtk-3.0.gir");
12 public class GirObject: Object {
15 public string nodetype;
16 public GirObject paramset;
17 public string package;
18 public GirObject return_value = null;
19 public bool is_instance;
21 public bool is_varargs;
25 public Gee.HashMap<string,GirObject> params;
26 public GLib.List<string> implements;
27 public GLib.List<GirObject> ctors;
28 public Gee.HashMap<string,GirObject> methods;
29 public Gee.HashMap<string,string> includes;
30 public Gee.HashMap<string,GirObject> classes;
31 public Gee.HashMap<string,GirObject> props;
32 public Gee.HashMap<string,GirObject> consts;
33 public Gee.HashMap<string,GirObject> signals;
35 public GirObject(string nodetype, string n) {
36 this.nodetype = nodetype;
38 this.params = new Gee.HashMap<string,GirObject>();
40 this.is_array = false;
41 this.is_instance = false;
42 this.is_varargs = false;
43 this.implements = new GLib.List<string>();
44 this.ctors = new GLib.List<GirObject>();
45 this.methods =new Gee.HashMap<string,GirObject>();
46 this.includes = new Gee.HashMap<string,string>();
47 this.classes= new Gee.HashMap<string,GirObject>();
48 this.props= new Gee.HashMap<string,GirObject>();
49 this.consts = new Gee.HashMap<string,GirObject>();
50 this.signals = new Gee.HashMap<string,GirObject>();
52 public JSON.Object toJSON()
54 var r = new JSON.Object();
55 r.set_string_member("nodetype", this.nodetype);
56 r.set_string_member("name", this.name);
57 // is_arary / is_instance / is_varargs..
59 if (this.implements.length() > 0) {
60 r.set_object_member("length", this.toJSONArray(this.implements));
63 if (this.params.size > 0) {
64 r.set_object_member("params", this.toJSONObject(this.params));
66 if (this.ctors.size > 0) {
67 r.set_object_member("ctors", this.toJSONObject(this.ctors));
69 if (this.methods.size > 0) {
70 r.set_object_member("methods", this.toJSONObject(this.methods));
72 if (this.includes.size > 0) {
73 r.set_object_member("includes", this.toJSONObject(this.includes));
75 if (this.classes.size > 0) {
76 r.set_object_member("classes", this.toJSONObject(this.classes));
78 if (this.props.size > 0) {
79 r.set_object_member("props", this.toJSONObject(this.props));
81 if (this.consts.size > 0) {
82 r.set_object_member("consts", this.toJSONObject(this.consts));
84 if (this.signals.size > 0) {
85 r.set_object_member("cosignalsnsts", this.toJSONObject(this.signals));
94 public class Gir : GirObject {
96 public string package;
99 //Gee.Hashmap<string,what> nodes;
101 public Gir (string file)
103 base("Package",file);
104 //this.nodes = new Gee.Hashmap<string,what>();
106 var doc = Xml.Parser.parse_file (file);
107 var root = doc->get_root_element();
108 this.walk( root, (GirObject) this );
112 public void walk(Xml.Node* element, GirObject? parent)
114 var n = element->get_prop("name");
118 print(element->name + " (" + parent.name + "==>" + n +")\n");
119 switch (element->name) {
124 parent.includes.set(n, element->get_prop("version"));
140 //break; // not handled..
143 var c = new GirObject("Class", parent.name + "." + n);
144 parent.classes.set(parent.name + "." + n, c);
145 c.parent = element->get_prop("parent");
150 var c = new GirObject("Interface", parent.name + "." + n);
151 parent.classes.set(parent.name + "." + n, c);
152 c.parent = element->get_prop("parent");
158 parent.doc = element->get_content();
162 parent.implements.append(n);
166 var c = new GirObject("Ctor",n);
167 parent.ctors.append(c);
168 parent = (GirObject)c;
172 var c = new GirObject("Return", "return-value");
173 parent.return_value = c;
174 parent = (GirObject)c;
177 case "virtual-method": // not sure...
180 var c = new GirObject("Signal",n);
181 parent.signals.set(n,c);
185 case "signal": // Glib:signal
186 var c = new GirObject("Signal",n);
187 parent.signals.set(n,c);
193 case "callback": // not sure...
199 return; // no children?
203 var c = new GirObject("Method",n);
204 parent.methods.set(n,c);
209 var c = new GirObject("Paramset",n);
214 case "instance-parameter":
215 var c = new GirObject("Param",n);
216 c.is_instance = true;
217 parent.params.set(n,c);
222 var c = new GirObject("Param",n);
223 parent.params.set(n,c);
229 var c = new GirObject("Prop",n);
230 parent.props.set(n,c);
235 var c = new GirObject("Function",n);
236 parent.methods.set(n,c);
241 parent.is_array = true;
242 break; // type is added soon..
245 parent.is_varargs= true;
249 var c = new GirObject("Const",n);
250 c.value = element->get_prop("value");
251 parent.consts.set(n,c);
257 var c = new GirObject("Enum",n);
258 parent.consts.set(n,c);
263 var c = new GirObject("EnumMember",n);
264 c.value = element->get_prop("value");
265 parent.consts.set(n,c);
270 case "doc-deprecated":
273 case "record": // struct?
275 case "bitfield": // ignore?
277 case "prerequisite": // ignore?
280 print("UNHANDLED" + element->name +"\n");
284 if (element->name == "signal") {
289 if (element->name == "return-value") {
290 path += ".return-value";
292 print(path + ":" + element->name + "\n");
294 //var d = getAttribute(element,'doc');
296 // Seed.print(path + ':' + d);
299 for (Xml.Node* iter = element->children; iter != null; iter = iter->next) {
300 if (iter->type == Xml.ElementType.TEXT_NODE) {
303 this.walk(iter, parent);