1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 Association for Decentralized
3 * Information Management in Industry THTH ry.
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the THTH Simantics
6 * Division Member Component License which accompanies this
7 * distribution, and is available at
8 * http://www.simantics.org/legal/sdmcl-v10.html
11 * Semantum Oy - initial API and implementation
12 *******************************************************************************/
13 package org.simantics.document.server.io;
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.Collections;
18 import java.util.List;
20 public class JSONObjectUtils {
22 public static String getText(IJSONObject object) {
23 return getValueOrDefault(object, "text", "");
26 public static String getLink(IJSONObject object) {
27 return getValueOrDefault(object, "link", "");
30 public static String getTarget(IJSONObject object) {
31 return getValueOrDefault(object, "target", "");
34 public static String getHyperLinkTarget(IJSONObject object){
35 return getValueOrDefault(object, "hyperlinkTarget", "");
38 public static String getWidth(IJSONObject object) {
39 return getValueOrDefault(object, "width", "");
42 public static String getHeight(IJSONObject object) {
43 return getValueOrDefault(object, "height", "");
46 public static String getParent(IJSONObject object) {
47 return object.getValue("parent");
50 public static boolean enabled(IJSONObject object) {
51 return getValueOrDefault(object, "enabled", true);
54 public static boolean visible(IJSONObject object) {
55 return getValueOrDefault(object, "visible", true);
58 public static boolean readOnly(IJSONObject object) {
59 return getValueOrDefault(object, "readOnly", false);
62 public static boolean selected(IJSONObject object) {
63 return getValueOrDefault(object, "selected", false);
66 public static boolean followEditMode(IJSONObject object) {
67 return getValueOrDefault(object, "followEditMode", true);
70 public static String getType(IJSONObject object) {
71 String result = (String)object.getValue("type");
72 if(result == null) throw new IllegalStateException("No type for object " + object);
76 public static String getId(IJSONObject object) {
77 String result = (String)object.getValue("id");
78 if(result == null) throw new IllegalStateException("No type for object " + object);
82 public static <T> T getValueOrDefault(IJSONObject object, String key, T defaultValue) {
84 T value = (T)object.getValue(key);
85 if(value != null) return value;
86 } catch (ClassCastException e) {
92 public static String[] getStringArrayValueOrDefault(IJSONObject object, String key, String[] defaultValue) {
94 Object o = object.getValue(key);
95 if (o instanceof List) {
96 @SuppressWarnings("unchecked")
97 List<String> s = (List<String>) o;
98 return s.toArray(new String[s.size()]);
99 } else if (o instanceof String[]) {
102 } catch (ClassCastException e) {
108 public static float[] getFloatArrayValueOrDefault(IJSONObject object, String key, float[] defaultValue) {
110 Object o = object.getValue(key);
111 if (o instanceof List) {
112 @SuppressWarnings("unchecked")
113 List<Float> list = (List<Float>) o;
114 float[] result = new float[list.size()];
116 for (float value : list) {
117 result[pos++] = value;
120 } else if (o instanceof float[]) {
123 } catch (ClassCastException e) {
129 public static int[] getIntArrayValueOrDefault(IJSONObject object, String key, int[] defaultValue) {
131 Object o = object.getValue(key);
132 if (o instanceof List) {
133 @SuppressWarnings("unchecked")
134 List<Integer> list = (List<Integer>) o;
135 int[] result = new int[list.size()];
141 } else if (o instanceof int[]) {
144 } catch (ClassCastException e) {
150 public static double[] getDoubleArrayValueOrDefault(IJSONObject object, String key, double[] defaultValue) {
152 Object o = object.getValue(key);
153 if (o instanceof List) {
154 @SuppressWarnings("unchecked")
155 List<Double> list = (List<Double>) o;
156 double[] result = new double[list.size()];
158 for (double value : list) {
159 result[pos++] = value;
162 } else if (o instanceof double[]) {
165 } catch (ClassCastException e) {
171 // public static String[] getObjectArrayValue(IJSONObject object, String key) {
173 // String value = (String)object.getValue(key);
174 // if(value != null && value.startsWith("[") && value.endsWith("]")) {
176 // if(value.length() == 2) return new String[0];
178 // value = value.substring(1, value.length() - 1);
179 // String[] split = value.split(",");
181 // for(int i = 0; i < split.length; i++) {
182 // if(split[i].startsWith("\"") && split[i].endsWith("\""))
183 // split[i] = split[i].substring(1, split[i].length() - 1);
188 // } catch (ClassCastException e) {
189 // e.printStackTrace();
196 public static Collection<ICommand> getCommands(IJSONObject object) {
198 Collection<ICommand> result = object.getValue("commands");
199 if(result != null) return result;
200 } catch (ClassCastException e) {
203 return Collections.emptyList();
206 @SuppressWarnings("unchecked")
207 public static Collection<IChartItem> getChartItems(IJSONObject object) {
209 Object values = object.getValue("values");
211 return Collections.emptyList();
212 if (values instanceof String) {
213 String valuess = (String) values;
214 if (valuess.length() == 0)
215 return Collections.emptyList();
217 return (List<IChartItem>) values;
218 } catch (ClassCastException e) {
222 return Collections.emptyList();
225 // public static TaskSeriesCollection getGanttTasks(IJSONObject object) {
226 // TaskSeriesCollection collection = new TaskSeriesCollection();
229 // //JSONArray seriesArray = object.getJSONArray("ganttSeries");
230 // @SuppressWarnings("unchecked")
231 // List<IJSONObject> seriesArray = (List<IJSONObject>) object.getValue("ganttSeries");
232 // for(IJSONObject seriesObject : seriesArray) {
233 // //IJSONObject seriesObject = seriesArray.getJSONObject(i);
235 // TaskSeries series = new TaskSeries(getStringValueOrNull(seriesObject, "name"));
237 // //JSONArray tasksArray = seriesObject.getJSONArray("tasks");
238 // @SuppressWarnings("unchecked")
239 // List<IJSONObject> tasksArray = (List<IJSONObject>) object.getValue("tasks");
241 // for(IJSONObject taskObject : tasksArray) {
242 // //IJSONObject taskObject = tasksArray.getJSONObject(j);
243 // String name = getStringValueOrNull(taskObject, "name");
244 // Double start = getDoubleValueOrNull(taskObject, "start");
245 // Double end = getDoubleValueOrNull(taskObject, "end");
247 //// System.out.println("Task: " + name + ", start: " + start + ", end: " + end);
249 // if(start >= 0 && end >= 0 && start < end) {
250 // Task task = new Task(name, new Date(start.intValue()), new Date(end.intValue()));
253 // //JSONArray subtasksArray = taskObject.getJSONArray("subtasks");
254 // @SuppressWarnings("unchecked")
255 // List<IJSONObject> subtasksArray = (List<IJSONObject>) object.getValue("subtasks");
256 //// System.out.println("\tFound " + subtasksArray.length() + " subtasks");
257 // for(IJSONObject subtaskObject : subtasksArray) {
258 // //IJSONObject subtaskObject = subtasksArray.getJSONObject(s);
259 // String subtaskname = getStringValueOrNull(subtaskObject, "name");
260 // Double subtaskstart = getDoubleValueOrNull(subtaskObject, "start");
261 // Double subtaskend = getDoubleValueOrNull(subtaskObject, "end");
262 // if(subtaskstart >= 0 && subtaskend >= 0 && subtaskstart < subtaskend) {
263 // Task subtask = new Task(subtaskname + " subtask " + (subtasksArray.indexOf(subtaskObject)), new Date(subtaskstart.intValue()), new Date(subtaskend.intValue()));
264 // task.addSubtask(subtask);
265 //// System.out.println("\tSubTask: " + subtaskname + ", start: " + subtaskstart + ", end: " + subtaskend);
268 // } catch (ClassCastException e) {
269 // // Some tasks may not have subtasks
270 // e.printStackTrace();
277 // collection.add(series);
279 // } catch (ClassCastException e) {
280 // e.printStackTrace();
282 // return collection;
285 @SuppressWarnings("unchecked")
286 public static Collection<IDataDefinition> getDataDefinitions(IJSONObject object) {
288 Collection<IDataDefinition> result = (List<IDataDefinition>) object.getValue("dataDefinitions");
289 if(result != null) return result;
290 } catch (ClassCastException e) {
293 return Collections.emptyList();
296 @SuppressWarnings("unchecked")
297 public static Collection<ITableCell> getTableCell(IJSONObject object) {
299 Object tableCells = object.getValue("tableCells");
300 if (tableCells instanceof String) {
301 String tableCellsS = (String) tableCells;
302 if (tableCellsS.length() == 0)
303 return Collections.emptyList();
305 if (tableCells != null) {
306 return (List<ITableCell>) tableCells;
308 return Collections.emptyList();
310 } catch (ClassCastException e) {
313 return Collections.emptyList();
316 public static Collection<FileInfo> getFiles(IJSONObject object) {
318 @SuppressWarnings("unchecked")
319 List<IJSONObject> fileArray = (List<IJSONObject>) object.getValue("files");
320 ArrayList<FileInfo> fileList = new ArrayList<FileInfo>();
321 for(IJSONObject f : fileArray) {
322 String name = getValueOrDefault(f, "name", "");
323 String content = getValueOrDefault(f, "content", "");
324 Long resourceId = getValueOrDefault(f, "resourceId", 0L);
325 Long creationTimeMillis = getValueOrDefault(f, "creationTimeMillis", 0L);
326 boolean canModify = getValueOrDefault(f, "canModify", false);
327 if(name != null && content != null) {
328 fileList.add(new FileInfo(name, content, resourceId, creationTimeMillis, canModify));
332 } catch (ClassCastException e) {
336 return Collections.emptyList();
339 public static List<IListItem> getListItems(IJSONObject object) {
340 Object items = object.getValue("listItems");
341 if (items instanceof List<?>)
342 return (List<IListItem>) items;
344 return Collections.<IListItem>emptyList();