1 package com.randomnoun.common;
2
3 import java.io.IOException;
4 import java.io.Writer;
5
6
7
8
9
10 import java.lang.reflect.InvocationTargetException;
11 import java.lang.reflect.Method;
12 import java.util.ArrayList;
13 import java.util.Arrays;
14 import java.util.Collections;
15 import java.util.Comparator;
16 import java.util.Date;
17 import java.util.HashMap;
18 import java.util.Iterator;
19 import java.util.List;
20 import java.util.Map;
21 import java.util.Set;
22 import java.util.concurrent.ConcurrentHashMap;
23
24 import jakarta.servlet.http.HttpServletRequest;
25
26 import org.apache.commons.beanutils.PropertyUtils;
27
28 import com.randomnoun.common.io.StringBuilderWriter;
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51 public class Struct {
52
53
54
55
56
57
58 private static Map<Class<?>, Map<String,Method>> gettersCache;
59
60
61
62
63
64
65 private static Map<Class<?>, Map<String,Method>> settersCache;
66
67
68
69
70 public static final String DATE_FORMAT_MICROSOFT = "microsoft";
71
72
73 public static final String DATE_FORMAT_NUMERIC = "numeric";
74
75
76 public static interface ToStringReturnsJson { }
77
78
79 public static interface ToJson {
80 public String toJson();
81 }
82
83
84
85
86
87 public static interface ToJsonFormat {
88 public String toJson(String jsonFormat);
89 }
90
91
92
93
94
95 public static interface WriteJsonFormat {
96 public void writeJsonFormat(Writer w, String jsonFormat) throws IOException;
97 }
98
99
100
101
102
103
104 static class ListComparator implements Comparator
105 {
106 public int compare(Object o1, Object o2) {
107 if (o1 == null && o2 == null) {
108 return 0;
109 } else if (o1 == null) {
110 return -1;
111 } else if (o2 == null) {
112 return 1;
113 }
114 return ((Comparable)o1).compareTo(o2);
115 }
116 }
117
118
119 public static class ListContainingNullComparator extends ListComparator { };
120
121
122
123
124
125
126 public static class StructuredListComparator implements Comparator {
127
128 private String fieldName;
129
130
131
132
133
134
135 public StructuredListComparator(String fieldName) {
136 this.fieldName = fieldName;
137 }
138
139
140
141
142
143 public int compare(Object a, Object b)
144 throws IllegalArgumentException {
145 if (!(a instanceof Map)) {
146 throw new IllegalArgumentException("List must be composed of Maps");
147 }
148 if (!(b instanceof Map)) {
149 throw new IllegalArgumentException("List must be composed of Maps");
150 }
151
152 Map mapA = (Map) a;
153 Map mapB = (Map) b;
154 if (!mapA.containsKey(fieldName)) {
155 throw new IllegalArgumentException("keyField '" + fieldName + "' not found in Map");
156 }
157 if (!mapB.containsKey(fieldName)) {
158 throw new IllegalArgumentException("keyField '" + fieldName + "' not found in Map");
159 }
160
161 Object objectA = mapA.get(fieldName);
162 if (!(objectA instanceof Comparable)) {
163 throw new IllegalArgumentException("keyField '" + fieldName + "' element must implement Comparable");
164 }
165 return ((Comparable) objectA).compareTo(mapB.get(fieldName));
166 }
167 }
168
169
170
171
172
173
174 public static class StructuredListComparatorIgnoreCase implements Comparator {
175
176
177 private String fieldName;
178
179
180
181
182
183
184 public StructuredListComparatorIgnoreCase(String fieldName) {
185 this.fieldName = fieldName;
186 }
187
188
189
190
191
192 public int compare(Object a, Object b)
193 throws IllegalArgumentException {
194 if (!(a instanceof Map)) {
195 throw new IllegalArgumentException("List must be composed of Maps");
196 }
197 if (!(b instanceof Map)) {
198 throw new IllegalArgumentException("List must be composed of Maps");
199 }
200
201 Map mapA = (Map) a;
202 Map mapB = (Map) b;
203 if (!mapA.containsKey(fieldName)) {
204 throw new IllegalArgumentException("keyField '" + fieldName + "' not found in Map");
205 }
206 if (!mapB.containsKey(fieldName)) {
207 throw new IllegalArgumentException("keyField '" + fieldName + "' not found in Map");
208 }
209
210 String stringA = (String) mapA.get(fieldName);
211 return stringA.compareToIgnoreCase((String) mapB.get(fieldName));
212 }
213 }
214
215
216
217
218
219
220
221
222
223
224 static public void sortStructuredList(List list, String keyField) {
225 if (list == null) { throw new NullPointerException("Cannot search null list"); }
226 if (keyField == null) { throw new NullPointerException("Cannot search for null keyField"); }
227
228 Comparator comparator = new StructuredListComparator(keyField);
229 Collections.sort(list, comparator);
230 }
231
232
233
234
235
236
237
238
239
240
241 static public void sortStructuredListIgnoreCase(List list, String keyField) {
242 if (list == null) { throw new NullPointerException("Cannot search null list"); }
243 if (keyField == null) { throw new NullPointerException("Cannot search for null keyField"); }
244
245 Comparator comparator = new StructuredListComparatorIgnoreCase(keyField);
246 Collections.sort(list, comparator);
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 public static void setFromRequest(Object obj, HttpServletRequest request) {
291 setFromRequest(obj, request, null);
292 }
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310 public static void setFromRequest(Object obj, HttpServletRequest request, String[] fields) {
311 Iterator paramListIter;
312
313 if (fields == null) {
314 paramListIter = request.getParameterMap().keySet().iterator();
315 } else {
316 paramListIter = Arrays.asList(fields).iterator();
317 }
318
319 while (paramListIter.hasNext()) {
320 String parameter = (String) paramListIter.next();
321 if (parameter.endsWith("[]")) {
322 parameter = parameter.substring(0, parameter.length()-2);
323 String values[] = request.getParameterValues(parameter);
324 if (values!=null) {
325 for (int i=0; i<values.length; i++) {
326 values[i] = Text.replaceString(values[i], "\015\012", "\n");
327 }
328 }
329 setValue(obj, parameter, values, true, true, true);
330 } else {
331 String value = request.getParameter(parameter);
332
333
334 value = Text.replaceString(value, "\015\012", "\n");
335
336
337
338
339
340
341 setValue(obj, parameter, value, true, true, true);
342
343 }
344 }
345 }
346
347
348
349
350
351
352
353
354
355
356
357 public static void setFromMap(Object obj, Map map,
358 boolean ignoreMissingSetter, boolean convertStrings, boolean createMissingElements)
359 {
360 setFromMap(obj, map, ignoreMissingSetter, convertStrings, createMissingElements, null);
361 }
362
363
364
365
366
367
368
369
370
371
372
373
374 public static void setFromMap(Object obj, Map map,
375 boolean ignoreMissingSetter, boolean convertStrings, boolean createMissingElements,
376 String[] fields)
377 {
378 if (fields == null) {
379 for (Iterator<?> i = map.entrySet().iterator(); i.hasNext(); ) {
380 Map.Entry<?, ?> entry = (Map.Entry<?, ?>) i.next();
381 setValue(obj, (String) entry.getKey(), entry.getValue(), ignoreMissingSetter, convertStrings, createMissingElements);
382 }
383 } else {
384 for (int i = 0; i<fields.length; i++) {
385 setValue(obj, fields[i], map.get(fields[i]), ignoreMissingSetter, convertStrings, createMissingElements);
386 }
387 }
388 }
389
390
391
392
393
394
395
396
397
398
399
400
401 public static void setFromObject(Object targetObj, Object sourceObj,
402 boolean ignoreMissingSetter, boolean convertStrings, boolean createMissingElements,
403 String[] fields)
404 {
405
406 if (fields == null) {
407
408
409
410
411
412
413 throw new UnsupportedOperationException("null field list not supported");
414 } else {
415 for (int i = 0; i<fields.length; i++) {
416 setValue(targetObj, fields[i], getValue(sourceObj, fields[i]), ignoreMissingSetter, convertStrings, createMissingElements);
417 }
418 }
419 }
420
421
422
423
424
425
426
427
428
429
430
431
432
433 private static Method getGetterMethod(Class<?> clazz, String propertyName) {
434 Map<String,Method> getters = (Map<String,Method>) gettersCache.get(clazz);
435
436 if (getters == null) {
437 getters = new HashMap<String,Method>();
438
439 Method[] methods = clazz.getMethods();
440 Method method;
441
442
443 for (int i = 0; i < methods.length; i++) {
444 method = methods[i];
445
446 if (method.getName().startsWith("get") && method.getParameterTypes().length == 1) {
447 String property = method.getName().substring(3);
448 getters.put(property.toLowerCase(), method);
449 }
450 }
451 gettersCache.put(clazz, getters);
452 }
453
454 return (Method) getters.get(propertyName.toLowerCase());
455 }
456
457
458
459
460
461
462
463
464
465
466
467
468 private static Method getSetterMethod(Class<?> clazz, String propertyName) {
469 Map<String,Method> setters = (Map<String, Method>) settersCache.get(clazz);
470
471 if (setters == null) {
472 setters = new HashMap<String,Method>();
473
474 Method[] methods = clazz.getMethods();
475 Method method;
476
477
478 for (int i = 0; i < methods.length; i++) {
479 method = methods[i];
480
481 if (method.getName().startsWith("set") && method.getParameterTypes().length == 1) {
482 String property = method.getName().substring(3);
483 setters.put(property.toLowerCase(), method);
484 }
485 }
486 settersCache.put(clazz, setters);
487 }
488
489 return (Method) setters.get(propertyName.toLowerCase());
490 }
491
492
493 private static void callSetterMethodWithString(Object target, Method setter, String value) {
494 Class<?> clazz = setter.getParameterTypes()[0];
495 Object realValue;
496
497 if (clazz.equals(String.class)) {
498 realValue = value;
499 } else if (clazz.equals(boolean.class)) {
500 realValue = Boolean.valueOf(value);
501 } else if (clazz.equals(long.class)) {
502 realValue = value.equals("") ? new Long(0) : new Long(value);
503 } else if (clazz.equals(int.class)) {
504 realValue = value.equals("") ? new Integer(0) : new Integer(value);
505 } else if (clazz.equals(double.class)) {
506 realValue = value.equals("") ? new Double(0) : new Double(value);
507 } else if (clazz.equals(float.class)) {
508 realValue = value.equals("") ? new Float(0) : new Float(value);
509 } else if (clazz.equals(short.class)) {
510 realValue = value.equals("") ? new Short((short) 0) : new Short(value);
511
512
513 } else if (clazz.equals(Long.class)) {
514 realValue = value.equals("") ? null : new Long(value);
515 } else if (clazz.equals(Integer.class)) {
516 realValue = value.equals("") ? null : new Integer(value);
517 } else if (clazz.equals(Double.class)) {
518 realValue = value.equals("") ? null : new Double(value);
519 } else if (clazz.equals(Float.class)) {
520 realValue = value.equals("") ? null : new Float(value);
521 } else if (clazz.equals(Short.class)) {
522 realValue = value.equals("") ? null : new Short(value);
523
524
525
526 } else {
527 throw new IllegalArgumentException("Cannot convert string value to " + "'" + clazz.getName() + "' required for setter method '" + setter.getName() + "'");
528 }
529
530 callSetterMethod(target, setter, realValue);
531 }
532
533
534 private static Object callGetterMethod(Object target, Method getter) {
535 Object[] methodArgs = new Object[0];
536
537 try {
538 return getter.invoke(target, methodArgs);
539 } catch (InvocationTargetException ite) {
540
541 throw (IllegalArgumentException) new IllegalArgumentException("Exception calling method '" + getter.getName() + "' on object '" + target.getClass().getName() + "': " + ite.getMessage()).initCause(ite);
542 } catch (IllegalAccessException iae) {
543 throw (IllegalArgumentException) new IllegalArgumentException("Exception calling method '" + getter.getName() + "' on object '" + target.getClass().getName() + "': " + iae.getMessage()).initCause(iae);
544 }
545 }
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561 private static boolean isAssignmentSafe(Class variableClass, Class value) {
562
563 return variableClass.isAssignableFrom(value) ||
564 (variableClass.equals(boolean.class) && value.equals(Boolean.class)) ||
565 (variableClass.equals(char.class) && value.equals(Character.class)) ||
566 (variableClass.equals(byte.class) && value.equals(Byte.class)) ||
567 (variableClass.equals(short.class) && value.equals(Short.class)) ||
568 (variableClass.equals(int.class) && value.equals(Integer.class)) ||
569 (variableClass.equals(long.class) && value.equals(Long.class)) ||
570 (variableClass.equals(float.class) && value.equals(Float.class)) ||
571 (variableClass.equals(double.class) && value.equals(Double.class));
572 }
573
574
575 private static void callSetterMethod(Object target, Method setter, Object value) {
576
577 Object[] methodArgs = new Object[1];
578 methodArgs[0] = value;
579
580 if (value!=null) {
581 Class<?> setterParamClass = setter.getParameterTypes()[0];
582 Class<?> valueClass = value.getClass();
583
584
585 if (setterParamClass.equals(Long.class) && Number.class.isAssignableFrom(valueClass)) {
586 methodArgs[0] = new Long(((Number)value).longValue());
587 } else if (setterParamClass.equals(long.class) && Number.class.isAssignableFrom(valueClass)) {
588 methodArgs[0] = new Long(((Number)value).longValue());
589 } else {
590 if (!isAssignmentSafe(setterParamClass, value.getClass())) {
591
592
593
594 throw new IllegalArgumentException("Exception calling method '" + setter.getName() +
595 "' on object '" + target.getClass().getName() + "': value object of type '" +
596 valueClass.getName() + "' is not assignment-compatible with setter parameter '" +
597 setterParamClass.getName() + "'");
598 }
599 }
600 }
601
602 try {
603 setter.invoke(target, methodArgs);
604 } catch (InvocationTargetException ite) {
605
606 throw (IllegalArgumentException) new IllegalArgumentException("Exception calling method '" + setter.getName() + "' on object '" + target.getClass().getName() + "': " + ite.getMessage()).initCause(ite);
607 } catch (IllegalAccessException iae) {
608 throw (IllegalArgumentException) new IllegalArgumentException("Exception calling method '" + setter.getName() + "' on object '" + target.getClass().getName() + "': " + iae.getMessage()).initCause(iae);
609 } catch (Exception e) {
610
611 throw (IllegalArgumentException) new IllegalArgumentException("Exception calling method '" + setter.getName() + "' on object '" + target.getClass().getName() + "': " + e.getMessage()).initCause(e);
612 }
613 }
614
615
616
617
618
619
620
621
622
623
624 static public Object getValue(Object object, String key) {
625
626
627
628
629
630
631
632
633 try {
634 return PropertyUtils.getProperty(object, key);
635 } catch (Exception e) {
636 return null;
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 static public void setValue(Object object, String key, Object value, boolean ignoreMissingSetter, boolean convertStrings, boolean createMissingElements)
681 throws NumberFormatException
682 {
683 if (object == null) { throw new NullPointerException("null object"); }
684 if (key == null) { throw new NullPointerException("null key"); }
685
686
687
688
689
690
691 Object ref = object;
692
693 int parseState = 0;
694 int length = key.length();
695 String element;
696 int elementInt = -1;
697 List lastList = null;
698 String parentName = null;
699 Object parentRef = null;
700
701 if (object instanceof List) { lastList = (List) object; }
702
703 char ch;
704 StringBuffer buffer = new StringBuffer();
705
706 for (int pos = 0; pos < length; pos++) {
707 ch = key.charAt(pos);
708
709
710 switch (parseState) {
711
712 case 0:
713 if (ch == '[') {
714 buffer.setLength(0);
715 parseState = 1;
716 } else {
717 buffer.setLength(0);
718 buffer.append(ch);
719 parseState = 2;
720 }
721 break;
722
723 case 3:
724 if (ch == '[') {
725 buffer.setLength(0);
726 parseState = 1;
727 } else if (ch == '.') {
728 buffer.setLength(0);
729 parseState = 2;
730 } else {
731 throw new IllegalArgumentException("Expecting '[' or '.' after ']' in key '" + key + "'; found '" + ch + "'");
732 }
733 break;
734
735 case 1:
736 if (ch >= '0' && ch <= '9') {
737 buffer.append(ch);
738 } else if (ch == ']') {
739 element = buffer.toString();
740 elementInt = Integer.parseInt(element);
741
742 if (ref == null) {
743
744 if ((parentRef instanceof Map) && createMissingElements) {
745 ref = new ArrayList();
746 ((Map) parentRef).put(parentName, ref);
747 } else if ((parentRef instanceof List) && createMissingElements) {
748 ref = new ArrayList();
749 ((List) parentRef).set(Integer.parseInt(parentName), ref);
750 } else {
751 throw new IllegalArgumentException("Could not retrieve indexed property " + element + " in key '" + key + "' from null object");
752 }
753 }
754
755 if (ref instanceof List) {
756 lastList = (List) ref;
757
758
759
760 if (lastList.size() <= elementInt) {
761 setListElement(lastList, elementInt, null);
762 }
763
764 parentName = element;
765 parentRef = ref;
766 ref = lastList.get(elementInt);
767 } else {
768
769
770
771 throw new IllegalArgumentException("Could not retrieve indexed property " + element + " in key '" + key + "' from object of type '" + ref.getClass().getName() + "'");
772 }
773
774 parseState = 3;
775 } else {
776 throw new IllegalArgumentException("Illegal character '" + ch + "'" + " found in list index");
777 }
778 break;
779
780 case 2:
781 if (ch != '.' && ch != '[') {
782 buffer.append(ch);
783 } else {
784
785 element = buffer.toString();
786
787 if (ref == null) {
788 if ((parentRef instanceof List) && createMissingElements) {
789 ref = new HashMap();
790 ((List) parentRef).set(Integer.parseInt(parentName), ref);
791 } else if ((parentRef instanceof Map) && createMissingElements) {
792 ref = new HashMap();
793 ((Map) parentRef).put(parentName, ref);
794 } else {
795 throw new IllegalArgumentException("Could not retrieve mapped property '" + element + "' in key '" + key + "' from null object");
796 }
797 }
798
799 if (element.equals("")) {
800 throw new IllegalArgumentException("Could not retrieve empty mapped property '" + element + "' in key '" + key + "'");
801 } else if (ref instanceof Map) {
802 parentName = element;
803 parentRef = ref;
804 ref = ((Map) ref).get(element);
805 } else {
806
807 Method getter = getGetterMethod(ref.getClass(), element);
808
809 if (getter == null) {
810 throw new IllegalArgumentException("Could not retrieve mapped property '" + element + "' in key '" + key + "' for class '" + ref.getClass().getName() + "': no getter method found");
811 }
812
813 parentName = null;
814 parentRef = null;
815 ref = callGetterMethod(ref, getter);
816 }
817
818 buffer.setLength(0);
819
820 if (ch == '[') {
821 parseState = 1;
822 } else if (ch == '.') {
823 parseState = 2;
824 } else {
825 throw new IllegalStateException("Unexpected character '" + ch + "' parsing key '" + key + "'");
826 }
827 }
828 break;
829
830 default:
831 throw new IllegalStateException("Unexpected state " + parseState + " parsing key '" + key + "'");
832 }
833 }
834
835 if (parseState == 0) {
836 throw new IllegalStateException("Unexpected error after parsing key '" + key + "'");
837 } else if (parseState == 1) {
838 throw new IllegalStateException("Missing ']' in key '" + key + "'");
839 } else if (parseState == 2) {
840
841 element = buffer.toString();
842
843 if (ref == null) {
844 if ((parentRef instanceof Map) && createMissingElements) {
845 ref = new HashMap();
846 ((Map) parentRef).put(parentName, ref);
847 } else if ((parentRef instanceof List) && createMissingElements) {
848 ref = new HashMap();
849 ((List) parentRef).set(Integer.parseInt(parentName), ref);
850 } else {
851 throw new IllegalArgumentException("Could not set mapped property '" + element + "' in key '" + key + "' for null object");
852 }
853 }
854
855 if (element.equals("")) {
856 throw new IllegalArgumentException("Could not set empty mapped property '" + element + "' in key '" + key + "'");
857 } else if (ref instanceof Map) {
858 ((Map) ref).put(element, value);
859 } else {
860 Method setter = getSetterMethod(ref.getClass(), element);
861
862 if (setter == null) {
863
864 if (!ignoreMissingSetter) {
865 throw new IllegalArgumentException("Could not set mapped property '" + element + "' in key '" + key + "' for class '" + ref.getClass().getName() + "': no setter method found");
866 }
867 } else {
868
869 try {
870 if (convertStrings && ((value == null) || (value instanceof String))) {
871 if (value==null) { value = ""; }
872 callSetterMethodWithString(ref, setter, (String) value);
873 } else {
874 callSetterMethod(ref, setter, value);
875 }
876 } catch (Exception e) {
877 throw (IllegalArgumentException) new IllegalArgumentException("Could not set field '" + key + "' with value '" + value + "'").initCause(e);
878 }
879 }
880 }
881 } else if (parseState == 3) {
882
883
884
885 ref = lastList;
886
887 if (ref == null) {
888 throw new IllegalArgumentException("Could not set indexed property '" + elementInt + "' in key '" + key + "' for null object");
889 }
890
891 if (ref instanceof List) {
892 ((List) ref).set(elementInt, value);
893 } else {
894 throw new IllegalArgumentException("Could not set indexed property " + elementInt + " in key '" + key + "' in object of type '" + ref.getClass().getName() + "'");
895 }
896 }
897 }
898
899
900
901
902
903
904
905
906
907
908
909 public static void setListElement(List list, int index, Object object) {
910 int size;
911
912 if (list == null) {
913 throw new NullPointerException("list parameter must not be null");
914 }
915
916 if (index < 0) {
917 throw new IndexOutOfBoundsException("index parameter must be >= 0");
918 }
919
920 size = list.size();
921
922 while (index >= size) {
923 list.add(null);
924 size = size + 1;
925 }
926
927 list.set(index, object);
928 }
929
930
931
932
933
934
935
936
937
938
939
940
941
942 static public Map getStructuredListItem(List list, String keyField, long longValue) {
943 if (list == null) { throw new NullPointerException("Cannot search null list"); }
944 if (keyField == null) { throw new NullPointerException("Cannot search for null keyField"); }
945
946 Map row;
947 Object foundKey;
948 for (Iterator i = list.iterator(); i.hasNext();) {
949 try {
950 row = (Map) i.next();
951 } catch (ClassCastException cce) {
952 throw (IllegalStateException) new IllegalStateException("List must be composed of Maps").initCause(cce);
953 }
954 foundKey = row.get(keyField);
955 if (foundKey != null) {
956 if (!(foundKey instanceof Number)) {
957 throw (IllegalStateException) new IllegalStateException("Key is not numeric (found '" + foundKey.getClass().getName() + "' instead)");
958 }
959 if (((Number) foundKey).longValue() == longValue) {
960 return row;
961 }
962 }
963 }
964 return null;
965 }
966
967
968
969
970
971
972
973
974
975
976
977
978
979 static public Object getStructuredListObject(List list, String keyField, long longValue) {
980 if (list == null) { throw new NullPointerException("Cannot search null list"); }
981 if (keyField == null) { throw new NullPointerException("Cannot search for null keyField"); }
982 Object row;
983 Object foundKey;
984 for (Iterator i = list.iterator(); i.hasNext();) {
985 row = i.next();
986 foundKey = Struct.getValue(row, keyField);
987 if (foundKey != null) {
988 if (!(foundKey instanceof Number)) {
989 throw (IllegalStateException) new IllegalStateException("Key is not numeric (found '" + foundKey.getClass().getName() + "' instead)");
990 }
991 if (((Number) foundKey).longValue() == longValue) {
992 return row;
993 }
994 }
995 }
996 return null;
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 static public Map getStructuredListItem(List list, String keyField, Object key) {
1031 if (list == null) { throw new NullPointerException("Cannot search null list"); }
1032 if (keyField == null) { throw new NullPointerException("Cannot search for null keyField"); }
1033 Map row;
1034 Object foundKey;
1035 for (Iterator i = list.iterator(); i.hasNext();) {
1036
1037 try {
1038 row = (Map) i.next();
1039 } catch (ClassCastException cce) {
1040 throw (IllegalArgumentException) new IllegalArgumentException("List must be composed of Maps").initCause(cce);
1041 }
1042 foundKey = row.get(keyField);
1043 if (foundKey == null && key==null) {
1044 return row;
1045 } else {
1046 if (foundKey!=null && foundKey.equals(key)) {
1047 return row;
1048 }
1049 }
1050 }
1051 return null;
1052 }
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069 static public Map getStructuredListItem2(List list, String keyField, long longValue, String keyField2, long longValue2) {
1070 if (list == null) { throw new NullPointerException("Cannot search null list"); }
1071 if (keyField == null) { throw new NullPointerException("Cannot search for null keyField"); }
1072 if (keyField2 == null) { throw new NullPointerException("Cannot search for null keyField2"); }
1073
1074 Map row;
1075 Object foundKey, foundKey2;
1076 for (Iterator i = list.iterator(); i.hasNext();) {
1077 try {
1078 row = (Map) i.next();
1079 } catch (ClassCastException cce) {
1080 throw (IllegalStateException) new IllegalStateException("List must be composed of Maps").initCause(cce);
1081 }
1082 foundKey = row.get(keyField);
1083 if (foundKey != null) {
1084 if (!(foundKey instanceof Number)) {
1085 throw (IllegalStateException) new IllegalStateException("Key is not numeric (found '" + foundKey.getClass().getName() + "' instead)");
1086 }
1087 if (((Number) foundKey).longValue() == longValue) {
1088
1089 foundKey2 = row.get(keyField2);
1090 if (foundKey2 != null) {
1091 if (!(foundKey2 instanceof Number)) {
1092 throw (IllegalStateException) new IllegalStateException("Key2 is not numeric (found '" + foundKey2.getClass().getName() + "' instead)");
1093 }
1094 }
1095 if (((Number) foundKey2).longValue() == longValue2) {
1096 return row;
1097 }
1098 }
1099 }
1100 }
1101 return null;
1102 }
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120 static public Object getStructuredListObject(List list, String keyField, Object key) {
1121 if (list == null) { throw new NullPointerException("Cannot search null list"); }
1122 if (keyField == null) { throw new NullPointerException("Cannot search for null keyField"); }
1123 Object row;
1124 Object foundKey;
1125 for (Iterator i = list.iterator(); i.hasNext();) {
1126
1127 row = i.next();
1128 foundKey = Struct.getValue(row, keyField);
1129 if (foundKey == null && key==null) {
1130 return row;
1131 } else {
1132 if (foundKey!=null && foundKey.equals(key)) {
1133 return row;
1134 }
1135 }
1136 }
1137 return null;
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 public static List getStructuredListColumn(List list, String columnName) {
1185 if (list == null) {
1186 throw new NullPointerException("list must not be empty");
1187 }
1188
1189 if (columnName == null) {
1190 throw new NullPointerException("columnName must not be empty");
1191 }
1192
1193 if (list.size() == 0) {
1194 return Collections.EMPTY_LIST;
1195 }
1196
1197 ArrayList result = new ArrayList(list.size());
1198 Iterator it = list.iterator();
1199
1200 while (it.hasNext()) {
1201 Object obj = it.next();
1202 if (obj instanceof Map) {
1203 Map map = (Map) obj;
1204 if (map==null) {
1205 result.add(null);
1206 } else {
1207 Object o = map.get(columnName);
1208 result.add(o);
1209 }
1210 } else {
1211 Object o = getValue(obj, columnName);
1212 result.add(o);
1213 }
1214 }
1215
1216 return result;
1217 }
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251 static public String structuredListToString(String topLevelName, List list) {
1252 String s;
1253 Object value;
1254 int index = 0;
1255
1256 if (list==null) {
1257 return topLevelName + " = (List) null\n";
1258 }
1259
1260 s = topLevelName + " = [\n";
1261
1262 for (Iterator i = list.iterator(); i.hasNext();) {
1263 value = i.next();
1264
1265 if (value == null) {
1266 s = s + " " + index + ": null\n";
1267 } else if (value instanceof String) {
1268 s = s + " " + index + ": '" + Text.getDisplayString("", (String) value) + "'\n";
1269 } else if (value.getClass().isArray()) {
1270 List wrapper = Arrays.asList((Object[]) value);
1271 s = s + " " + index + ": (" + value.getClass() + ") " + Text.indent(" ", structuredListToString(String.valueOf(index), wrapper));
1272 } else if (value instanceof Map) {
1273 s = s + Text.indent(" ", structuredMapToString(String.valueOf(index), (Map) value));
1274 } else if (value instanceof List) {
1275 s = s + Text.indent(" ", structuredListToString(String.valueOf(index), (List) value));
1276 } else if (value instanceof Set) {
1277 s = s + Text.indent(" ", structuredSetToString(String.valueOf(index), (Set) value));
1278 } else {
1279 s = s + " " + index + ": (" + Text.getLastComponent(value.getClass().getName()) + ") " + Text.getDisplayString("", value.toString()) + "\n";
1280 }
1281
1282 index = index + 1;
1283 }
1284
1285 s = s + "]\n";
1286
1287 return s;
1288 }
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321 static public String structuredSetToString(String topLevelName, Set set) {
1322 String s;
1323 Object value;
1324 int index = 0;
1325
1326 if (set==null) {
1327 return topLevelName + " = (Set) null\n";
1328 }
1329
1330 s = topLevelName + " = (\n";
1331
1332 for (Iterator i = set.iterator(); i.hasNext();) {
1333 value = i.next();
1334
1335 if (value == null) {
1336 s = s + " " + index + ": null\n";
1337 } else if (value instanceof String) {
1338 s = s + Text.getDisplayString("", (String) value) + "'\n";
1339 } else if (value.getClass().isArray()) {
1340 List wrapper = Arrays.asList((Object[]) value);
1341 s = s + Text.indent(" ", structuredListToString(String.valueOf(index), wrapper));
1342 } else if (value instanceof Map) {
1343 s = s + Text.indent(" ", structuredMapToString(String.valueOf(index), (Map) value));
1344 } else if (value instanceof List) {
1345 s = s + Text.indent(" ", structuredListToString(String.valueOf(index), (List) value));
1346 } else if (value instanceof Set) {
1347 s = s + Text.indent(" ", structuredSetToString(String.valueOf(index), (Set) value));
1348 } else {
1349 s = s + " (" + Text.getLastComponent(value.getClass().getName()) + ") " + Text.getDisplayString("", value.toString()) + "\n";
1350 }
1351
1352 index = index + 1;
1353 }
1354
1355 s = s + ")\n";
1356
1357 return s;
1358 }
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393 static public String structuredMapToString(String topLevelName, Map map) {
1394 String s;
1395 String key;
1396 Object value;
1397
1398 if (map==null) {
1399 return topLevelName + " = (Map) null\n";
1400 }
1401
1402
1403 List list = new ArrayList(map.keySet());
1404 Collections.sort(list, new ListContainingNullComparator());
1405
1406 s = topLevelName + " = {\n";
1407
1408 for (Iterator i = list.iterator(); i.hasNext();) {
1409 key = (String) i.next();
1410 value = map.get(key);
1411
1412 if (value == null) {
1413 s = s + " " + ((key == null) ? "null" : key) + " => null\n";
1414 } else if (value instanceof String) {
1415 s = s + " " + ((key == null) ? "null" : key) + " => '" + Text.getDisplayString(key, (String) value) + "'\n";
1416 } else if (value.getClass().isArray()) {
1417
1418
1419 if (value instanceof Object[]) {
1420 List wrapper = Arrays.asList((Object[])value);
1421 s = s + Text.indent(" ", structuredListToString(key, wrapper));
1422 } else if (value instanceof double[]) {
1423
1424
1425
1426 double[] daSrc = (double[]) value;
1427 Double[] daTgt = new Double[daSrc.length];
1428 for (int j=0; j<daSrc.length; j++) { daTgt[j]=daSrc[j]; }
1429 List arrayList = Arrays.asList((Object[])daTgt);
1430 s = s + Text.indent(" ", structuredListToString(key, arrayList));
1431 } else if (value instanceof int[]) {
1432 int[] daSrc = (int[]) value;
1433 Integer[] daTgt = new Integer[daSrc.length];
1434 for (int j=0; j<daSrc.length; j++) { daTgt[j]=daSrc[j]; }
1435 List arrayList = Arrays.asList((Object[])daTgt);
1436 s = s + Text.indent(" ", structuredListToString(key, arrayList));
1437
1438 } else {
1439 throw new UnsupportedOperationException("Cannot convert primitive array to String");
1440 }
1441
1442 } else if (value instanceof Map) {
1443 s = s + Text.indent(" ", structuredMapToString(key, (Map) value));
1444 } else if (value instanceof List) {
1445 s = s + Text.indent(" ", structuredListToString(key, (List) value));
1446 } else if (value instanceof Set) {
1447 s = s + Text.indent(" ", structuredSetToString(key, (Set) value));
1448 } else {
1449 s = s + " " + ((key == null) ? "null" : key) + " => (" + Text.getLastComponent(value.getClass().getName()) + ") " + Text.getDisplayString(key, value.toString()) + "\n";
1450 }
1451 }
1452
1453 s = s + "}\n";
1454
1455 return s;
1456 }
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492 static public String arrayToString(String topLevelName, Object list) {
1493 if (list==null) {
1494 return topLevelName + "[] = (Array) null\n";
1495 }
1496 if (!list.getClass().isArray()) {
1497 throw new IllegalArgumentException("object must be array");
1498 }
1499 List wrapper = Arrays.asList((Object[]) list);
1500 return structuredListToString(topLevelName + "[]", wrapper);
1501 }
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511 static public String structuredListToJson(List list)
1512 {
1513 return structuredListToJson(list, "microsoft");
1514 }
1515
1516
1517
1518
1519
1520
1521
1522
1523 public static String structuredListToJson(List list, String jsonFormat)
1524 {
1525 StringBuilderWriter w = new StringBuilderWriter(list.size() * 2);
1526 try {
1527 structuredListToJson(w, list, jsonFormat);
1528 } catch (IOException e) {
1529 throw new IllegalStateException("IOException in StringBuilderWriter", e);
1530 }
1531 return w.toString();
1532 }
1533
1534 public static void structuredListToJson(Writer w, List list, String jsonFormat) throws IOException {
1535 if (list==null) {
1536 w.append("null");
1537 return;
1538 }
1539
1540 Object value;
1541 int index = 0;
1542 w.append('[');
1543 for (Iterator i = list.iterator(); i.hasNext(); ) {
1544 value = i.next();
1545 if (value == null) {
1546 w.append("null");
1547 } else if (value instanceof String) {
1548 w.append('\"').append(Text.escapeJavascript((String) value)).append('\"');
1549 } else if (value instanceof WriteJsonFormat) {
1550 ((WriteJsonFormat) value).writeJsonFormat(w, jsonFormat);
1551 } else if (value instanceof ToJsonFormat) {
1552 w.append(((ToJsonFormat) value).toJson(jsonFormat));
1553 } else if (value instanceof ToJson) {
1554 w.append(((ToJson) value).toJson());
1555 } else if (value instanceof ToStringReturnsJson) {
1556 w.append(value.toString());
1557 } else if (value instanceof Map) {
1558 structuredMapToJson(w, (Map) value, jsonFormat);
1559 } else if (value instanceof List) {
1560 structuredListToJson(w, (List) value, jsonFormat);
1561 } else if (value instanceof Number) {
1562 w.append(value.toString());
1563 } else if (value instanceof Boolean) {
1564 w.append(value.toString());
1565 } else if (value instanceof java.util.Date) {
1566
1567
1568 w.append(toDate((java.util.Date)value, jsonFormat));
1569 } else if (value.getClass().isArray()) {
1570 if (value instanceof Object[]) {
1571 List arrayList = Arrays.asList((Object[])value);
1572 structuredListToJson(w, (List)arrayList, jsonFormat);
1573 } else if (value instanceof double[]) {
1574
1575
1576 double[] daSrc = (double[]) value;
1577 Double[] daTgt = new Double[daSrc.length];
1578 for (int j=0; j<daSrc.length; j++) { daTgt[j]=daSrc[j]; }
1579 List arrayList = Arrays.asList((Object[])daTgt);
1580 structuredListToJson(w, (List) arrayList, jsonFormat);
1581 } else if (value instanceof int[]) {
1582 int[] daSrc = (int[]) value;
1583 Integer[] daTgt = new Integer[daSrc.length];
1584 for (int j=0; j<daSrc.length; j++) { daTgt[j]=daSrc[j]; }
1585 List arrayList = Arrays.asList((Object[])daTgt);
1586 structuredListToJson(w, (List) arrayList, jsonFormat);
1587 } else {
1588 throw new UnsupportedOperationException("Cannot convert primitive array to JSON");
1589 }
1590 } else {
1591 throw new RuntimeException("Cannot translate Java object " +
1592 value.getClass().getName() + " to javascript value");
1593 }
1594 index = index + 1;
1595 if (i.hasNext()) {
1596 w.append(',');
1597 }
1598 }
1599 w.append("]\n");
1600
1601 }
1602
1603
1604
1605
1606
1607
1608
1609 static public String structuredMapToJson(Map map)
1610 {
1611 return structuredMapToJson(map, DATE_FORMAT_MICROSOFT);
1612 }
1613
1614
1615
1616
1617
1618
1619
1620
1621 public static String structuredMapToJson(Map map, String jsonFormat) {
1622 StringBuilderWriter w = new StringBuilderWriter();
1623 try {
1624 structuredMapToJson(w, map, jsonFormat);
1625 } catch (IOException e) {
1626 throw new IllegalStateException("IOException in StringBuilderWriter", e);
1627 }
1628 return w.toString();
1629 }
1630
1631 public static void structuredMapToJson(Writer w, Map map, String jsonFormat) throws IOException {
1632 if (map==null) {
1633 w.append("null");
1634 return;
1635 }
1636
1637 Map.Entry entry;
1638
1639 Object key;
1640 String keyJson;
1641 Object value;
1642 List list = new ArrayList(map.keySet());
1643
1644 Collections.sort(list, new ListComparator());
1645 boolean isFirst = true;
1646
1647 w.append("{");
1648
1649 for (Iterator i = list.iterator(); i.hasNext();) {
1650 key = (Object) i.next();
1651 if (key instanceof String) {
1652 keyJson = "\"" + Text.escapeJavascript((String) key) + "\"";
1653 } else if (key instanceof Number) {
1654 keyJson = "\"" + String.valueOf(key) + "\"";
1655 } else {
1656 throw new IllegalArgumentException("Cannot convert key type " + key.getClass().getName() + " to javascript value");
1657 }
1658 value = map.get(key);
1659 if (key == null || key.equals("")) {
1660 continue;
1661 } else if (value == null) {
1662 continue;
1663 } else if (value instanceof String) {
1664 if (!isFirst) { w.append(","); }
1665 w.append(keyJson);
1666 w.append( ": \"");
1667 w.append(Text.escapeJavascript((String) value));
1668 w.append("\"");
1669 } else if (value instanceof WriteJsonFormat) {
1670 if (!isFirst) { w.append(","); }
1671 w.append(keyJson);
1672 w.append(": ");
1673 ((WriteJsonFormat) value).writeJsonFormat(w, jsonFormat);
1674 } else if (value instanceof ToJsonFormat) {
1675 if (!isFirst) { w.append(","); }
1676 w.append(keyJson);
1677 w.append(": ");
1678 w.append(((ToJsonFormat) value).toJson(jsonFormat));
1679 } else if (value instanceof ToJson) {
1680 if (!isFirst) { w.append(","); }
1681 w.append(keyJson);
1682 w.append(": ");
1683 w.append(((ToJson) value).toJson());
1684 } else if (value instanceof ToStringReturnsJson) {
1685 if (!isFirst) { w.append(","); }
1686 w.append(keyJson);
1687 w.append(": ");
1688 w.append(value.toString());
1689 } else if (value instanceof Map) {
1690 if (!isFirst) { w.append(","); }
1691 w.append(keyJson);
1692 w.append(": ");
1693 structuredMapToJson(w, (Map) value, jsonFormat);
1694 } else if (value instanceof List) {
1695 if (!isFirst) { w.append(","); }
1696 w.append(keyJson);
1697 w.append(": ");
1698 structuredListToJson(w, (List) value, jsonFormat);
1699 } else if (value instanceof Number) {
1700 if (!isFirst) { w.append(","); }
1701 w.append(keyJson);
1702 w.append(": ");
1703 w.append(value.toString());
1704 } else if (value instanceof Boolean) {
1705 if (!isFirst) { w.append(","); }
1706 w.append(keyJson);
1707 w.append(": ");
1708 w.append(value.toString());
1709 } else if (value instanceof java.util.Date) {
1710
1711
1712 if (!isFirst) { w.append(","); }
1713 w.append(keyJson);
1714 w.append(": ");
1715 w.append(toDate((java.util.Date)value, jsonFormat));
1716 } else if (value.getClass().isArray()) {
1717
1718 if (value instanceof Object[]) {
1719 List arrayList = Arrays.asList((Object[])value);
1720 if (!isFirst) { w.append(","); }
1721 w.append(keyJson);
1722 w.append(": ");
1723 structuredListToJson(w, (List)arrayList, jsonFormat);
1724 } else if (value instanceof double[]) {
1725
1726
1727 double[] daSrc = (double[]) value;
1728 Double[] daTgt = new Double[daSrc.length];
1729 for (int j=0; j<daSrc.length; j++) { daTgt[j]=daSrc[j]; }
1730 List arrayList = Arrays.asList((Object[])daTgt);
1731 if (!isFirst) { w.append(","); }
1732 w.append(keyJson);
1733 w.append(": ");
1734 structuredListToJson(w, (List)arrayList, jsonFormat);
1735 } else if (value instanceof int[]) {
1736
1737
1738 int[] daSrc = (int[]) value;
1739 Integer[] daTgt = new Integer[daSrc.length];
1740 for (int j=0; j<daSrc.length; j++) { daTgt[j]=daSrc[j]; }
1741 List arrayList = Arrays.asList((Object[])daTgt);
1742 if (!isFirst) { w.append(","); }
1743 w.append(keyJson);
1744 w.append(": ");
1745 structuredListToJson(w, (List)arrayList, jsonFormat);
1746 } else if (value instanceof long[]) {
1747 long[] daSrc = (long[]) value;
1748 Long[] daTgt = new Long[daSrc.length];
1749 for (int j=0; j<daSrc.length; j++) { daTgt[j]=daSrc[j]; }
1750 List arrayList = Arrays.asList((Object[])daTgt);
1751 if (!isFirst) { w.append(","); }
1752 w.append(keyJson);
1753 w.append(": ");
1754 structuredListToJson(w, (List)arrayList, jsonFormat);
1755 } else if (value instanceof byte[]) {
1756 byte[] daSrc = (byte[]) value;
1757 Byte[] daTgt = new Byte[daSrc.length];
1758 for (int j=0; j<daSrc.length; j++) { daTgt[j]=daSrc[j]; }
1759 List arrayList = Arrays.asList((Object[])daTgt);
1760 if (!isFirst) { w.append(","); }
1761 w.append(keyJson);
1762 w.append(": ");
1763 structuredListToJson(w, (List)arrayList, jsonFormat);
1764
1765 } else {
1766 throw new UnsupportedOperationException("Cannot convert primitive array to JSON");
1767 }
1768 } else {
1769 throw new RuntimeException("Cannot translate Java object " + value.getClass().getName() + " to javascript value");
1770 }
1771 isFirst = false;
1772 }
1773
1774 w.append("}\n");
1775
1776 }
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788 public static String structuredListToFilteredJson(List list, String jsonFormat, String...validKeys) {
1789 StringBuilderWriter w = new StringBuilderWriter(list.size() * 2);
1790 try {
1791 structuredListToFilteredJson(w, list, jsonFormat, validKeys);
1792 } catch (IOException e) {
1793 throw new IllegalStateException("IOException in StringBuilderWriter", e);
1794 }
1795 return w.toString();
1796 }
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807 public static String structuredMapToFilteredJson(Map map, String jsonFormat, String...validKeys) {
1808 StringBuilderWriter w = new StringBuilderWriter();
1809 try {
1810 structuredMapToFilteredJson(w, map, jsonFormat, validKeys);
1811 } catch (IOException e) {
1812 throw new IllegalStateException("IOException in StringBuilderWriter", e);
1813 }
1814 return w.toString();
1815 }
1816
1817
1818 public static void structuredListToFilteredJson(Writer w, List list, String jsonFormat, String... validKeys) throws IOException {
1819 Object value;
1820 int index = 0;
1821 w.append('[');
1822 for (Iterator i = list.iterator(); i.hasNext(); ) {
1823 value = i.next();
1824 if (value == null) {
1825 w.append("null");
1826 } else if (value instanceof String) {
1827 w.append('\"').append(Text.escapeJavascript((String) value)).append('\"');
1828 } else if (value instanceof ToJsonFormat) {
1829 w.append(((ToJsonFormat) value).toJson(jsonFormat));
1830 } else if (value instanceof ToJson) {
1831 w.append(((ToJson) value).toJson());
1832 } else if (value instanceof ToStringReturnsJson) {
1833 w.append(value.toString());
1834 } else if (value instanceof Map) {
1835 structuredMapToFilteredJson(w, (Map) value, jsonFormat, validKeys);
1836 } else if (value instanceof List) {
1837 structuredListToFilteredJson(w, (List) value, jsonFormat, validKeys);
1838 } else if (value instanceof Number) {
1839 w.append(value.toString());
1840 } else if (value instanceof Boolean) {
1841 w.append(value.toString());
1842 } else if (value instanceof java.util.Date) {
1843
1844
1845 w.append(Struct.toDate((java.util.Date)value, jsonFormat));
1846 } else if (value.getClass().isArray()) {
1847 if (value instanceof Object[]) {
1848 List arrayList = Arrays.asList((Object[])value);
1849 structuredListToFilteredJson(w, (List)arrayList, jsonFormat, validKeys);
1850 } else if (value instanceof double[]) {
1851
1852
1853 double[] daSrc = (double[]) value;
1854 Double[] daTgt = new Double[daSrc.length];
1855 for (int j=0; j<daSrc.length; j++) { daTgt[j]=daSrc[j]; }
1856 List arrayList = Arrays.asList((Object[])daTgt);
1857 structuredListToFilteredJson(w, (List) arrayList, jsonFormat, validKeys);
1858 } else if (value instanceof int[]) {
1859 int[] daSrc = (int[]) value;
1860 Integer[] daTgt = new Integer[daSrc.length];
1861 for (int j=0; j<daSrc.length; j++) { daTgt[j]=daSrc[j]; }
1862 List arrayList = Arrays.asList((Object[])daTgt);
1863 structuredListToFilteredJson(w, (List) arrayList, jsonFormat, validKeys);
1864 } else {
1865 throw new UnsupportedOperationException("Cannot convert primitive array to JSON");
1866 }
1867 } else {
1868 throw new RuntimeException("Cannot translate Java object " +
1869 value.getClass().getName() + " to javascript value");
1870 }
1871 index = index + 1;
1872 if (i.hasNext()) {
1873 w.append(',');
1874 }
1875 }
1876 w.append("]");
1877 }
1878
1879 public static void structuredMapToFilteredJson(Writer w, Map map, String jsonFormat, String... validKeys) throws IOException {
1880
1881 String keyJson = null;
1882 Object value;
1883
1884
1885
1886
1887
1888 boolean isFirst = true;
1889
1890 w.append("{");
1891
1892 for (String key : validKeys) {
1893
1894 value = map.get(key);
1895 if (value != null) {
1896 if (key instanceof String) {
1897 keyJson = "\"" + key + "\"";
1898 } else {
1899 throw new IllegalArgumentException("Cannot convert key type " + key.getClass().getName() + " to javascript value");
1900 }
1901 }
1902 if (key == null || key.equals("")) {
1903 continue;
1904 } else if (value == null) {
1905 continue;
1906 } else if (value instanceof String) {
1907 if (!isFirst) { w.append(","); }
1908 w.append(keyJson);
1909 w.append( ": \"");
1910 w.append(Text.escapeJavascript((String) value));
1911 w.append("\"");
1912 } else if (value instanceof ToJsonFormat) {
1913 if (!isFirst) { w.append(","); }
1914 w.append(keyJson);
1915 w.append(": ");
1916 w.append(((ToJsonFormat) value).toJson(jsonFormat));
1917 } else if (value instanceof ToJson) {
1918 if (!isFirst) { w.append(","); }
1919 w.append(keyJson);
1920 w.append(": ");
1921 w.append(((ToJson) value).toJson());
1922 } else if (value instanceof ToStringReturnsJson) {
1923 if (!isFirst) { w.append(","); }
1924 w.append(keyJson);
1925 w.append(": ");
1926 w.append(value.toString());
1927 } else if (value instanceof Map) {
1928 if (!isFirst) { w.append(","); }
1929 w.append(keyJson);
1930 w.append(": ");
1931 structuredMapToFilteredJson(w, (Map) value, jsonFormat, validKeys);
1932 } else if (value instanceof List) {
1933 if (!isFirst) { w.append(","); }
1934 w.append(keyJson);
1935 w.append(": ");
1936 structuredListToFilteredJson(w, (List) value, jsonFormat, validKeys);
1937 } else if (value instanceof Number) {
1938 if (!isFirst) { w.append(","); }
1939 w.append(keyJson);
1940 w.append(": ");
1941 w.append(value.toString());
1942 } else if (value instanceof Boolean) {
1943 if (!isFirst) { w.append(","); }
1944 w.append(keyJson);
1945 w.append(": ");
1946 w.append(value.toString());
1947 } else if (value instanceof java.util.Date) {
1948
1949
1950 if (!isFirst) { w.append(","); }
1951 w.append(keyJson);
1952 w.append(": ");
1953 w.append(Struct.toDate((java.util.Date)value, jsonFormat));
1954 } else if (value.getClass().isArray()) {
1955
1956 if (value instanceof Object[]) {
1957 List arrayList = Arrays.asList((Object[])value);
1958 if (!isFirst) { w.append(","); }
1959 w.append(keyJson);
1960 w.append(": ");
1961 structuredListToFilteredJson(w, (List)arrayList, jsonFormat, validKeys);
1962 } else if (value instanceof double[]) {
1963
1964
1965 double[] daSrc = (double[]) value;
1966 Double[] daTgt = new Double[daSrc.length];
1967 for (int j=0; j<daSrc.length; j++) { daTgt[j]=daSrc[j]; }
1968 List arrayList = Arrays.asList((Object[])daTgt);
1969 if (!isFirst) { w.append(","); }
1970 w.append(keyJson);
1971 w.append(": ");
1972 structuredListToFilteredJson(w, (List)arrayList, jsonFormat, validKeys);
1973 } else if (value instanceof int[]) {
1974
1975
1976 int[] daSrc = (int[]) value;
1977 Integer[] daTgt = new Integer[daSrc.length];
1978 for (int j=0; j<daSrc.length; j++) { daTgt[j]=daSrc[j]; }
1979 List arrayList = Arrays.asList((Object[])daTgt);
1980 if (!isFirst) { w.append(","); }
1981 w.append(keyJson);
1982 w.append(": ");
1983 structuredListToFilteredJson(w, (List)arrayList, jsonFormat, validKeys);
1984 } else {
1985 throw new UnsupportedOperationException("Cannot convert primitive array to JSON");
1986 }
1987 } else {
1988 throw new RuntimeException("Cannot translate Java object " + value.getClass().getName() + " to javascript value");
1989 }
1990 isFirst = false;
1991 }
1992
1993 w.append("}");
1994 }
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010 static public String toDate(Date d, String jsonFormat) {
2011 if (jsonFormat==null || jsonFormat.equals("microsoft")) {
2012 return "\"\\/Date(" + d.getTime() + ")\\/\"";
2013 } else {
2014 return String.valueOf(d.getTime());
2015 }
2016 }
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037 static public Map<String, Object> newStructuredMap(Object... keyValuePairs) {
2038 if (keyValuePairs.length%2==1) { throw new IllegalArgumentException("keyValuePairs must have even number of elements"); }
2039 Map<String, Object> map = new HashMap();
2040 for (int i=0; i<keyValuePairs.length; i+=2) {
2041 String key = (String) keyValuePairs[i];
2042 map.put(key, keyValuePairs[i + 1]);
2043 }
2044 return map;
2045 }
2046
2047
2048
2049
2050
2051
2052
2053
2054 public static void renameStructuredListColumn(List<Map<String, Object>> rows, String srcColumnName, String destColumnName) {
2055 if (srcColumnName == null) { throw new NullPointerException("null srcColumnName"); }
2056 if (destColumnName == null) { throw new NullPointerException("null destColumnName"); }
2057 if (srcColumnName.equals(destColumnName)) { return; }
2058
2059 for (int i=0; i<rows.size(); i++) {
2060 Map<String,Object> row = rows.get(i);
2061 if (row.containsKey(srcColumnName)) {
2062 row.put(destColumnName, row.get(srcColumnName));
2063 row.remove(srcColumnName);
2064 }
2065 }
2066 }
2067
2068
2069
2070
2071
2072
2073
2074
2075 public static void addStructuredListColumn(List<Map<String, Object>> rows, String newColumnName, Object value) {
2076 for (int i=0; i<rows.size(); i++) {
2077 Map<String,Object> row = rows.get(i);
2078 row.put(newColumnName, value);
2079 }
2080 }
2081
2082 static {
2083
2084
2085 gettersCache = new ConcurrentHashMap();
2086 settersCache = new ConcurrentHashMap();
2087 }
2088 }