1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.utils.ui;
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;
24 * Utilities for locating OSGi bundle contents on local media.
26 public final class PathUtils {
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
37 * @return <code>null</code> if the specified file was not found under the
38 * specified bundle or the bundle is archived.
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);
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());
54 } catch (Exception e) {
61 * Get the absolute path to the specified file in the specified bundle as a
66 * @return <code>null</code> if the specified file was not found under the
69 public static String getAbsolutePathString(Bundle inBundle, String fullpath) {
70 IPath p = getAbsolutePath(inBundle, fullpath);
71 return (p != null) ? p.toOSString() : null;
74 public static IPath getAbsolutePath(String inBundle, String fullpath) {
75 Bundle b = Platform.getBundle(inBundle);
78 return getAbsolutePath(b, fullpath);
81 public static String getAbsolutePathString(String inBundle, String fullpath) {
82 Bundle b = Platform.getBundle(inBundle);
85 return getAbsolutePathString(b, fullpath);