1 package org.simantics.export.core.util;
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Collections;
6 import java.util.Comparator;
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;
35 public class ExporterUtils {
38 * Gets publisher location options from options variant.
43 * @return publisher options or null
44 * @throws ExportException
46 public static Variant getPublisherLocationOptions(ExportContext ctx, String publisherId, Variant options) throws ExportException {
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 );
57 * Checks if key exists in a preference node
61 * @return true if key exists
63 public static boolean containsKey(Preferences pref, String key) {
65 for (String x : pref.keys()) if ( x.equals(key) ) return true;
66 } catch (BackingStoreException e) {
73 * Get value of a key, in a preference node.
75 * @param pref (optional)
77 * @return if pref and value exists, the value.
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);
86 * Get value first from pref1, then pref2
87 * @param pref1 (optional) pririty preference node
88 * @param pref2 (optional) 2nd priority node
90 * @return possible value
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);
99 * Get value first from pref1, then pref2
100 * @param pref1 (optional) pririty preference node
101 * @param pref2 (optional) 2nd priority node
103 * @return possible value
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);
112 * Get value first from pref1, then pref2
113 * @param pref1 (optional) pririty preference node
114 * @param pref2 (optional) 2nd priority node
116 * @return possible value
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);
125 * Get value first from pref1, then pref2
126 * @param pref1 (optional) pririty preference node
127 * @param pref2 (optional) 2nd priority node
129 * @return possible value
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);
137 public static void setPrefString(Preferences pref, String key, String value) {
138 if ( pref == null ) return;
139 if ( value == null ) {
142 pref.put(key, value);
146 public static void setPrefInt(Preferences pref, String key, Integer value) {
147 if ( pref == null ) return;
148 if ( value == null ) {
151 pref.putInt(key, value);
155 public static void setPrefdouble(Preferences pref, String key, Double value) {
156 if ( pref == null ) return;
157 if ( value == null ) {
160 pref.putDouble(key, value);
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.
170 * @param ref child reference
171 * @param tagName (optional) the tag name
172 * @return true if value was set
174 public static boolean setUnionValue(Accessor ra, ChildReference ref, String tagName) {
175 if ( tagName == null ) return false;
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);
186 } catch (ReferenceException re) {
188 } catch (AccessorConstructionException e) {
191 } catch (AccessorException e) {
194 } catch (BindingException e) {
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.
206 * @param ref child reference
207 * @param tag the tag number (applied only if >=0)
208 * @return true if value was set
210 public static boolean setUnionValue(Accessor ra, ChildReference ref, int tag) {
211 if ( tag<0 ) return false;
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);
220 } catch (ReferenceException re) {
222 } catch (AccessorConstructionException e) {
225 } catch (AccessorException e) {
228 } catch (BindingException e) {
236 * Get union value as a String
240 * @return the union value as string or null if error occured
242 public static String getUnionValue(Accessor ra, ChildReference ref) {
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) {
250 } catch (AccessorConstructionException e) {
253 } catch (AccessorException e) {
266 public static int getUnionInt(Accessor ra, ChildReference ref) {
268 UnionAccessor ua = ra.getComponent( ref );
269 int tag = ua.getTag();
271 } catch (ReferenceException re) {
273 } catch (AccessorConstructionException e) {
276 } catch (AccessorException e) {
284 * Read string value from an location
288 * @return the value or null, if did not exist
290 public static String getString(Variant location, ChildReference ref) {
292 RecordAccessor ra = Accessors.getAccessor(location);
293 return (String) ra.getValue( ref, Bindings.STRING );
294 } catch (AccessorConstructionException e) {
296 } catch (AccessorException e) {
301 public static Boolean getBoolean(Variant location, ChildReference ref) {
303 RecordAccessor ra = Accessors.getAccessor(location);
304 return (Boolean) ra.getValue( ref, Bindings.BOOLEAN );
305 } catch (AccessorConstructionException e) {
307 } catch (AccessorException e) {
312 public static Comparator<Content> createExportPriorityComparator( final ExportExtensionPoint eep ) {
313 return new Comparator<Content>() {
316 public int compare(Content c1, Content c2) {
318 int p1 = Integer.MAX_VALUE;
319 for (Exporter e : eep.getExporters(c1.formatId, c1.contentTypeId))
320 p1 = Math.min(p1, e.getExportPriority());
322 int p2 = Integer.MAX_VALUE;
323 for (Exporter e : eep.getExporters(c2.formatId, c2.contentTypeId))
324 p2 = Math.min(p2, e.getExportPriority());
326 return Integer.signum( p1 - p2 );
333 * Takes a maplist of contents as input and filters out all that matches
338 * @return map with contents of given Uri.
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));
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));
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 );
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 );
377 public static Comparator<Content> CONTENT_COMPARATOR = new Comparator<Content>() {
379 public int compare(Content o1, Content o2) {
380 int c = AlphanumComparator.CASE_INSENSITIVE_COMPARATOR.compare(o1.url, o2.url);
383 c = AlphanumComparator.CASE_INSENSITIVE_COMPARATOR.compare(o1.formatId, o2.formatId);
390 public static List<Content> sortContent(List<Content> content) {
391 Collections.sort(content, CONTENT_COMPARATOR);
395 public static List<Content> sortedContent(List<Content> content) {
396 ArrayList<Content> sorted = new ArrayList<Content>(content);