src/Palete/GirObject.vala
authorAlan Knowles <alan@roojs.com>
Thu, 7 May 2015 05:14:23 +0000 (13:14 +0800)
committerAlan Knowles <alan@roojs.com>
Thu, 7 May 2015 05:14:23 +0000 (13:14 +0800)
src/Palete/GirObject.vala

index 8b13789..108815b 100644 (file)
@@ -1 +1,408 @@
+/**
+ * This is the base class for representing the vala API
+ *  
+ * it was originally based on parsing Gir files - but since then
+ * has evolved into using libvala  
+ * 
+ * 
+ */
 
+
+
+namespace Palete {
+       public errordomain GirError {
+               INVALID_TYPE,
+               NEED_IMPLEMENTING,
+               MISSING_FILE,
+               INVALID_VALUE,
+               INVALID_FORMAT
+       }
+       public class GirObject: Object {
+               public string name;
+               public string ns;
+               public string propertyof;
+               public string type;
+               public string nodetype;
+               public string  package;
+               
+               public GirObject paramset = null;
+               public GirObject return_value = null;
+                   
+               public bool is_instance;
+               public bool is_array;
+               public bool  is_varargs;
+               public bool  ctor_only; // specially added ctor properties..
+               public  string parent;
+               public  string value;
+               // to be filled in...
+        
+               public  string sig;
+
+               bool is_overlaid;
+
+               public  GirObject gparent;
+               public Gee.ArrayList<GirObject> params;
+               public Gee.ArrayList<string> implements;
+               public Gee.ArrayList<string> inherits; // full list of all classes and interfaces...
+               public Gee.HashMap<string,GirObject> ctors;
+               public Gee.HashMap<string,GirObject> methods;
+               public Gee.HashMap<string,string>    includes;
+               public Gee.HashMap<string,GirObject> classes;
+               public Gee.HashMap<string,GirObject> props;
+               public Gee.HashMap<string,GirObject> consts;
+               public Gee.HashMap<string,GirObject> signals;
+               public string doctxt;
+
+
+               
+               public GirObject(string nodetype, string n)
+               {
+                       this.nodetype = nodetype;
+                       this.name = n;
+                       this.ns = "";
+                       this.parent = "";
+                       this.type = "";
+                       this.propertyof = "";
+                       this.is_array = false;
+                       this.is_instance = false;
+                       this.is_varargs = false;
+                       this.ctor_only  =false;
+                       this.doctxt = "";
+               
+                       this.sig = "";
+
+                       this.gparent = null;
+                       
+                       this.implements = new Gee.ArrayList<string>();
+                       this.inherits  = new Gee.ArrayList<string>(); // list of all ancestors. (interfaces and parents)
+                       this.includes   = new Gee.HashMap<string,string>();
+
+                       this.params = new Gee.ArrayList<GirObject>();
+                       this.ctors      = new Gee.HashMap<string,GirObject>();
+                       this.methods    =new Gee.HashMap<string,GirObject>();
+
+                       this.classes    = new Gee.HashMap<string,GirObject>();
+                       this.props      = new Gee.HashMap<string,GirObject>();
+                       this.consts     = new Gee.HashMap<string,GirObject>();
+                       this.signals    = new Gee.HashMap<string,GirObject>();
+                       this.is_overlaid = false;
+                       this.paramset = null;
+               }
+
+               public string[] inheritsToStringArray()
+               {
+                       string[] ret = {};
+                       for(var i =0;i< this.inherits.size; i++) {
+                               ret += this.inherits.get(i);
+                       }
+                       return ret;
+
+               }
+
+               
+               public void  overlayParent()
+               {
+                       
+                       if (this.parent.length < 1 || this.is_overlaid) {
+                               this.is_overlaid = true;
+                               return;
+                       }
+                       // print("Overlaying " +this.name + " with " + this.parent + "\n");
+
+                       var pcls = this.clsToObject( this.parent);
+                       if (pcls == null) {
+                               throw new GirError.INVALID_VALUE("Could not find class : " + 
+                                       this.parent + " of " + this.name  + " in " + this.ns);
+                       }
+                       
+                       pcls.overlayParent( );
+                       this.copyFrom(pcls,false);
+                       for(var i=0; i < this.implements.size; i++) {
+                               var clsname = this.implements.get(i);
+                               var picls = this.clsToObject(clsname);
+                               this.copyFrom(picls,true);
+                       }
+                       this.is_overlaid = true;
+                       
+               }
+
+               public void overlayCtorProperties() 
+               {
+                       //print("Check overlay Ctor %s\n", this.name);
+                       if (!this.ctors.has_key("new")) {
+                               return;
+                       }
+                       var ctor = this.ctors.get("new");
+                       if (ctor.paramset == null || ctor.paramset.params.size < 1) {
+                               return;
+                       }
+                       //print("Found Ctor\n");
+                       var iter = ctor.paramset.params.list_iterator();
+                       while (iter.next()) {
+                               var n = iter.get().name;
+                               
+                               if (this.props.has_key(n)) {
+                                       continue;
+                               }
+                               if (n == "...") {
+                                       continue;
+                               }
+                               //print("Adding prop %s\n", n);
+                               
+                               // it's a new prop..
+                               var c = new GirObject("Prop",n);
+                               c.gparent = this;
+                               c.ns = this.ns;
+                               c.propertyof = this.name;
+                               c.type = iter.get().type;
+                               c.ctor_only = true;
+                               this.props.set(n, c);
+                       }
+                       
+
+               }
+
+
+               
+               public GirObject clsToObject(string in_pn)
+               {
+                       var pn = in_pn;
+                       var gir = Gir.factory (this.ns);
+                       if (in_pn.contains(".")) {
+                               gir =  Gir.factory(in_pn.split(".")[0]);
+                               pn = in_pn.split(".")[1];
+                       }
+                       
+                       return gir.classes.get(pn);
+
+                       
+               }
+               public string fqn() {
+                       // not sure if fqn really is correct here...
+                       // 
+                       return this.nodetype == "Class" || this.nodetype=="Interface"
+                                       ? this.name : (this.ns + this.name);
+               }
+               
+               public void copyFrom(GirObject pcls, bool is_interface) 
+               {
+
+                       this.inherits.add(pcls.fqn());
+
+                       var liter = pcls.inherits.list_iterator();
+                       while(liter.next()) {
+                       if (this.inherits.contains(liter.get())) {
+                                       continue;
+                               }
+                               this.inherits.add(liter.get()); 
+                       }          
+                       
+                       
+                       var iter = pcls.methods.map_iterator();
+                       while(iter.next()) {
+                       if (null != this.methods.get(iter.get_key())) {
+                                       continue;
+                               }
+                               
+                               this.methods.set(iter.get_key(), iter.get_value());
+                       }
+                       
+                       iter = pcls.props.map_iterator();
+                       while(iter.next()) {
+                                        if (null != this.props.get(iter.get_key())) {
+                                       continue;
+                               }
+                               
+                               this.props.set(iter.get_key(), iter.get_value());
+                       }               
+                       
+                       iter = pcls.signals.map_iterator();
+                       while(iter.next()) {
+                               if (null != this.signals.get(iter.get_key())) {
+                                               continue;
+                               }
+       
+                               this.signals.set(iter.get_key(), iter.get_value());
+                       }       
+               }
+               
+               public Json.Object toJSON()
+               {
+                   var r = new Json.Object();
+                   r.set_string_member("nodetype", this.nodetype);
+                   r.set_string_member("name", this.name);
+                               if (this.propertyof.length > 0) {
+                       r.set_string_member("of", this.propertyof);
+                   }
+                   if (this.type.length > 0) {
+                       r.set_string_member("type", this.type);
+                   }
+                   if (this.parent != null && this.parent.length > 0) {
+                       r.set_string_member("parent", this.parent);
+                   }
+                   if (this.sig.length > 0) {
+                       r.set_string_member("sig", this.sig);
+                   }
+               
+                   // is_arary / is_instance / is_varargs..
+
+               
+                       if (this.inherits.size > 0) {
+                       r.set_array_member("inherits", this.toJSONArrayString(this.inherits));
+                   }
+                   
+                   if (this.implements.size > 0) {
+                       r.set_array_member("implements", this.toJSONArrayString(this.implements));
+                   }
+                   
+                   if (this.params.size > 0) {
+                       r.set_array_member("params", this.toJSONArrayObject(this.params));
+                   }
+                   if (this.ctors.size > 0) {
+                       r.set_object_member("ctors", this.toJSONObject(this.ctors));
+                   }
+                   if (this.methods.size > 0) {
+                       r.set_object_member("methods", this.toJSONObject(this.methods));
+                   }
+                   if (this.includes.size > 0) {
+                       r.set_object_member("includes", this.toJSONObjectString(this.includes));
+                   }
+                   if (this.classes.size > 0) {
+                       r.set_object_member("classes", this.toJSONObject(this.classes));
+                   }
+                   if (this.props.size > 0) {
+                       r.set_object_member("props", this.toJSONObject(this.props));
+                   }
+                   if (this.consts.size > 0) {
+                       r.set_object_member("consts", this.toJSONObject(this.consts));
+                   }
+                   if (this.signals.size > 0) {
+                       r.set_object_member("signals", this.toJSONObject(this.signals));
+                   }
+                   if (this.paramset != null) {
+                       r.set_object_member("paramset", this.paramset.toJSON());
+                   }
+                   if (this.return_value != null) {
+                       r.set_object_member("return_value", this.return_value.toJSON());
+                   }
+                   return r;
+               }
+               public Json.Object toJSONObject(Gee.HashMap<string,GirObject> map)
+               {
+                   var r = new Json.Object();
+                   var iter = map.map_iterator();
+                   while(iter.next()) {
+                       r.set_object_member(iter.get_key(), iter.get_value().toJSON());
+                   }
+                   return r;
+               }
+               public Json.Object  toJSONObjectString(Gee.HashMap<string,string> map)
+               {
+                   var r = new Json.Object();
+                   var iter = map.map_iterator();
+                   while(iter.next()) {
+                       r.set_string_member(iter.get_key(), iter.get_value());
+                   }
+                   return r;
+               }
+               public Json.Array toJSONArrayString(Gee.ArrayList<string> map)
+               {
+                   var r = new Json.Array();
+                   for(var i =0;i< map.size;i++) {
+                   
+                       r.add_string_element(map.get(i));
+                   }
+                   return r;
+               }
+               public Json.Array toJSONArrayObject(Gee.ArrayList<GirObject> map)
+               {
+                   var r = new Json.Array();
+                   for(var i =0;i< map.size;i++) {
+                   
+                       r.add_object_element(map.get(i).toJSON());
+                   }
+                   return r;
+               }
+               public string asJSONString()
+               {
+                       var generator = new Json.Generator ();
+                       generator.indent = 4;
+                       generator.pretty = true;
+                       var n = new Json.Node(Json.NodeType.OBJECT);
+                       n.set_object(this.toJSON());
+                       generator.set_root(n);
+       
+                       return generator.to_data(null);
+               }
+
+               public GirObject fetchByFqn(string fqn) {
+                       //print("Searching (%s)%s for %s\n", this.nodetype, this.name, fqn);
+                       var bits = fqn.split(".");
+                       
+                       var ret = this.classes.get(bits[0]);
+                       if (ret != null) {
+                               if (bits.length < 2) {
+                                       return ret;
+                               }
+                               return ret.fetchByFqn(fqn.substring(bits[0].length+1));
+                       }
+
+                       ret = this.ctors.get(bits[0]);                  
+                       if (ret != null) {
+                               if (bits.length < 2) {
+                                       return ret;
+                               }
+                               return ret.fetchByFqn(fqn.substring(bits[0].length+1));
+                       }
+
+                       ret = this.methods.get(bits[0]);                        
+                       if (ret != null) {
+                               if (bits.length < 2) {
+                                       return ret;
+                               }
+                               return ret.fetchByFqn(fqn.substring(bits[0].length+1));
+                       }
+                       ret = this.props.get(bits[0]);                  
+                       if (ret != null) {
+                               if (bits.length < 2) {
+                                       return ret;
+                               }
+                               return ret.fetchByFqn(fqn.substring(bits[0].length+1));
+                       }
+                       ret = this.consts.get(bits[0]);                 
+                       if (ret != null) {
+                               if (bits.length < 2) {
+                                       return ret;
+                               }
+                               return ret.fetchByFqn(fqn.substring(bits[0].length+1));
+                       }
+
+                       ret = this.signals.get(bits[0]);                        
+                       if (ret != null) {
+                               if (bits.length < 2) {
+                                       return ret;
+                               }
+                               return ret.fetchByFqn(fqn.substring(bits[0].length+1));
+                       }
+                       if (this.paramset == null) {
+                               return null;
+                       }
+                       var iter = this.paramset.params.list_iterator();
+                       while (iter.next()) {
+                               var p = iter.get();
+                               if (p.name != bits[0]) {
+                                       continue;
+                               }
+                               return p;
+                       }
+                                
+                       // fixme - other queires? - enums?
+                       return null;
+               }
+
+               public string fqtype() {
+                       return Gir.fqtypeLookup(this.type, this.ns);
+               }
+       }
+