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 // not sure if fqn really is correct here...
192 return this.type == "Class" ? this.name : (this.ns + this.name);
195 public void copyFrom(GirObject pcls, bool is_interface)
198 this.inherits.add(pcls.fqn());
199 var iter = pcls.methods.map_iterator();
201 if (null != this.methods.get(iter.get_key())) {
205 this.methods.set(iter.get_key(), iter.get_value());
208 iter = pcls.props.map_iterator();
210 if (null != this.props.get(iter.get_key())) {
214 this.props.set(iter.get_key(), iter.get_value());
217 iter = pcls.signals.map_iterator();
219 if (null != this.signals.get(iter.get_key())) {
223 this.signals.set(iter.get_key(), iter.get_value());
227 public Json.Object toJSON()
229 var r = new Json.Object();
230 r.set_string_member("nodetype", this.nodetype);
231 r.set_string_member("name", this.name);
232 if (this.propertyof.length > 0) {
233 r.set_string_member("of", this.propertyof);
235 if (this.type.length > 0) {
236 r.set_string_member("type", this.type);
238 if (this.parent != null && this.parent.length > 0) {
239 r.set_string_member("parent", this.parent);
241 if (this.sig.length > 0) {
242 r.set_string_member("sig", this.sig);
245 // is_arary / is_instance / is_varargs..
247 if (this.implements.size > 0) {
248 r.set_array_member("length", this.toJSONArrayString(this.implements));
251 if (this.params.size > 0) {
252 r.set_array_member("params", this.toJSONArrayObject(this.params));
254 if (this.ctors.size > 0) {
255 r.set_object_member("ctors", this.toJSONObject(this.ctors));
257 if (this.methods.size > 0) {
258 r.set_object_member("methods", this.toJSONObject(this.methods));
260 if (this.includes.size > 0) {
261 r.set_object_member("includes", this.toJSONObjectString(this.includes));
263 if (this.classes.size > 0) {
264 r.set_object_member("classes", this.toJSONObject(this.classes));
266 if (this.props.size > 0) {
267 r.set_object_member("props", this.toJSONObject(this.props));
269 if (this.consts.size > 0) {
270 r.set_object_member("consts", this.toJSONObject(this.consts));
272 if (this.signals.size > 0) {
273 r.set_object_member("signals", this.toJSONObject(this.signals));
275 if (this.paramset != null) {
276 r.set_object_member("params", this.paramset.toJSON());
278 if (this.return_value != null) {
279 r.set_object_member("return_value", this.return_value.toJSON());
283 public Json.Object toJSONObject(Gee.HashMap<string,GirObject> map)
285 var r = new Json.Object();
286 var iter = map.map_iterator();
288 r.set_object_member(iter.get_key(), iter.get_value().toJSON());
292 public Json.Object toJSONObjectString(Gee.HashMap<string,string> map)
294 var r = new Json.Object();
295 var iter = map.map_iterator();
297 r.set_string_member(iter.get_key(), iter.get_value());
301 public Json.Array toJSONArrayString(Gee.ArrayList<string> map)
303 var r = new Json.Array();
304 for(var i =0;i< map.size;i++) {
306 r.add_string_element(map.get(i));
310 public Json.Array toJSONArrayObject(Gee.ArrayList<GirObject> map)
312 var r = new Json.Array();
313 for(var i =0;i< map.size;i++) {
315 r.add_object_element(map.get(i).toJSON());
320 public GirObject fetchByFqn(string fqn) {
321 //print("Searching (%s)%s for %s\n", this.nodetype, this.name, fqn);
322 var bits = fqn.split(".");
324 var ret = this.classes.get(bits[0]);
326 if (bits.length < 2) {
329 return ret.fetchByFqn(fqn.substring(bits[0].length+1));
332 ret = this.ctors.get(bits[0]);
334 if (bits.length < 2) {
337 return ret.fetchByFqn(fqn.substring(bits[0].length+1));
340 ret = this.methods.get(bits[0]);
342 if (bits.length < 2) {
345 return ret.fetchByFqn(fqn.substring(bits[0].length+1));
347 ret = this.props.get(bits[0]);
349 if (bits.length < 2) {
352 return ret.fetchByFqn(fqn.substring(bits[0].length+1));
354 ret = this.consts.get(bits[0]);
356 if (bits.length < 2) {
359 return ret.fetchByFqn(fqn.substring(bits[0].length+1));
362 ret = this.signals.get(bits[0]);
364 if (bits.length < 2) {
367 return ret.fetchByFqn(fqn.substring(bits[0].length+1));
369 if (this.paramset == null) {
372 var iter = this.paramset.params.list_iterator();
373 while (iter.next()) {
375 if (p.name != bits[0]) {
381 // fixme - other queires? - enums?
385 public string fqtype() {
386 return Gir.fqtypeLookup(this.type, this.ns);
394 public class Gir : GirObject {
396 static Gee.HashMap<string,Gir> cache = null;
397 //Gee.Hashmap<string,what> nodes;
401 public static Gir? factory(string ns) {
403 cache = new Gee.HashMap<string,Gir>();
405 var ret = cache.get(ns);
408 var add = new Gir(ns);
411 var iter = add.classes.map_iterator();
413 iter.get_value().overlayParent();
415 // loop again and add the ctor properties.
416 iter = add.classes.map_iterator();
418 iter.get_value().overlayCtorProperties();
429 public static GirObject? factoryFqn(string fqn)
431 var bits = fqn.split(".");
432 if (bits.length < 1) {
436 var f = (GirObject)factory(bits[0]);
438 if (bits.length == 1 || f ==null) {
441 return f.fetchByFqn(fqn.substring(bits[0].length+1)); // since classes are stored in fqn format...?
448 * guess the fqn of a type == eg. gboolean or Widget etc...
450 public static string fqtypeLookup(string type, string ns) {
452 if (g.classes.has_key(type)) {
453 return ns + "." + type;
456 if (g.consts.has_key(type)) {
457 return ns + "." + type;
461 // look at includes..
462 var iter = g.includes.map_iterator();
464 var ret = fqtypeLookup(type, iter.get_key());
476 public static string guessDefaultValueForType(string type) {
477 if (type.contains(".")) {
488 return "?"+ type + "?";
496 public Gir (string ns)
498 var xns = ns == "Glade" ? "Gladeui" : ns;
499 var gi = GI.Repository.get_default();
500 gi.require(xns, null, 0);
502 var ver = gi.get_version(xns);
503 unowned GLib.SList<string> pth = GI.Repository.get_search_path ();
504 var gir_path = pth.nth_data(0).replace("/lib/girepository-1.0", "/share/gir-1.0");
509 var file = gir_path + "/" + xns + "-" + ver + ".gir";
510 // print("ns: " + ns + "\n");
511 // print("ver: " + ver + "\n");
517 //this.nodes = new Gee.Hashmap<string,what>();
519 var doc = Xml.Parser.parse_file (file);
520 var root = doc->get_root_element();
521 this.walk( root, (GirObject) this );
526 public void walk(Xml.Node* element, GirObject? parent)
528 var n = element->get_prop("name");
532 print(element->ns->prefix + ":" + element->name + " (" + parent.name + "==>" + n +")\n");
533 switch (element->name) {
538 parent.includes.set(n, element->get_prop("version"));
554 //break; // not handled..
557 var c = new GirObject("Class", parent.name + "." + n);
558 parent.classes.set(n, c);
560 c.parent = element->get_prop("parent");
562 if (c.parent == null) {
569 var c = new GirObject("Interface", parent.name + "." + n);
571 parent.classes.set(n, c);
574 c.parent = element->get_prop("parent");
575 if (c.parent == null) {
583 parent.doctxt = element->get_content();
587 parent.implements.add(n);
592 var c = new GirObject("Ctor",n);
595 parent.ctors.set(n,c);
600 var c = new GirObject("Return", "return-value");
603 parent.return_value = c;
607 case "virtual-method": // not sure...
610 var c = new GirObject("Signal",n);
611 parent.signals.set(n,c);
615 case "signal": // Glib:signal
616 var c = new GirObject("Signal",n.replace("-", "_"));
619 parent.signals.set(n.replace("-", "_"),c);
625 case "callback": // not sure...
632 return; // no children?
636 var c = new GirObject("Method",n);
639 c.propertyof = parent.name;
640 parent.methods.set(n,c);
645 var c = new GirObject("Paramset",n);
652 case "instance-parameter":
653 var c = new GirObject("Param",n);
656 c.is_instance = true;
657 parent.params.add(c);
662 var c = new GirObject("Param",n);
665 parent.params.add(c);
667 this.checkParamOverride(c);
672 var c = new GirObject("Prop",n.replace("-", "_"));
675 c.propertyof = parent.name;
676 parent.props.set(n.replace("-", "_"),c);
681 var c = new GirObject("Function",n);
684 parent.methods.set(n,c);
689 parent.is_array = true;
690 break; // type is added soon..
693 parent.is_varargs= true;
697 var c = new GirObject("Const",n);
699 c.value = element->get_prop("value");
701 parent.consts.set(n,c);
707 var c = new GirObject("Enum",n);
710 parent.consts.set(n,c);
716 var c = new GirObject("EnumMember",n);
719 c.value = element->get_prop("value");
720 parent.consts.set(n,c);
725 case "doc-deprecated":
728 case "record": // struct?
733 case "prerequisite": // ignore?
735 case "union": // ignore?
738 print("UNHANDLED Gir file element: " + element->name +"\n");
742 if (element->name == "signal") {
747 if (element->name == "return-value") {
748 path += ".return-value";
750 print(path + ":" + element->name + "\n");
752 //var d = getAttribute(element,'doc');
754 // Seed.print(path + ':' + d);
757 for (Xml.Node* iter = element->children; iter != null; iter = iter->next) {
758 if (iter->type == Xml.ElementType.TEXT_NODE) {
761 this.walk(iter, parent);
765 public string doc(string what)
767 var ar = what.split(".");
768 var cls = this.classes.get(ar[1]);
769 if (ar.length == 2) {
770 return cls.doctxt != null ? cls.doctxt : "";
772 // return the property.. by default..
773 var pr = cls.props.get(ar[2]);
774 return pr.doctxt != null ? pr.doctxt : "";
780 public void checkParamOverride(GirObject c)
782 var parset = c.gparent;
783 if (parset == null || parset.nodetype != "Paramset") {
786 var method = parset.gparent;
787 if (method == null || method.nodetype != "Ctor") {
790 var cls = method.gparent;
791 if (cls == null || cls.nodetype != "Class") {
797 c.name = this.fetchOverride( cls.name, method.name, c.name);
799 public static bool overrides_loaded = false;
800 public static Gee.HashMap<string,string> overrides;
802 public string fetchOverride( string cls, string method, string param)
804 // overrides should be in a file Gir.overides
805 // in that "Gtk.Label.new.str" : "label"
806 this.loadOverrides();
807 var key = "%s.%s.%s".printf(cls,method,param);
808 //print("Chekcing for key %s\n", key);
809 if (!overrides.has_key(key)) {
812 return overrides.get(key);
816 public void loadOverrides()
818 if (overrides_loaded) {
822 var pa = new Json.Parser();
823 pa.load_from_file(Builder4.Application.configDirectory() + "/resources/Gir.overides");
824 var node = pa.get_root();
826 if (node.get_node_type () != Json.NodeType.OBJECT) {
827 throw new GirError.INVALID_FORMAT ("Error loading gir.overides : Unexpected element type %s", node.type_name ());
829 overrides = new Gee.HashMap<string,string>();
832 var obj = node.get_object ();
835 obj.foreach_member((o , key, value) => {
838 var v = obj.get_string_member(key);
841 overrides.set(key, v);
845 overrides_loaded = true;