]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.server.io/src/org/simantics/document/server/io/JSONObjectUtils.java
Make Write-interfaces as @FunctionalInterface for lambdas
[simantics/platform.git] / bundles / org.simantics.document.server.io / src / org / simantics / document / server / io / JSONObjectUtils.java
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
9  *
10  * Contributors:
11  *     Semantum Oy - initial API and implementation
12  *******************************************************************************/
13 package org.simantics.document.server.io;
14
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.Collections;
18 import java.util.List;
19
20 public class JSONObjectUtils {
21     
22     public static String getText(IJSONObject object) {
23         return getValueOrDefault(object, "text", "");
24     }
25     
26     public static String getLink(IJSONObject object) {
27         return getValueOrDefault(object, "link", "");
28     }
29     
30     public static String getTarget(IJSONObject object) {
31         return getValueOrDefault(object, "target", "");
32     }
33     
34     public static String getHyperLinkTarget(IJSONObject object){
35         return getValueOrDefault(object, "hyperlinkTarget", "");
36     }
37     
38     public static String getWidth(IJSONObject object) {
39         return getValueOrDefault(object, "width", "");
40     }
41     
42     public static String getHeight(IJSONObject object) {
43         return getValueOrDefault(object, "height", "");
44     }
45
46     public static String getParent(IJSONObject object) {
47         return object.getValue("parent");
48     }
49     
50     public static boolean enabled(IJSONObject object) {
51         return getValueOrDefault(object, "enabled", true);
52     }
53     
54     public static boolean visible(IJSONObject object) {
55         return getValueOrDefault(object, "visible", true);
56     }
57     
58     public static boolean readOnly(IJSONObject object) {
59         return getValueOrDefault(object, "readOnly", false);
60     }
61     
62     public static boolean selected(IJSONObject object) {
63         return getValueOrDefault(object, "selected", false);
64     }
65
66     public static boolean followEditMode(IJSONObject object) {
67         return getValueOrDefault(object, "followEditMode", true);
68     }
69
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);
73         return result;
74     }
75
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);
79         return result;
80     }
81     
82     public static <T> T getValueOrDefault(IJSONObject object, String key, T defaultValue) {
83         try {
84                 T value = (T)object.getValue(key);
85                 if(value != null) return value;
86         } catch (ClassCastException e) {
87             e.printStackTrace();
88         }
89         return defaultValue;        
90     }
91     
92     public static String[] getStringArrayValueOrDefault(IJSONObject object, String key, String[] defaultValue) {
93         try {
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[]) {
100                 return (String[]) o;
101             }
102         } catch (ClassCastException e) {
103             e.printStackTrace();
104         }
105         return defaultValue;
106     }
107
108     public static float[] getFloatArrayValueOrDefault(IJSONObject object, String key, float[] defaultValue) {
109         try {
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()];
115                 int pos = 0;
116                 for (float value : list) {
117                         result[pos++] = value;
118                 }
119                 return result;
120             } else if (o instanceof float[]) {
121                 return (float[]) o;
122             }
123         } catch (ClassCastException e) {
124             e.printStackTrace();
125         }
126         return defaultValue;
127     }
128
129     public static int[] getIntArrayValueOrDefault(IJSONObject object, String key, int[] defaultValue) {
130         try {
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()];
136                 int pos = 0;
137                 for (int i : list) {
138                         result[pos++] = i;
139                 }
140                 return result;
141             } else if (o instanceof int[]) {
142                 return (int[]) o;
143             }
144         } catch (ClassCastException e) {
145             e.printStackTrace();
146         }
147         return defaultValue;
148     }
149
150     public static double[] getDoubleArrayValueOrDefault(IJSONObject object, String key, double[] defaultValue) {
151         try {
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()];
157                 int pos = 0;
158                 for (double value : list) {
159                         result[pos++] = value;
160                 }
161                 return result;
162             } else if (o instanceof double[]) {
163                 return (double[]) o;
164             }
165         } catch (ClassCastException e) {
166             e.printStackTrace();
167         }
168         return defaultValue;
169     }
170     
171 //    public static String[] getObjectArrayValue(IJSONObject object, String key) {
172 //        try {
173 //            String value = (String)object.getValue(key);
174 //            if(value != null && value.startsWith("[") && value.endsWith("]")) {
175 //              
176 //              if(value.length() == 2) return new String[0];
177 //              
178 //                value = value.substring(1, value.length() - 1);
179 //                String[] split = value.split(",");
180 //                
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);
184 //                }
185 //                
186 //                return split;
187 //            }
188 //        } catch (ClassCastException e) {
189 //            e.printStackTrace();
190 //            return null;
191 //        }
192 //        
193 //        return null;
194 //    }
195
196     public static Collection<ICommand> getCommands(IJSONObject object) {
197         try {
198                 Collection<ICommand> result = object.getValue("commands");
199                 if(result != null) return result;
200         } catch (ClassCastException e) {
201             e.printStackTrace();
202         }
203         return Collections.emptyList();
204     }
205     
206     @SuppressWarnings("unchecked")
207     public static Collection<IChartItem> getChartItems(IJSONObject object) {
208         try {
209             Object values = object.getValue("values");
210             if (values == null)
211                 return Collections.emptyList();
212             if (values instanceof String) {
213                 String valuess = (String) values;
214                 if (valuess.length() == 0)
215                     return Collections.emptyList();
216             }
217             return (List<IChartItem>) values;
218         } catch (ClassCastException e) {
219             e.printStackTrace();
220         }
221         
222         return Collections.emptyList();
223     }
224     
225 //    public static TaskSeriesCollection getGanttTasks(IJSONObject object) {
226 //        TaskSeriesCollection collection = new TaskSeriesCollection();
227 //        try {
228 //            
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);
234 //                
235 //                TaskSeries series = new TaskSeries(getStringValueOrNull(seriesObject, "name"));
236 //                
237 //                //JSONArray tasksArray = seriesObject.getJSONArray("tasks");
238 //                @SuppressWarnings("unchecked")
239 //                List<IJSONObject> tasksArray = (List<IJSONObject>) object.getValue("tasks");
240 //                
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");
246 //                    
247 ////                    System.out.println("Task: " + name + ", start: " + start + ", end: " + end);
248 //                    
249 //                    if(start >= 0 && end >= 0 && start < end) {
250 //                        Task task = new Task(name,  new Date(start.intValue()), new Date(end.intValue()));
251 //                        
252 //                        try {
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);
266 //                                }
267 //                            }
268 //                        } catch (ClassCastException e) {
269 //                            // Some tasks may not have subtasks
270 //                            e.printStackTrace();
271 //                        }
272 //                        
273 //                        series.add(task);
274 //                    }
275 //                }
276 //                
277 //                collection.add(series);
278 //            }
279 //        } catch (ClassCastException e) {
280 //            e.printStackTrace();
281 //        }
282 //        return collection;
283 //    }
284     
285     @SuppressWarnings("unchecked")
286     public static Collection<IDataDefinition> getDataDefinitions(IJSONObject object) {
287         try {
288                 Collection<IDataDefinition> result = (List<IDataDefinition>) object.getValue("dataDefinitions");
289                 if(result != null) return result;
290         } catch (ClassCastException e) {
291             e.printStackTrace();
292         }
293         return Collections.emptyList();
294     }
295     
296     @SuppressWarnings("unchecked")
297     public static Collection<ITableCell> getTableCell(IJSONObject object) {
298         try {
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();
304             }
305             if (tableCells != null) {
306                 return (List<ITableCell>) tableCells;
307             } else {
308                 return Collections.emptyList();
309             }
310         } catch (ClassCastException e) {
311             e.printStackTrace();
312         }
313         return Collections.emptyList();
314     }
315     
316     public static Collection<FileInfo> getFiles(IJSONObject object) {
317         try {
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));
329                 }
330             }
331             return fileList;
332         } catch (ClassCastException e) {
333             e.printStackTrace();
334         }
335         
336         return Collections.emptyList();
337     }
338
339     public static List<IListItem> getListItems(IJSONObject object) {
340         Object items =  object.getValue("listItems");
341         if (items instanceof List<?>)
342             return (List<IListItem>) items;
343         else
344             return Collections.<IListItem>emptyList();
345     }
346 }