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 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) { // true is the default, so encoding is needed.
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     Boolean omit = command.getOmit();
142     if (omit != null && omit) { // false is the default, so encoding is needed.
143       encode(builder, "omit", omit);
144     }
145 
146     if (builder.length() - initialLength > 0) {
147       assert builder.charAt(builder.length() - 1) == ',';
148       builder.deleteCharAt(builder.length() - 1);
149     }
150 
151     builder.append("},");
152   }
153 
154   static void encode(StringBuilder builder, String name, Popup popup) {
155     builder.append("\"");
156     builder.append(name);
157     builder.append("\":{");
158     int initialLength = builder.length();
159 
160     String command = popup.getCommand();
161     if (command != null) {
162       encode(builder, "command", command);
163     }
164     Boolean immediate = popup.isImmediate();
165     if (immediate != null) {
166       encode(builder, "immediate", immediate);
167     }
168     if (builder.length() - initialLength > 0) {
169       assert builder.charAt(builder.length() - 1) == ',';
170       builder.deleteCharAt(builder.length() - 1);
171     }
172 
173     builder.append("},");
174   }
175 
176 }