1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.uif.util;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.rice.core.api.util.type.TypeUtils;
20
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Set;
24
25
26
27
28
29
30 public class ScriptUtils {
31
32
33
34
35
36
37
38
39
40
41
42
43
44 public static String translateValue(Object value) {
45 String jsValue = "";
46
47 if (value == null) {
48 jsValue = "null";
49 return jsValue;
50 }
51
52 if (value instanceof List) {
53 jsValue = "[";
54
55 List<Object> list = (List<Object>) value;
56 for (Object listItem : list) {
57 jsValue += translateValue(listItem);
58 jsValue += ",";
59 }
60 jsValue = StringUtils.removeEnd(jsValue, ",");
61
62 jsValue += "]";
63 } else if (value instanceof Set) {
64 jsValue = "[";
65
66 Set<Object> set = (Set<Object>) value;
67 for (Object setItem : set) {
68 jsValue += translateValue(setItem);
69 jsValue += ",";
70 }
71 jsValue = StringUtils.removeEnd(jsValue, ",");
72
73 jsValue += "]";
74 } else if (value instanceof Map) {
75 jsValue = "{";
76
77 Map<Object, Object> map = (Map<Object, Object>) value;
78 for (Map.Entry<Object, Object> mapEntry : map.entrySet()) {
79 jsValue += mapEntry.getKey().toString() + ":";
80 jsValue += translateValue(mapEntry.getValue());
81 jsValue += ",";
82 }
83 jsValue = StringUtils.removeEnd(jsValue, ",");
84
85 jsValue += "}";
86 } else {
87 boolean quoteValue = true;
88
89 Class<?> valueClass = value.getClass();
90 if (TypeUtils.isBooleanClass(valueClass) || TypeUtils.isDecimalClass(valueClass) || TypeUtils
91 .isIntegralClass(valueClass)) {
92 quoteValue = false;
93 }
94
95 if (quoteValue) {
96 jsValue = "\"";
97 }
98
99
100 jsValue += value.toString();
101
102 if (quoteValue) {
103 jsValue += "\"";
104 }
105 }
106
107 return jsValue;
108 }
109
110
111
112
113
114
115
116 public static String escapeName(String name) {
117 name = name.replace("'", "\\'");
118 return name;
119 }
120 }