]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.export.core/src/org/simantics/export/core/util/ExporterUtils.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.export.core / src / org / simantics / export / core / util / ExporterUtils.java
1 package org.simantics.export.core.util;\r
2 \r
3 import java.util.ArrayList;\r
4 import java.util.Collection;\r
5 import java.util.Collections;\r
6 import java.util.Comparator;\r
7 import java.util.List;\r
8 \r
9 import org.osgi.service.prefs.BackingStoreException;\r
10 import org.osgi.service.prefs.Preferences;\r
11 import org.simantics.databoard.Accessors;\r
12 import org.simantics.databoard.Bindings;\r
13 import org.simantics.databoard.accessor.Accessor;\r
14 import org.simantics.databoard.accessor.RecordAccessor;\r
15 import org.simantics.databoard.accessor.UnionAccessor;\r
16 import org.simantics.databoard.accessor.error.AccessorConstructionException;\r
17 import org.simantics.databoard.accessor.error.AccessorException;\r
18 import org.simantics.databoard.accessor.error.ReferenceException;\r
19 import org.simantics.databoard.accessor.reference.ChildReference;\r
20 import org.simantics.databoard.accessor.reference.LabelReference;\r
21 import org.simantics.databoard.binding.Binding;\r
22 import org.simantics.databoard.binding.error.BindingException;\r
23 import org.simantics.databoard.binding.mutable.Variant;\r
24 import org.simantics.databoard.type.Component;\r
25 import org.simantics.databoard.type.UnionType;\r
26 import org.simantics.export.core.ExportContext;\r
27 import org.simantics.export.core.ExportExtensionPoint;\r
28 import org.simantics.export.core.error.ExportException;\r
29 import org.simantics.export.core.intf.Exporter;\r
30 import org.simantics.export.core.intf.Publisher;\r
31 import org.simantics.export.core.manager.Content;\r
32 import org.simantics.utils.datastructures.MapList;\r
33 import org.simantics.utils.strings.AlphanumComparator;\r
34 \r
35 public class ExporterUtils {\r
36 \r
37         /**\r
38          * Gets publisher location options from options variant.\r
39          * \r
40          * @param ctx\r
41          * @param publisherId\r
42          * @param options\r
43          * @return publisher options or null\r
44          * @throws ExportException\r
45          */\r
46         public static Variant getPublisherLocationOptions(ExportContext ctx, String publisherId, Variant options) throws ExportException {\r
47                 try {                   \r
48                         Publisher publisher = ctx.eep.getPublisher(publisherId);\r
49                         Variant locationOptions = options.getComponent( new LabelReference( publisher.label() ) );\r
50                         return locationOptions;\r
51                 } catch (AccessorConstructionException e) {\r
52                         throw new ExportException( e ); \r
53                 }\r
54         }\r
55         \r
56         /**\r
57          * Checks if key exists in a preference node\r
58          * \r
59          * @param pref\r
60          * @param key\r
61          * @return true if key exists\r
62          */\r
63         public static boolean containsKey(Preferences pref, String key) {\r
64                 try {\r
65                         for (String x : pref.keys()) if ( x.equals(key) ) return true;\r
66                 } catch (BackingStoreException e) {\r
67                         e.printStackTrace();\r
68                 } \r
69                 return false;           \r
70         }\r
71         \r
72         /**\r
73          * Get value of a key, in a preference node.\r
74          * \r
75          * @param pref (optional)\r
76          * @param key key\r
77          * @return if pref and value exists, the value.\r
78          */\r
79         public static String getPrefString(Preferences pref, String key) {\r
80                 if ( pref == null ) return null;\r
81                 if ( !containsKey(pref, key) ) return null;\r
82                 return pref.get(key, null);\r
83         }\r
84 \r
85         /**\r
86          * Get value first from pref1, then pref2\r
87          * @param pref1 (optional) pririty preference node\r
88          * @param pref2 (optional) 2nd priority node\r
89          * @param key \r
90          * @return possible value\r
91          */\r
92         public static String getPrefString(Preferences pref1, Preferences pref2, String key) {\r
93                 if ( pref1 != null && containsKey(pref1, key) ) return pref1.get(key, null);\r
94                 if ( pref2 != null && containsKey(pref2, key) ) return pref2.get(key, null);\r
95                 return null;\r
96         }\r
97 \r
98         /**\r
99          * Get value first from pref1, then pref2\r
100          * @param pref1 (optional) pririty preference node\r
101          * @param pref2 (optional) 2nd priority node\r
102          * @param key \r
103          * @return possible value\r
104          */\r
105         public static Integer getPrefInteger(Preferences pref1, Preferences pref2, String key) {\r
106                 if ( pref1 != null && containsKey(pref1, key) ) return pref1.getInt(key, -1);\r
107                 if ( pref2 != null && containsKey(pref2, key) ) return pref2.getInt(key, -1);\r
108                 return null;\r
109         }\r
110 \r
111         /**\r
112          * Get value first from pref1, then pref2\r
113          * @param pref1 (optional) pririty preference node\r
114          * @param pref2 (optional) 2nd priority node\r
115          * @param key \r
116          * @return possible value\r
117          */\r
118         public static Boolean getPrefBoolean(Preferences pref1, Preferences pref2, String key) {\r
119                 if ( pref1 != null && containsKey(pref1, key) ) return pref1.getBoolean(key, false);\r
120                 if ( pref2 != null && containsKey(pref2, key) ) return pref2.getBoolean(key, false);\r
121                 return null;\r
122         }\r
123         \r
124         /**\r
125          * Get value first from pref1, then pref2\r
126          * @param pref1 (optional) pririty preference node\r
127          * @param pref2 (optional) 2nd priority node\r
128          * @param key \r
129          * @return possible value\r
130          */\r
131         public static Double getPrefDouble(Preferences pref1, Preferences pref2, String key) {\r
132                 if ( pref1 != null && containsKey(pref1, key) ) return pref1.getDouble(key, 0.0);\r
133                 if ( pref2 != null && containsKey(pref2, key) ) return pref2.getDouble(key, 0.0);\r
134                 return null;\r
135         }\r
136         \r
137         public static void setPrefString(Preferences pref, String key, String value) {\r
138                 if ( pref == null ) return;\r
139                 if ( value == null ) {\r
140                         pref.remove(key); \r
141                 } else {\r
142                         pref.put(key, value);\r
143                 }\r
144         }\r
145         \r
146         public static void setPrefInt(Preferences pref, String key, Integer value) {\r
147                 if ( pref == null ) return;\r
148                 if ( value == null ) {\r
149                         pref.remove(key); \r
150                 } else {\r
151                         pref.putInt(key, value);\r
152                 }\r
153         }\r
154         \r
155         public static void setPrefdouble(Preferences pref, String key, Double value) {\r
156                 if ( pref == null ) return;\r
157                 if ( value == null ) {\r
158                         pref.remove(key); \r
159                 } else {\r
160                         pref.putDouble(key, value);\r
161                 }\r
162         }\r
163 \r
164         /**\r
165          * Set enum or union value to an accessor, using the string name of the enum value.\r
166          * If value doesn't exist, false is returned.\r
167          * If tagName doesn't exist, false is returned.\r
168          * \r
169          * @param ra accessor\r
170          * @param ref child reference\r
171          * @param tagName (optional) the tag name\r
172          * @return true if value was set\r
173          */\r
174         public static boolean setUnionValue(Accessor ra, ChildReference ref, String tagName) {\r
175                 if ( tagName == null ) return false;\r
176         try {                   \r
177                 UnionAccessor ua = ra.getComponent( ref );\r
178                 UnionType ut = ua.type();               \r
179                 int tag = ut.getComponentIndex2( tagName );\r
180                 if ( tag<0 ) return false;\r
181                 Component c = ut.components[tag];\r
182                 Binding cb = Bindings.getMutableBinding(c.type);\r
183                 Object o = cb.createDefault();\r
184                 ua.setComponentValue( tag, cb, o);\r
185                 return true;\r
186         } catch (ReferenceException re) {               \r
187                 return false;\r
188         } catch (AccessorConstructionException e) {\r
189                 e.printStackTrace();\r
190                 return false;\r
191                 } catch (AccessorException e) {\r
192                         e.printStackTrace();\r
193                 return false;\r
194                 } catch (BindingException e) {\r
195                         e.printStackTrace();\r
196                 return false;\r
197                 }                                               \r
198         }\r
199 \r
200         /**\r
201          * Set enum or union value to an accessor, using the string name of the enum value.\r
202          * If value doesn't exist, false is returned.\r
203          * If tagName doesn't exist, false is returned.\r
204          * \r
205          * @param ra accessor\r
206          * @param ref child reference\r
207          * @param tag the tag number (applied only if >=0)\r
208          * @return true if value was set\r
209          */\r
210         public static boolean setUnionValue(Accessor ra, ChildReference ref, int tag) {\r
211                 if ( tag<0 ) return false;\r
212         try {                   \r
213                 UnionAccessor ua = ra.getComponent( ref );\r
214                 UnionType ut = ua.type();               \r
215                 Component c = ut.components[tag];\r
216                 Binding cb = Bindings.getMutableBinding(c.type);\r
217                 Object o = cb.createDefault();\r
218                 ua.setComponentValue( tag, cb, o);\r
219                 return true;\r
220         } catch (ReferenceException re) {               \r
221                 return false;\r
222         } catch (AccessorConstructionException e) {\r
223                 e.printStackTrace();\r
224                 return false;\r
225                 } catch (AccessorException e) {\r
226                         e.printStackTrace();\r
227                 return false;\r
228                 } catch (BindingException e) {\r
229                         e.printStackTrace();\r
230                 return false;\r
231                 }                                               \r
232         }\r
233         \r
234         \r
235         /**\r
236          * Get union value as a String\r
237          * \r
238          * @param ra\r
239          * @param ref\r
240          * @return the union value as string or null if error occured\r
241          */\r
242         public static String getUnionValue(Accessor ra, ChildReference ref) {\r
243         try {                   \r
244                 UnionAccessor ua = ra.getComponent( ref );\r
245                 UnionType ut = ua.type();\r
246                 int tag = ua.getTag();\r
247                 return ut.components[tag].name;\r
248         } catch (ReferenceException re) {               \r
249                 return null;\r
250         } catch (AccessorConstructionException e) {\r
251                 e.printStackTrace();\r
252                 return null;\r
253                 } catch (AccessorException e) {\r
254                         e.printStackTrace();\r
255                 return null;\r
256                 }                                               \r
257                 \r
258         }\r
259 \r
260         /**\r
261          * \r
262          * @param ra\r
263          * @param ref\r
264          * @return tag or -1\r
265          */\r
266         public static int getUnionInt(Accessor ra, ChildReference ref) {\r
267         try {                   \r
268                 UnionAccessor ua = ra.getComponent( ref );\r
269                 int tag = ua.getTag();\r
270                 return tag;\r
271         } catch (ReferenceException re) {               \r
272                 return -1;\r
273         } catch (AccessorConstructionException e) {\r
274                 e.printStackTrace();\r
275                 return -1;\r
276                 } catch (AccessorException e) {\r
277                         e.printStackTrace();\r
278                 return -1;\r
279                 }                                               \r
280                 \r
281         }\r
282         \r
283         /**\r
284          * Read string value from an location\r
285          * \r
286          * @param location\r
287          * @param ref\r
288          * @return the value or null, if did not exist\r
289          */\r
290         public static String getString(Variant location, ChildReference ref) {\r
291                 try {\r
292                         RecordAccessor ra = Accessors.getAccessor(location);\r
293                         return (String) ra.getValue( ref, Bindings.STRING );\r
294                 } catch (AccessorConstructionException e) {\r
295                         return null;\r
296                 } catch (AccessorException e) {\r
297                         return null;\r
298                 }                                       \r
299         }\r
300         \r
301         public static Boolean getBoolean(Variant location, ChildReference ref) {\r
302                 try {\r
303                         RecordAccessor ra = Accessors.getAccessor(location);\r
304                         return (Boolean) ra.getValue( ref, Bindings.BOOLEAN );\r
305                 } catch (AccessorConstructionException e) {\r
306                         return null;\r
307                 } catch (AccessorException e) {\r
308                         return null;\r
309                 }                                       \r
310         }\r
311         \r
312         public static Comparator<Content> createExportPriorityComparator( final ExportExtensionPoint eep ) {\r
313                 return new Comparator<Content>() {\r
314 \r
315                         @Override\r
316                         public int compare(Content c1, Content c2) {\r
317                                 \r
318                                 int p1 = Integer.MAX_VALUE;\r
319                                 for (Exporter e : eep.getExporters(c1.formatId, c1.contentTypeId)) \r
320                                         p1 = Math.min(p1, e.getExportPriority());\r
321                                 \r
322                                 int p2 = Integer.MAX_VALUE;\r
323                                 for (Exporter e : eep.getExporters(c2.formatId, c2.contentTypeId)) \r
324                                         p2 = Math.min(p2, e.getExportPriority());\r
325                                 \r
326                                 return Integer.signum( p1 - p2 );\r
327                         }\r
328                         \r
329                 };              \r
330         }\r
331         \r
332         /**\r
333          * Takes a maplist of contents as input and filters out all that matches\r
334          * to the given uri.  \r
335          * \r
336          * @param map\r
337          * @param contentUri\r
338          * @return map with contents of given Uri.\r
339          */\r
340         public static MapList<Content, Content> filterByUri(MapList<Content, Content> map, String contentUri) {\r
341                 if ( map == null ) return null;\r
342                 MapList<Content, Content> result = new MapList<Content, Content>();\r
343                 for ( Content key : map.getKeys() ) {                                           \r
344                         if ( key.url.equals(contentUri) ) {\r
345                                 result.addAll(key, map.getValuesUnsafe(key));\r
346                         }\r
347                 }\r
348                 return result;\r
349         }\r
350         \r
351         public static MapList<String, Content> toStringMap(MapList<Content, Content> map) {\r
352                 if ( map == null ) return null;\r
353                 MapList<String, Content> result = new MapList<String, Content>();\r
354                 for ( Content key : map.getKeys() ) {\r
355                         result.addAll(key.url, map.getValuesUnsafe(key));\r
356                 }               \r
357                 return result;\r
358         }\r
359         \r
360         public static List<Content> filterContents(Collection<Content> contents, String contentTypeId, String formatId) {\r
361                 if ( contents==null ) return null;\r
362                 List<Content> result = new ArrayList<Content>(contents.size());\r
363                 for ( Content content : contents ) {\r
364                         if ( contentTypeId!=null && !content.contentTypeId.equals(contentTypeId) ) continue;\r
365                         if ( formatId!=null && !content.formatId.equals(formatId) ) continue;\r
366                         result.add( content );\r
367                 }\r
368                 return result;\r
369         }\r
370         \r
371         public static List<String> toUris(Collection<Content> contents) {\r
372                 List<String> result = new ArrayList<String>(contents.size());\r
373                 for ( Content content : contents ) result.add( content.url );           \r
374                 return result;\r
375         }       \r
376 \r
377         public static Comparator<Content> CONTENT_COMPARATOR = new Comparator<Content>() {\r
378                 @Override\r
379                 public int compare(Content o1, Content o2) {\r
380                         int c = AlphanumComparator.CASE_INSENSITIVE_COMPARATOR.compare(o1.url, o2.url);\r
381                         if (c != 0)\r
382                                 return c;\r
383                         c = AlphanumComparator.CASE_INSENSITIVE_COMPARATOR.compare(o1.formatId, o2.formatId);\r
384                         if (c != 0)\r
385                                 return c;\r
386                         return 0;\r
387                 }\r
388         };\r
389 \r
390         public static List<Content> sortContent(List<Content> content) {\r
391                 Collections.sort(content, CONTENT_COMPARATOR);\r
392                 return content;\r
393         }\r
394 \r
395         public static List<Content> sortedContent(List<Content> content) {\r
396                 ArrayList<Content> sorted = new ArrayList<Content>(content);\r
397                 sortContent(sorted);\r
398                 return sorted;\r
399         }\r
400 \r
401 }\r