]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/PathUtils.java
Sync git svn branch with SVN repository r33269.
[simantics/platform.git] / bundles / org.simantics.utils.ui / src / org / simantics / utils / ui / PathUtils.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.utils.ui;\r
13 \r
14 import java.io.File;\r
15 import java.net.URL;\r
16 \r
17 import org.eclipse.core.runtime.FileLocator;\r
18 import org.eclipse.core.runtime.IPath;\r
19 import org.eclipse.core.runtime.Path;\r
20 import org.eclipse.core.runtime.Platform;\r
21 import org.osgi.framework.Bundle;\r
22 \r
23 /**\r
24  * Utilities for locating OSGi bundle contents on local media.\r
25  */\r
26 public final class PathUtils {\r
27     \r
28     /**\r
29      * Get the absolute path to the specified file in the specified bundle as an\r
30      * IPath. An absolute path is only available for files that are extracted\r
31      * onto any local or network mapped location. Files inside archived bundles\r
32      * aren't considered to have an absolute path since they cannot be executed\r
33      * as such.\r
34      * \r
35      * @param inBundle\r
36      * @param fullpath\r
37      * @return <code>null</code> if the specified file was not found under the\r
38      *         specified bundle or the bundle is archived.\r
39      */\r
40     public static IPath getAbsolutePath(Bundle inBundle, String fullpath) {\r
41 //        System.out.println("getAbsolutePath: " + inBundle + ", " + fullpath);\r
42         IPath path = new Path(fullpath);\r
43         URL u = FileLocator.find(inBundle, path, null);\r
44         if (u != null) {\r
45             try {\r
46                 u = FileLocator.resolve(u);\r
47 //                System.out.println("  PROTOCOL: " + u.getProtocol());\r
48 //                System.out.println("  FILE: " + u.getFile());\r
49                 // an absolute path is only available for the file protocol.\r
50                 if ("file".equals(u.getProtocol())) {\r
51                     IPath p = new Path(new File(u.getFile()).getAbsolutePath());\r
52                     return p;\r
53                 }\r
54             } catch (Exception e) {\r
55             }\r
56         }\r
57         return null;\r
58     }\r
59 \r
60     /**\r
61      * Get the absolute path to the specified file in the specified bundle as a\r
62      * String.\r
63      * \r
64      * @param inBundle\r
65      * @param fullpath\r
66      * @return <code>null</code> if the specified file was not found under the\r
67      *         specified bundle\r
68      */\r
69     public static String getAbsolutePathString(Bundle inBundle, String fullpath) {\r
70         IPath p = getAbsolutePath(inBundle, fullpath);\r
71         return (p != null) ? p.toOSString() : null;\r
72     }\r
73 \r
74     public static IPath getAbsolutePath(String inBundle, String fullpath) {\r
75         Bundle b = Platform.getBundle(inBundle);\r
76         if (b == null)\r
77             return null;\r
78         return getAbsolutePath(b, fullpath);\r
79     }\r
80 \r
81     public static String getAbsolutePathString(String inBundle, String fullpath) {\r
82         Bundle b = Platform.getBundle(inBundle);\r
83         if (b == null)\r
84             return null;\r
85         return getAbsolutePathString(b, fullpath);\r
86     }\r
87     \r
88 }\r