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      boolean colon = false;
36      for (final String item : value) {
37        if (colon) {
38          builder.append(",");
39        }
40        builder.append("\"");
41        builder.append(item);
42        builder.append("\"");
43        colon = true;
44      }
45      builder.append("],");
46    }
47  
48    static void encode(final StringBuilder builder, final String name, final Boolean value) {
49      builder.append("\"");
50      builder.append(name);
51      builder.append("\":");
52      builder.append(Boolean.toString(value));
53      builder.append(",");
54    }
55  
56    static void encode(final StringBuilder builder, final String name, final Integer value) {
57      builder.append("\"");
58      builder.append(name);
59      builder.append("\":");
60      builder.append(Integer.toString(value));
61      builder.append(",");
62    }
63  
64    static void encode(final StringBuilder builder, final String name, String value) {
65      value = value.replaceAll("\\\"", "\\\\\\\"");
66      builder.append("\"");
67      builder.append(name);
68      builder.append("\":\"");
69      builder.append(value);
70      builder.append("\",");
71    }
72  
73    public static String encode(final CommandMap commandMap) {
74      final StringBuilder builder = new StringBuilder();
75      builder.append("{");
76      final int initialLength = builder.length();
77  
78      final Command click = commandMap.getClick();
79      if (click != null) {
80        encode(builder, "click", click);
81      }
82  
83      final Map<String, Command> other = commandMap.getOther();
84      if (other != null) {
85        for(final Map.Entry<String, Command> entry : other.entrySet()) {
86          encode(builder, entry.getKey(), entry.getValue());
87        }
88      }
89  
90      if (builder.length() - initialLength > 0) {
91        assert builder.charAt(builder.length() - 1) == ',';
92        builder.deleteCharAt(builder.length() - 1);
93      }
94  
95      builder.append("}");
96      return builder.toString();
97    }
98  
99    static void encode(final StringBuilder builder, final String name, final Command command) {
100     builder.append("\"");
101     builder.append(name);
102     builder.append("\":{");
103     final int initialLength = builder.length();
104 
105     final String action = command.getAction();
106     if (action != null) {
107       encode(builder, "action", action);
108     }
109     final Boolean transition = command.getTransition();
110     if (transition != null && !transition) { // true is the default, so encoding is needed.
111       encode(builder, "transition", transition);
112     }
113     final String target = command.getTarget();
114     if (target != null) {
115       encode(builder, "target", target);
116     }
117     final String url = command.getUrl();
118     if (url != null) {
119       encode(builder, "url", url);
120     }
121     final String[] partially = command.getPartially();
122     if (partially != null && partially.length > 0) {
123       if (partially.length == 1) {
124         encode(builder, "partially", partially[0]);
125       } else {
126         encode(builder, "partially", partially);
127       }
128     }
129     final String focus = command.getFocus();
130     if (focus != null) {
131       encode(builder, "focus", focus);
132     }
133     final String confirmation = command.getConfirmation();
134     if (confirmation != null) {
135       encode(builder, "confirmation", confirmation);
136     }
137     final Integer delay  = command.getDelay();
138     if (delay != null) {
139       encode(builder, "delay", delay);
140     }
141     final Popup popup = command.getPopup();
142     if (popup != null) {
143       encode(builder, "popup", popup);
144     }
145     final String script = command.getScript();
146     if (script != null) {
147       encode(builder, "script", script);
148     }
149     final Boolean omit = command.getOmit();
150     if (omit != null && omit) { // false is the default, so encoding is needed.
151       encode(builder, "omit", omit);
152     }
153 
154     if (builder.length() - initialLength > 0) {
155       assert builder.charAt(builder.length() - 1) == ',';
156       builder.deleteCharAt(builder.length() - 1);
157     }
158 
159     builder.append("},");
160   }
161 
162   static void encode(final StringBuilder builder, final String name, final Popup popup) {
163     builder.append("\"");
164     builder.append(name);
165     builder.append("\":{");
166     final int initialLength = builder.length();
167 
168     final String command = popup.getCommand();
169     if (command != null) {
170       encode(builder, "command", command);
171     }
172     final Boolean immediate = popup.isImmediate();
173     if (immediate != null) {
174       encode(builder, "immediate", immediate);
175     }
176     if (builder.length() - initialLength > 0) {
177       assert builder.charAt(builder.length() - 1) == ',';
178       builder.deleteCharAt(builder.length() - 1);
179     }
180 
181     builder.append("},");
182   }
183 
184   public static String encode(final DateTimeI18n dateTimeI18n) {
185     final StringBuilder builder = new StringBuilder();
186     builder.append("{");
187     final int initialLength = builder.length();
188 
189     encode(builder, "monthNames", dateTimeI18n.getMonthNames());
190     encode(builder, "monthNamesShort", dateTimeI18n.getMonthNamesShort());
191     encode(builder, "dayNames", dateTimeI18n.getDayNames());
192     encode(builder, "dayNamesShort", dateTimeI18n.getDayNamesShort());
193     encode(builder, "dayNamesMin", dateTimeI18n.getDayNamesMin());
194     encode(builder, "firstDay", dateTimeI18n.getFirstDay());
195 
196     if (builder.length() - initialLength > 0) {
197       assert builder.charAt(builder.length() - 1) == ',';
198       builder.deleteCharAt(builder.length() - 1);
199     }
200     builder.append("}");
201     return builder.toString();
202   }
203 }