本文将编写一个基于JSF标签的名为"locallyDefinedBean"插件。JSF标签可以在运行时将Bean变量加到jsp-JSF而中,在这里,我们将通过使用JSF EL(所谓JSF EL就是JSF EXPRESSION LANGUAGE)来模拟在设计时向JSP-JSF添加变量的过程。下面是具体的实现过程。
1. 首先,使用File->New->Project->Plug-in Project来建立一个空的插件工程,这个工程使用JBuilder2007提供的默认值,并在工程名中输入myplugin。如图1所示:

图1
2. 打开工程浏览器,切换到dependencies标签。并加入如图2所示的依靠库。

图2 dependencies标签
3. 通过File->New->Class建立一个java类: LocallyDeclaredBeanFactory.,并使这个类从org.eclipse.jst.jsf.context.symbol.internal.provisional.source.AbstractContextSymbolFactory继续。如图3所示。

图3
4. 打开Java源码编辑器。你将看到两个根据抽象类自动产生的方法。然后将supports方法替换为我们自己的supports方法。代码如下:
public boolean supports(IAdaptable context)
{
return context.getAdapter(IStrUCturedDocumentContext.class) != null;
}
上面的代码表示只能调用IstructuredDocumentContext工厂。进入讨论组讨论。
5. 使用如下的代码替换internalCreate中的代码
protected ISymbol internalCreate(String symbolName, int scope, IAdaptable context, List problems)
{
//得到上下文
final IStructuredDocumentContext sContext =(IStructuredDocumentContext)
context.getAdapter(IstructuredDocumentContext.class);
// 为上下文建立一个DOM上下文
final IDOMContextResolver domResolver =
IStructuredDocumentContextResolverFactory.INSTANCE.getDOMContextResolver(sContext);
if (domResolver != null)
{
final Node curNode = domResolver.getNode();
// node必须是xml属性
if (curNode instanceof Attr)
{
final Attr attr = (Attr) curNode;
final Node owningElement = attr.getOwnerElement();
if (owningElement != null)
{
IProject iProject = workspaceResolver.getProject();
if (iProject != null)
{
return handleSymbolCreation(symbolName, sContext, attr, owningElement, iProject);
}
}
}
}
return null;
}
6. 下面让我们加一个private方法来建立符号(Symbol)。