3 // valac gitlive/app.Builder.js/JsRender/Lang.vala gitlive/app.Builder.js/JsRender/Node.vala --pkg gee-1.0 --pkg=json-glib-1.0 -o /tmp/Lang ;/tmp/Lang
5 public class JsRender.Node {
7 public GLib.List<Node> items; // child items..
9 public Gee.HashMap<string,string> props; // the properties..
10 public Gee.HashMap<string,string> listeners; // the listeners..
11 public string xvala_cls;
12 public string xvala_xcls; // 'Xcls_' + id;
13 public string xvala_id; // item id or ""
19 this.items = new GLib.List<Node>();
20 this.props = new Gee.HashMap<string,string>();
21 this.listeners = new Gee.HashMap<string,string>();
22 this.is_array = false;
33 public bool hasChildren()
35 return this.items.length() > 0;
37 public bool hasXnsType()
39 if (this.props.get("|xns") != null && this.props.get("xtype") != null) {
47 if (!this.hasXnsType ()) {
50 return this.props.get("|xns") + "." + this.props.get("xtype");
54 // wrapper around get props that returns empty string if not found.
55 public string get(string key)
57 var k = this.props.get(key);
62 k = this.props.get("|" + key);
72 /* creates javascript based on the rules */
73 public Node? findProp(string n) {
74 for(var i=0;i< this.items.length();i++) {
75 var p = this.items.nth_data(i).get("*prop");
76 if (this.items.nth_data(i).get("*prop").length < 1) {
80 return this.items.nth_data(i);
88 public string mungeToString (bool isListener, string pad, GLib.List<string> doubleStringProps)
92 pad = pad.length < 1 ? " " : pad;
97 //isListener = isListener || false;
99 //var keys = this.keys();
100 var isArray = this.isArray();
103 var els = new GLib.List<string>();
104 var skip = new Gee.ArrayList<string>();
105 if (!isArray && this.hasXnsType() ) {
106 // this.mungeXtype(obj['|xns'] + '.' + obj['xtype'], els); ??????
113 //var newitems = new Gee.ArrayList<JsRender.Node>();
114 var oprops = new Gee.HashMap<string,Node>();
116 if (!isArray && this.hasChildren()) {
119 for (var ii =0; ii< this.items.length(); ii++) {
120 var pl = this.items.nth_data(ii);
121 if (!pl.props.has_key("*prop")) {
126 //print(JSON.stringify(pl,null,4));
128 //var prop = pl['*prop'] + '';
129 //delete pl['*prop'];
130 var prop = pl.get("*prop");
132 if (! Regex.match_simple("\\[\\]$", prop)) {
133 // it's a standard prop..
135 // munge property..??
136 oprops.set(prop, pl);
143 prop = prop.substring(0, -2); //strip []
144 // it's an array type..
145 if (!oprops.has_key(prop)) {
147 oprops.set(prop, cn);
150 // ignores the fact it might be duplciated...
151 oprops.get(prop).is_array = true;
152 oprops.get(prop).items.append(pl);
159 //obj.items = newitems;
160 //if (!obj.items.length) {
165 if (this.isArray()) {
168 for (var i=0;i< this.items.length();i++) {
169 var el = this.items.nth_data(i);
171 els.append("%d".printf(i) + " : " + el.mungeToString(false, pad,doubleStringProps));
174 var spad = pad.substring(0, pad.length-4);
176 pad + string.join(",\n" + pad , els) +
187 func_regex = new Regex("^\\s+|\\s+$");
189 print("failed to build regex");
192 var piter = this.props.map_iterator();
193 while (piter.next() ) {
194 var k = piter.get_key();
195 var v = piter.get_value();
197 if (skip.contains(k) ) {
202 string leftv = k[0] == '|' ? k.substring(1) : k;
203 // skip builder stuff. prefixed with '.' .. just like unix fs..
204 if (leftv[0] == '.') { // |. or . -- do not output..
213 if (Lang.isKeyword(leftv) || Lang.isBuiltin(leftv)) {
214 left = "'" + leftv + "'";
215 } else if (Regex.match_simple("[^A-Za-z_]+",leftv)) { // not plain a-z... - quoted.
216 var val = this.quoteString(leftv);
218 left = "'" + val.substring(1, val.length-1).replace("'", "\\'") + "'";
225 // change the lines...
229 str = func_regex.replace(v,v.length, 0, "");
231 print("regex failed");
235 var lines = str.split("\n");
236 if (lines.length > 1) {
237 str = string.join("\n" + pad, lines);
240 els.append(left + str);
244 // next.. is it a function..
246 // does not hapepnd with arrays..
247 if (v.length < 1) { //if (typeof(el) == 'string' && !obj[i].length) { //skip empty.
253 str = func_regex.replace(v,v.length, 0, "");
255 print("regex failed");
259 var lines = str.split("\n");
260 if (lines.length > 1) {
261 str = string.join("\n" + pad, lines);
264 els.append(left + str);
270 if (Lang.isNumber(v) || Lang.isBoolean(v)) { // boolean or number...?
271 els.append(left + v );
276 if (doubleStringProps.length() < 1) {
277 els.append(left + this.quoteString(v));
281 if (doubleStringProps.index(k) > -1) {
282 els.append(left + this.quoteString(v));
286 // single quote.. v.substring(1, v.length-1).replace("'", "\\'") + "'";
287 els.append(left + "'" + v.substring(1, v.length-1).replace("'", "\\'") + "'");
294 var iter = oprops.map_iterator();
295 while (iter.next()) {
296 var k = iter.get_key();
297 var vo = iter.get_value();
298 string leftv = k[0] == '|' ? k.substring(1) : k;
299 if (Lang.isKeyword(leftv) || Lang.isBuiltin(leftv)) {
300 left = "'" + leftv + "'";
301 } else if (Regex.match_simple("[^A-Za-z_]+",leftv)) { // not plain a-z... - quoted.
302 var val = this.quoteString(leftv);
304 left = "'" + val.substring(1, val.length-1).replace("'", "\\'") + "'";
310 var right = vo.mungeToString(k == "listeners", pad + " ",doubleStringProps);
312 //if (!left.length && isArray) print(right);
314 if (right.length > 0){
315 els.append(left + right);
320 if (els.length() < 1) {
325 var spad = pad.substring(0, pad.length-4);
327 pad + string.join(",\n" + pad , els) +
335 static Json.Generator gen = null;
337 public string quoteString(string str)
339 if (Node.gen == null) {
340 Node.gen = new Json.Generator();
342 var builder = new Json.Builder();
343 builder.add_string_value(str);
344 Node.gen.set_root (builder.get_root ());
345 return Node.gen.to_data (null);
348 public void loadFromJson(Json.Object obj) {
349 obj.foreach_member((o , key, value) => {
350 if (key == "items") {
351 var ar = value.get_array();
352 ar.foreach_element( (are, ix, el) => {
353 var node = new Node();
354 node.loadFromJson(el.get_object());
355 this.items.append(node);
359 if (key == "listeners") {
360 var li = value.get_object();
361 obj.foreach_member((lio , li_key, li_value) => {
362 this.listeners.set(li_key, li_value.get_string());
367 this.props.set(key, value.get_string());