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