X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.utils.ui%2Fsrc%2Forg%2Fsimantics%2Futils%2Fui%2FPathUtils.java;fp=bundles%2Forg.simantics.utils.ui%2Fsrc%2Forg%2Fsimantics%2Futils%2Fui%2FPathUtils.java;h=65a7d65d663857d0a22050ea6a2fdeda70304bd1;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git 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 index 000000000..65a7d65d6 --- /dev/null +++ b/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/PathUtils.java @@ -0,0 +1,88 @@ +/******************************************************************************* + * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.utils.ui; + +import java.io.File; +import java.net.URL; + +import org.eclipse.core.runtime.FileLocator; +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.Path; +import org.eclipse.core.runtime.Platform; +import org.osgi.framework.Bundle; + +/** + * Utilities for locating OSGi bundle contents on local media. + */ +public final class PathUtils { + + /** + * Get the absolute path to the specified file in the specified bundle as an + * IPath. An absolute path is only available for files that are extracted + * onto any local or network mapped location. Files inside archived bundles + * aren't considered to have an absolute path since they cannot be executed + * as such. + * + * @param inBundle + * @param fullpath + * @return null if the specified file was not found under the + * specified bundle or the bundle is archived. + */ + public static IPath getAbsolutePath(Bundle inBundle, String fullpath) { +// System.out.println("getAbsolutePath: " + inBundle + ", " + fullpath); + IPath path = new Path(fullpath); + URL u = FileLocator.find(inBundle, path, null); + if (u != null) { + try { + u = FileLocator.resolve(u); +// System.out.println(" PROTOCOL: " + u.getProtocol()); +// System.out.println(" FILE: " + u.getFile()); + // an absolute path is only available for the file protocol. + if ("file".equals(u.getProtocol())) { + IPath p = new Path(new File(u.getFile()).getAbsolutePath()); + return p; + } + } catch (Exception e) { + } + } + return null; + } + + /** + * Get the absolute path to the specified file in the specified bundle as a + * String. + * + * @param inBundle + * @param fullpath + * @return null if the specified file was not found under the + * specified bundle + */ + public static String getAbsolutePathString(Bundle inBundle, String fullpath) { + IPath p = getAbsolutePath(inBundle, fullpath); + return (p != null) ? p.toOSString() : null; + } + + public static IPath getAbsolutePath(String inBundle, String fullpath) { + Bundle b = Platform.getBundle(inBundle); + if (b == null) + return null; + return getAbsolutePath(b, fullpath); + } + + public static String getAbsolutePathString(String inBundle, String fullpath) { + Bundle b = Platform.getBundle(inBundle); + if (b == null) + return null; + return getAbsolutePathString(b, fullpath); + } + +}