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..
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;
58 public GirObject(string nodetype, string n)
60 this.nodetype = nodetype;
66 this.is_array = false;
67 this.is_instance = false;
68 this.is_varargs = false;
69 this.ctor_only =false;
76 this.implements = new Gee.ArrayList<string>();
77 this.inherits = new Gee.ArrayList<string>(); // list of all ancestors. (interfaces and parents)
78 this.includes = new Gee.HashMap<string,string>();
80 this.params = new Gee.ArrayList<GirObject>();
81 this.ctors = new Gee.HashMap<string,GirObject>();
82 this.methods =new Gee.HashMap<string,GirObject>();
84 this.classes = new Gee.HashMap<string,GirObject>();
85 this.props = new Gee.HashMap<string,GirObject>();
86 this.consts = new Gee.HashMap<string,GirObject>();
87 this.signals = new Gee.HashMap<string,GirObject>();
88 this.is_overlaid = false;
92 public string[] inheritsToStringArray()
95 for(var i =0;i< this.inherits.size; i++) {
96 ret += this.inherits.get(i);
103 public void overlayParent()
106 if (this.parent.length < 1 || this.is_overlaid) {
107 this.is_overlaid = true;
110 // print("Overlaying " +this.name + " with " + this.parent + "\n");
112 var pcls = this.clsToObject( this.parent);
114 throw new GirError.INVALID_VALUE("Could not find class : " +
115 this.parent + " of " + this.name + " in " + this.ns);
118 pcls.overlayParent( );
119 this.copyFrom(pcls,false);
120 for(var i=0; i < this.implements.size; i++) {
121 var clsname = this.implements.get(i);
122 var picls = this.clsToObject(clsname);
123 this.copyFrom(picls,true);
125 this.is_overlaid = true;
129 public void overlayCtorProperties()
131 //print("Check overlay Ctor %s\n", this.name);
132 if (!this.ctors.has_key("new")) {
135 var ctor = this.ctors.get("new");
136 if (ctor.paramset == null || ctor.paramset.params.size < 1) {
139 //print("Found Ctor\n");
140 var iter = ctor.paramset.params.list_iterator();
141 while (iter.next()) {
142 var n = iter.get().name;
144 if (this.props.has_key(n)) {
150 //print("Adding prop %s\n", n);
153 var c = new GirObject("Prop",n);
156 c.propertyof = this.name;
157 c.type = iter.get().type;
159 this.props.set(n, c);
167 public string fqn() {
168 // not sure if fqn really is correct here...
170 return this.nodetype == "Class" || this.nodetype=="Interface"
171 ? this.name : (this.ns + this.name);
174 public void copyFrom(GirObject pcls, bool is_interface)
177 this.inherits.add(pcls.fqn());
179 var liter = pcls.inherits.list_iterator();
180 while(liter.next()) {
181 if (this.inherits.contains(liter.get())) {
184 this.inherits.add(liter.get());
188 var iter = pcls.methods.map_iterator();
190 if (null != this.methods.get(iter.get_key())) {
194 this.methods.set(iter.get_key(), iter.get_value());
197 iter = pcls.props.map_iterator();
199 if (null != this.props.get(iter.get_key())) {
203 this.props.set(iter.get_key(), iter.get_value());
206 iter = pcls.signals.map_iterator();
208 if (null != this.signals.get(iter.get_key())) {
212 this.signals.set(iter.get_key(), iter.get_value());
216 public Json.Object toJSON()
218 var r = new Json.Object();
219 r.set_string_member("nodetype", this.nodetype);
220 r.set_string_member("name", this.name);
221 if (this.propertyof.length > 0) {
222 r.set_string_member("of", this.propertyof);
224 if (this.type.length > 0) {
225 r.set_string_member("type", this.type);
227 if (this.parent != null && this.parent.length > 0) {
228 r.set_string_member("parent", this.parent);
230 if (this.sig.length > 0) {
231 r.set_string_member("sig", this.sig);
234 // is_arary / is_instance / is_varargs..
237 if (this.inherits.size > 0) {
238 r.set_array_member("inherits", this.toJSONArrayString(this.inherits));
241 if (this.implements.size > 0) {
242 r.set_array_member("implements", this.toJSONArrayString(this.implements));
245 if (this.params.size > 0) {
246 r.set_array_member("params", this.toJSONArrayObject(this.params));
248 if (this.ctors.size > 0) {
249 r.set_object_member("ctors", this.toJSONObject(this.ctors));
251 if (this.methods.size > 0) {
252 r.set_object_member("methods", this.toJSONObject(this.methods));
254 if (this.includes.size > 0) {
255 r.set_object_member("includes", this.toJSONObjectString(this.includes));
257 if (this.classes.size > 0) {
258 r.set_object_member("classes", this.toJSONObject(this.classes));
260 if (this.props.size > 0) {
261 r.set_object_member("props", this.toJSONObject(this.props));
263 if (this.consts.size > 0) {
264 r.set_object_member("consts", this.toJSONObject(this.consts));
266 if (this.signals.size > 0) {
267 r.set_object_member("signals", this.toJSONObject(this.signals));
269 if (this.paramset != null) {
270 r.set_object_member("paramset", this.paramset.toJSON());
272 if (this.return_value != null) {
273 r.set_object_member("return_value", this.return_value.toJSON());
277 public Json.Object toJSONObject(Gee.HashMap<string,GirObject> map)
279 var r = new Json.Object();
280 var iter = map.map_iterator();
282 r.set_object_member(iter.get_key(), iter.get_value().toJSON());
286 public Json.Object toJSONObjectString(Gee.HashMap<string,string> map)
288 var r = new Json.Object();
289 var iter = map.map_iterator();
291 r.set_string_member(iter.get_key(), iter.get_value());
295 public Json.Array toJSONArrayString(Gee.ArrayList<string> map)
297 var r = new Json.Array();
298 for(var i =0;i< map.size;i++) {
300 r.add_string_element(map.get(i));
304 public Json.Array toJSONArrayObject(Gee.ArrayList<GirObject> map)
306 var r = new Json.Array();
307 for(var i =0;i< map.size;i++) {
309 r.add_object_element(map.get(i).toJSON());
313 public string asJSONString()
315 var generator = new Json.Generator ();
316 generator.indent = 4;
317 generator.pretty = true;
318 var n = new Json.Node(Json.NodeType.OBJECT);
319 n.set_object(this.toJSON());
320 generator.set_root(n);
322 return generator.to_data(null);
326 public GirObject fetchByFqn(string fqn) {
327 //print("Searching (%s)%s for %s\n", this.nodetype, this.name, fqn);
328 var bits = fqn.split(".");
330 var ret = this.classes.get(bits[0]);
332 if (bits.length < 2) {
335 return ret.fetchByFqn(fqn.substring(bits[0].length+1));
338 ret = this.ctors.get(bits[0]);
340 if (bits.length < 2) {
343 return ret.fetchByFqn(fqn.substring(bits[0].length+1));
346 ret = this.methods.get(bits[0]);
348 if (bits.length < 2) {
351 return ret.fetchByFqn(fqn.substring(bits[0].length+1));
353 ret = this.props.get(bits[0]);
355 if (bits.length < 2) {
358 return ret.fetchByFqn(fqn.substring(bits[0].length+1));
360 ret = this.consts.get(bits[0]);
362 if (bits.length < 2) {
365 return ret.fetchByFqn(fqn.substring(bits[0].length+1));
368 ret = this.signals.get(bits[0]);
370 if (bits.length < 2) {
373 return ret.fetchByFqn(fqn.substring(bits[0].length+1));
375 if (this.paramset == null) {
378 var iter = this.paramset.params.list_iterator();
379 while (iter.next()) {
381 if (p.name != bits[0]) {
387 // fixme - other queires? - enums?
391 * -----------------------------------------------
392 * code relating to the structure loader ....
396 public GirObject clsToObject(string in_pn)
402 var gir = Gir.factory (this.ns);
403 if (in_pn.contains(".")) {
404 gir = Gir.factory(in_pn.split(".")[0]);
405 pn = in_pn.split(".")[1];
409 var gir = VapiParser.factory (this.ns);
410 if (in_pn.contains(".")) {
411 gir = VapiParser.factory(in_pn.split(".")[0]);
412 pn = in_pn.split(".")[1];
416 return gir.classes.get(pn);
420 public string fqtype() {
421 return Gir.fqtypeLookup(this.type, this.ns);
423 /* return Gir.fqtypeLookup(this.type, this.ns); */