]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - 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
diff --git a/bundles/org.simantics.export.core/src/org/simantics/export/core/util/ExporterUtils.java b/bundles/org.simantics.export.core/src/org/simantics/export/core/util/ExporterUtils.java
new file mode 100644 (file)
index 0000000..346c1e7
--- /dev/null
@@ -0,0 +1,401 @@
+package org.simantics.export.core.util;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+import java.util.Collections;\r
+import java.util.Comparator;\r
+import java.util.List;\r
+\r
+import org.osgi.service.prefs.BackingStoreException;\r
+import org.osgi.service.prefs.Preferences;\r
+import org.simantics.databoard.Accessors;\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.accessor.Accessor;\r
+import org.simantics.databoard.accessor.RecordAccessor;\r
+import org.simantics.databoard.accessor.UnionAccessor;\r
+import org.simantics.databoard.accessor.error.AccessorConstructionException;\r
+import org.simantics.databoard.accessor.error.AccessorException;\r
+import org.simantics.databoard.accessor.error.ReferenceException;\r
+import org.simantics.databoard.accessor.reference.ChildReference;\r
+import org.simantics.databoard.accessor.reference.LabelReference;\r
+import org.simantics.databoard.binding.Binding;\r
+import org.simantics.databoard.binding.error.BindingException;\r
+import org.simantics.databoard.binding.mutable.Variant;\r
+import org.simantics.databoard.type.Component;\r
+import org.simantics.databoard.type.UnionType;\r
+import org.simantics.export.core.ExportContext;\r
+import org.simantics.export.core.ExportExtensionPoint;\r
+import org.simantics.export.core.error.ExportException;\r
+import org.simantics.export.core.intf.Exporter;\r
+import org.simantics.export.core.intf.Publisher;\r
+import org.simantics.export.core.manager.Content;\r
+import org.simantics.utils.datastructures.MapList;\r
+import org.simantics.utils.strings.AlphanumComparator;\r
+\r
+public class ExporterUtils {\r
+\r
+       /**\r
+        * Gets publisher location options from options variant.\r
+        * \r
+        * @param ctx\r
+        * @param publisherId\r
+        * @param options\r
+        * @return publisher options or null\r
+        * @throws ExportException\r
+        */\r
+       public static Variant getPublisherLocationOptions(ExportContext ctx, String publisherId, Variant options) throws ExportException {\r
+               try {                   \r
+                       Publisher publisher = ctx.eep.getPublisher(publisherId);\r
+                       Variant locationOptions = options.getComponent( new LabelReference( publisher.label() ) );\r
+                       return locationOptions;\r
+               } catch (AccessorConstructionException e) {\r
+                       throw new ExportException( e ); \r
+               }\r
+       }\r
+       \r
+       /**\r
+        * Checks if key exists in a preference node\r
+        * \r
+        * @param pref\r
+        * @param key\r
+        * @return true if key exists\r
+        */\r
+       public static boolean containsKey(Preferences pref, String key) {\r
+               try {\r
+                       for (String x : pref.keys()) if ( x.equals(key) ) return true;\r
+               } catch (BackingStoreException e) {\r
+                       e.printStackTrace();\r
+               } \r
+               return false;           \r
+       }\r
+       \r
+       /**\r
+        * Get value of a key, in a preference node.\r
+        * \r
+        * @param pref (optional)\r
+        * @param key key\r
+        * @return if pref and value exists, the value.\r
+        */\r
+       public static String getPrefString(Preferences pref, String key) {\r
+               if ( pref == null ) return null;\r
+               if ( !containsKey(pref, key) ) return null;\r
+               return pref.get(key, null);\r
+       }\r
+\r
+       /**\r
+        * Get value first from pref1, then pref2\r
+        * @param pref1 (optional) pririty preference node\r
+        * @param pref2 (optional) 2nd priority node\r
+        * @param key \r
+        * @return possible value\r
+        */\r
+       public static String getPrefString(Preferences pref1, Preferences pref2, String key) {\r
+               if ( pref1 != null && containsKey(pref1, key) ) return pref1.get(key, null);\r
+               if ( pref2 != null && containsKey(pref2, key) ) return pref2.get(key, null);\r
+               return null;\r
+       }\r
+\r
+       /**\r
+        * Get value first from pref1, then pref2\r
+        * @param pref1 (optional) pririty preference node\r
+        * @param pref2 (optional) 2nd priority node\r
+        * @param key \r
+        * @return possible value\r
+        */\r
+       public static Integer getPrefInteger(Preferences pref1, Preferences pref2, String key) {\r
+               if ( pref1 != null && containsKey(pref1, key) ) return pref1.getInt(key, -1);\r
+               if ( pref2 != null && containsKey(pref2, key) ) return pref2.getInt(key, -1);\r
+               return null;\r
+       }\r
+\r
+       /**\r
+        * Get value first from pref1, then pref2\r
+        * @param pref1 (optional) pririty preference node\r
+        * @param pref2 (optional) 2nd priority node\r
+        * @param key \r
+        * @return possible value\r
+        */\r
+       public static Boolean getPrefBoolean(Preferences pref1, Preferences pref2, String key) {\r
+               if ( pref1 != null && containsKey(pref1, key) ) return pref1.getBoolean(key, false);\r
+               if ( pref2 != null && containsKey(pref2, key) ) return pref2.getBoolean(key, false);\r
+               return null;\r
+       }\r
+       \r
+       /**\r
+        * Get value first from pref1, then pref2\r
+        * @param pref1 (optional) pririty preference node\r
+        * @param pref2 (optional) 2nd priority node\r
+        * @param key \r
+        * @return possible value\r
+        */\r
+       public static Double getPrefDouble(Preferences pref1, Preferences pref2, String key) {\r
+               if ( pref1 != null && containsKey(pref1, key) ) return pref1.getDouble(key, 0.0);\r
+               if ( pref2 != null && containsKey(pref2, key) ) return pref2.getDouble(key, 0.0);\r
+               return null;\r
+       }\r
+       \r
+       public static void setPrefString(Preferences pref, String key, String value) {\r
+               if ( pref == null ) return;\r
+               if ( value == null ) {\r
+                       pref.remove(key); \r
+               } else {\r
+                       pref.put(key, value);\r
+               }\r
+       }\r
+       \r
+       public static void setPrefInt(Preferences pref, String key, Integer value) {\r
+               if ( pref == null ) return;\r
+               if ( value == null ) {\r
+                       pref.remove(key); \r
+               } else {\r
+                       pref.putInt(key, value);\r
+               }\r
+       }\r
+       \r
+       public static void setPrefdouble(Preferences pref, String key, Double value) {\r
+               if ( pref == null ) return;\r
+               if ( value == null ) {\r
+                       pref.remove(key); \r
+               } else {\r
+                       pref.putDouble(key, value);\r
+               }\r
+       }\r
+\r
+       /**\r
+        * Set enum or union value to an accessor, using the string name of the enum value.\r
+        * If value doesn't exist, false is returned.\r
+        * If tagName doesn't exist, false is returned.\r
+        * \r
+        * @param ra accessor\r
+        * @param ref child reference\r
+        * @param tagName (optional) the tag name\r
+        * @return true if value was set\r
+        */\r
+       public static boolean setUnionValue(Accessor ra, ChildReference ref, String tagName) {\r
+               if ( tagName == null ) return false;\r
+       try {                   \r
+               UnionAccessor ua = ra.getComponent( ref );\r
+               UnionType ut = ua.type();               \r
+               int tag = ut.getComponentIndex2( tagName );\r
+               if ( tag<0 ) return false;\r
+               Component c = ut.components[tag];\r
+               Binding cb = Bindings.getMutableBinding(c.type);\r
+               Object o = cb.createDefault();\r
+               ua.setComponentValue( tag, cb, o);\r
+               return true;\r
+       } catch (ReferenceException re) {               \r
+               return false;\r
+       } catch (AccessorConstructionException e) {\r
+               e.printStackTrace();\r
+               return false;\r
+               } catch (AccessorException e) {\r
+                       e.printStackTrace();\r
+               return false;\r
+               } catch (BindingException e) {\r
+                       e.printStackTrace();\r
+               return false;\r
+               }                                               \r
+       }\r
+\r
+       /**\r
+        * Set enum or union value to an accessor, using the string name of the enum value.\r
+        * If value doesn't exist, false is returned.\r
+        * If tagName doesn't exist, false is returned.\r
+        * \r
+        * @param ra accessor\r
+        * @param ref child reference\r
+        * @param tag the tag number (applied only if >=0)\r
+        * @return true if value was set\r
+        */\r
+       public static boolean setUnionValue(Accessor ra, ChildReference ref, int tag) {\r
+               if ( tag<0 ) return false;\r
+       try {                   \r
+               UnionAccessor ua = ra.getComponent( ref );\r
+               UnionType ut = ua.type();               \r
+               Component c = ut.components[tag];\r
+               Binding cb = Bindings.getMutableBinding(c.type);\r
+               Object o = cb.createDefault();\r
+               ua.setComponentValue( tag, cb, o);\r
+               return true;\r
+       } catch (ReferenceException re) {               \r
+               return false;\r
+       } catch (AccessorConstructionException e) {\r
+               e.printStackTrace();\r
+               return false;\r
+               } catch (AccessorException e) {\r
+                       e.printStackTrace();\r
+               return false;\r
+               } catch (BindingException e) {\r
+                       e.printStackTrace();\r
+               return false;\r
+               }                                               \r
+       }\r
+       \r
+       \r
+       /**\r
+        * Get union value as a String\r
+        * \r
+        * @param ra\r
+        * @param ref\r
+        * @return the union value as string or null if error occured\r
+        */\r
+       public static String getUnionValue(Accessor ra, ChildReference ref) {\r
+       try {                   \r
+               UnionAccessor ua = ra.getComponent( ref );\r
+               UnionType ut = ua.type();\r
+               int tag = ua.getTag();\r
+               return ut.components[tag].name;\r
+       } catch (ReferenceException re) {               \r
+               return null;\r
+       } catch (AccessorConstructionException e) {\r
+               e.printStackTrace();\r
+               return null;\r
+               } catch (AccessorException e) {\r
+                       e.printStackTrace();\r
+               return null;\r
+               }                                               \r
+               \r
+       }\r
+\r
+       /**\r
+        * \r
+        * @param ra\r
+        * @param ref\r
+        * @return tag or -1\r
+        */\r
+       public static int getUnionInt(Accessor ra, ChildReference ref) {\r
+       try {                   \r
+               UnionAccessor ua = ra.getComponent( ref );\r
+               int tag = ua.getTag();\r
+               return tag;\r
+       } catch (ReferenceException re) {               \r
+               return -1;\r
+       } catch (AccessorConstructionException e) {\r
+               e.printStackTrace();\r
+               return -1;\r
+               } catch (AccessorException e) {\r
+                       e.printStackTrace();\r
+               return -1;\r
+               }                                               \r
+               \r
+       }\r
+       \r
+       /**\r
+        * Read string value from an location\r
+        * \r
+        * @param location\r
+        * @param ref\r
+        * @return the value or null, if did not exist\r
+        */\r
+       public static String getString(Variant location, ChildReference ref) {\r
+               try {\r
+                       RecordAccessor ra = Accessors.getAccessor(location);\r
+                       return (String) ra.getValue( ref, Bindings.STRING );\r
+               } catch (AccessorConstructionException e) {\r
+                       return null;\r
+               } catch (AccessorException e) {\r
+                       return null;\r
+               }                                       \r
+       }\r
+       \r
+       public static Boolean getBoolean(Variant location, ChildReference ref) {\r
+               try {\r
+                       RecordAccessor ra = Accessors.getAccessor(location);\r
+                       return (Boolean) ra.getValue( ref, Bindings.BOOLEAN );\r
+               } catch (AccessorConstructionException e) {\r
+                       return null;\r
+               } catch (AccessorException e) {\r
+                       return null;\r
+               }                                       \r
+       }\r
+       \r
+       public static Comparator<Content> createExportPriorityComparator( final ExportExtensionPoint eep ) {\r
+               return new Comparator<Content>() {\r
+\r
+                       @Override\r
+                       public int compare(Content c1, Content c2) {\r
+                               \r
+                               int p1 = Integer.MAX_VALUE;\r
+                               for (Exporter e : eep.getExporters(c1.formatId, c1.contentTypeId)) \r
+                                       p1 = Math.min(p1, e.getExportPriority());\r
+                               \r
+                               int p2 = Integer.MAX_VALUE;\r
+                               for (Exporter e : eep.getExporters(c2.formatId, c2.contentTypeId)) \r
+                                       p2 = Math.min(p2, e.getExportPriority());\r
+                               \r
+                               return Integer.signum( p1 - p2 );\r
+                       }\r
+                       \r
+               };              \r
+       }\r
+       \r
+       /**\r
+        * Takes a maplist of contents as input and filters out all that matches\r
+        * to the given uri.  \r
+        * \r
+        * @param map\r
+        * @param contentUri\r
+        * @return map with contents of given Uri.\r
+        */\r
+       public static MapList<Content, Content> filterByUri(MapList<Content, Content> map, String contentUri) {\r
+               if ( map == null ) return null;\r
+               MapList<Content, Content> result = new MapList<Content, Content>();\r
+               for ( Content key : map.getKeys() ) {                                           \r
+                       if ( key.url.equals(contentUri) ) {\r
+                               result.addAll(key, map.getValuesUnsafe(key));\r
+                       }\r
+               }\r
+               return result;\r
+       }\r
+       \r
+       public static MapList<String, Content> toStringMap(MapList<Content, Content> map) {\r
+               if ( map == null ) return null;\r
+               MapList<String, Content> result = new MapList<String, Content>();\r
+               for ( Content key : map.getKeys() ) {\r
+                       result.addAll(key.url, map.getValuesUnsafe(key));\r
+               }               \r
+               return result;\r
+       }\r
+       \r
+       public static List<Content> filterContents(Collection<Content> contents, String contentTypeId, String formatId) {\r
+               if ( contents==null ) return null;\r
+               List<Content> result = new ArrayList<Content>(contents.size());\r
+               for ( Content content : contents ) {\r
+                       if ( contentTypeId!=null && !content.contentTypeId.equals(contentTypeId) ) continue;\r
+                       if ( formatId!=null && !content.formatId.equals(formatId) ) continue;\r
+                       result.add( content );\r
+               }\r
+               return result;\r
+       }\r
+       \r
+       public static List<String> toUris(Collection<Content> contents) {\r
+               List<String> result = new ArrayList<String>(contents.size());\r
+               for ( Content content : contents ) result.add( content.url );           \r
+               return result;\r
+       }       \r
+\r
+       public static Comparator<Content> CONTENT_COMPARATOR = new Comparator<Content>() {\r
+               @Override\r
+               public int compare(Content o1, Content o2) {\r
+                       int c = AlphanumComparator.CASE_INSENSITIVE_COMPARATOR.compare(o1.url, o2.url);\r
+                       if (c != 0)\r
+                               return c;\r
+                       c = AlphanumComparator.CASE_INSENSITIVE_COMPARATOR.compare(o1.formatId, o2.formatId);\r
+                       if (c != 0)\r
+                               return c;\r
+                       return 0;\r
+               }\r
+       };\r
+\r
+       public static List<Content> sortContent(List<Content> content) {\r
+               Collections.sort(content, CONTENT_COMPARATOR);\r
+               return content;\r
+       }\r
+\r
+       public static List<Content> sortedContent(List<Content> content) {\r
+               ArrayList<Content> sorted = new ArrayList<Content>(content);\r
+               sortContent(sorted);\r
+               return sorted;\r
+       }\r
+\r
+}\r