介绍org.eclipse.core.runtime中工具类

此文说明org.eclipse.core.runtime下的几个常用类,他们为插件开发提供了便捷的方法.

 

*Assert,参数校验

---在插件开发中,经常需要对参数做校验,如NullPointerException,IllegalArgumentException异常等.一般的代码方式如if(arg==null) throw new RuntimeException("msg").

简化这类代码的方式使用org.eclipse.core.runtime.Assert,如上改写成Assert.isNotNull(arg,"msg").

---org.eclipse.jface.util.Assert与runtime中的Assert功能一样,只是被废弃了.

---和java的assert关键字不同在于,java的assert抛出Error异常.

 

*Path,处理文件路径,绝对或相对路径

---fromOSString和new Path(pathString);

构造path;并且在windows操作符时,若pathString有'\\',则转换为'/';

---String getFileExtension(),文件后缀

---File toFile(),转换为文件

Path方法测试//在xp下测试代码
Path path =new Path("D:\\lx\\TestLocale.java");
System.out.println(path.toOSString());                                 //D:\lx\TestLocale.java
System.out.println(path.toPortableString());                           //D:/lx/TestLocale.java
System.out.println(path.segment(0));                                   //lx
System.out.println(path.segment(1));                                   //TestLocale.java
System.out.println(path.isAbsolute());                                 //true
System.out.println(path.isRoot());                                     //false
System.out.println(path.isUNC());                                      //false
System.out.println(path.getFileExtension());                           //java
System.out.println(path.getDevice());                                  //D:
System.out.println(path.makeRelative().toOSString());                  //D:lx\TestLocale.java
System.out.println(path.makeAbsolute().toOSString());                  //D:\lx\TestLocale.java
System.out.println(path.removeLastSegments(1).toOSString());           //D:\lx
System.out.println(path.removeFirstSegments(1).toOSString());          //D:TestLocale.java

Path path =new Path("home/dzh/lx/TestLocale.java");
System.out.println(path.toOSString());                                 //home\dzh\lx\TestLocale.java
System.out.println(path.toPortableString());                           //home/dzh/lx/TestLocale.java
System.out.println(path.segment(0));                                   //home
System.out.println(path.segment(1));                                   //dzh
System.out.println(path.isAbsolute());                                 //false
System.out.println(path.isRoot());                                     //false
System.out.println(path.isUNC());                                      //false
System.out.println(path.getFileExtension());                           //java
System.out.println(path.getDevice());                                  //null
System.out.println(path.makeRelative().toOSString());                  //home\dzh\lx\TestLocale.java
System.out.println(path.makeAbsolute().toOSString());                  //\home\dzh\lx\TestLocale.java
System.out.println(path.removeLastSegments(2).toOSString());           //home\dzh
System.out.println(path.removeFirstSegments(2).toOSString());          //lx\TestLocale.java		

 

*FileLocator,查找bundle中的文件

---File getBundleFile(Bundle bundle),获取bundle文件

getBundleFile源码public static File getBundleFile(Bundle bundle) throws IOException {
	URL rootEntry = bundle.getEntry("/"); //$NON-NLS-1$ 
	rootEntry = resolve(rootEntry); //利用resolve协议转换
	if ("file".equals(rootEntry.getProtocol())) //$NON-NLS-1$
		return new File(rootEntry.getPath());
	if ("jar".equals(rootEntry.getProtocol())) { //$NON-NLS-1$
		String path = rootEntry.getPath();
		if (path.startsWith("file:")) {
			// strip off the file: and the !/
			path = path.substring(5, path.length() - 2);
			return new File(path);
		}
	}
	throw new IOException("Unknown protocol"); //$NON-NLS-1$
}

这里提供了一种获取bundle内文件的方法:url =bundle.getEntry()—>url =resolve(url)--->new File(url.getPath()).

---URL find(Bundle bundle, IPath path, Map override),返回bundle中内容的url,协议bundleentry

path是相对bundle的Path,如new Path("META-INF/MANIFEST.MF");支持一些特殊标识使用,

$nl$ - for language specific information
$os$ - for operating system specific information
$ws$ - for windowing system specific information

如new Path("$nl$/plugin.properties"),特殊标识的含义请见注释说明.

override的作用如其名用于替换默认的环境信息;在overriade=null或没有这个特殊标志定义时,对前面的特殊标志解释采用默认环境的设置,否则overrdie中记录的信息优先级高于默认值.

注意:

a.查询范围是bundle和bundle的所有fragment.

---URL[] findEntries(Bundle bundle, IPath path, Map override)

从bundle和它的fragment中查找对应的path,并返回url.

---InputStream openStream(Bundle bundle, IPath file, boolean substituteArgs)

获取bundle中对应文件的InputSteam.

substituteArgs为true表示支持特殊标识,false则表示不支持特殊标识.

从源码看

openStream源码public static final InputStream openStream(Bundle bundle, IPath file,    boolean substituteArgs) throws IOException {
	URL url = null;
	if (!substituteArgs) {
		url = findInPlugin(bundle, file, null);
		if (url == null)
			url = findInFragments(bundle, file, null);
	} else {
		url = FindSupport.find(bundle, file);
	}
	if (url != null)
		return url.openStream();
	throw new IOException("Cannot find " + file.toString()); //$NON-NLS-1$
}

 

---URL resolve(URL url)和URL toFileURL(URL url),协议转换

2者利用URLConverter服务实现将bundleentry:之类转换为file:等格式,转换后就可将此url作为其他类的参数了,如File(url.getPath());

osgi的URLConverter服务还没研究过,暂不论.

转载于:https://www.cnblogs.com/bronte/articles/1861445.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值