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;
27 public string direction; // used for vala in/out/ref...
29 public GirObject paramset = null;
30 public GirObject return_value = null;
32 public bool is_instance;
34 public bool is_varargs;
35 public bool ctor_only; // specially added ctor properties..
42 public bool is_overlaid;
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;
56 public Gee.ArrayList<string> optvalues; // used by Roo only..
61 public GirObject(string nodetype, string n)
63 this.nodetype = nodetype;
69 this.is_array = false;
70 this.is_instance = false;
71 this.is_varargs = false;
72 this.ctor_only =false;
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>();
83 this.params = new Gee.ArrayList<GirObject>();
84 this.ctors = new Gee.HashMap<string,GirObject>();
85 this.methods =new Gee.HashMap<string,GirObject>();
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>();
92 this.optvalues = new Gee.ArrayList<string>();
93 this.is_overlaid = false;
97 public string[] inheritsToStringArray()
100 for(var i =0;i< this.inherits.size; i++) {
101 ret += this.inherits.get(i);
108 public void overlayParent()
111 if (this.parent.length < 1 || this.is_overlaid) {
112 this.is_overlaid = true;
116 //print("Overlaying " +this.name + " with " + this.parent + "\n");
118 var pcls = this.clsToObject( this.parent);
121 //throw new GirError.INVALID_VALUE("Could not find class : " +
122 // this.parent + " of " + this.name + " in " + this.ns);
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);
132 this.is_overlaid = true;
136 public void overlayCtorProperties()
138 //print("Check overlay Ctor %s\n", this.name);
139 if (!this.ctors.has_key("new")) {
142 var ctor = this.ctors.get("new");
143 if (ctor.paramset == null || ctor.paramset.params.size < 1) {
146 //print("Found Ctor\n");
147 var iter = ctor.paramset.params.list_iterator();
148 while (iter.next()) {
149 var n = iter.get().name;
151 if (this.props.has_key(n)) {
157 //print("Adding prop %s\n", n);
160 var c = new GirObject("Prop",n);
163 c.propertyof = this.name;
164 c.type = iter.get().type;
166 this.props.set(n, c);
174 public string fqn() {
175 // not sure if fqn really is correct here...
177 return this.nodetype == "Class" || this.nodetype=="Interface"
178 ? this.name : (this.ns + this.name);
181 public void copyFrom(GirObject pcls, bool is_interface)
184 this.inherits.add(pcls.fqn());
186 var liter = pcls.inherits.list_iterator();
187 while(liter.next()) {
188 if (this.inherits.contains(liter.get())) {
191 this.inherits.add(liter.get());
195 var iter = pcls.methods.map_iterator();
197 if (null != this.methods.get(iter.get_key())) {
201 this.methods.set(iter.get_key(), iter.get_value());
204 iter = pcls.props.map_iterator();
206 if (null != this.props.get(iter.get_key())) {
210 this.props.set(iter.get_key(), iter.get_value());
213 iter = pcls.signals.map_iterator();
215 if (null != this.signals.get(iter.get_key())) {
219 this.signals.set(iter.get_key(), iter.get_value());
223 public Json.Object toJSON()
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);
231 if (this.type.length > 0) {
232 r.set_string_member("type", this.type);
234 if (this.parent != null && this.parent.length > 0) {
235 r.set_string_member("parent", this.parent);
237 if (this.sig.length > 0) {
238 r.set_string_member("sig", this.sig);
241 // is_arary / is_instance / is_varargs..
244 if (this.inherits.size > 0) {
245 r.set_array_member("inherits", this.toJSONArrayString(this.inherits));
248 if (this.implements.size > 0) {
249 r.set_array_member("implements", this.toJSONArrayString(this.implements));
252 if (this.params.size > 0) {
253 r.set_array_member("params", this.toJSONArrayObject(this.params));
255 if (this.ctors.size > 0) {
256 r.set_object_member("ctors", this.toJSONObject(this.ctors));
258 if (this.methods.size > 0) {
259 r.set_object_member("methods", this.toJSONObject(this.methods));
261 if (this.includes.size > 0) {
262 r.set_object_member("includes", this.toJSONObjectString(this.includes));
264 if (this.classes.size > 0) {
265 r.set_object_member("classes", this.toJSONObject(this.classes));
267 if (this.props.size > 0) {
268 r.set_object_member("props", this.toJSONObject(this.props));
270 if (this.consts.size > 0) {
271 r.set_object_member("consts", this.toJSONObject(this.consts));
273 if (this.signals.size > 0) {
274 r.set_object_member("signals", this.toJSONObject(this.signals));
276 if (this.paramset != null) {
277 r.set_object_member("paramset", this.paramset.toJSON());
279 if (this.return_value != null) {
280 r.set_object_member("return_value", this.return_value.toJSON());
284 public Json.Object toJSONObject(Gee.HashMap<string,GirObject> map)
286 var r = new Json.Object();
287 var iter = map.map_iterator();
289 r.set_object_member(iter.get_key(), iter.get_value().toJSON());
293 public Json.Object toJSONObjectString(Gee.HashMap<string,string> map)
295 var r = new Json.Object();
296 var iter = map.map_iterator();
298 r.set_string_member(iter.get_key(), iter.get_value());
302 public Json.Array toJSONArrayString(Gee.ArrayList<string> map)
304 var r = new Json.Array();
305 for(var i =0;i< map.size;i++) {
307 r.add_string_element(map.get(i));
311 public Json.Array toJSONArrayObject(Gee.ArrayList<GirObject> map)
313 var r = new Json.Array();
314 for(var i =0;i< map.size;i++) {
316 r.add_object_element(map.get(i).toJSON());
320 public string asJSONString()
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);
329 return generator.to_data(null);
333 public GirObject fetchByFqn(string fqn) {
334 //print("Searching (%s)%s for %s\n", this.nodetype, this.name, fqn);
335 var bits = fqn.split(".");
337 var ret = this.classes.get(bits[0]);
339 if (bits.length < 2) {
342 return ret.fetchByFqn(fqn.substring(bits[0].length+1));
345 ret = this.ctors.get(bits[0]);
347 if (bits.length < 2) {
350 return ret.fetchByFqn(fqn.substring(bits[0].length+1));
353 ret = this.methods.get(bits[0]);
355 if (bits.length < 2) {
358 return ret.fetchByFqn(fqn.substring(bits[0].length+1));
360 ret = this.props.get(bits[0]);
362 if (bits.length < 2) {
365 return ret.fetchByFqn(fqn.substring(bits[0].length+1));
367 ret = this.consts.get(bits[0]);
369 if (bits.length < 2) {
372 return ret.fetchByFqn(fqn.substring(bits[0].length+1));
375 ret = this.signals.get(bits[0]);
377 if (bits.length < 2) {
380 return ret.fetchByFqn(fqn.substring(bits[0].length+1));
382 if (this.paramset == null) {
385 var iter = this.paramset.params.list_iterator();
386 while (iter.next()) {
388 if (p.name != bits[0]) {
394 // fixme - other queires? - enums?
398 * -----------------------------------------------
399 * code relating to the structure loader ....
403 public GirObject clsToObject(string in_pn)
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];
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];
423 return gir.classes.get(pn);
427 public string fqtype() {
428 return Gir.fqtypeLookup(this.type, this.ns);
430 /* return Gir.fqtypeLookup(this.type, this.ns); */