2 * This is the base class for representing the vala API
4 * it was originally based on parsing Gir files - but since then
5 * has evolved into using libvala
13 public errordomain GirError {
20 public class GirObject: Object {
23 public string propertyof;
25 public string nodetype;
26 public string package;
28 public GirObject paramset = null;
29 public GirObject return_value = null;
31 public bool is_instance;
33 public bool is_varargs;
34 public bool ctor_only; // specially added ctor properties..
41 public bool is_overlaid;
43 public GirObject gparent;
44 public Gee.ArrayList<GirObject> params;
45 public Gee.ArrayList<string> implements;
46 public Gee.ArrayList<string> inherits; // full list of all classes and interfaces...
47 public Gee.HashMap<string,GirObject> ctors;
48 public Gee.HashMap<string,GirObject> methods;
49 public Gee.HashMap<string,string> includes;
50 public Gee.HashMap<string,GirObject> classes;
51 public Gee.HashMap<string,GirObject> props;
52 public Gee.HashMap<string,GirObject> consts;
53 public Gee.HashMap<string,GirObject> signals;
55 public Gee.ArrayList<string> optvalues; // used by Roo only..
60 public GirObject(string nodetype, string n)
62 this.nodetype = nodetype;
68 this.is_array = false;
69 this.is_instance = false;
70 this.is_varargs = false;
71 this.ctor_only =false;
78 this.implements = new Gee.ArrayList<string>();
79 this.inherits = new Gee.ArrayList<string>(); // list of all ancestors. (interfaces and parents)
80 this.includes = new Gee.HashMap<string,string>();
82 this.params = new Gee.ArrayList<GirObject>();
83 this.ctors = new Gee.HashMap<string,GirObject>();
84 this.methods =new Gee.HashMap<string,GirObject>();
86 this.classes = new Gee.HashMap<string,GirObject>();
87 this.props = new Gee.HashMap<string,GirObject>();
88 this.consts = new Gee.HashMap<string,GirObject>();
89 this.signals = new Gee.HashMap<string,GirObject>();
91 this.optvalues = new Gee.ArrayList<string>();
92 this.is_overlaid = false;
96 public string[] inheritsToStringArray()
99 for(var i =0;i< this.inherits.size; i++) {
100 ret += this.inherits.get(i);
107 public void overlayParent()
110 if (this.parent.length < 1 || this.is_overlaid) {
111 this.is_overlaid = true;
115 //print("Overlaying " +this.name + " with " + this.parent + "\n");
117 var pcls = this.clsToObject( this.parent);
120 //throw new GirError.INVALID_VALUE("Could not find class : " +
121 // this.parent + " of " + this.name + " in " + this.ns);
124 pcls.overlayParent( );
125 this.copyFrom(pcls,false);
126 for(var i=0; i < this.implements.size; i++) {
127 var clsname = this.implements.get(i);
128 var picls = this.clsToObject(clsname);
129 this.copyFrom(picls,true);
131 this.is_overlaid = true;
135 public void overlayCtorProperties()
137 //print("Check overlay Ctor %s\n", this.name);
138 if (!this.ctors.has_key("new")) {
141 var ctor = this.ctors.get("new");
142 if (ctor.paramset == null || ctor.paramset.params.size < 1) {
145 //print("Found Ctor\n");
146 var iter = ctor.paramset.params.list_iterator();
147 while (iter.next()) {
148 var n = iter.get().name;
150 if (this.props.has_key(n)) {
156 //print("Adding prop %s\n", n);
159 var c = new GirObject("Prop",n);
162 c.propertyof = this.name;
163 c.type = iter.get().type;
165 this.props.set(n, c);
173 public string fqn() {
174 // not sure if fqn really is correct here...
176 return this.nodetype == "Class" || this.nodetype=="Interface"
177 ? this.name : (this.ns + this.name);
180 public void copyFrom(GirObject pcls, bool is_interface)
183 this.inherits.add(pcls.fqn());
185 var liter = pcls.inherits.list_iterator();
186 while(liter.next()) {
187 if (this.inherits.contains(liter.get())) {
190 this.inherits.add(liter.get());
194 var iter = pcls.methods.map_iterator();
196 if (null != this.methods.get(iter.get_key())) {
200 this.methods.set(iter.get_key(), iter.get_value());
203 iter = pcls.props.map_iterator();
205 if (null != this.props.get(iter.get_key())) {
209 this.props.set(iter.get_key(), iter.get_value());
212 iter = pcls.signals.map_iterator();
214 if (null != this.signals.get(iter.get_key())) {
218 this.signals.set(iter.get_key(), iter.get_value());
222 public Json.Object toJSON()
224 var r = new Json.Object();
225 r.set_string_member("nodetype", this.nodetype);
226 r.set_string_member("name", this.name);
227 if (this.propertyof.length > 0) {
228 r.set_string_member("of", this.propertyof);
230 if (this.type.length > 0) {
231 r.set_string_member("type", this.type);
233 if (this.parent != null && this.parent.length > 0) {
234 r.set_string_member("parent", this.parent);
236 if (this.sig.length > 0) {
237 r.set_string_member("sig", this.sig);
240 // is_arary / is_instance / is_varargs..
243 if (this.inherits.size > 0) {
244 r.set_array_member("inherits", this.toJSONArrayString(this.inherits));
247 if (this.implements.size > 0) {
248 r.set_array_member("implements", 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("paramset", 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());
319 public string asJSONString()
321 var generator = new Json.Generator ();
322 generator.indent = 4;
323 generator.pretty = true;
324 var n = new Json.Node(Json.NodeType.OBJECT);
325 n.set_object(this.toJSON());
326 generator.set_root(n);
328 return generator.to_data(null);
332 public GirObject fetchByFqn(string fqn) {
333 //print("Searching (%s)%s for %s\n", this.nodetype, this.name, fqn);
334 var bits = fqn.split(".");
336 var ret = this.classes.get(bits[0]);
338 if (bits.length < 2) {
341 return ret.fetchByFqn(fqn.substring(bits[0].length+1));
344 ret = this.ctors.get(bits[0]);
346 if (bits.length < 2) {
349 return ret.fetchByFqn(fqn.substring(bits[0].length+1));
352 ret = this.methods.get(bits[0]);
354 if (bits.length < 2) {
357 return ret.fetchByFqn(fqn.substring(bits[0].length+1));
359 ret = this.props.get(bits[0]);
361 if (bits.length < 2) {
364 return ret.fetchByFqn(fqn.substring(bits[0].length+1));
366 ret = this.consts.get(bits[0]);
368 if (bits.length < 2) {
371 return ret.fetchByFqn(fqn.substring(bits[0].length+1));
374 ret = this.signals.get(bits[0]);
376 if (bits.length < 2) {
379 return ret.fetchByFqn(fqn.substring(bits[0].length+1));
381 if (this.paramset == null) {
384 var iter = this.paramset.params.list_iterator();
385 while (iter.next()) {
387 if (p.name != bits[0]) {
393 // fixme - other queires? - enums?
397 * -----------------------------------------------
398 * code relating to the structure loader ....
402 public GirObject clsToObject(string in_pn)
408 var gir = Gir.factory (this.ns);
409 if (in_pn.contains(".")) {
410 gir = Gir.factory(in_pn.split(".")[0]);
411 pn = in_pn.split(".")[1];
415 var gir = Gir.factory (this.ns);
416 if (in_pn.contains(".")) {
417 gir = Gir.factory(in_pn.split(".")[0]);
418 pn = in_pn.split(".")[1];
422 return gir.classes.get(pn);
426 public string fqtype() {
427 return Gir.fqtypeLookup(this.type, this.ns);
429 /* return Gir.fqtypeLookup(this.type, this.ns); */