resources/RooUsage.txt
[app.Builder.js] / src / Palete / GirObject.vala
1 /**
2  * This is the base class for representing the vala API
3  *  
4  * it was originally based on parsing Gir files - but since then
5  * has evolved into using libvala  
6  * 
7  * 
8  */
9
10
11
12 namespace Palete {
13         public errordomain GirError {
14                 INVALID_TYPE,
15                 NEED_IMPLEMENTING,
16                 MISSING_FILE,
17                 INVALID_VALUE,
18                 INVALID_FORMAT
19         }
20         public class GirObject: Object {
21                 public string name;
22                 public string ns;
23                 public string propertyof;
24                 public string type;
25                 public string nodetype;
26                 public string package;
27                 public string direction; // used for vala in/out/ref...
28                 
29                 public GirObject paramset = null;
30                 public GirObject return_value = null;
31                     
32                 public bool is_instance;
33                 public bool is_array;
34                 public bool  is_varargs;
35                 public bool  ctor_only; // specially added ctor properties..
36                 public  string parent;
37                 public  string value;
38                 // to be filled in...
39          
40                 public  string sig;
41
42                 public bool is_overlaid;
43
44                 public  GirObject gparent;
45                 public Gee.ArrayList<GirObject> params;
46                 public Gee.ArrayList<string> implements;
47                 public Gee.ArrayList<string> inherits; // full list of all classes and interfaces...
48                 public Gee.HashMap<string,GirObject> ctors;
49                 public Gee.HashMap<string,GirObject> methods;
50                 public Gee.HashMap<string,string>    includes;
51                 public Gee.HashMap<string,GirObject> classes;
52                 public Gee.HashMap<string,GirObject> props;
53                 public Gee.HashMap<string,GirObject> consts;
54                 public Gee.HashMap<string,GirObject> signals;
55                 
56                 public Gee.ArrayList<string> optvalues; // used by Roo only..
57                 public string doctxt;
58
59
60                 
61                 public GirObject(string nodetype, string n)
62                 {
63                         this.nodetype = nodetype;
64                         this.name = n;
65                         this.ns = "";
66                         this.parent = "";
67                         this.type = "";
68                         this.propertyof = "";
69                         this.is_array = false;
70                         this.is_instance = false;
71                         this.is_varargs = false;
72                         this.ctor_only  =false;
73                         this.doctxt = "";
74                 
75                         this.sig = "";
76
77                         this.gparent = null;
78                         
79                         this.implements = new Gee.ArrayList<string>();
80                         this.inherits  = new Gee.ArrayList<string>(); // list of all ancestors. (interfaces and parents)
81                         this.includes   = new Gee.HashMap<string,string>();
82
83                         this.params = new Gee.ArrayList<GirObject>();
84                         this.ctors      = new Gee.HashMap<string,GirObject>();
85                         this.methods    =new Gee.HashMap<string,GirObject>();
86
87                         this.classes    = new Gee.HashMap<string,GirObject>();
88                         this.props      = new Gee.HashMap<string,GirObject>();
89                         this.consts     = new Gee.HashMap<string,GirObject>();
90                         this.signals    = new Gee.HashMap<string,GirObject>();
91                         
92                         this.optvalues = new Gee.ArrayList<string>();
93                         this.is_overlaid = false;
94                         this.paramset = null;
95                 }
96
97                 public string[] inheritsToStringArray()
98                 {
99                         string[] ret = {};
100                         for(var i =0;i< this.inherits.size; i++) {
101                                 ret += this.inherits.get(i);
102                         }
103                         return ret;
104
105                 }
106
107                 
108                 public void  overlayParent()
109                 {
110                         
111                         if (this.parent.length < 1 || this.is_overlaid) {
112                                 this.is_overlaid = true;
113                                 return;
114                         }
115                          
116                         //print("Overlaying " +this.name + " with " + this.parent + "\n");
117
118                         var pcls = this.clsToObject( this.parent);
119                         if (pcls == null) {
120                                 return;
121                                 //throw new GirError.INVALID_VALUE("Could not find class : " + 
122                                 //      this.parent + " of " + this.name  + " in " + this.ns);
123                         }
124                         
125                         pcls.overlayParent( );
126                         this.copyFrom(pcls,false);
127                         for(var i=0; i < this.implements.size; i++) {
128                                 var clsname = this.implements.get(i);
129                                 var picls = this.clsToObject(clsname);
130                                 this.copyFrom(picls,true);
131                         }
132                         this.is_overlaid = true;
133                         
134                 }
135
136                 public void overlayCtorProperties() 
137                 {
138                         //print("Check overlay Ctor %s\n", this.name);
139                         if (!this.ctors.has_key("new")) {
140                                 return;
141                         }
142                         var ctor = this.ctors.get("new");
143                         if (ctor.paramset == null || ctor.paramset.params.size < 1) {
144                                 return;
145                         }
146                         //print("Found Ctor\n");
147                         var iter = ctor.paramset.params.list_iterator();
148                         while (iter.next()) {
149                                 var n = iter.get().name;
150                                 
151                                 if (this.props.has_key(n)) {
152                                         continue;
153                                 }
154                                 if (n == "...") {
155                                         continue;
156                                 }
157                                 //print("Adding prop %s\n", n);
158                                 
159                                 // it's a new prop..
160                                 var c = new GirObject("Prop",n);
161                                 c.gparent = this;
162                                 c.ns = this.ns;
163                                 c.propertyof = this.name;
164                                 c.type = iter.get().type;
165                                 c.ctor_only = true;
166                                 this.props.set(n, c);
167                         }
168                         
169
170                 }
171
172
173                 
174                 public string fqn() {
175                         // not sure if fqn really is correct here...
176                         // 
177                         return this.nodetype == "Class" || this.nodetype=="Interface"
178                                         ? this.name : (this.ns + this.name);
179                 }
180                 
181                 public void copyFrom(GirObject pcls, bool is_interface) 
182                 {
183
184                         this.inherits.add(pcls.fqn());
185
186                         var liter = pcls.inherits.list_iterator();
187                         while(liter.next()) {
188                         if (this.inherits.contains(liter.get())) {
189                                         continue;
190                                 }
191                                 this.inherits.add(liter.get()); 
192                         }          
193                         
194                         
195                         var iter = pcls.methods.map_iterator();
196                         while(iter.next()) {
197                         if (null != this.methods.get(iter.get_key())) {
198                                         continue;
199                                 }
200                                 
201                                 this.methods.set(iter.get_key(), iter.get_value());
202                         }
203                         
204                         iter = pcls.props.map_iterator();
205                         while(iter.next()) {
206                                  if (null != this.props.get(iter.get_key())) {
207                                         continue;
208                                 }
209                                 
210                                 this.props.set(iter.get_key(), iter.get_value());
211                         }               
212                         
213                         iter = pcls.signals.map_iterator();
214                         while(iter.next()) {
215                                 if (null != this.signals.get(iter.get_key())) {
216                                                 continue;
217                                 }
218         
219                                 this.signals.set(iter.get_key(), iter.get_value());
220                         }       
221                 }
222                 
223                 public Json.Object toJSON()
224                 {
225                     var r = new Json.Object();
226                     r.set_string_member("nodetype", this.nodetype);
227                     r.set_string_member("name", this.name);
228                                 if (this.propertyof.length > 0) {
229                         r.set_string_member("of", this.propertyof);
230                     }
231                     if (this.type.length > 0) {
232                         r.set_string_member("type", this.type);
233                     }
234                     if (this.parent != null && this.parent.length > 0) {
235                         r.set_string_member("parent", this.parent);
236                     }
237                     if (this.sig.length > 0) {
238                         r.set_string_member("sig", this.sig);
239                     }
240                 
241                     // is_arary / is_instance / is_varargs..
242
243                 
244                         if (this.inherits.size > 0) {
245                         r.set_array_member("inherits", this.toJSONArrayString(this.inherits));
246                     }
247                     
248                     if (this.implements.size > 0) {
249                         r.set_array_member("implements", this.toJSONArrayString(this.implements));
250                     }
251                     
252                     if (this.params.size > 0) {
253                         r.set_array_member("params", this.toJSONArrayObject(this.params));
254                     }
255                     if (this.ctors.size > 0) {
256                         r.set_object_member("ctors", this.toJSONObject(this.ctors));
257                     }
258                     if (this.methods.size > 0) {
259                         r.set_object_member("methods", this.toJSONObject(this.methods));
260                     }
261                     if (this.includes.size > 0) {
262                         r.set_object_member("includes", this.toJSONObjectString(this.includes));
263                     }
264                     if (this.classes.size > 0) {
265                         r.set_object_member("classes", this.toJSONObject(this.classes));
266                     }
267                     if (this.props.size > 0) {
268                         r.set_object_member("props", this.toJSONObject(this.props));
269                     }
270                     if (this.consts.size > 0) {
271                         r.set_object_member("consts", this.toJSONObject(this.consts));
272                     }
273                     if (this.signals.size > 0) {
274                         r.set_object_member("signals", this.toJSONObject(this.signals));
275                     }
276                     if (this.paramset != null) {
277                         r.set_object_member("paramset", this.paramset.toJSON());
278                     }
279                     if (this.return_value != null) {
280                         r.set_object_member("return_value", this.return_value.toJSON());
281                     }
282                     return r;
283                 }
284                 public Json.Object toJSONObject(Gee.HashMap<string,GirObject> map)
285                 {
286                     var r = new Json.Object();
287                     var iter = map.map_iterator();
288                     while(iter.next()) {
289                         r.set_object_member(iter.get_key(), iter.get_value().toJSON());
290                     }
291                     return r;
292                 }
293                 public Json.Object  toJSONObjectString(Gee.HashMap<string,string> map)
294                 {
295                     var r = new Json.Object();
296                     var iter = map.map_iterator();
297                     while(iter.next()) {
298                         r.set_string_member(iter.get_key(), iter.get_value());
299                     }
300                     return r;
301                 }
302                 public Json.Array toJSONArrayString(Gee.ArrayList<string> map)
303                 {
304                     var r = new Json.Array();
305                     for(var i =0;i< map.size;i++) {
306                     
307                         r.add_string_element(map.get(i));
308                     }
309                     return r;
310                 }
311                 public Json.Array toJSONArrayObject(Gee.ArrayList<GirObject> map)
312                 {
313                     var r = new Json.Array();
314                     for(var i =0;i< map.size;i++) {
315                     
316                         r.add_object_element(map.get(i).toJSON());
317                     }
318                     return r;
319                 }
320                 public string asJSONString()
321                 {
322                         var generator = new Json.Generator ();
323                         generator.indent = 4;
324                         generator.pretty = true;
325                         var n = new Json.Node(Json.NodeType.OBJECT);
326                         n.set_object(this.toJSON());
327                         generator.set_root(n);
328         
329                         return generator.to_data(null);
330                 }
331
332  
333                 public GirObject fetchByFqn(string fqn) {
334                         //print("Searching (%s)%s for %s\n", this.nodetype, this.name, fqn);
335                         var bits = fqn.split(".");
336                         
337                         var ret = this.classes.get(bits[0]);
338                         if (ret != null) {
339                                 if (bits.length < 2) {
340                                         return ret;
341                                 }
342                                 return ret.fetchByFqn(fqn.substring(bits[0].length+1));
343                         }
344
345                         ret = this.ctors.get(bits[0]);                  
346                         if (ret != null) {
347                                 if (bits.length < 2) {
348                                         return ret;
349                                 }
350                                 return ret.fetchByFqn(fqn.substring(bits[0].length+1));
351                         }
352
353                         ret = this.methods.get(bits[0]);                        
354                         if (ret != null) {
355                                 if (bits.length < 2) {
356                                         return ret;
357                                 }
358                                 return ret.fetchByFqn(fqn.substring(bits[0].length+1));
359                         }
360                         ret = this.props.get(bits[0]);                  
361                         if (ret != null) {
362                                 if (bits.length < 2) {
363                                         return ret;
364                                 }
365                                 return ret.fetchByFqn(fqn.substring(bits[0].length+1));
366                         }
367                         ret = this.consts.get(bits[0]);                 
368                         if (ret != null) {
369                                 if (bits.length < 2) {
370                                         return ret;
371                                 }
372                                 return ret.fetchByFqn(fqn.substring(bits[0].length+1));
373                         }
374
375                         ret = this.signals.get(bits[0]);                        
376                         if (ret != null) {
377                                 if (bits.length < 2) {
378                                         return ret;
379                                 }
380                                 return ret.fetchByFqn(fqn.substring(bits[0].length+1));
381                         }
382                         if (this.paramset == null) {
383                                 return null;
384                         }
385                         var iter = this.paramset.params.list_iterator();
386                         while (iter.next()) {
387                                 var p = iter.get();
388                                 if (p.name != bits[0]) {
389                                         continue;
390                                 }
391                                 return p;
392                         }
393                                  
394                         // fixme - other queires? - enums?
395                         return null;
396                 }
397                 /**
398                  *  -----------------------------------------------
399                  *  code relating to the structure loader ....
400                  * 
401                  */
402                  
403                 public GirObject clsToObject(string in_pn)
404                 {
405                         var pn = in_pn;
406                         /*
407                         
408                         
409                         var gir = Gir.factory (this.ns);
410                         if (in_pn.contains(".")) {
411                                 gir =  Gir.factory(in_pn.split(".")[0]);
412                                 pn = in_pn.split(".")[1];
413                         }
414                         */
415                         
416                         var gir = Gir.factory (this.ns);
417                         if (in_pn.contains(".")) {
418                                 gir =  Gir.factory(in_pn.split(".")[0]);
419                                 pn = in_pn.split(".")[1];
420                         }
421                         
422                         
423                         return gir.classes.get(pn);
424
425                         
426                 }
427                 public string fqtype() {
428                         return Gir.fqtypeLookup(this.type, this.ns);
429                         
430                         /* return Gir.fqtypeLookup(this.type, this.ns); */
431                 }
432         }
433             
434 }