resources/RooUsage.txt
[app.Builder.js] / old-javascript / Palete / Palete.vala
1
2 namespace Palete 
3 {
4
5
6         public errordomain Error {
7                 INVALID_TYPE,
8                 NEED_IMPLEMENTING,
9                 MISSING_FILE,
10                 INVALID_VALUE
11         }
12
13         public class Usage : Object 
14         {
15                 public Gee.ArrayList<string> left;
16                 public Gee.ArrayList<string> right;
17                 public Usage(Gee.ArrayList<string> ileft, Gee.ArrayList<string> iright)
18                 {
19                         this.left = ileft;
20                         this.right=  iright;
21                 }
22                 public bool leftHas(string s)
23                 {
24                         for(var i = 0 ; i < this.left.size; i++) {
25                                 var m = this.left.get(i);
26                                 if (s == m) {
27                                         return true;
28                                 }
29                                 if (!m.contains(":")) {
30                                         continue;
31                                 }
32                                 var ar = m.split(":");
33                                 if (ar[0] == s) {
34                                         return true;
35                                 }
36                         }
37                         return false;
38                                 
39                 }
40
41                 
42         }
43
44         
45
46     static Gee.HashMap<string,Palete>? cache = null;
47     
48     public static Palete factory(string xtype)
49     {
50         if (cache == null) {
51             cache = new Gee.HashMap<string,Palete>();
52         }
53         if (cache.get(xtype) != null) {
54             return cache.get(xtype);
55         }
56         switch(xtype) {
57             case "Gtk":
58                 cache.set(xtype, new Gtk());
59                 break;
60             case "Roo":
61                 cache.set(xtype, new Roo());
62                 break;
63             default:
64                 throw new Error.INVALID_TYPE("invalid argument to Palete factory");
65         }
66         return cache.get(xtype);
67     }
68        
69
70     public abstract class Palete : Object 
71     {
72         
73        
74         public string name;
75
76         public Gee.ArrayList<Usage> map;
77                 
78         public Palete()
79         {
80             // nothing?
81                 this.map = null;
82         }
83         
84         
85         //map : false, // array of mappings   left: [] , right : []
86         
87         //items : false, // the tree of nodes.
88         
89         
90
91
92         string  guessName(JsRender.Node ar) throws Error // turns the object into full name.
93         {
94             throw new Error.NEED_IMPLEMENTING("xxx. guessName needs implimenting");
95         }
96             
97
98             
99         /**
100          * gather a  list of potentional objects that can be added..
101          * 
102          */
103         /*
104         listChildren: function (existing) {
105             existing = existing || [];
106            // existing.push('*top'); // always have top
107             var ret  = []; 
108             console.log("GATHER LIST? " + this.map.length);
109             
110             
111             function addRight(right) {
112                 right.forEach(function(r) {
113                     if (ret.indexOf(r) > -1) {
114                         return;
115                     }
116                     ret.push(r);
117                 });
118             }
119             
120             this.map.forEach(function(m) {
121                 var done = false
122                 m.left.forEach( function(left) {
123                     if (done) return; 
124                     
125                     var l = left.replace(/:.*$/, '');
126                    // print("chk:" + l + " in " + existing.join(',')); 
127                     if (existing.indexOf(l) > -1) {
128                         addRight(m.right);
129                         done =true;
130                         //return true; // no more needed..
131                     }
132                 });
133                 
134             });
135             ret.sort();
136             
137            // console.dump(ret);
138             return ret;
139             
140             
141             
142         },
143         */
144         
145
146             
147         public string[] getChildList(string in_rval)
148         {
149
150                 if (this.map == null) {
151                         this.load();
152                 }
153                 // empty == *top
154                 
155                 var rval = in_rval == "" ? "*top" : in_rval; 
156                                 
157                                 // should be a bit more than this..
158                         // -> it should look for all elements that inherit 
159                 string[] ret = {};
160                 var rt = new Gee.ArrayList<string>();
161                 for (var i = 0; i < this.map.size; i++) {
162                         var m = this.map.get(i);
163                                 
164                         if (!m.leftHas(rval)) {
165                                 continue;
166                         }
167                         print("found LEFT, adding right\n");
168                 
169                         for(var ii =0; ii < m.right.size; ii++) {
170                                 var l = m.right.get(ii);
171                                         
172                                 if (rt.index_of(l) > -1) {
173                                     continue;
174                                 }
175                                 //print("ADD " + string.joinv(", ", ret) + "\n");
176                                 ret += l;
177                                 rt.add(l);
178                         }
179                         
180                 
181                 }
182                 print ("drop list for %s is:\n%s\n", rval, string.joinv("\n", ret));
183                 //console.log("DROP LIST:");
184                 //console.dump(ret);
185                 return ret;
186             
187         }
188
189             
190         public string[] getDropList(string rval)
191         {
192
193                 if (this.map == null) {
194                         this.load();
195                 }
196
197                                 
198                                 // should be a bit more than this..
199                         // -> it should look for all elements that inherit 
200                 string[] ret = {};
201                 var rt = new Gee.ArrayList<string>();
202                 for (var i = 0; i < this.map.size; i++) {
203                         var m = this.map.get(i);
204                                 
205                         if (m.right.index_of(rval) < 0) {
206                                 continue;
207                         }
208                         //print("found RIGHT, adding left\n");
209                 
210                         for(var ii =0; ii < m.left.size; ii++) {
211                                 var l = m.left.get(ii);
212                                         
213                                 if (rt.index_of(l) > -1) {
214                                     continue;
215                                 }
216                                 //print("ADD " + string.joinv(", ", ret) + "\n");
217                                 ret += l;
218                                 rt.add(l);
219                         }
220                         
221                 
222                 }
223                  print ("drop list for %s is:\n%s\n", rval, string.joinv("\n", ret));
224                 //console.log("DROP LIST:");
225                 //console.dump(ret);
226                 return ret;
227             
228         }
229         /**
230          * basic guess type.. 
231          * 
232          * /
233         findType : function (data, prop, value)
234         {
235             if (prop[0] == '|') {
236                 return 'function';
237             }
238             return typeof(value);
239         },
240         
241         
242         findOptions : function(ename)
243         {
244             switch(ename.toLowerCase()) {
245                 case 'boolean': 
246                     return [ 'true', 'false' ];
247                 // everything else does not have options.
248                 case 'string': 
249                 case 'utf8': 
250                 case 'int': 
251                 case 'uint': 
252                 case 'function': 
253                     return false;
254                 default: 
255                     console.log("OOPS: = unknown type: " + ename);
256                     return false;
257             }
258         },
259         confirmCanAdd: function(parent, child) {
260             // confirms that one obj can be added to another.
261             // returns true, for items, or list of properties that can hold it..
262             return true;
263             
264         },
265         getDefaultPack: function(pname, cname) {
266             return 'add';
267         },
268         */
269         public void saveTemplate (string name, JsRender.Node data)
270         {
271
272                         var gn = data.fqn();
273             // store it in user's directory..
274             var appdir =  GLib.Environment.get_home_dir() + "/.Builder"; 
275
276                         
277             if (!GLib.FileUtils.test(appdir+ "/" + gn, GLib.FileTest.IS_DIR)) {
278                                 GLib.File.new_for_path (appdir+ "/" + gn).make_directory ();
279                                 
280             }
281             GLib.FileUtils.set_contents(appdir+ "/" + gn + "/" +  name + ".json", data.toJsonString());
282             
283         }
284         
285         /**
286          * list templates - in home directory (and app dir in future...)
287          * @param {String} name  - eg. Gtk.Window..
288          * @return {Array} list of templates available..
289          */
290           
291         public  GLib.List<string> listTemplates (JsRender.Node node)
292         {
293             
294                 var gn = node.fqn();
295             
296                 var ret = new GLib.List<string>();
297                 var dir= GLib.Environment.get_home_dir() + "/.Builder/" + gn;
298                 if (!GLib.FileUtils.test(dir, GLib.FileTest.IS_DIR)) {
299                         return ret;
300                 }
301                         
302
303
304                                     
305                 var f = File.new_for_path(dir);
306         
307             var file_enum = f.enumerate_children(GLib.FileAttribute.STANDARD_DISPLAY_NAME, GLib.FileQueryInfoFlags.NONE, null);
308              
309             FileInfo next_file; 
310             while ((next_file = file_enum.next_file(null)) != null) {
311                 var n = next_file.get_display_name();
312                         if (!Regex.match_simple ("\\.json$", n)) {
313                                         continue;
314                                 }
315                                 ret.append( dir + "/" + n);
316             }
317             return ret;
318             
319         }
320  
321         public JsRender.Node? loadTemplate(string path)
322         {
323
324                 var pa = new Json.Parser();
325                 pa.load_from_file(path);
326                 var node = pa.get_root();
327
328                 if (node.get_node_type () != Json.NodeType.OBJECT) {
329                         return null;
330                 }
331                 var obj = node.get_object ();
332
333                 var ret = new JsRender.Node();
334
335
336                 ret.loadFromJson(obj, 1);
337                 ret.ref(); // not sure if needed -- but we had a case where ret became uninitialized?
338                 
339                 return ret;
340          }
341           public   void  loadUsageFile (string fname) {
342
343
344
345                         
346                 print("Palete Load called\n");
347                 string raw;
348                 if (!FileUtils.test (fname, FileTest.EXISTS)) {
349                         throw new Error.MISSING_FILE(fname + " not found");
350                 }
351         
352                 FileUtils.get_contents(fname, out raw);
353               // print(data);
354                 var data  = raw.split("\n");
355                 var state = 0;
356                 var cfg = new Gee.ArrayList<Usage>();
357                 var left = new Gee.ArrayList<string>();
358                 var right = new Gee.ArrayList<string>();
359         
360                 for (var i = 0; i < data.length; i++) {
361                         var d = data[i].strip();
362                         //print("READ " + d + "\n");
363                         if (
364                                 d.length < 1
365                             ||
366                              Regex.match_simple ("^\\s+$", d)
367                             ||
368                                 Regex.match_simple ("^\\s*/", d)
369                      ){
370                             continue;
371                         }
372                                 
373                         if (Regex.match_simple ("^left:", d)) { 
374                             state = 1;
375                             if (left.size > 0 ){
376                                 cfg.add(new Usage( left, right));
377                                                 }
378                             left = new Gee.ArrayList<string>();
379                                         right = new Gee.ArrayList<string>();
380                             continue;
381                         }
382                          if (Regex.match_simple ("^right:", d)) { 
383                             state = 2;
384                             continue;
385                         }
386                         if (state == 1) {
387                                                 //print("add left: " + d + "\n");
388                             left.add(d);
389                             continue;
390                         }
391                                         //print("add Right: " + d + "\n");
392                         right.add(d);
393                         //Seed.quit();
394                        
395                 }
396                 if (left.size > 0 ){
397                         cfg.add(new Usage( left, right));
398                 }
399                 this.map = cfg;
400
401         }
402
403         
404           
405         public Gee.HashMap<int,string>  validateCode(
406                         string code, 
407                         string property, 
408                         string ptype,
409                         JsRender.JsRender file,
410                         JsRender.Node node
411                      ) 
412         {   
413
414                 //print("validate code (%s) %s\n", file.language, code);
415                  
416                 
417                 if (file.language == "js" && (ptype == "listener" || property[0] == '|')) {
418                         var ret = new Gee.HashMap<int,string>();
419                         
420                         //var cd = new JSCore.ClassDefinitionEmpty();
421                         string errmsg;
422                         var line = Javascript.singleton().validate(
423                                   "var __aaa___ = " + code, out errmsg);
424
425                         if (line < 0) {
426                                 return ret;
427                         }
428                         ret.set(line, errmsg);
429                         return ret;
430                 }
431                 if (file.language == "vala" ) { // not sure if we need to validate property
432                         
433                         var vs = new ValaSource(file);
434                         //var cd = new JSCore.ClassDefinitionEmpty();
435                         var ret = vs.checkFileWithNodePropChange(
436                                 node, 
437                                 property, 
438                                 ptype,
439                                 code
440                          );
441                         return ret;
442                 }
443                 var ret = new Gee.HashMap<int,string>();
444
445                 //print ("not javascript\n");
446                 return ret;
447
448         }
449
450             
451           
452         public abstract void fillPack(JsRender.Node node,JsRender.Node parent);
453         public abstract void load();
454         public abstract Gee.HashMap<string,GirObject> getPropertiesFor(string ename, string type);
455         public abstract GirObject? getClass(string ename);
456     }
457
458
459 }
460
461
462