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.is_array = false;
32 public bool hasChildren()
34 return this.items.length() > 0;
36 public bool hasXnsType()
38 if (this.props.get("|xns") != null && this.props.get("xtype") != null) {
46 if (!this.hasXnsType ()) {
49 this.props.get("|xns") + "." + this.props.get("xtype");
53 // wrapper around get props that returns empty string if not found.
54 public string get(string key)
56 var k = this.props.get(key);
61 k = this.props.get("|" + key);
71 /* creates javascript based on the rules */
72 public Node? findProp(string n) {
73 for(var i=0;i< this.items.length();i++) {
74 var p = this.items.nth_data(i).get("*prop");
75 if (this.items.nth_data(i).get("*prop").length < 1) {
79 return this.items.nth_data(i);
87 public string mungeToString (bool isListener, string pad, GLib.List<string> doubleStringProps)
91 pad = pad.length < 1 ? " " : pad;
96 //isListener = isListener || false;
98 //var keys = this.keys();
99 var isArray = this.isArray();
102 var els = new GLib.List<string>();
103 var skip = new Gee.ArrayList<string>();
104 if (!isArray && this.hasXnsType() ) {
105 // this.mungeXtype(obj['|xns'] + '.' + obj['xtype'], els); ??????
112 //var newitems = new Gee.ArrayList<JsRender.Node>();
113 var oprops = new Gee.HashMap<string,Node>();
115 if (!isArray && this.hasChildren()) {
118 for (var ii =0; ii< this.items.length(); ii++) {
119 var pl = this.items.nth_data(ii);
120 if (!pl.props.has_key("*prop")) {
125 //print(JSON.stringify(pl,null,4));
127 //var prop = pl['*prop'] + '';
128 //delete pl['*prop'];
129 var prop = pl.get("*prop");
131 if (! Regex.match_simple("\\[\\]$", prop)) {
132 // it's a standard prop..
134 // munge property..??
135 oprops.set(prop, pl);
142 prop = prop.substring(0, -2); //strip []
143 // it's an array type..
144 if (!oprops.has_key(prop)) {
146 oprops.set(prop, cn);
149 // ignores the fact it might be duplciated...
150 oprops.get(prop).is_array = true;
151 oprops.get(prop).items.append(pl);
158 //obj.items = newitems;
159 //if (!obj.items.length) {
164 if (this.isArray()) {
167 for (var i=0;i< this.items.length();i++) {
168 var el = this.items.nth_data(i);
170 els.append("%d".printf(i) + " : " + el.mungeToString(false, pad,doubleStringProps));
173 var spad = pad.substring(0, pad.length-4);
175 pad + string.join(",\n" + pad , els) +
186 func_regex = new Regex("^\\s+|\\s+$");
188 print("failed to build regex");
191 var piter = this.props.map_iterator();
192 while (piter.next() ) {
193 var k = piter.get_key();
194 var v = piter.get_value();
196 if (skip.contains(k) ) {
201 string leftv = k[0] == '|' ? k.substring(1) : k;
202 // skip builder stuff. prefixed with '.' .. just like unix fs..
203 if (leftv[0] == '.') { // |. or . -- do not output..
212 if (Lang.isKeyword(leftv) || Lang.isBuiltin(leftv)) {
213 left = "'" + leftv + "'";
214 } else if (Regex.match_simple("[^A-Za-z_]+",leftv)) { // not plain a-z... - quoted.
215 var val = this.quoteString(leftv);
217 left = "'" + val.substring(1, val.length-1).replace("'", "\\'") + "'";
224 // change the lines...
228 str = func_regex.replace(v,v.length, 0, "");
230 print("regex failed");
234 var lines = str.split("\n");
235 if (lines.length > 1) {
236 str = string.join("\n" + pad, lines);
239 els.append(left + str);
243 // next.. is it a function..
245 // does not hapepnd with arrays..
246 if (v.length < 1) { //if (typeof(el) == 'string' && !obj[i].length) { //skip empty.
252 str = func_regex.replace(v,v.length, 0, "");
254 print("regex failed");
258 var lines = str.split("\n");
259 if (lines.length > 1) {
260 str = string.join("\n" + pad, lines);
263 els.append(left + str);
269 if (Lang.isNumber(v) || Lang.isBoolean(v)) { // boolean or number...?
270 els.append(left + v );
275 if (doubleStringProps.length() < 1) {
276 els.append(left + this.quoteString(v));
280 if (doubleStringProps.index(k) > -1) {
281 els.append(left + this.quoteString(v));
285 // single quote.. v.substring(1, v.length-1).replace("'", "\\'") + "'";
286 els.append(left + "'" + v.substring(1, v.length-1).replace("'", "\\'") + "'");
293 var iter = oprops.map_iterator();
294 while (iter.next()) {
295 var k = iter.get_key();
296 var vo = iter.get_value();
297 string leftv = k[0] == '|' ? k.substring(1) : k;
298 if (Lang.isKeyword(leftv) || Lang.isBuiltin(leftv)) {
299 left = "'" + leftv + "'";
300 } else if (Regex.match_simple("[^A-Za-z_]+",leftv)) { // not plain a-z... - quoted.
301 var val = this.quoteString(leftv);
303 left = "'" + val.substring(1, val.length-1).replace("'", "\\'") + "'";
309 var right = vo.mungeToString(k == "listeners", pad + " ",doubleStringProps);
311 //if (!left.length && isArray) print(right);
313 if (right.length > 0){
314 els.append(left + right);
319 if (els.length() < 1) {
324 var spad = pad.substring(0, pad.length-4);
326 pad + string.join(",\n" + pad , els) +
334 static Json.Generator gen = null;
336 public string quoteString(string str)
338 if (Node.gen == null) {
339 Node.gen = new Json.Generator();
341 var builder = new Json.Builder();
342 builder.add_string_value(str);
343 Node.gen.set_root (builder.get_root ());
344 return Node.gen.to_data (null);
347 public void loadFromJson(Json.Object obj) {
348 obj.foreach_member((o , key, value) => {
349 if (key == "items") {
350 var ar = value.get_array();
351 ar.foreach_element( (are, ix, el) => {
352 var node = new Node();
353 node.loadFromJson(el.get_object());
354 this.items.append(node);
358 if (key == "listeners") {
359 var li = value.get_object();
360 obj.foreach_member((lio , li_key, li_value) => {
361 this.listeners.set(li_key, li_value.get_string());
366 this.props.set(key, value.get_string());