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