1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.myfaces.tobago.renderkit.html;
21
22 import java.util.Map;
23
24 public class JsonUtils {
25
26 private JsonUtils() {
27 }
28
29 private static void encode(StringBuilder builder, String name, String[] value) {
30 builder.append("\"");
31 builder.append(name);
32 builder.append("\":\"");
33 boolean colon = false;
34 for (String item : value) {
35 if (colon) {
36 builder.append(",");
37 }
38 builder.append(item);
39 colon = true;
40 }
41 builder.append("\",");
42 }
43
44 static void encode(StringBuilder builder, String name, Boolean value) {
45 builder.append("\"");
46 builder.append(name);
47 builder.append("\":");
48 builder.append(Boolean.toString(value));
49 builder.append(",");
50 }
51
52 static void encode(StringBuilder builder, String name, Integer value) {
53 builder.append("\"");
54 builder.append(name);
55 builder.append("\":");
56 builder.append(Integer.toString(value));
57 builder.append(",");
58 }
59
60 static void encode(StringBuilder builder, String name, String value) {
61 value = value.replaceAll("\\\"", "\\\\\\\"");
62 builder.append("\"");
63 builder.append(name);
64 builder.append("\":\"");
65 builder.append(value);
66 builder.append("\",");
67 }
68
69 public static String encode(CommandMap commandMap) {
70 StringBuilder builder = new StringBuilder();
71 builder.append("{");
72 int initialLength = builder.length();
73
74 Command click = commandMap.getClick();
75 if (click != null) {
76 encode(builder, "click", click);
77 }
78
79 final Map<String, Command> other = commandMap.getOther();
80 if (other != null) {
81 for(Map.Entry<String, Command> entry : other.entrySet()) {
82 encode(builder, entry.getKey(), entry.getValue());
83 }
84 }
85
86 if (builder.length() - initialLength > 0) {
87 assert builder.charAt(builder.length() - 1) == ',';
88 builder.deleteCharAt(builder.length() - 1);
89 }
90
91 builder.append("}");
92 return builder.toString();
93 }
94
95 static void encode(StringBuilder builder, String name, Command command) {
96 builder.append("\"");
97 builder.append(name);
98 builder.append("\":{");
99 int initialLength = builder.length();
100
101 String action = command.getAction();
102 if (action != null) {
103 encode(builder, "action", action);
104 }
105 Boolean transition = command.getTransition();
106 if (transition != null && !transition) {
107 encode(builder, "transition", transition);
108 }
109 String target = command.getTarget();
110 if (target != null) {
111 encode(builder, "target", target);
112 }
113 String url = command.getUrl();
114 if (url != null) {
115 encode(builder, "url", url);
116 }
117 String[] partially = command.getPartially();
118 if (partially != null && partially.length > 0) {
119 encode(builder, "partially", partially);
120 }
121 String focus = command.getFocus();
122 if (focus != null) {
123 encode(builder, "focus", focus);
124 }
125 String confirmation = command.getConfirmation();
126 if (confirmation != null) {
127 encode(builder, "confirmation", confirmation);
128 }
129 Integer delay = command.getDelay();
130 if (delay != null) {
131 encode(builder, "delay", delay);
132 }
133 Popup popup = command.getPopup();
134 if (popup != null) {
135 encode(builder, "popup", popup);
136 }
137 String script = command.getScript();
138 if (script != null) {
139 encode(builder, "script", script);
140 }
141
142 if (builder.length() - initialLength > 0) {
143 assert builder.charAt(builder.length() - 1) == ',';
144 builder.deleteCharAt(builder.length() - 1);
145 }
146
147 builder.append("},");
148 }
149
150 static void encode(StringBuilder builder, String name, Popup popup) {
151 builder.append("\"");
152 builder.append(name);
153 builder.append("\":{");
154 int initialLength = builder.length();
155
156 String command = popup.getCommand();
157 if (command != null) {
158 encode(builder, "command", command);
159 }
160 Boolean immediate = popup.isImmediate();
161 if (immediate != null) {
162 encode(builder, "immediate", immediate);
163 }
164 if (builder.length() - initialLength > 0) {
165 assert builder.charAt(builder.length() - 1) == ',';
166 builder.deleteCharAt(builder.length() - 1);
167 }
168
169 builder.append("},");
170 }
171
172 }