]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/PathUtils.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.utils.ui / src / org / simantics / utils / ui / PathUtils.java
diff --git a/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/PathUtils.java b/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/PathUtils.java
new file mode 100644 (file)
index 0000000..65a7d65
--- /dev/null
@@ -0,0 +1,88 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.utils.ui;\r
+\r
+import java.io.File;\r
+import java.net.URL;\r
+\r
+import org.eclipse.core.runtime.FileLocator;\r
+import org.eclipse.core.runtime.IPath;\r
+import org.eclipse.core.runtime.Path;\r
+import org.eclipse.core.runtime.Platform;\r
+import org.osgi.framework.Bundle;\r
+\r
+/**\r
+ * Utilities for locating OSGi bundle contents on local media.\r
+ */\r
+public final class PathUtils {\r
+    \r
+    /**\r
+     * Get the absolute path to the specified file in the specified bundle as an\r
+     * IPath. An absolute path is only available for files that are extracted\r
+     * onto any local or network mapped location. Files inside archived bundles\r
+     * aren't considered to have an absolute path since they cannot be executed\r
+     * as such.\r
+     * \r
+     * @param inBundle\r
+     * @param fullpath\r
+     * @return <code>null</code> if the specified file was not found under the\r
+     *         specified bundle or the bundle is archived.\r
+     */\r
+    public static IPath getAbsolutePath(Bundle inBundle, String fullpath) {\r
+//        System.out.println("getAbsolutePath: " + inBundle + ", " + fullpath);\r
+        IPath path = new Path(fullpath);\r
+        URL u = FileLocator.find(inBundle, path, null);\r
+        if (u != null) {\r
+            try {\r
+                u = FileLocator.resolve(u);\r
+//                System.out.println("  PROTOCOL: " + u.getProtocol());\r
+//                System.out.println("  FILE: " + u.getFile());\r
+                // an absolute path is only available for the file protocol.\r
+                if ("file".equals(u.getProtocol())) {\r
+                    IPath p = new Path(new File(u.getFile()).getAbsolutePath());\r
+                    return p;\r
+                }\r
+            } catch (Exception e) {\r
+            }\r
+        }\r
+        return null;\r
+    }\r
+\r
+    /**\r
+     * Get the absolute path to the specified file in the specified bundle as a\r
+     * String.\r
+     * \r
+     * @param inBundle\r
+     * @param fullpath\r
+     * @return <code>null</code> if the specified file was not found under the\r
+     *         specified bundle\r
+     */\r
+    public static String getAbsolutePathString(Bundle inBundle, String fullpath) {\r
+        IPath p = getAbsolutePath(inBundle, fullpath);\r
+        return (p != null) ? p.toOSString() : null;\r
+    }\r
+\r
+    public static IPath getAbsolutePath(String inBundle, String fullpath) {\r
+        Bundle b = Platform.getBundle(inBundle);\r
+        if (b == null)\r
+            return null;\r
+        return getAbsolutePath(b, fullpath);\r
+    }\r
+\r
+    public static String getAbsolutePathString(String inBundle, String fullpath) {\r
+        Bundle b = Platform.getBundle(inBundle);\r
+        if (b == null)\r
+            return null;\r
+        return getAbsolutePathString(b, fullpath);\r
+    }\r
+    \r
+}\r