1 package org.simantics.export.core.util;
\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
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
35 public class ExporterUtils {
\r
38 * Gets publisher location options from options variant.
\r
41 * @param publisherId
\r
43 * @return publisher options or null
\r
44 * @throws ExportException
\r
46 public static Variant getPublisherLocationOptions(ExportContext ctx, String publisherId, Variant options) throws ExportException {
\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
57 * Checks if key exists in a preference node
\r
61 * @return true if key exists
\r
63 public static boolean containsKey(Preferences pref, String key) {
\r
65 for (String x : pref.keys()) if ( x.equals(key) ) return true;
\r
66 } catch (BackingStoreException e) {
\r
67 e.printStackTrace();
\r
73 * Get value of a key, in a preference node.
\r
75 * @param pref (optional)
\r
77 * @return if pref and value exists, the value.
\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
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
90 * @return possible value
\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
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
103 * @return possible value
\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
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
116 * @return possible value
\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
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
129 * @return possible value
\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
137 public static void setPrefString(Preferences pref, String key, String value) {
\r
138 if ( pref == null ) return;
\r
139 if ( value == null ) {
\r
142 pref.put(key, value);
\r
146 public static void setPrefInt(Preferences pref, String key, Integer value) {
\r
147 if ( pref == null ) return;
\r
148 if ( value == null ) {
\r
151 pref.putInt(key, value);
\r
155 public static void setPrefdouble(Preferences pref, String key, Double value) {
\r
156 if ( pref == null ) return;
\r
157 if ( value == null ) {
\r
160 pref.putDouble(key, value);
\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
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
174 public static boolean setUnionValue(Accessor ra, ChildReference ref, String tagName) {
\r
175 if ( tagName == null ) return false;
\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
186 } catch (ReferenceException re) {
\r
188 } catch (AccessorConstructionException e) {
\r
189 e.printStackTrace();
\r
191 } catch (AccessorException e) {
\r
192 e.printStackTrace();
\r
194 } catch (BindingException e) {
\r
195 e.printStackTrace();
\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
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
210 public static boolean setUnionValue(Accessor ra, ChildReference ref, int tag) {
\r
211 if ( tag<0 ) return false;
\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
220 } catch (ReferenceException re) {
\r
222 } catch (AccessorConstructionException e) {
\r
223 e.printStackTrace();
\r
225 } catch (AccessorException e) {
\r
226 e.printStackTrace();
\r
228 } catch (BindingException e) {
\r
229 e.printStackTrace();
\r
236 * Get union value as a String
\r
240 * @return the union value as string or null if error occured
\r
242 public static String getUnionValue(Accessor ra, ChildReference ref) {
\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
250 } catch (AccessorConstructionException e) {
\r
251 e.printStackTrace();
\r
253 } catch (AccessorException e) {
\r
254 e.printStackTrace();
\r
264 * @return tag or -1
\r
266 public static int getUnionInt(Accessor ra, ChildReference ref) {
\r
268 UnionAccessor ua = ra.getComponent( ref );
\r
269 int tag = ua.getTag();
\r
271 } catch (ReferenceException re) {
\r
273 } catch (AccessorConstructionException e) {
\r
274 e.printStackTrace();
\r
276 } catch (AccessorException e) {
\r
277 e.printStackTrace();
\r
284 * Read string value from an location
\r
288 * @return the value or null, if did not exist
\r
290 public static String getString(Variant location, ChildReference ref) {
\r
292 RecordAccessor ra = Accessors.getAccessor(location);
\r
293 return (String) ra.getValue( ref, Bindings.STRING );
\r
294 } catch (AccessorConstructionException e) {
\r
296 } catch (AccessorException e) {
\r
301 public static Boolean getBoolean(Variant location, ChildReference ref) {
\r
303 RecordAccessor ra = Accessors.getAccessor(location);
\r
304 return (Boolean) ra.getValue( ref, Bindings.BOOLEAN );
\r
305 } catch (AccessorConstructionException e) {
\r
307 } catch (AccessorException e) {
\r
312 public static Comparator<Content> createExportPriorityComparator( final ExportExtensionPoint eep ) {
\r
313 return new Comparator<Content>() {
\r
316 public int compare(Content c1, Content c2) {
\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
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
326 return Integer.signum( p1 - p2 );
\r
333 * Takes a maplist of contents as input and filters out all that matches
\r
334 * to the given uri.
\r
337 * @param contentUri
\r
338 * @return map with contents of given Uri.
\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
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
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
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
377 public static Comparator<Content> CONTENT_COMPARATOR = new Comparator<Content>() {
\r
379 public int compare(Content o1, Content o2) {
\r
380 int c = AlphanumComparator.CASE_INSENSITIVE_COMPARATOR.compare(o1.url, o2.url);
\r
383 c = AlphanumComparator.CASE_INSENSITIVE_COMPARATOR.compare(o1.formatId, o2.formatId);
\r
390 public static List<Content> sortContent(List<Content> content) {
\r
391 Collections.sort(content, CONTENT_COMPARATOR);
\r
395 public static List<Content> sortedContent(List<Content> content) {
\r
396 ArrayList<Content> sorted = new ArrayList<Content>(content);
\r
397 sortContent(sorted);
\r