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.nodetype == "Class" || this.nodetype=="Interface"
193 ? this.name : (this.ns + this.name);
196 public void copyFrom(GirObject pcls, bool is_interface)
199 this.inherits.add(pcls.fqn());
201 var liter = pcls.inherits.list_iterator();
202 while(liter.next()) {
203 if (this.inherits.contains(liter.get())) {
206 this.inherits.add(liter.get());
210 var iter = pcls.methods.map_iterator();
212 if (null != this.methods.get(iter.get_key())) {
216 this.methods.set(iter.get_key(), iter.get_value());
219 iter = pcls.props.map_iterator();
221 if (null != this.props.get(iter.get_key())) {
225 this.props.set(iter.get_key(), iter.get_value());
228 iter = pcls.signals.map_iterator();
230 if (null != this.signals.get(iter.get_key())) {
234 this.signals.set(iter.get_key(), iter.get_value());
238 public Json.Object toJSON()
240 var r = new Json.Object();
241 r.set_string_member("nodetype", this.nodetype);
242 r.set_string_member("name", this.name);
243 if (this.propertyof.length > 0) {
244 r.set_string_member("of", this.propertyof);
246 if (this.type.length > 0) {
247 r.set_string_member("type", this.type);
249 if (this.parent != null && this.parent.length > 0) {
250 r.set_string_member("parent", this.parent);
252 if (this.sig.length > 0) {
253 r.set_string_member("sig", this.sig);
256 // is_arary / is_instance / is_varargs..
259 if (this.inherits.size > 0) {
260 r.set_array_member("inherits", this.toJSONArrayString(this.inherits));
263 if (this.implements.size > 0) {
264 r.set_array_member("implements", this.toJSONArrayString(this.implements));
267 if (this.params.size > 0) {
268 r.set_array_member("params", this.toJSONArrayObject(this.params));
270 if (this.ctors.size > 0) {
271 r.set_object_member("ctors", this.toJSONObject(this.ctors));
273 if (this.methods.size > 0) {
274 r.set_object_member("methods", this.toJSONObject(this.methods));
276 if (this.includes.size > 0) {
277 r.set_object_member("includes", this.toJSONObjectString(this.includes));
279 if (this.classes.size > 0) {
280 r.set_object_member("classes", this.toJSONObject(this.classes));
282 if (this.props.size > 0) {
283 r.set_object_member("props", this.toJSONObject(this.props));
285 if (this.consts.size > 0) {
286 r.set_object_member("consts", this.toJSONObject(this.consts));
288 if (this.signals.size > 0) {
289 r.set_object_member("signals", this.toJSONObject(this.signals));
291 if (this.paramset != null) {
292 r.set_object_member("paramset", this.paramset.toJSON());
294 if (this.return_value != null) {
295 r.set_object_member("return_value", this.return_value.toJSON());
299 public Json.Object toJSONObject(Gee.HashMap<string,GirObject> map)
301 var r = new Json.Object();
302 var iter = map.map_iterator();
304 r.set_object_member(iter.get_key(), iter.get_value().toJSON());
308 public Json.Object toJSONObjectString(Gee.HashMap<string,string> map)
310 var r = new Json.Object();
311 var iter = map.map_iterator();
313 r.set_string_member(iter.get_key(), iter.get_value());
317 public Json.Array toJSONArrayString(Gee.ArrayList<string> map)
319 var r = new Json.Array();
320 for(var i =0;i< map.size;i++) {
322 r.add_string_element(map.get(i));
326 public Json.Array toJSONArrayObject(Gee.ArrayList<GirObject> map)
328 var r = new Json.Array();
329 for(var i =0;i< map.size;i++) {
331 r.add_object_element(map.get(i).toJSON());
335 public string asJSONString()
337 var generator = new Json.Generator ();
338 generator.indent = 4;
339 generator.pretty = true;
340 var n = new Json.Node(Json.NodeType.OBJECT);
341 n.set_object(this.toJSON());
342 generator.set_root(n);
344 return generator.to_data(null);
348 public GirObject fetchByFqn(string fqn) {
349 //print("Searching (%s)%s for %s\n", this.nodetype, this.name, fqn);
350 var bits = fqn.split(".");
352 var ret = this.classes.get(bits[0]);
354 if (bits.length < 2) {
357 return ret.fetchByFqn(fqn.substring(bits[0].length+1));
360 ret = this.ctors.get(bits[0]);
362 if (bits.length < 2) {
365 return ret.fetchByFqn(fqn.substring(bits[0].length+1));
368 ret = this.methods.get(bits[0]);
370 if (bits.length < 2) {
373 return ret.fetchByFqn(fqn.substring(bits[0].length+1));
375 ret = this.props.get(bits[0]);
377 if (bits.length < 2) {
380 return ret.fetchByFqn(fqn.substring(bits[0].length+1));
382 ret = this.consts.get(bits[0]);
384 if (bits.length < 2) {
387 return ret.fetchByFqn(fqn.substring(bits[0].length+1));
390 ret = this.signals.get(bits[0]);
392 if (bits.length < 2) {
395 return ret.fetchByFqn(fqn.substring(bits[0].length+1));
397 if (this.paramset == null) {
400 var iter = this.paramset.params.list_iterator();
401 while (iter.next()) {
403 if (p.name != bits[0]) {
409 // fixme - other queires? - enums?
413 public string fqtype() {
414 return Gir.fqtypeLookup(this.type, this.ns);
422 public class Gir : GirObject {
424 static Gee.HashMap<string,Gir> cache = null;
425 //Gee.Hashmap<string,what> nodes;
429 public static Gir? factory(string ns) {
431 cache = new Gee.HashMap<string,Gir>();
433 var ret = cache.get(ns);
436 var add = new Gir(ns);
439 var iter = add.classes.map_iterator();
441 iter.get_value().overlayParent();
443 // loop again and add the ctor properties.
444 iter = add.classes.map_iterator();
446 iter.get_value().overlayCtorProperties();
457 public static GirObject? factoryFqn(string fqn)
459 var bits = fqn.split(".");
460 if (bits.length < 1) {
464 var f = (GirObject)factory(bits[0]);
466 if (bits.length == 1 || f ==null) {
469 return f.fetchByFqn(fqn.substring(bits[0].length+1)); // since classes are stored in fqn format...?
476 * guess the fqn of a type == eg. gboolean or Widget etc...
478 public static string fqtypeLookup(string type, string ns) {
480 if (g.classes.has_key(type)) {
481 return ns + "." + type;
484 if (g.consts.has_key(type)) {
485 return ns + "." + type;
489 // look at includes..
490 var iter = g.includes.map_iterator();
492 // skip empty namespaces on include..?
493 if ( iter.get_key() == "") {
496 var ret = fqtypeLookup(type, iter.get_key());
508 public static string guessDefaultValueForType(string type) {
509 //print("guessDefaultValueForType: %s\n", type);
510 if (type.length < 1 || type.contains(".")) {
521 return "?"+ type + "?";
529 public Gir (string ns)
531 var xns = ns == "Glade" ? "Gladeui" : ns;
532 var gi = GI.Repository.get_default();
533 gi.require(xns, null, 0);
535 var ver = gi.get_version(xns);
536 unowned GLib.SList<string> pth = GI.Repository.get_search_path ();
537 var gir_path = pth.nth_data(0).replace("/lib/girepository-1.0", "/share/gir-1.0");
542 var file = gir_path + "/" + xns + "-" + ver + ".gir";
543 // print("ns: " + ns + "\n");
544 // print("ver: " + ver + "\n");
550 //this.nodes = new Gee.Hashmap<string,what>();
552 var doc = Xml.Parser.parse_file (file);
553 var root = doc->get_root_element();
554 this.walk( root, (GirObject) this );
559 public void walk(Xml.Node* element, GirObject? parent)
561 var n = element->get_prop("name");
562 // ignore null or c:include...
563 if (n == null || (element->ns->prefix != null && element->ns->prefix == "c")) {
566 //print("%s:%s (%s ==> %s\n", element->ns->prefix , element->name , parent.name , n);
567 switch (element->name) {
572 parent.includes.set(n, element->get_prop("version"));
588 //break; // not handled..
591 var c = new GirObject("Class", parent.name + "." + n);
592 parent.classes.set(n, c);
594 c.parent = element->get_prop("parent");
596 if (c.parent == null) {
603 var c = new GirObject("Interface", parent.name + "." + n);
605 parent.classes.set(n, c);
608 c.parent = element->get_prop("parent");
609 if (c.parent == null) {
617 parent.doctxt = element->get_content();
621 parent.implements.add(n);
626 var c = new GirObject("Ctor",n);
629 parent.ctors.set(n,c);
634 var c = new GirObject("Return", "return-value");
637 parent.return_value = c;
641 case "virtual-method": // not sure...
644 var c = new GirObject("Signal",n);
645 parent.signals.set(n,c);
649 case "signal": // Glib:signal
650 var c = new GirObject("Signal",n.replace("-", "_"));
653 parent.signals.set(n.replace("-", "_"),c);
659 case "callback": // not sure...
666 return; // no children?
670 var c = new GirObject("Method",n);
673 c.propertyof = parent.name;
674 parent.methods.set(n,c);
679 var c = new GirObject("Paramset",n);
686 case "instance-parameter":
688 // looks like this is the C first arg, that is ignored (as it is
689 // treated as 'this' )
690 var c = new GirObject("Param",n);
693 c.is_instance = true;
694 parent.params.add(c);
699 var c = new GirObject("Param",n);
702 parent.params.add(c);
704 this.checkParamOverride(c);
709 var c = new GirObject("Prop",n.replace("-", "_"));
712 c.propertyof = parent.name;
713 parent.props.set(n.replace("-", "_"),c);
718 var c = new GirObject("Function",n);
721 parent.methods.set(n,c);
726 parent.is_array = true;
727 break; // type is added soon..
730 parent.is_varargs= true;
734 var c = new GirObject("Const",n);
736 c.value = element->get_prop("value");
738 parent.consts.set(n,c);
744 var c = new GirObject("Enum",n);
747 parent.consts.set(n,c);
753 var c = new GirObject("EnumMember",n);
756 c.value = element->get_prop("value");
757 parent.consts.set(n,c);
762 case "doc-deprecated":
765 case "record": // struct?
770 case "prerequisite": // ignore?
772 case "union": // ignore?
775 print("UNHANDLED Gir file element: " + element->name +"\n");
779 if (element->name == "signal") {
784 if (element->name == "return-value") {
785 path += ".return-value";
787 print(path + ":" + element->name + "\n");
789 //var d = getAttribute(element,'doc');
791 // Seed.print(path + ':' + d);
794 for (Xml.Node* iter = element->children; iter != null; iter = iter->next) {
795 if (iter->type == Xml.ElementType.TEXT_NODE) {
798 this.walk(iter, parent);
802 public string doc(string what)
804 var ar = what.split(".");
805 var cls = this.classes.get(ar[1]);
806 if (ar.length == 2) {
807 return cls.doctxt != null ? cls.doctxt : "";
809 // return the property.. by default..
810 var pr = cls.props.get(ar[2]);
811 return pr.doctxt != null ? pr.doctxt : "";
817 public void checkParamOverride(GirObject c)
819 var parset = c.gparent;
820 if (parset == null || parset.nodetype != "Paramset") {
823 var method = parset.gparent;
824 if (method == null || method.nodetype != "Ctor") {
827 var cls = method.gparent;
828 if (cls == null || cls.nodetype != "Class") {
834 c.name = this.fetchOverride( cls.name, method.name, c.name);
836 public static bool overrides_loaded = false;
837 public static Gee.HashMap<string,string> overrides;
839 public string fetchOverride( string cls, string method, string param)
841 // overrides should be in a file Gir.overides
842 // in that "Gtk.Label.new.str" : "label"
843 this.loadOverrides();
844 var key = "%s.%s.%s".printf(cls,method,param);
845 //print("Chekcing for key %s\n", key);
846 if (!overrides.has_key(key)) {
849 return overrides.get(key);
853 public void loadOverrides()
855 if (overrides_loaded) {
859 var pa = new Json.Parser();
860 pa.load_from_file(Builder4.Application.configDirectory() + "/resources/Gir.overides");
861 var node = pa.get_root();
863 if (node.get_node_type () != Json.NodeType.OBJECT) {
864 throw new GirError.INVALID_FORMAT ("Error loading gir.overides : Unexpected element type %s", node.type_name ());
866 overrides = new Gee.HashMap<string,string>();
869 var obj = node.get_object ();
872 obj.foreach_member((o , key, value) => {
875 var v = obj.get_string_member(key);
878 overrides.set(key, v);
882 overrides_loaded = true;