2 // valac -g --pkg gee-1.0 --pkg libxml-2.0 --pkg gobject-introspection-1.0 --pkg json-glib-1.0 Palete/Gir.vala -o /tmp/Gir
4 public static int main (string[] args) {
6 var g = Palete.Gir.factory("Gtk");
7 var test = g.classes.get("ToolButton");
10 var generator = new Json.Generator ();
11 var n = new Json.Node(Json.NodeType.OBJECT);
12 n.set_object(test.toJSON());
13 generator.set_root(n);
15 generator.pretty = true;
17 print(generator.to_data(null));
22 public errordomain GirError {
29 public class GirObject: Object {
32 public string propertyof;
34 public string nodetype;
35 public string package;
37 public GirObject paramset = null;
38 public GirObject return_value = null;
40 public bool is_instance;
42 public bool is_varargs;
43 public bool ctor_only; // specially added ctor properties..
52 public GirObject gparent;
53 public Gee.ArrayList<GirObject> params;
54 public Gee.ArrayList<string> implements;
55 public Gee.ArrayList<string> inherits; // full list of all classes and interfaces...
56 public Gee.HashMap<string,GirObject> ctors;
57 public Gee.HashMap<string,GirObject> methods;
58 public Gee.HashMap<string,string> includes;
59 public Gee.HashMap<string,GirObject> classes;
60 public Gee.HashMap<string,GirObject> props;
61 public Gee.HashMap<string,GirObject> consts;
62 public Gee.HashMap<string,GirObject> signals;
67 public GirObject(string nodetype, string n)
69 this.nodetype = nodetype;
75 this.is_array = false;
76 this.is_instance = false;
77 this.is_varargs = false;
78 this.ctor_only =false;
85 this.implements = new Gee.ArrayList<string>();
86 this.inherits = new Gee.ArrayList<string>(); // list of all ancestors. (interfaces and parents)
87 this.includes = new Gee.HashMap<string,string>();
89 this.params = new Gee.ArrayList<GirObject>();
90 this.ctors = new Gee.HashMap<string,GirObject>();
91 this.methods =new Gee.HashMap<string,GirObject>();
93 this.classes = new Gee.HashMap<string,GirObject>();
94 this.props = new Gee.HashMap<string,GirObject>();
95 this.consts = new Gee.HashMap<string,GirObject>();
96 this.signals = new Gee.HashMap<string,GirObject>();
97 this.is_overlaid = false;
101 public string[] inheritsToStringArray()
104 for(var i =0;i< this.inherits.size; i++) {
105 ret += this.inherits.get(i);
112 public void overlayParent()
115 if (this.parent.length < 1 || this.is_overlaid) {
116 this.is_overlaid = true;
119 // print("Overlaying " +this.name + " with " + this.parent + "\n");
121 var pcls = this.clsToObject( this.parent);
123 throw new GirError.INVALID_VALUE("Could not find class : " +
124 this.parent + " of " + this.name + " in " + this.ns);
127 pcls.overlayParent( );
128 this.copyFrom(pcls,false);
129 for(var i=0; i < this.implements.size; i++) {
130 var clsname = this.implements.get(i);
131 var picls = this.clsToObject(clsname);
132 this.copyFrom(picls,true);
134 this.is_overlaid = true;
138 public void overlayCtorProperties()
140 //print("Check overlay Ctor %s\n", this.name);
141 if (!this.ctors.has_key("new")) {
144 var ctor = this.ctors.get("new");
145 if (ctor.paramset == null || ctor.paramset.params.size < 1) {
148 //print("Found Ctor\n");
149 var iter = ctor.paramset.params.list_iterator();
150 while (iter.next()) {
151 var n = iter.get().name;
153 if (this.props.has_key(n)) {
159 //print("Adding prop %s\n", n);
162 var c = new GirObject("Prop",n);
165 c.propertyof = this.name;
166 c.type = iter.get().type;
168 this.props.set(n, c);
176 public GirObject clsToObject(string in_pn)
179 var gir = Gir.factory (this.ns);
180 if (in_pn.contains(".")) {
181 gir = Gir.factory(in_pn.split(".")[0]);
182 pn = in_pn.split(".")[1];
185 return gir.classes.get(pn);
189 public string fqn() {
190 return this.ns + this.name;
193 public void copyFrom(GirObject pcls, bool is_interface)
196 this.inherits.add(pcls.fqn());
197 var iter = pcls.methods.map_iterator();
199 if (null != this.methods.get(iter.get_key())) {
203 this.methods.set(iter.get_key(), iter.get_value());
206 iter = pcls.props.map_iterator();
208 if (null != this.props.get(iter.get_key())) {
212 this.props.set(iter.get_key(), iter.get_value());
215 iter = pcls.signals.map_iterator();
217 if (null != this.signals.get(iter.get_key())) {
221 this.signals.set(iter.get_key(), iter.get_value());
225 public Json.Object toJSON()
227 var r = new Json.Object();
228 r.set_string_member("nodetype", this.nodetype);
229 r.set_string_member("name", this.name);
230 if (this.propertyof.length > 0) {
231 r.set_string_member("of", this.propertyof);
233 if (this.type.length > 0) {
234 r.set_string_member("type", this.type);
236 if (this.parent != null && this.parent.length > 0) {
237 r.set_string_member("parent", this.parent);
239 if (this.sig.length > 0) {
240 r.set_string_member("sig", this.sig);
243 // is_arary / is_instance / is_varargs..
245 if (this.implements.size > 0) {
246 r.set_array_member("length", this.toJSONArrayString(this.implements));
249 if (this.params.size > 0) {
250 r.set_array_member("params", this.toJSONArrayObject(this.params));
252 if (this.ctors.size > 0) {
253 r.set_object_member("ctors", this.toJSONObject(this.ctors));
255 if (this.methods.size > 0) {
256 r.set_object_member("methods", this.toJSONObject(this.methods));
258 if (this.includes.size > 0) {
259 r.set_object_member("includes", this.toJSONObjectString(this.includes));
261 if (this.classes.size > 0) {
262 r.set_object_member("classes", this.toJSONObject(this.classes));
264 if (this.props.size > 0) {
265 r.set_object_member("props", this.toJSONObject(this.props));
267 if (this.consts.size > 0) {
268 r.set_object_member("consts", this.toJSONObject(this.consts));
270 if (this.signals.size > 0) {
271 r.set_object_member("signals", this.toJSONObject(this.signals));
273 if (this.paramset != null) {
274 r.set_object_member("params", this.paramset.toJSON());
276 if (this.return_value != null) {
277 r.set_object_member("return_value", this.return_value.toJSON());
281 public Json.Object toJSONObject(Gee.HashMap<string,GirObject> map)
283 var r = new Json.Object();
284 var iter = map.map_iterator();
286 r.set_object_member(iter.get_key(), iter.get_value().toJSON());
290 public Json.Object toJSONObjectString(Gee.HashMap<string,string> map)
292 var r = new Json.Object();
293 var iter = map.map_iterator();
295 r.set_string_member(iter.get_key(), iter.get_value());
299 public Json.Array toJSONArrayString(Gee.ArrayList<string> map)
301 var r = new Json.Array();
302 for(var i =0;i< map.size;i++) {
304 r.add_string_element(map.get(i));
308 public Json.Array toJSONArrayObject(Gee.ArrayList<GirObject> map)
310 var r = new Json.Array();
311 for(var i =0;i< map.size;i++) {
313 r.add_object_element(map.get(i).toJSON());
318 public GirObject fetchByFqn(string fqn) {
319 //print("Searching (%s)%s for %s\n", this.nodetype, this.name, fqn);
320 var bits = fqn.split(".");
322 var ret = this.classes.get(bits[0]);
324 if (bits.length < 2) {
327 return ret.fetchByFqn(fqn.substring(bits[0].length+1));
330 ret = this.ctors.get(bits[0]);
332 if (bits.length < 2) {
335 return ret.fetchByFqn(fqn.substring(bits[0].length+1));
338 ret = this.methods.get(bits[0]);
340 if (bits.length < 2) {
343 return ret.fetchByFqn(fqn.substring(bits[0].length+1));
345 ret = this.props.get(bits[0]);
347 if (bits.length < 2) {
350 return ret.fetchByFqn(fqn.substring(bits[0].length+1));
352 ret = this.consts.get(bits[0]);
354 if (bits.length < 2) {
357 return ret.fetchByFqn(fqn.substring(bits[0].length+1));
360 ret = this.signals.get(bits[0]);
362 if (bits.length < 2) {
365 return ret.fetchByFqn(fqn.substring(bits[0].length+1));
367 if (this.paramset == null) {
370 var iter = this.paramset.params.list_iterator();
371 while (iter.next()) {
373 if (p.name != bits[0]) {
379 // fixme - other queires? - enums?
383 public string fqtype() {
384 return Gir.fqtypeLookup(this.type, this.ns);
392 public class Gir : GirObject {
394 static Gee.HashMap<string,Gir> cache = null;
395 //Gee.Hashmap<string,what> nodes;
399 public static Gir? factory(string ns) {
401 cache = new Gee.HashMap<string,Gir>();
403 var ret = cache.get(ns);
406 var add = new Gir(ns);
409 var iter = add.classes.map_iterator();
411 iter.get_value().overlayParent();
413 // loop again and add the ctor properties.
414 iter = add.classes.map_iterator();
416 iter.get_value().overlayCtorProperties();
427 public static GirObject? factoryFqn(string fqn)
429 var bits = fqn.split(".");
430 if (bits.length < 1) {
434 var f = (GirObject)factory(bits[0]);
436 if (bits.length == 1 || f ==null) {
439 return f.fetchByFqn(fqn.substring(bits[0].length+1)); // since classes are stored in fqn format...?
446 * guess the fqn of a type == eg. gboolean or Widget etc...
448 public static string fqtypeLookup(string type, string ns) {
450 if (g.classes.has_key(type)) {
451 return ns + "." + type;
454 if (g.consts.has_key(type)) {
455 return ns + "." + type;
459 // look at includes..
460 var iter = g.includes.map_iterator();
462 var ret = fqtypeLookup(type, iter.get_key());
474 public static string guessDefaultValueForType(string type) {
475 if (type.contains(".")) {
486 return "?"+ type + "?";
494 public Gir (string ns)
496 var xns = ns == "Glade" ? "Gladeui" : ns;
497 var gi = GI.Repository.get_default();
498 gi.require(xns, null, 0);
500 var ver = gi.get_version(xns);
501 unowned GLib.SList<string> pth = GI.Repository.get_search_path ();
502 var gir_path = pth.nth_data(0).replace("/lib/girepository-1.0", "/share/gir-1.0");
507 var file = gir_path + "/" + xns + "-" + ver + ".gir";
508 // print("ns: " + ns + "\n");
509 // print("ver: " + ver + "\n");
515 //this.nodes = new Gee.Hashmap<string,what>();
517 var doc = Xml.Parser.parse_file (file);
518 var root = doc->get_root_element();
519 this.walk( root, (GirObject) this );
524 public void walk(Xml.Node* element, GirObject? parent)
526 var n = element->get_prop("name");
530 //print(element->name + " (" + parent.name + "==>" + n +")\n");
531 switch (element->name) {
536 parent.includes.set(n, element->get_prop("version"));
552 //break; // not handled..
555 var c = new GirObject("Class", parent.name + "." + n);
556 parent.classes.set(n, c);
558 c.parent = element->get_prop("parent");
560 if (c.parent == null) {
567 var c = new GirObject("Interface", parent.name + "." + n);
569 parent.classes.set(n, c);
572 c.parent = element->get_prop("parent");
573 if (c.parent == null) {
581 parent.doctxt = element->get_content();
585 parent.implements.add(n);
590 var c = new GirObject("Ctor",n);
593 parent.ctors.set(n,c);
598 var c = new GirObject("Return", "return-value");
601 parent.return_value = c;
605 case "virtual-method": // not sure...
608 var c = new GirObject("Signal",n);
609 parent.signals.set(n,c);
613 case "signal": // Glib:signal
614 var c = new GirObject("Signal",n.replace("-", "_"));
617 parent.signals.set(n.replace("-", "_"),c);
623 case "callback": // not sure...
630 return; // no children?
634 var c = new GirObject("Method",n);
637 c.propertyof = parent.name;
638 parent.methods.set(n,c);
643 var c = new GirObject("Paramset",n);
650 case "instance-parameter":
651 var c = new GirObject("Param",n);
654 c.is_instance = true;
655 parent.params.add(c);
660 var c = new GirObject("Param",n);
663 parent.params.add(c);
665 this.checkParamOverride(c);
670 var c = new GirObject("Prop",n.replace("-", "_"));
673 c.propertyof = parent.name;
674 parent.props.set(n.replace("-", "_"),c);
679 var c = new GirObject("Function",n);
682 parent.methods.set(n,c);
687 parent.is_array = true;
688 break; // type is added soon..
691 parent.is_varargs= true;
695 var c = new GirObject("Const",n);
697 c.value = element->get_prop("value");
699 parent.consts.set(n,c);
705 var c = new GirObject("Enum",n);
708 parent.consts.set(n,c);
714 var c = new GirObject("EnumMember",n);
717 c.value = element->get_prop("value");
718 parent.consts.set(n,c);
723 case "doc-deprecated":
726 case "record": // struct?
731 case "prerequisite": // ignore?
733 case "union": // ignore?
736 print("UNHANDLED Gir file element: " + element->name +"\n");
740 if (element->name == "signal") {
745 if (element->name == "return-value") {
746 path += ".return-value";
748 print(path + ":" + element->name + "\n");
750 //var d = getAttribute(element,'doc');
752 // Seed.print(path + ':' + d);
755 for (Xml.Node* iter = element->children; iter != null; iter = iter->next) {
756 if (iter->type == Xml.ElementType.TEXT_NODE) {
759 this.walk(iter, parent);
763 public string doc(string what)
765 var ar = what.split(".");
766 var cls = this.classes.get(ar[1]);
767 if (ar.length == 2) {
768 return cls.doctxt != null ? cls.doctxt : "";
770 // return the property.. by default..
771 var pr = cls.props.get(ar[2]);
772 return pr.doctxt != null ? pr.doctxt : "";
778 public void checkParamOverride(GirObject c)
780 var parset = c.gparent;
781 if (parset == null || parset.nodetype != "Paramset") {
784 var method = parset.gparent;
785 if (method == null || method.nodetype != "Ctor") {
788 var cls = method.gparent;
789 if (cls == null || cls.nodetype != "Class") {
795 c.name = this.fetchOverride( cls.name, method.name, c.name);
797 public static bool overrides_loaded = false;
798 public static Gee.HashMap<string,string> overrides;
800 public string fetchOverride( string cls, string method, string param)
802 // overrides should be in a file Gir.overides
803 // in that "Gtk.Label.new.str" : "label"
804 this.loadOverrides();
805 var key = "%s.%s.%s".printf(cls,method,param);
806 //print("Chekcing for key %s\n", key);
807 if (!overrides.has_key(key)) {
810 return overrides.get(key);
814 public void loadOverrides()
816 if (overrides_loaded) {
820 var pa = new Json.Parser();
821 pa.load_from_file(Builder4.Application.configDirectory() + "/resources/Gir.overides");
822 var node = pa.get_root();
824 if (node.get_node_type () != Json.NodeType.OBJECT) {
825 throw new GirError.INVALID_FORMAT ("Error loading gir.overides : Unexpected element type %s", node.type_name ());
827 overrides = new Gee.HashMap<string,string>();
830 var obj = node.get_object ();
833 obj.foreach_member((o , key, value) => {
836 var v = obj.get_string_member(key);
839 overrides.set(key, v);
843 overrides_loaded = true;