-
Notifications
You must be signed in to change notification settings - Fork 0
/
compiled.js
1380 lines (1380 loc) · 48.2 KB
/
compiled.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Created by Evan on 10/17/2016.
*/
var Utils = (function () {
function Utils() {
}
Utils.isAString = function (input) {
var startMark = input.indexOf("\"");
var endMark = input.lastIndexOf("\"");
/*var newString = input.substring(startMark, endMark);
if(newString.length > 0){
return true;
}
else{
return false;
}*/
if (startMark != endMark) {
return true;
}
else {
return false;
}
};
Utils.replaceAll = function (str, find, replace) {
return str.replace(new RegExp(find, 'g'), replace);
};
Utils.isNormalInteger = function (str) {
var n = ~~Number(str);
return String(n) === str && n >= 0;
};
Utils.shaveQuotes = function (input) {
return input.substring(1, input.length - 1);
};
Utils.getMiddleText = function (input) {
var startMark = input.indexOf(".");
var endMark = input.lastIndexOf(".");
return input.substring(startMark + 1, endMark);
};
return Utils;
})();
///<reference path="Utils.ts"/>
/**
* Created by Evan on 10/17/2016.
*/
var StringBuilder = (function () {
function StringBuilder() {
this.strings = [];
this.indentation = 0;
}
StringBuilder.prototype.removeCommentsMode = function (input) {
this.removeComments = input;
};
StringBuilder.prototype.setIdentation = function (indentation) {
this.indentation = indentation;
};
StringBuilder.prototype.append = function (input) {
var n = this.indentation;
while (n > 0) {
this.strings.push("\t");
n--;
}
this.strings.push(input);
};
StringBuilder.prototype.toString = function () {
if (this.strings.length > 0) {
var joined = this.strings.join();
if (this.removeComments) {
joined = Utils.replaceAll(joined, ",", "");
}
return joined;
}
else if (this.strings.length == 1) {
return this.strings[0];
}
else {
return "";
}
};
return StringBuilder;
})();
///<reference path="StringBuilder.ts"/>
///<reference path="Environment.ts"/>
/**
* Created by Evan on 10/17/2016.
*/
var Layout = (function () {
function Layout(rootTag) {
this.rootTag = "";
this.namespaces = [];
this.attributes = [];
this.children = [];
this.rootTag = rootTag;
}
Layout.prototype.setRoot = function (tag) {
this.rootTag = tag;
};
Layout.prototype.addNamespace = function (name, uri) {
this.namespaces.push([name, uri]);
};
Layout.prototype.hasAttribute = function (attr_name) {
for (var i = 0; i < this.attributes.length; i++) {
if (this.attributes[i][1] == attr_name) {
return i;
}
}
return -1;
};
Layout.prototype.getAttribute = function (attr_name) {
for (var _i = 0, _a = this.attributes; _i < _a.length; _i++) {
var attribute = _a[_i];
if (attribute[1] == attr_name) {
return attribute[2];
}
}
};
Layout.prototype.addAttribute = function (namespace, attr_name, value) {
console.log("Adding attribute: " + namespace + ", " + attr_name + ", " + value);
var hasAttr = this.hasAttribute(attr_name);
if (hasAttr != -1) {
this.attributes[hasAttr] = [namespace, attr_name, value];
return;
}
else {
this.attributes.push([namespace, attr_name, value]);
}
};
Layout.prototype.addChild = function (child) {
this.children.push(child);
};
Layout.prototype.emitXML = function () {
return this.emitXMLIndent(0);
};
Layout.prototype.emitXMLIndent = function (tabNum) {
var builder = new StringBuilder();
builder.setIdentation(tabNum);
var rootPrefix = "<" + this.rootTag + "\n";
var rootSuffix = "</" + this.rootTag + ">";
builder.append(rootPrefix);
for (var _i = 0, _a = this.namespaces; _i < _a.length; _i++) {
var xmlns = _a[_i];
builder.append("\t" + "xmlns:" + xmlns[0] + "=\"" + xmlns[1] + "\"\n");
}
for (var _b = 0, _c = this.attributes; _b < _c.length; _b++) {
var attr = _c[_b];
builder.append("\t" + attr[0] + ":" + attr[1] + "=\"" + attr[2] + "\"\n");
}
if (this.children.length == 0) {
builder.append("/>\n");
}
else {
builder.append(">\n");
for (var _d = 0, _e = this.children; _d < _e.length; _d++) {
var child = _e[_d];
builder.append("\n" + child.emitXMLIndent(tabNum + 1));
}
builder.append(rootSuffix);
}
builder.removeCommentsMode(true);
return builder.toString();
};
Layout.prototype.copy = function (environment) {
console.log("Starting copy...");
//construct json object
var jsonObj = {};
jsonObj.root = this.rootTag;
jsonObj.namespaces = {};
for (var _i = 0, _a = this.namespaces; _i < _a.length; _i++) {
var namespace = _a[_i];
jsonObj.namespaces[namespace[0]] = namespace[1];
}
jsonObj.children = [];
for (var _b = 0, _c = this.attributes; _b < _c.length; _b++) {
var attribute = _c[_b];
if (jsonObj[attribute[0]] == null) {
jsonObj[attribute[0]] = {};
}
jsonObj[attribute[0]][attribute[1]] = attribute[2];
}
var wrappedLayout = Layout.fromJSON(environment, jsonObj);
for (var _d = 0, _e = this.children; _d < _e.length; _d++) {
var child = _e[_d];
wrappedLayout.addChild(child.copy(environment));
}
console.log("Wrapped");
return wrappedLayout;
};
Layout.fromJSON = function (environment, json) {
var jsonRoot = json.root;
var result = new Layout(jsonRoot);
var jsonNamespaces = json.namespaces;
for (var key in jsonNamespaces) {
result.addNamespace(key, jsonNamespaces[key]);
}
var jsonChildren = json.children;
for (var entry in jsonChildren) {
var child = environment.getVariable(entry).content;
result.addChild(child);
}
var numObjs = Object.keys(json).length;
console.log("Number of json params: " + numObjs);
for (var i = 3; i < numObjs; i++) {
var namespace = Object.keys(json)[i];
console.log("Namespace: " + namespace);
var jsonAttrs = json[namespace];
console.log("Num attributes: " + Object.keys(jsonAttrs).length);
for (var key in jsonAttrs) {
result.addAttribute(namespace, key, jsonAttrs[key]);
}
}
return result;
};
return Layout;
})();
///<reference path="Layout.ts"/>
/**
* Created by Evan on 10/17/2016.
*/
var NamedVariable = (function () {
function NamedVariable(name, content) {
this.name = name;
this.content = content;
}
NamedVariable.prototype.toString = function () {
return "Name: " + this.name + "\nLayout:\n" + this.content.emitXML() + "\nJSON:\n" + JSON.stringify(this.content);
};
return NamedVariable;
})();
///<reference path="NamedVariable.ts"/>
/**
* Created by Evan on 10/17/2016.
*/
var NamedVariableCollection = (function () {
function NamedVariableCollection() {
this.variables = [];
}
NamedVariableCollection.prototype.add = function (variable) {
this.variables.push(variable);
};
NamedVariableCollection.prototype.containsName = function (name) {
for (var _i = 0, _a = this.variables; _i < _a.length; _i++) {
var variable = _a[_i];
console.log(variable.name + " vs " + name);
if (variable.name == name) {
return true;
}
}
return false;
};
NamedVariableCollection.prototype.get = function (name) {
for (var _i = 0, _a = this.variables; _i < _a.length; _i++) {
var variable = _a[_i];
if (variable.name == name) {
return variable;
}
}
return null;
};
NamedVariableCollection.prototype.toString = function () {
var builder = new StringBuilder();
for (var _i = 0, _a = this.variables; _i < _a.length; _i++) {
var variable = _a[_i];
builder.append(variable.toString());
builder.append("\n");
}
return builder.toString();
};
return NamedVariableCollection;
})();
/**
* Created by Evan on 10/19/2016.
*/
var FilePayload = (function () {
function FilePayload() {
this.files = {};
}
FilePayload.prototype.addFile = function (filename, file) {
this.files[filename] = file;
};
FilePayload.prototype.toZip = function () {
var zip = require('adm-zip');
var zipper = new zip();
for (var filename in this.files) {
var tempBuffer = new Buffer(this.files[filename]);
zipper.addFile(filename, tempBuffer, '', 0644 << 16);
}
console.log(zipper.getEntries().length);
for (var _i = 0, _a = zipper.getEntries(); _i < _a.length; _i++) {
var entry = _a[_i];
console.log(entry.name);
entry.isDirectiory = false;
}
return zipper.toBuffer();
};
return FilePayload;
})();
///<reference path="NamedVariableCollection.ts"/>
///<reference path="Layout.ts"/>
///<reference path="NamedVariable.ts"/>
///<reference path="Command.ts"/>
///<reference path="FilePayload.ts"/>
/**
* Created by Evan on 10/17/2016.
*/
var Environment = (function () {
function Environment() {
this.project = new FilePayload();
this.variables = new NamedVariableCollection();
}
Environment.prototype.addNewVariable = function (name, content) {
var newVar = new NamedVariable(name, content);
this.variables.add(newVar);
};
Environment.prototype.getVariable = function (name) {
//check for indexed children
return this.variables.get(name);
};
Environment.prototype.runCommand = function (chosenCommand) {
try {
chosenCommand.run(this);
}
catch (e) {
console.log(e);
}
};
Environment.prototype.hasVariable = function (variableName) {
if (this.variables.containsName(variableName)) {
return true;
}
return false;
};
Environment.prototype.getAllVariables = function () {
return this.variables;
};
Environment.prototype.getResult = function () {
return this.project.toZip();
};
Environment.prototype.addToProject = function (filename, filetext) {
this.project.addFile(filename, filetext);
};
return Environment;
})();
///<reference path="Environment.ts"/>
/**
* Created by Evan on 10/17/2016.
*/
///<reference path="Command.ts"/>
///<reference path="Environment.ts"/>
var CommandImplementation = (function () {
function CommandImplementation() {
}
CommandImplementation.prototype.setRunnable = function (arg) {
this.runnable = arg;
};
CommandImplementation.prototype.run = function (environment) {
this.runnable(environment);
};
return CommandImplementation;
})();
///<reference path="Environment.ts"/>
///<reference path="Command.ts"/>
///<reference path="CommandImplementation.ts"/>
/**
* Created by Evan on 10/17/2016.
*/
var CommandBuilder = (function () {
function CommandBuilder() {
}
CommandBuilder.build = function (runnable) {
var impl = new CommandImplementation();
impl.setRunnable(runnable);
return impl;
};
return CommandBuilder;
})();
///<reference path="Command.ts"/>
/**
* Created by Evan on 10/17/2016.
*/
///<reference path="Command.ts"/>
///<reference path="Environment.ts"/>
///<reference path="CommandBuilder.ts"/>
///<reference path="Rule.ts"/>
/**
* Created by Evan on 10/17/2016.
*/
var AddChildRule = (function () {
function AddChildRule() {
}
AddChildRule.prototype.checkToken = function (token) {
console.log("Inside add child");
var tokens = token.split(" ");
if (!(tokens[0].split(".").length == 2)) {
console.log(tokens[0].split(".").length);
console.log(tokens[0].split(".")[0]);
console.log("1");
return false;
}
var firstDot = tokens[0].indexOf(".");
var firstPar = tokens[0].indexOf("(");
var arg = tokens[0].substring(firstDot + 1, firstPar + 1);
if (!(arg == "addChild(")) {
console.log("2 " + arg);
return false;
}
return true;
};
AddChildRule.prototype.getCommand = function (token) {
var run = function (environment) {
var tokens = token.split(" ");
var varHeader = tokens[0];
var firstDot = tokens[0].indexOf(".");
var firstPar = tokens[0].indexOf("(");
var lastPar = tokens[0].lastIndexOf(")");
var arg = tokens[0].substring(firstPar + 1, lastPar);
var varName = tokens[0].substring(0, firstDot);
if (!(environment.hasVariable(varName))) {
throw new Error("No such variable \"" + varName + "\" : " + token);
}
if (!(environment.hasVariable(arg))) {
throw new Error("No such variable \"" + arg + "\" : " + token);
}
environment.getVariable(varName).content.addChild(environment.getVariable(arg).content);
};
return CommandBuilder.build(run);
};
return AddChildRule;
})();
///<reference path="Rule.ts"/>
///<reference path="Utils.ts"/>
///<reference path="Command.ts"/>
///<reference path="Environment.ts"/>
///<reference path="CommandBuilder.ts"/>
/**
* Created by Evan on 10/17/2016.
*/
var AddNamespaceRule = (function () {
function AddNamespaceRule() {
}
AddNamespaceRule.prototype.checkToken = function (token) {
var tokens = token.split(" ");
if (!(tokens[1] == "=")) {
return false;
}
if (!(Utils.isAString(tokens[2]))) {
return false;
}
if (!(tokens[0].split(".").length == 3)) {
return false;
}
if (!(Utils.getMiddleText(tokens[0]) == "namespaces")) {
return false;
}
return true;
};
AddNamespaceRule.prototype.getCommand = function (token) {
var run = function (environment) {
var tokens = token.split(" ");
var varHeader = tokens[0];
var firstDot = tokens[0].indexOf(".");
var secondDot = tokens[0].lastIndexOf(".");
var varName = tokens[0].substring(0, firstDot);
if (!(environment.hasVariable(varName))) {
throw new Error("No such variable \"" + varName + "\" : " + token);
}
var newNamespaceName;
newNamespaceName = tokens[0].substring(secondDot + 1, tokens[0].length);
var valueStr = Utils.shaveQuotes(tokens[2]);
environment.getVariable(varName).content.addNamespace(newNamespaceName, valueStr);
};
return CommandBuilder.build(run);
};
return AddNamespaceRule;
})();
///<reference path="Rule.ts"/>
///<reference path="Command.ts"/>
///<reference path="Environment.ts"/>
///<reference path="NamedVariableCollection.ts"/>
///<reference path="CommandBuilder.ts"/>
/**
* Created by Evan on 10/17/2016.
*/
var ColorifyRule = (function () {
function ColorifyRule() {
}
ColorifyRule.prototype.checkToken = function (token) {
if (!(token == "Colorify()")) {
return false;
}
return true;
};
ColorifyRule.prototype.getCommand = function (token) {
var run = function (environment) {
var variables = environment.getAllVariables();
for (var _i = 0, _a = variables.variables; _i < _a.length; _i++) {
var variable = _a[_i];
if (variable.content.hasAttribute("background") == -1) {
var randomNum = Math.floor(Math.random() * 89999 + 10000);
variable.content.addAttribute("android", "background", "#FF" + randomNum);
}
}
};
return CommandBuilder.build(run);
};
return ColorifyRule;
})();
/**
* Created by Evan on 10/17/2016.
*/
var Constants = (function () {
function Constants() {
}
Constants.isValidType = function (type) {
for (var _i = 0, _a = this.VALID_VIEW_TYPES; _i < _a.length; _i++) {
var entry = _a[_i];
if (entry == type) {
return true;
}
}
return false;
};
Constants.hasRootFilter = function (type) {
for (var _i = 0, _a = this.ROOT_FILTERS; _i < _a.length; _i++) {
var entry = _a[_i];
if (entry[0] == type) {
return true;
}
}
return false;
};
Constants.getRootFilter = function (type) {
for (var _i = 0, _a = this.ROOT_FILTERS; _i < _a.length; _i++) {
var entry = _a[_i];
if (entry[0] == type) {
return entry[1];
}
}
};
Constants.getRootTag = function (type) {
for (var _i = 0, _a = this.VALID_VIEW_TYPES; _i < _a.length; _i++) {
var entry = _a[_i];
if (entry == type) {
if (Constants.hasRootFilter(type)) {
return Constants.getRootFilter(type);
}
else {
return entry;
}
}
}
};
Constants.VALID_VIEW_TYPES = [
"Layout",
"ActionMenuView",
"AutoCompleteTextView",
"Button",
"CalendarView",
"CheckBox",
"CheckedTextView",
"Chronometer",
"DatePicker",
"DigitalClock",
"EditText",
"ExpandableListView",
"FrameLayout",
"Gallery",
"GridLayout",
"GridView",
"HorizontalScrollView",
"ImageButton",
"ImageSwitcher",
"ImageView",
"LinearLayout",
"ListPopupWindow",
"ListView",
"MediaController",
"MultiAutoCompleteTextView",
"NumberPicker",
"OverScroller",
"PopupMenu",
"PopupWindow",
"ProgressBar",
"QuickContactBadge",
"RadioButton",
"RadioGroup",
"RatingBar",
"RelativeLayout",
"RemoteViews",
"Scroller",
"ScrollView",
"SearchView",
"SeekBar",
"SlidingDrawer",
"Space",
"Spinner",
"StackView",
"Switch",
"TabHost",
"TableLayout",
"TableRow",
"TabWidget",
"TextClock",
"TextSwitcher",
"TextView",
"TimePicker",
"Toast",
"ToggleButton",
"Toolbar",
"TwoLineListItem",
"View",
"VideoView",
"ViewAnimator",
"ViewFlipper",
"ViewSwitcher",
"ZoomButton",
"PercentRelativeLayout"
];
Constants.ROOT_FILTERS = [
["PercentRelativeLayout", "android.support.percent.PercentRelativeLayout"],
["Layout", ""]
];
Constants.LAYOUT_METHODS = [
"addChild",
"setMatchParent",
"setWrapContent"
];
Constants.INDEPENDENT_METHODS = [
"GenerateIds",
"Colorify"
];
return Constants;
})();
///<reference path="Rule.ts"/>
///<reference path="Constants.ts"/>
///<reference path="Command.ts"/>
///<reference path="Environment.ts"/>
///<reference path="Layout.ts"/>
///<reference path="CommandBuilder.ts"/>
/**
* Created by Evan on 10/17/2016.
*/
var DeclareThroughCopyRule = (function () {
function DeclareThroughCopyRule() {
}
DeclareThroughCopyRule.prototype.checkToken = function (token) {
console.log("Declare through copy");
var tokens = token.split(" ");
if (!(tokens[2] == "=")) {
console.log("1");
return false;
}
if (!Constants.isValidType(tokens[0])) {
console.log("2");
return false;
}
if (!(tokens[3].split(".").length == 2)) {
console.log("3");
return false;
}
var firstDot = tokens[3].indexOf(".");
var firstPar = tokens[3].indexOf("(");
var arg = tokens[3].substring(firstDot + 1, firstPar);
if (!(arg == "copy")) {
console.log("4");
console.log(arg);
return false;
}
return true;
};
DeclareThroughCopyRule.prototype.getCommand = function (token) {
//define run function
var run = function (environment) {
var tokens = token.split(" ");
//variable name
var variableName = token.split(" ")[1];
if (environment.hasVariable(variableName)) {
throw new Error("Variable " + variableName + " already defined: " + token);
}
var firstDot = tokens[3].indexOf(".");
var copiedObjName = tokens[3].substring(0, firstDot);
if (!(environment.hasVariable(copiedObjName))) {
throw new Error("No such variable \"" + copiedObjName + "\" : " + token);
}
console.log("copied name: " + copiedObjName);
var locatedVar = environment.getVariable(copiedObjName);
console.log("located name: " + locatedVar.name);
console.log("located content: " + JSON.stringify(locatedVar.content));
var tempLayout = locatedVar.content;
console.log("content extracted");
var newLayout = tempLayout.copy(environment);
console.log("var name: " + variableName);
console.log("content: " + JSON.stringify(newLayout));
//add variable
environment.addNewVariable(variableName, newLayout);
};
//build command
return CommandBuilder.build(run);
};
return DeclareThroughCopyRule;
})();
///<reference path="Rule.ts"/>
///<reference path="Utils.ts"/>
///<reference path="Command.ts"/>
///<reference path="Environment.ts"/>
///<reference path="CommandBuilder.ts"/>
/**
* Created by Evan on 10/17/2016.
*/
var ErrorRule = (function () {
function ErrorRule() {
}
ErrorRule.prototype.getCommand = function (token) {
return undefined;
};
ErrorRule.prototype.checkToken = function (token) {
return undefined;
};
return ErrorRule;
})();
///<reference path="Rule.ts"/>
///<reference path="ErrorRule.ts"/>
/**
* Created by Evan on 10/17/2016.
*/
var RuleCollection = (function () {
function RuleCollection() {
this.rules = [];
}
RuleCollection.prototype.getObjName = function (obj) {
return Object.prototype.toString.call(obj).slice(8, -1);
};
RuleCollection.prototype.findRuleFor = function (token) {
console.log("Inside findRuleFor");
var result = null;
for (var _i = 0, _a = this.rules; _i < _a.length; _i++) {
var rule = _a[_i];
console.log("Inside checkToken");
var isClear = rule.checkToken(token);
if (isClear) {
if (result != null) {
this.throwDuplicateRuleWarning(token);
}
result = rule;
console.log("Chosen: " + this.getObjName(result) + "\n");
}
else {
console.log("Tried: " + this.getObjName(result) + "\n");
}
}
if (result == null) {
return new ErrorRule();
}
else {
return result;
}
};
RuleCollection.prototype.throwDuplicateRuleWarning = function (token) {
console.log("Duplicate rule warning at: " + token);
};
RuleCollection.prototype.add = function (rule) {
this.rules.push(rule);
};
RuleCollection.prototype.addAll = function (rules) {
for (var _i = 0; _i < rules.length; _i++) {
var rule = rules[_i];
this.add(rule);
}
};
return RuleCollection;
})();
///<reference path="Rule.ts"/>
///<reference path="Command.ts"/>
///<reference path="Environment.ts"/>
///<reference path="NamedVariableCollection.ts"/>
///<reference path="CommandBuilder.ts"/>
/**
* Created by Evan on 10/17/2016.
*/
var GenerateIdsRule = (function () {
function GenerateIdsRule() {
}
GenerateIdsRule.prototype.checkToken = function (token) {
if (!(token == "GenerateIds()")) {
return false;
}
return true;
};
GenerateIdsRule.prototype.getCommand = function (token) {
var run = function (environment) {
var variables = environment.getAllVariables();
for (var _i = 0, _a = variables.variables; _i < _a.length; _i++) {
var variable = _a[_i];
if (variable.content.hasAttribute("id") == -1) {
variable.content.addAttribute("android", "id", variable.name);
}
}
for (var _b = 0, _c = variables.variables; _b < _c.length; _b++) {
var variable = _c[_b];
GenerateIdsRule.generateIdsForChildren(environment, variable.content);
}
};
return CommandBuilder.build(run);
};
GenerateIdsRule.generateIdsForChildren = function (environment, layout) {
var i = 0;
for (var _i = 0, _a = layout.children; _i < _a.length; _i++) {
var child = _a[_i];
i++;
if (child.hasAttribute("id") == -1) {
var parentID = layout.getAttribute("id");
child.addAttribute("android", "id", parentID + "_child_" + i);
}
GenerateIdsRule.generateIdsForChildren(environment, child);
}
};
return GenerateIdsRule;
})();
///<reference path="Rule.ts"/>
///<reference path="Utils.ts"/>
///<reference path="Command.ts"/>
///<reference path="Environment.ts"/>
///<reference path="CommandBuilder.ts"/>
///<reference path="Constants.ts"/>
/**
* Created by Evan on 10/17/2016.
*/
var JsonDeclarationRule = (function () {
function JsonDeclarationRule() {
}
JsonDeclarationRule.prototype.checkToken = function (token) {
console.log("Checking JSON rule");
var tokens = token.split(" ");
if (!(tokens[2] == "=")) {
console.log("1");
return false;
}
if (!Constants.isValidType(tokens[0])) {
console.log(2);
console.log(tokens[0]);
return false;
}
if (!(tokens[3].charAt(0) == "{")) {
console.log("3");
return false;
}
return true;
};
JsonDeclarationRule.prototype.getCommand = function (token) {
//define run function
var run = function (environment) {
console.log("Inside run");
//variable name
var variableName = token.split(" ")[1];
console.log("Variable name: " + variableName);
if (environment.hasVariable(variableName)) {
throw new Error("Variable " + variableName + " already defined: " + token);
}
//json object isolation
var startBracket = token.indexOf("{");
var endBracket = token.lastIndexOf("}");
var jsonText = token.substring(startBracket, endBracket + 1);
console.log(jsonText);
var jsonObj;
try {
jsonObj = JSON.parse(jsonText);
}
catch (e) {
throw new Error("Invalid JSON at: " + token + "\n\n" + e.toString());
}
console.log("Creating object from JSON...");
var newLayout = Layout.fromJSON(environment, jsonObj);
if (newLayout.rootTag == null) {
var rootString = Constants.getRootTag(token.split(" ")[0]);
newLayout.rootTag = rootString;
}
//add variable
environment.addNewVariable(variableName, newLayout);
};
//build command
return CommandBuilder.build(run);
};
return JsonDeclarationRule;
})();
///<reference path="Rule.ts"/>
///<reference path="Utils.ts"/>
///<reference path="Command.ts"/>
///<reference path="Environment.ts"/>
///<reference path="CommandBuilder.ts"/>
/**
* Created by Evan on 10/17/2016.
*/
var SetAndroidAttributeRule = (function () {
function SetAndroidAttributeRule() {
}
SetAndroidAttributeRule.prototype.checkToken = function (token) {
console.log("Checking android attribute rule");
var tokens = token.split(" ");
if (!(tokens[1] == "=")) {
console.log("1");
return false;
}
if (!(Utils.isAString(tokens[2]))) {
console.log("2");
return false;
}
if (!(tokens[0].split(".").length <= 2)) {
console.log("3");
if (!(Utils.getMiddleText(tokens[0]) == "android")) {
console.log("3.1");
return false;
}
}
return true;
};
SetAndroidAttributeRule.prototype.getCommand = function (token) {
var run = function (environment) {
var tokens = token.split(" ");
var varHeader = tokens[0];
var firstDot = tokens[0].indexOf(".");
var secondDot = tokens[0].lastIndexOf(".");
var varName = tokens[0].substring(0, firstDot);
if (!(environment.hasVariable(varName))) {
throw new Error("No such variable \"" + varName + "\" : " + token);
}
var attrName;
if (varHeader.split(".").length == 2) {
attrName = tokens[0].substring(firstDot + 1, tokens[0].length);
}
if (varHeader.split(".").length == 3) {
attrName = tokens[0].substring(secondDot + 1, tokens[0].length);
}
var valueStr = Utils.shaveQuotes(tokens[2]);
environment.getVariable(varName).content.addAttribute("android", attrName, valueStr);
};
return CommandBuilder.build(run);
};
return SetAndroidAttributeRule;
})();
///<reference path="Rule.ts"/>
///<reference path="Utils.ts"/>
///<reference path="Command.ts"/>
///<reference path="Environment.ts"/>
///<reference path="CommandBuilder.ts"/>
/**
* Created by Evan on 10/17/2016.
*/
var SetCustomNamespaceAttributeRule = (function () {
function SetCustomNamespaceAttributeRule() {
}
SetCustomNamespaceAttributeRule.prototype.checkToken = function (token) {
console.log("Set Custom namespace");
var tokens = token.split(" ");
if (!(tokens[1] == "=")) {
return false;
}
if (!(Utils.isAString(tokens[2]))) {
return false;
}
if (!(tokens[0].split(".").length == 3)) {
return false;
}
console.log(Utils.getMiddleText(tokens[0]));
if (Utils.getMiddleText(tokens[0]) == "android") {
return false;
}
if (Utils.getMiddleText(tokens[0]) == "namespaces") {
return false;
}
return true;
};
SetCustomNamespaceAttributeRule.prototype.getCommand = function (token) {
var run = function (environment) {
var tokens = token.split(" ");
var varHeader = tokens[0];
var firstDot = tokens[0].indexOf(".");
var secondDot = tokens[0].lastIndexOf(".");
var varName = tokens[0].substring(0, firstDot);
if (!(environment.hasVariable(varName))) {
throw new Error("No such variable \"" + varName + "\" : " + token);
}
var namespaceName = tokens[0].substring(firstDot + 1, secondDot);
var attrName;
attrName = tokens[0].substring(secondDot + 1, tokens[0].length);
var valueStr = Utils.shaveQuotes(tokens[2]);
environment.getVariable(varName).content.addAttribute(namespaceName, attrName, valueStr);
};
return CommandBuilder.build(run);
};
return SetCustomNamespaceAttributeRule;
})();
///<reference path="Rule.ts"/>
///<reference path="Command.ts"/>
///<reference path="Environment.ts"/>
///<reference path="CommandBuilder.ts"/>
/**
* Created by Evan on 10/17/2016.
*/
var SetMatchParentRule = (function () {
function SetMatchParentRule() {
}
SetMatchParentRule.prototype.checkToken = function (token) {