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 GLib.List<Node> items; // child items..
9 Gee.HashMap<string,string> props; // the properties..
17 this.items = new GLib.List<Node>();
18 this.props = new Gee.HashMap<string,string>();
19 this.is_array = false;
30 public bool hasChildren()
32 return this.items.length() > 0;
34 public bool hasXnsType()
36 if (this.props.get("|xns") != null && this.props.get("xtype") != null) {
42 // wrapper around get props that returns empty string if not found.
43 public string get(string key)
45 var k = this.props.get(key);
53 /* creates javascript based on the rules */
57 public string mungeToString (bool isListener, string pad, GLib.List<string> doubleStringProps)
61 pad = pad.length < 1 ? " " : pad;
66 //isListener = isListener || false;
68 //var keys = this.keys();
69 var isArray = this.isArray();
72 var els = new GLib.List<string>();
73 var skip = new Gee.ArrayList<string>();
74 if (!isArray && this.hasXnsType() ) {
75 // this.mungeXtype(obj['|xns'] + '.' + obj['xtype'], els); ??????
82 //var newitems = new Gee.ArrayList<JsRender.Node>();
83 var oprops = new Gee.HashMap<string,Node>();
85 if (!isArray && this.hasChildren()) {
88 for (var ii =0; ii< this.items.length(); ii++) {
89 var pl = this.items.nth_data(ii);
90 if (!pl.props.has_key("*prop")) {
95 //print(JSON.stringify(pl,null,4));
97 //var prop = pl['*prop'] + '';
99 var prop = pl.get("*prop");
101 if (! Regex.match_simple("\\[\\]$", prop)) {
102 // it's a standard prop..
104 // munge property..??
105 oprops.set(prop, pl);
112 prop = prop.substring(0, -2); //strip []
113 // it's an array type..
114 if (!oprops.has_key(prop)) {
116 oprops.set(prop, cn);
119 // ignores the fact it might be duplciated...
120 oprops.get(prop).is_array = true;
121 oprops.get(prop).items.append(pl);
128 //obj.items = newitems;
129 //if (!obj.items.length) {
134 if (this.isArray()) {
137 for (var i=0;i< this.items.length();i++) {
138 var el = this.items.nth_data(i);
140 els.append("%d".printf(i) + " : " + el.mungeToString(false, pad,doubleStringProps));
143 var spad = pad.substring(0, pad.length-4);
145 pad + string.join(",\n" + pad , els) +
156 func_regex = new Regex("^\\s+|\\s+$");
158 print("failed to build regex");
161 var piter = this.props.map_iterator();
162 while (piter.next() ) {
163 var k = piter.get_key();
164 var v = piter.get_value();
166 if (skip.contains(k) ) {
171 string leftv = k[0] == '|' ? k.substring(1) : k;
172 // skip builder stuff. prefixed with '.' .. just like unix fs..
173 if (leftv[0] == '.') { // |. or . -- do not output..
182 if (Lang.isKeyword(leftv) || Lang.isBuiltin(leftv)) {
183 left = "'" + leftv + "'";
184 } else if (Regex.match_simple("[^A-Za-z_]+",leftv)) { // not plain a-z... - quoted.
185 var val = this.quoteString(leftv);
187 left = "'" + val.substring(1, val.length-1).replace("'", "\\'") + "'";
194 // change the lines...
198 str = func_regex.replace(v,v.length, 0, "");
200 print("regex failed");
204 var lines = str.split("\n");
205 if (lines.length > 1) {
206 str = string.join("\n" + pad, lines);
209 els.append(left + str);
213 // next.. is it a function..
215 // does not hapepnd with arrays..
216 if (v.length < 1) { //if (typeof(el) == 'string' && !obj[i].length) { //skip empty.
222 str = func_regex.replace(v,v.length, 0, "");
224 print("regex failed");
228 var lines = str.split("\n");
229 if (lines.length > 1) {
230 str = string.join("\n" + pad, lines);
233 els.append(left + str);
239 if (Lang.isNumber(v) || Lang.isBoolean(v)) { // boolean or number...?
240 els.append(left + v );
245 if (doubleStringProps.length() < 1) {
246 els.append(left + this.quoteString(v));
250 if (doubleStringProps.index(k) > -1) {
251 els.append(left + this.quoteString(v));
255 // single quote.. v.substring(1, v.length-1).replace("'", "\\'") + "'";
256 els.append(left + "'" + v.substring(1, v.length-1).replace("'", "\\'") + "'");
263 var iter = oprops.map_iterator();
264 while (iter.next()) {
265 var k = iter.get_key();
266 var vo = iter.get_value();
267 string leftv = k[0] == '|' ? k.substring(1) : k;
268 if (Lang.isKeyword(leftv) || Lang.isBuiltin(leftv)) {
269 left = "'" + leftv + "'";
270 } else if (Regex.match_simple("[^A-Za-z_]+",leftv)) { // not plain a-z... - quoted.
271 var val = this.quoteString(leftv);
273 left = "'" + val.substring(1, val.length-1).replace("'", "\\'") + "'";
279 var right = vo.mungeToString(k == "listeners", pad + " ",doubleStringProps);
281 //if (!left.length && isArray) print(right);
283 if (right.length > 0){
284 els.append(left + right);
289 if (els.length() < 1) {
294 var spad = pad.substring(0, pad.length-4);
296 pad + string.join(",\n" + pad , els) +
304 static Json.Generator gen = null;
306 public string quoteString(string str)
308 if (Node.gen == null) {
309 Node.gen = new Json.Generator();
311 var builder = new Json.Builder();
312 builder.add_string_value(str);
313 Node.gen.set_root (builder.get_root ());
314 return Node.gen.to_data (null);
317 void loadFromJson(Json.Object obj) {
318 obj.foreach_member(o , key, value) {
319 if (key == "items") {
320 var ar = value.get_array();
321 ar.foreach_element( (are, ix, el) {
327 if (key == "listeners") {
331 this.props.set(key, value.get_string());