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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
|
/*
@ITMillApache2LicenseForJavaFiles@
*/
package com.vaadin.terminal.gwt.server;
import java.io.PrintWriter;
import java.io.Serializable;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;
import java.util.Stack;
import java.util.Vector;
import com.vaadin.Application;
import com.vaadin.terminal.ApplicationResource;
import com.vaadin.terminal.ExternalResource;
import com.vaadin.terminal.PaintException;
import com.vaadin.terminal.PaintTarget;
import com.vaadin.terminal.Paintable;
import com.vaadin.terminal.Resource;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.terminal.VariableOwner;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.ClientWidget;
import com.vaadin.ui.Component;
import com.vaadin.ui.CustomLayout;
/**
* User Interface Description Language Target.
*
* @author IT Mill Ltd.
* @version
* @VERSION@
* @since 5.0
*/
@SuppressWarnings("serial")
public class JsonPaintTarget implements PaintTarget {
/* Document type declarations */
private final static String UIDL_ARG_NAME = "name";
private final Stack<String> mOpenTags;
private final Stack<JsonTag> openJsonTags;
private final PrintWriter uidlBuffer;
private boolean closed = false;
private final AbstractCommunicationManager manager;
private int changes = 0;
private Set<Object> usedResources = new HashSet<Object>();
private boolean customLayoutArgumentsOpen = false;
private JsonTag tag;
private int errorsOpen;
private boolean cacheEnabled = false;
private Collection<Paintable> paintedComponents = new HashSet<Paintable>();
private Collection<Paintable> identifiersCreatedDueRefPaint;
private Collection<Class<? extends Paintable>> usedPaintableTypes = new LinkedList<Class<? extends Paintable>>();
/**
* Creates a new XMLPrintWriter, without automatic line flushing.
*
* @param variableMap
* @param manager
* @param outWriter
* A character-output stream.
* @throws PaintException
* if the paint operation failed.
*/
public JsonPaintTarget(AbstractCommunicationManager manager, PrintWriter outWriter,
boolean cachingRequired) throws PaintException {
this.manager = manager;
// Sets the target for UIDL writing
uidlBuffer = outWriter;
// Initialize tag-writing
mOpenTags = new Stack<String>();
openJsonTags = new Stack<JsonTag>();
cacheEnabled = cachingRequired;
}
public void startTag(String tagName) throws PaintException {
startTag(tagName, false);
}
/**
* Prints the element start tag.
*
* <pre>
* Todo:
* Checking of input values
*
* </pre>
*
* @param tagName
* the name of the start tag.
* @throws PaintException
* if the paint operation failed.
*
*/
public void startTag(String tagName, boolean isChildNode)
throws PaintException {
// In case of null data output nothing:
if (tagName == null) {
throw new NullPointerException();
}
// Ensures that the target is open
if (closed) {
throw new PaintException(
"Attempted to write to a closed PaintTarget.");
}
if (tag != null) {
openJsonTags.push(tag);
}
// Checks tagName and attributes here
mOpenTags.push(tagName);
tag = new JsonTag(tagName);
if ("error".equals(tagName)) {
errorsOpen++;
}
customLayoutArgumentsOpen = false;
}
/**
* Prints the element end tag.
*
* If the parent tag is closed before every child tag is closed an
* PaintException is raised.
*
* @param tag
* the name of the end tag.
* @throws Paintexception
* if the paint operation failed.
*/
public void endTag(String tagName) throws PaintException {
// In case of null data output nothing:
if (tagName == null) {
throw new NullPointerException();
}
// Ensure that the target is open
if (closed) {
throw new PaintException(
"Attempted to write to a closed PaintTarget.");
}
if (openJsonTags.size() > 0) {
final JsonTag parent = openJsonTags.pop();
String lastTag = "";
lastTag = mOpenTags.pop();
if (!tagName.equalsIgnoreCase(lastTag)) {
throw new PaintException("Invalid UIDL: wrong ending tag: '"
+ tagName + "' expected: '" + lastTag + "'.");
}
// simple hack which writes error uidl structure into attribute
if ("error".equals(lastTag)) {
if (errorsOpen == 1) {
parent.addAttribute("\"error\":[\"error\",{}"
+ tag.getData() + "]");
} else {
// sub error
parent.addData(tag.getJSON());
}
errorsOpen--;
} else {
parent.addData(tag.getJSON());
}
tag = parent;
} else {
changes++;
uidlBuffer.print(((changes > 1) ? "," : "") + tag.getJSON());
tag = null;
}
}
/**
* Substitutes the XML sensitive characters with predefined XML entities.
*
* @param xml
* the String to be substituted.
* @return A new string instance where all occurrences of XML sensitive
* characters are substituted with entities.
*/
static public String escapeXML(String xml) {
if (xml == null || xml.length() <= 0) {
return "";
}
return escapeXML(new StringBuilder(xml)).toString();
}
/**
* Substitutes the XML sensitive characters with predefined XML entities.
*
* @param xml
* the String to be substituted.
* @return A new StringBuilder instance where all occurrences of XML
* sensitive characters are substituted with entities.
*
*/
static StringBuilder escapeXML(StringBuilder xml) {
if (xml == null || xml.length() <= 0) {
return new StringBuilder("");
}
final StringBuilder result = new StringBuilder(xml.length() * 2);
for (int i = 0; i < xml.length(); i++) {
final char c = xml.charAt(i);
final String s = toXmlChar(c);
if (s != null) {
result.append(s);
} else {
result.append(c);
}
}
return result;
}
static public String escapeJSON(String s) {
if (s == null) {
return "";
}
final StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
final char ch = s.charAt(i);
switch (ch) {
case '"':
sb.append("\\\"");
break;
case '\\':
sb.append("\\\\");
break;
case '\b':
sb.append("\\b");
break;
case '\f':
sb.append("\\f");
break;
case '\n':
sb.append("\\n");
break;
case '\r':
sb.append("\\r");
break;
case '\t':
sb.append("\\t");
break;
case '/':
sb.append("\\/");
break;
default:
if (ch >= '\u0000' && ch <= '\u001F') {
final String ss = Integer.toHexString(ch);
sb.append("\\u");
for (int k = 0; k < 4 - ss.length(); k++) {
sb.append('0');
}
sb.append(ss.toUpperCase());
} else {
sb.append(ch);
}
}
}
return sb.toString();
}
/**
* Substitutes a XML sensitive character with predefined XML entity.
*
* @param c
* the Character to be replaced with an entity.
* @return String of the entity or null if character is not to be replaced
* with an entity.
*/
private static String toXmlChar(char c) {
switch (c) {
case '&':
return "&"; // & => &
case '>':
return ">"; // > => >
case '<':
return "<"; // < => <
case '"':
return """; // " => "
case '\'':
return "'"; // ' => '
default:
return null;
}
}
/**
* Prints XML-escaped text.
*
* @param str
* @throws PaintException
* if the paint operation failed.
*
*/
public void addText(String str) throws PaintException {
tag.addData("\"" + escapeJSON(str) + "\"");
}
/**
* Adds a boolean attribute to component. Atributes must be added before any
* content is written.
*
* @param name
* the Attribute name.
* @param value
* the Attribute value.
* @throws PaintException
* if the paint operation failed.
*/
public void addAttribute(String name, boolean value) throws PaintException {
tag.addAttribute("\"" + name + "\":" + (value ? "true" : "false"));
}
/**
* Adds a resource attribute to component. Attributes must be added before
* any content is written.
*
* @param name
* the Attribute name.
* @param value
* the Attribute value.
*
* @throws PaintException
* if the paint operation failed.
*/
public void addAttribute(String name, Resource value) throws PaintException {
if (value instanceof ExternalResource) {
addAttribute(name, ((ExternalResource) value).getURL());
} else if (value instanceof ApplicationResource) {
final ApplicationResource r = (ApplicationResource) value;
final Application a = r.getApplication();
if (a == null) {
throw new PaintException(
"Application not specified for resorce "
+ value.getClass().getName());
}
String uri;
if (a.getURL() != null) {
uri = a.getURL().getPath();
} else {
uri = "";
}
if (uri.length() > 0 && uri.charAt(uri.length() - 1) != '/') {
uri += "/";
}
uri += a.getRelativeLocation(r);
addAttribute(name, uri);
} else if (value instanceof ThemeResource) {
final String uri = "theme://"
+ ((ThemeResource) value).getResourceId();
addAttribute(name, uri);
} else {
throw new PaintException("Ajax adapter does not "
+ "support resources of type: "
+ value.getClass().getName());
}
}
/**
* Adds a integer attribute to component. Atributes must be added before any
* content is written.
*
* @param name
* the Attribute name.
* @param value
* the Attribute value.
*
* @throws PaintException
* if the paint operation failed.
*/
public void addAttribute(String name, int value) throws PaintException {
tag.addAttribute("\"" + name + "\":" + String.valueOf(value));
}
/**
* Adds a long attribute to component. Atributes must be added before any
* content is written.
*
* @param name
* the Attribute name.
* @param value
* the Attribute value.
*
* @throws PaintException
* if the paint operation failed.
*/
public void addAttribute(String name, long value) throws PaintException {
tag.addAttribute("\"" + name + "\":" + String.valueOf(value));
}
/**
* Adds a float attribute to component. Atributes must be added before any
* content is written.
*
* @param name
* the Attribute name.
* @param value
* the Attribute value.
*
* @throws PaintException
* if the paint operation failed.
*/
public void addAttribute(String name, float value) throws PaintException {
tag.addAttribute("\"" + name + "\":" + String.valueOf(value));
}
/**
* Adds a double attribute to component. Atributes must be added before any
* content is written.
*
* @param name
* the Attribute name.
* @param value
* the Attribute value.
*
* @throws PaintException
* if the paint operation failed.
*/
public void addAttribute(String name, double value) throws PaintException {
tag.addAttribute("\"" + name + "\":" + String.valueOf(value));
}
/**
* Adds a string attribute to component. Atributes must be added before any
* content is written.
*
* @param name
* the String attribute name.
* @param value
* the String attribute value.
*
* @throws PaintException
* if the paint operation failed.
*/
public void addAttribute(String name, String value) throws PaintException {
// In case of null data output nothing:
if ((value == null) || (name == null)) {
throw new NullPointerException(
"Parameters must be non-null strings");
}
tag.addAttribute("\"" + name + "\": \"" + escapeJSON(value) + "\"");
if (customLayoutArgumentsOpen && "template".equals(name)) {
getUsedResources().add("layouts/" + value + ".html");
}
if (name.equals("locale")) {
manager.requireLocale(value);
}
}
public void addAttribute(String name, Map<?, ?> value)
throws PaintException {
StringBuilder sb = new StringBuilder();
sb.append("\"");
sb.append(name);
sb.append("\": ");
sb.append("{");
for (Iterator<?> it = value.keySet().iterator(); it.hasNext();) {
Object key = it.next();
Object mapValue = value.get(key);
sb.append("\"");
if (key instanceof Paintable) {
Paintable paintable = (Paintable) key;
sb.append(getPaintIdentifier(paintable));
} else {
sb.append(escapeJSON(key.toString()));
}
sb.append("\":");
if (mapValue instanceof Float || mapValue instanceof Integer
|| mapValue instanceof Double
|| mapValue instanceof Boolean
|| mapValue instanceof Alignment) {
sb.append(mapValue);
} else {
sb.append("\"");
sb.append(escapeJSON(mapValue.toString()));
sb.append("\"");
}
if (it.hasNext()) {
sb.append(",");
}
}
sb.append("}");
tag.addAttribute(sb.toString());
}
public void addAttribute(String name, Object[] values) {
// In case of null data output nothing:
if ((values == null) || (name == null)) {
throw new NullPointerException(
"Parameters must be non-null strings");
}
final StringBuilder buf = new StringBuilder();
buf.append("\"" + name + "\":[");
for (int i = 0; i < values.length; i++) {
if (i > 0) {
buf.append(",");
}
buf.append("\"");
buf.append(escapeJSON(values[i].toString()));
buf.append("\"");
}
buf.append("]");
tag.addAttribute(buf.toString());
}
/**
* Adds a string type variable.
*
* @param owner
* the Listener for variable changes.
* @param name
* the Variable name.
* @param value
* the Variable initial value.
*
* @throws PaintException
* if the paint operation failed.
*/
public void addVariable(VariableOwner owner, String name, String value)
throws PaintException {
tag.addVariable(new StringVariable(owner, name, escapeJSON(value)));
}
/**
* Adds a int type variable.
*
* @param owner
* the Listener for variable changes.
* @param name
* the Variable name.
* @param value
* the Variable initial value.
*
* @throws PaintException
* if the paint operation failed.
*/
public void addVariable(VariableOwner owner, String name, int value)
throws PaintException {
tag.addVariable(new IntVariable(owner, name, value));
}
/**
* Adds a long type variable.
*
* @param owner
* the Listener for variable changes.
* @param name
* the Variable name.
* @param value
* the Variable initial value.
*
* @throws PaintException
* if the paint operation failed.
*/
public void addVariable(VariableOwner owner, String name, long value)
throws PaintException {
tag.addVariable(new LongVariable(owner, name, value));
}
/**
* Adds a float type variable.
*
* @param owner
* the Listener for variable changes.
* @param name
* the Variable name.
* @param value
* the Variable initial value.
*
* @throws PaintException
* if the paint operation failed.
*/
public void addVariable(VariableOwner owner, String name, float value)
throws PaintException {
tag.addVariable(new FloatVariable(owner, name, value));
}
/**
* Adds a double type variable.
*
* @param owner
* the Listener for variable changes.
* @param name
* the Variable name.
* @param value
* the Variable initial value.
*
* @throws PaintException
* if the paint operation failed.
*/
public void addVariable(VariableOwner owner, String name, double value)
throws PaintException {
tag.addVariable(new DoubleVariable(owner, name, value));
}
/**
* Adds a boolean type variable.
*
* @param owner
* the Listener for variable changes.
* @param name
* the Variable name.
* @param value
* the Variable initial value.
*
* @throws PaintException
* if the paint operation failed.
*/
public void addVariable(VariableOwner owner, String name, boolean value)
throws PaintException {
tag.addVariable(new BooleanVariable(owner, name, value));
}
/**
* Adds a string array type variable.
*
* @param owner
* the Listener for variable changes.
* @param name
* the Variable name.
* @param value
* the Variable initial value.
*
* @throws PaintException
* if the paint operation failed.
*/
public void addVariable(VariableOwner owner, String name, String[] value)
throws PaintException {
tag.addVariable(new ArrayVariable(owner, name, value));
}
/**
* Adds a upload stream type variable.
*
* TODO not converted for JSON
*
* @param owner
* the Listener for variable changes.
* @param name
* the Variable name.
*
* @throws PaintException
* if the paint operation failed.
*/
public void addUploadStreamVariable(VariableOwner owner, String name)
throws PaintException {
startTag("uploadstream");
addAttribute(UIDL_ARG_NAME, name);
endTag("uploadstream");
}
/**
* Prints the single text section.
*
* Prints full text section. The section data is escaped
*
* @param sectionTagName
* the name of the tag.
* @param sectionData
* the section data to be printed.
* @throws PaintException
* if the paint operation failed.
*/
public void addSection(String sectionTagName, String sectionData)
throws PaintException {
tag.addData("{\"" + sectionTagName + "\":\"" + escapeJSON(sectionData)
+ "\"}");
}
/**
* Adds XML directly to UIDL.
*
* @param xml
* the Xml to be added.
* @throws PaintException
* if the paint operation failed.
*/
public void addUIDL(String xml) throws PaintException {
// Ensure that the target is open
if (closed) {
throw new PaintException(
"Attempted to write to a closed PaintTarget.");
}
// Make sure that the open start tag is closed before
// anything is written.
// Escape and write what was given
if (xml != null) {
tag.addData("\"" + escapeJSON(xml) + "\"");
}
}
/**
* Adds XML section with namespace.
*
* @param sectionTagName
* the name of the tag.
* @param sectionData
* the section data.
* @param namespace
* the namespace to be added.
* @throws PaintException
* if the paint operation failed.
*
* @see com.vaadin.terminal.PaintTarget#addXMLSection(String, String,
* String)
*/
public void addXMLSection(String sectionTagName, String sectionData,
String namespace) throws PaintException {
// Ensure that the target is open
if (closed) {
throw new PaintException(
"Attempted to write to a closed PaintTarget.");
}
startTag(sectionTagName);
if (namespace != null) {
addAttribute("xmlns", namespace);
}
if (sectionData != null) {
tag.addData("\"" + escapeJSON(sectionData) + "\"");
}
endTag(sectionTagName);
}
/**
* Gets the UIDL already printed to stream. Paint target must be closed
* before the <code>getUIDL</code> can be called.
*
* @return the UIDL.
*/
public String getUIDL() {
if (closed) {
return uidlBuffer.toString();
}
throw new IllegalStateException(
"Tried to read UIDL from open PaintTarget");
}
/**
* Closes the paint target. Paint target must be closed before the
* <code>getUIDL</code> can be called. Subsequent attempts to write to paint
* target. If the target was already closed, call to this function is
* ignored. will generate an exception.
*
* @throws PaintException
* if the paint operation failed.
*/
public void close() throws PaintException {
if (tag != null) {
uidlBuffer.write(tag.getJSON());
}
flush();
closed = true;
}
/**
* Method flush.
*/
private void flush() {
uidlBuffer.flush();
}
/*
* (non-Javadoc)
*
* @see com.vaadin.terminal.PaintTarget#startTag(com.vaadin.terminal
* .Paintable, java.lang.String)
*/
public boolean startTag(Paintable paintable, String tagName)
throws PaintException {
startTag(tagName, true);
final boolean isPreviouslyPainted = manager.hasPaintableId(paintable)
&& (identifiersCreatedDueRefPaint == null || !identifiersCreatedDueRefPaint
.contains(paintable));
final String id = manager.getPaintableId(paintable);
paintable.addListener(manager);
addAttribute("id", id);
paintedComponents.add(paintable);
if (paintable instanceof CustomLayout) {
customLayoutArgumentsOpen = true;
}
return cacheEnabled && isPreviouslyPainted;
}
public void paintReference(Paintable paintable, String referenceName)
throws PaintException {
final String id = getPaintIdentifier(paintable);
addAttribute(referenceName, id);
}
public String getPaintIdentifier(Paintable paintable) throws PaintException {
if (!manager.hasPaintableId(paintable)) {
if (identifiersCreatedDueRefPaint == null) {
identifiersCreatedDueRefPaint = new HashSet<Paintable>();
}
identifiersCreatedDueRefPaint.add(paintable);
}
return manager.getPaintableId(paintable);
}
/*
* (non-Javadoc)
*
* @see com.vaadin.terminal.PaintTarget#addCharacterData(java.lang.String )
*/
public void addCharacterData(String text) throws PaintException {
if (text != null) {
tag.addData(text);
}
}
/**
* This is basically a container for UI components variables, that will be
* added at the end of JSON object.
*
* @author mattitahvonen
*
*/
class JsonTag implements Serializable {
boolean firstField = false;
Vector<Object> variables = new Vector<Object>();
Vector<Object> children = new Vector<Object>();
Vector<Object> attr = new Vector<Object>();
StringBuilder data = new StringBuilder();
public boolean childrenArrayOpen = false;
private boolean childNode = false;
private boolean tagClosed = false;
public JsonTag(String tagName) {
data.append("[\"" + tagName + "\"");
}
private void closeTag() {
if (!tagClosed) {
data.append(attributesAsJsonObject());
data.append(getData());
// Writes the end (closing) tag
data.append("]");
tagClosed = true;
}
}
public String getJSON() {
if (!tagClosed) {
closeTag();
}
return data.toString();
}
public void openChildrenArray() {
if (!childrenArrayOpen) {
// append("c : [");
childrenArrayOpen = true;
// firstField = true;
}
}
public void closeChildrenArray() {
// append("]");
// firstField = false;
}
public void setChildNode(boolean b) {
childNode = b;
}
public boolean isChildNode() {
return childNode;
}
public String startField() {
if (firstField) {
firstField = false;
return "";
} else {
return ",";
}
}
/**
*
* @param s
* json string, object or array
*/
public void addData(String s) {
children.add(s);
}
public String getData() {
final StringBuilder buf = new StringBuilder();
final Iterator<Object> it = children.iterator();
while (it.hasNext()) {
buf.append(startField());
buf.append(it.next());
}
return buf.toString();
}
public void addAttribute(String jsonNode) {
attr.add(jsonNode);
}
private String attributesAsJsonObject() {
final StringBuilder buf = new StringBuilder();
buf.append(startField());
buf.append("{");
for (final Iterator<Object> iter = attr.iterator(); iter.hasNext();) {
final String element = (String) iter.next();
buf.append(element);
if (iter.hasNext()) {
buf.append(",");
}
}
buf.append(tag.variablesAsJsonObject());
buf.append("}");
return buf.toString();
}
public void addVariable(Variable v) {
variables.add(v);
}
private String variablesAsJsonObject() {
if (variables.size() == 0) {
return "";
}
final StringBuilder buf = new StringBuilder();
buf.append(startField());
buf.append("\"v\":{");
final Iterator<Object> iter = variables.iterator();
while (iter.hasNext()) {
final Variable element = (Variable) iter.next();
buf.append(element.getJsonPresentation());
if (iter.hasNext()) {
buf.append(",");
}
}
buf.append("}");
return buf.toString();
}
class TagCounter {
int count;
public TagCounter() {
count = 0;
}
public void increment() {
count++;
}
public String postfix(String s) {
if (count > 0) {
return s + count;
}
return s;
}
}
}
abstract class Variable implements Serializable {
String name;
public abstract String getJsonPresentation();
}
class BooleanVariable extends Variable implements Serializable {
boolean value;
public BooleanVariable(VariableOwner owner, String name, boolean v) {
value = v;
this.name = name;
}
@Override
public String getJsonPresentation() {
return "\"" + name + "\":" + (value == true ? "true" : "false");
}
}
class StringVariable extends Variable implements Serializable {
String value;
public StringVariable(VariableOwner owner, String name, String v) {
value = v;
this.name = name;
}
@Override
public String getJsonPresentation() {
return "\"" + name + "\":\"" + value + "\"";
}
}
class IntVariable extends Variable implements Serializable {
int value;
public IntVariable(VariableOwner owner, String name, int v) {
value = v;
this.name = name;
}
@Override
public String getJsonPresentation() {
return "\"" + name + "\":" + value;
}
}
class LongVariable extends Variable implements Serializable {
long value;
public LongVariable(VariableOwner owner, String name, long v) {
value = v;
this.name = name;
}
@Override
public String getJsonPresentation() {
return "\"" + name + "\":" + value;
}
}
class FloatVariable extends Variable implements Serializable {
float value;
public FloatVariable(VariableOwner owner, String name, float v) {
value = v;
this.name = name;
}
@Override
public String getJsonPresentation() {
return "\"" + name + "\":" + value;
}
}
class DoubleVariable extends Variable implements Serializable {
double value;
public DoubleVariable(VariableOwner owner, String name, double v) {
value = v;
this.name = name;
}
@Override
public String getJsonPresentation() {
return "\"" + name + "\":" + value;
}
}
class ArrayVariable extends Variable implements Serializable {
String[] value;
public ArrayVariable(VariableOwner owner, String name, String[] v) {
value = v;
this.name = name;
}
@Override
public String getJsonPresentation() {
StringBuilder sb = new StringBuilder();
sb.append("\"");
sb.append(name);
sb.append("\":[");
for (int i = 0; i < value.length;) {
sb.append("\"");
sb.append(escapeJSON(value[i]));
sb.append("\"");
i++;
if (i < value.length) {
sb.append(",");
}
}
sb.append("]");
return sb.toString();
}
}
public Set<Object> getUsedResources() {
return usedResources;
}
/**
* Method to check if paintable is already painted into this target.
*
* @param p
* @return true if is not yet painted into this target and is connected to
* app
*/
public boolean needsToBePainted(Paintable p) {
if (paintedComponents.contains(p)) {
return false;
} else if (((Component) p).getApplication() == null) {
return false;
} else {
return true;
}
}
@SuppressWarnings("unchecked")
public String getTag(Paintable paintable) {
/*
* Client widget annotation is searched from component hierarchy to
* detect the component that presumably has client side implementation.
* The server side name is used in the transportation, but encoded into
* integer strings to optimized transferred data.
*/
Class<? extends Paintable> class1 = paintable.getClass();
ClientWidget annotation = class1.getAnnotation(ClientWidget.class);
while (annotation == null) {
Class<?> superclass = class1.getSuperclass();
if (superclass != null
&& Paintable.class.isAssignableFrom(superclass)) {
class1 = (Class<? extends Paintable>) superclass;
annotation = class1.getAnnotation(ClientWidget.class);
} else {
System.out
.append("Warning: no superclass of givent has ClientWidget"
+ " annotation. Component will not be mapped correctly on client side.");
break;
}
}
usedPaintableTypes.add(class1);
return CommunicationManager.getTagForType(class1);
}
Collection<Class<? extends Paintable>> getUsedPaintableTypes() {
return usedPaintableTypes;
}
}
|