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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.utils.ui;
\r
14 import java.io.File;
\r
15 import java.net.URL;
\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
24 * Utilities for locating OSGi bundle contents on local media.
\r
26 public final class PathUtils {
\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
37 * @return <code>null</code> if the specified file was not found under the
\r
38 * specified bundle or the bundle is archived.
\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
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
54 } catch (Exception e) {
\r
61 * Get the absolute path to the specified file in the specified bundle as a
\r
66 * @return <code>null</code> if the specified file was not found under the
\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
74 public static IPath getAbsolutePath(String inBundle, String fullpath) {
\r
75 Bundle b = Platform.getBundle(inBundle);
\r
78 return getAbsolutePath(b, fullpath);
\r
81 public static String getAbsolutePathString(String inBundle, String fullpath) {
\r
82 Bundle b = Platform.getBundle(inBundle);
\r
85 return getAbsolutePathString(b, fullpath);
\r