最近写了一个Eclipse插件,可以找到当前eclipse安装目录中的所有插件的可扩展点。现将代码贴上来,与大家共享。
Plugin.xml:
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.0"?>
<plugin
id="org.eclipse.contribution.FindAllExtensionPoint"
name="FindAllExtensionPoint Plug-in"
version="1.0.0"
provider-name="">
<runtime>
<library name="FindAllExtensionPoint.jar" />
</runtime>
<requires>
<import plugin="org.eclipse.ui.views"/>
<import plugin="org.eclipse.ui"/>
<import plugin="org.eclipse.core.runtime"/>
</requires>
<extension point="org.eclipse.ui.views">
<view
name="Find All Extension-point Info"
icon="icons\pt.gif"
class="org.eclipse.contribution.FindAllExtensionPoint.FAViews"
id="org.eclipse.contribution.FindAllExtensionPoint.FAViews">
</view>
</extension>
</plugin>
在src目录下面新加一个包,名字叫做org.eclipse.contribution.FindAllExtensionPoint
里面加入两个.java文件:
FAViews.java
/*
* Created on 2005-11-5
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package org.eclipse.contribution.FindAllExtensionPoint;
/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
import java.io.*;
import java.util.List;
import java.util.ArrayList;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.*;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.jface.viewers.TableViewer;
public class FAViews extends ViewPart{
private List PointList;
private TableViewer viewer;
/* (non-Javadoc)
* @see org.eclipse.ui.IWorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
*/
public void createPartControl(Composite parent) {
// TODO Auto-generated method stub
Table table = new Table(parent,SWT.SINGLE |
SWT.V_SCROLL | SWT.FULL_SELECTION);
table.setHeaderVisible(true);
table.setLinesVisible(true);
TableColumn column = new TableColumn(table,SWT.NONE,0);
column.setText("Eclipse所有扩展点");
column.setWidth(1000);
column.setAlignment(SWT.LEFT);
PlugMain();
viewer = new TableViewer(table);
for(int i=0;i<PointList.size();i++)
{
viewer.add(PointList.get(i));
}
}
/* (non-Javadoc)s
* @see org.eclipse.ui.IWorkbenchPart#setFocus()
*/
public void setFocus() {
// TODO Auto-generated method stub
viewer.getControl().setFocus();
}
public TableViewer getViewer(){
return viewer;
}
private void PlugMain(){
File PlugInDir = new File(GetPlugInPath());
File DirList[] = PlugInDir.listFiles();
PointList = new ArrayList();
int i;
for(i=0;i<DirList.length;i++){
if(DirList[i].isDirectory()){
File PlugInFile = new File(DirList[i].getPath(),"Plugin.xml");
if(PlugInFile.exists()){
GetExtensionPoint(DirList[i].getName(),PlugInFile.getPath());
}
}
}
}
private static String GetPlugInPath(){
String path = new File("").getAbsolutePath();
path = path + "\Plugins\";
return path;
}
private void GetExtensionPoint(String DirName,String XmlFilePath){
ParseXMLInfo PXML = new ParseXMLInfo();
List TempList = PXML.GetPluginID(DirName,XmlFilePath);
for(int i=0;i<TempList.size();i++)
{
PointList.add(TempList.get(i));
}
}
}
ParseXMLInfo.java:
/*
* Created on 2005-11-5
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package org.eclipse.contribution.FindAllExtensionPoint;
/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
import javax.xml.parsers.*;
import org.w3c.dom.*;
import java.util.List;
import java.util.ArrayList;
public class ParseXMLInfo {
private DocumentBuilderFactory factory=null;
private DocumentBuilder builder=null;
private Document doc=null;
public ParseXMLInfo(){
}
private boolean connXml(String xmlFIleName){
try{
factory = DocumentBuilderFactory.newInstance();
builder = factory.newDocumentBuilder();
doc = builder.parse(xmlFIleName);
doc.normalize();
return true;
}
catch(Exception e)
{
return false;
}
}
public List GetPluginID(String DirName,String xmlFileName){
List list = new ArrayList();
NodeList EPNodeList;
Element EPNode;
Attr p;
String s = "";
if (connXml(xmlFileName)){
list.add(DirName);
EPNodeList = doc.getElementsByTagName("extension-point");
for(int i=0;i<EPNodeList.getLength();i++)
{
EPNode = (Element)EPNodeList.item(i);
p = EPNode.getAttributeNode("id");
if(EPNode.hasAttribute("id")){
s= " " + p.getNodeValue();
list.add(s);
}
}
}
return list;
}
}
build.properties:
bin.includes = FindAllExtensionPoint.jar,\
FindAllExtensionPointsrc.zip,\
plugin.xml,\
icons/
source.FindAllExtensionPoint.jar = src-FindAllExtensionPoint/
然后导出成.zip文件,随后解压到plugins目录下面,如果你用的是eclipse3,则需要删除configure目录,如果用的是eclipse2,则可以直接启动eclpse,选择windows->show views->other->Find all Extension-all info就可以了。
