从Eclipse插件中读取资源

来源:互联网 发布:pyqt5 for windows 编辑:程序博客网 时间:2024/04/30 10:38
可以通过Eclipse里的OSGi的Bundle类,获取插件目录下的某个文件的输入流:
1. Bundle bundle = Platform.getBundle(Activator.PLUGIN_ID);   
2. URL url = bundle.getResource("/icon/xx.txt");   
3. InputStream is = FileLocator.toFileURL(url).openStream();  

从Bundle查找资源:
Bundle bundle = Platform.getBundle(pluginId);
URL fullpathString = BundleUtility.find(bundle, filePath);

Enumeration org.osgi.framework.Bundle.findEntries(String path, String filePattern, boolean recurse)
Returns entries in this bundle and its attached fragments. This bundle's
class loader is not used to search for entries. Only the contents of this bundle
and its attached fragments are searched for the specified entries. If this
bundle's state is INSTALLED, this method must attempt to resolve
this bundle before attempting to find entries.

This method is intended to be used to obtain configuration, setup,
localization and other information from this bundle. This method takes into
account that the "contents" of this bundle can be extended with fragments. This
"bundle space" is not a namespace with unique members; the same entry name can
be present multiple times. This method therefore returns an enumeration of URL
objects. These URLs can come from different JARs but have the same path name.
This method can either return only entries in the specified path or recurse into
subdirectories returning entries in the directory tree beginning at the
specified path. Fragments can be attached after this bundle is resolved,
possibly changing the set of URLs returned by this method. If this bundle is not
resolved, only the entries in the JAR file of this bundle are returned.

Examples:  // List all XML files in the OSGI-INF directory and below
 Enumeration e = b.findEntries("OSGI-INF", "*.xml", true);
 
 // Find a specific localization file
 Enumeration e = b
         .findEntries("OSGI-INF/l10n", "bundle_nl_DU.properties", false);
 if (e.hasMoreElements())
     return (URL) e.nextElement();
 
Note: Jar and zip files are not required to include directory entries. URLs to directory entries will not be returned if the bundle contents do not contain directory entries. Parameters: path The path name in which to look. The path is always relative to the root of this bundle and may begin with "/". A path value of "/" indicates the root of this bundle. filePattern The file name pattern for selecting entries in the specified path. The pattern is only matched against the last element of the entry path. If the entry is a directory then the trailing "/" is not used for pattern matching. Substring matching is supported, as specified in the Filter specification, using the wildcard character ("*"). If null is specified, this is equivalent to "*" and matches all files. recurse If true, recurse into subdirectories. Otherwise only return entries from the specified path. Returns: An enumeration of URL objects for each matching entry, or null if an entry could not be found or if the caller does not have the appropriate AdminPermission[this,RESOURCE], and the Java Runtime Environment supports permissions. The URLs are sorted such that entries from this bundle are returned first followed by the entries from attached fragments in ascending bundle id order. If this bundle is a fragment, then only matching entries in this fragment are returned.

0 0