View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.renderkit.html;
21  
22  import org.apache.myfaces.tobago.internal.context.DateTimeI18n;
23  
24  import java.util.Map;
25  
26  public class JsonUtils {
27  
28    private JsonUtils() {
29    }
30  
31    private static void encode(final StringBuilder builder, final String name, final String[] value) {
32      builder.append("\"");
33      builder.append(name);
34      builder.append("\":");
35      encode(builder, value);
36      builder.append(",");
37    }
38  
39    public static void encode(StringBuilder builder, String[] value) {
40      builder.append("[");
41      boolean colon = false;
42      for (final String item : value) {
43        if (colon) {
44          builder.append(",");
45        }
46        builder.append("\"");
47        builder.append(item);
48        builder.append("\"");
49        colon = true;
50      }
51      builder.append("]");
52    }
53  
54    static void encode(final StringBuilder builder, final String name, final Boolean value) {
55      builder.append("\"");
56      builder.append(name);
57      builder.append("\":");
58      builder.append(Boolean.toString(value));
59      builder.append(",");
60    }
61  
62    static void encode(final StringBuilder builder, final String name, final Integer value) {
63      builder.append("\"");
64      builder.append(name);
65      builder.append("\":");
66      builder.append(Integer.toString(value));
67      builder.append(",");
68    }
69  
70    static void encode(final StringBuilder builder, final String name, String value) {
71      value = value.replaceAll("\\\"", "\\\\\\\""); // todo: optimize
72      builder.append("\"");
73      builder.append(name);
74      builder.append("\":\"");
75      builder.append(value);
76      builder.append("\",");
77    }
78  
79    public static String encode(final CommandMap commandMap) {
80      final StringBuilder builder = new StringBuilder();
81      builder.append("{");
82      final int initialLength = builder.length();
83  
84      final Command click = commandMap.getClick();
85      if (click != null) {
86        encode(builder, "click", click);
87      }
88  
89      final Map<String, Command> other = commandMap.getOther();
90      if (other != null) {
91        for(final Map.Entry<String, Command> entry : other.entrySet()) {
92          encode(builder, entry.getKey(), entry.getValue());
93        }
94      }
95  
96      if (builder.length() - initialLength > 0) {
97        assert builder.charAt(builder.length() - 1) == ',';
98        builder.deleteCharAt(builder.length() - 1);
99      }
100 
101     builder.append("}");
102     return builder.toString();
103   }
104 
105   static void encode(final StringBuilder builder, final String name, final Command command) {
106     builder.append("\"");
107     builder.append(name);
108     builder.append("\":{");
109     final int initialLength = builder.length();
110 
111     final String action = command.getAction();
112     if (action != null) {
113       encode(builder, "action", action);
114     }
115     final Boolean transition = command.getTransition();
116     if (transition != null && !transition) { // true is the default, so encoding is needed.
117       encode(builder, "transition", transition);
118     }
119     final String target = command.getTarget();
120     if (target != null) {
121       encode(builder, "target", target);
122     }
123     final String url = command.getUrl();
124     if (url != null) {
125       encode(builder, "url", url);
126     }
127     final String[] partially = command.getPartially();
128     if (partially != null && partially.length > 0) {
129       if (partially.length == 1) {
130         encode(builder, "partially", partially[0]);
131       } else {
132         encode(builder, "partially", partially);
133       }
134     }
135     final String focus = command.getFocus();
136     if (focus != null) {
137       encode(builder, "focus", focus);
138     }
139     final String confirmation = command.getConfirmation();
140     if (confirmation != null) {
141       encode(builder, "confirmation", confirmation);
142     }
143     final Integer delay  = command.getDelay();
144     if (delay != null) {
145       encode(builder, "delay", delay);
146     }
147     final Popup popup = command.getPopup();
148     if (popup != null) {
149       encode(builder, "popup", popup);
150     }
151     final String script = command.getScript();
152     if (script != null) {
153       encode(builder, "script", script);
154     }
155     final Boolean omit = command.getOmit();
156     if (omit != null && omit) { // false is the default, so encoding is needed.
157       encode(builder, "omit", omit);
158     }
159 
160     if (builder.length() - initialLength > 0) {
161       assert builder.charAt(builder.length() - 1) == ',';
162       builder.deleteCharAt(builder.length() - 1);
163     }
164 
165     builder.append("},");
166   }
167 
168   static void encode(final StringBuilder builder, final String name, final Popup popup) {
169     builder.append("\"");
170     builder.append(name);
171     builder.append("\":{");
172     final int initialLength = builder.length();
173 
174     final String command = popup.getCommand();
175     if (command != null) {
176       encode(builder, "command", command);
177     }
178     final Boolean immediate = popup.isImmediate();
179     if (immediate != null) {
180       encode(builder, "immediate", immediate);
181     }
182     if (builder.length() - initialLength > 0) {
183       assert builder.charAt(builder.length() - 1) == ',';
184       builder.deleteCharAt(builder.length() - 1);
185     }
186 
187     builder.append("},");
188   }
189 
190   public static String encode(final DateTimeI18n dateTimeI18n) {
191     final StringBuilder builder = new StringBuilder();
192     builder.append("{");
193     final int initialLength = builder.length();
194 
195     encode(builder, "monthNames", dateTimeI18n.getMonthNames());
196     encode(builder, "monthNamesShort", dateTimeI18n.getMonthNamesShort());
197     encode(builder, "dayNames", dateTimeI18n.getDayNames());
198     encode(builder, "dayNamesShort", dateTimeI18n.getDayNamesShort());
199     encode(builder, "dayNamesMin", dateTimeI18n.getDayNamesMin());
200     encode(builder, "firstDay", dateTimeI18n.getFirstDay());
201 
202     if (builder.length() - initialLength > 0) {
203       assert builder.charAt(builder.length() - 1) == ',';
204       builder.deleteCharAt(builder.length() - 1);
205     }
206     builder.append("}");
207     return builder.toString();
208   }
209 
210   public static String encode(final String[] strings) {
211     if (strings == null) {
212       return null;
213     }
214     final StringBuilder builder = new StringBuilder();
215     encode(builder, strings);
216     return builder.toString();
217   }
218 }