AutoCAD 3DMAX C语言 Pro/E UG JAVA编程 PHP编程 Maya动画 Matlab应用 Android
Photoshop Word Excel flash VB编程 VC编程 Coreldraw SolidWorks A Designer Unity3D
 首页 > JAVA编程

用JBuilder2007开发扩展JSF标签的插件

51自学网 2015-09-02 http://www.wanshiok.com

 

  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)。

private ISymbol handleSymbolCreation(final String symbolName,
final IStructuredDocumentContext context,
final Attr attr,
final Node owningElement,
final IProject project)
{
 // 建立Tab库
 final ITaglibContextResolver resolver =IStructuredDocumentContextResolverFactory.INSTANCE
.getTaglibContextResolver(context);

 if (resolver == null || !resolver.canResolveContext(context))
 {
  return null;
 }
 final String uri = resolver.getTagURIForNodeName(owningElement);
 IBeanInstanceSymbol beanSymbol = null;
 // 处理核心标签库
 if ("http://oracle.com/tutorial/fake/taglib".equals(uri))
 {
  final String elementName = owningElement.getLocalName();
  final String attrName = attr.getName();
  if ("locallyDeclaredBean".equals(elementName))
  {
   if ("var".equals(attrName))
   {
    final NamedNodeMap attrMap = owningElement.getAttributes();
    final Node baseNameNode = attrMap.getNamedItem("classname");

    if (baseNameNode instanceof Attr)
    {
     // 得到bean的名字
     final String className = ((Attr)baseNameNode).getValue();
     // 建立一个新的空Bean实例符号
     beanSymbol = SymbolFactory.eINSTANCE.createIBeanInstanceSymbol();
     beanSymbol.setName(attr.getValue());
     try
     {
      IJavaProject javaProject = JavaCore.create(project);
      IType type = javaProject.findType(className);

      if (type != null)
      {
       IJavaTypeDescriptor2 javaTypeDescriptor = SymbolFactory.eINSTANCE.createIJavaTypeDescriptor2();
       javaTypeDescriptor.setType(type);
       beanSymbol.setJavaTypeDescriptor(javaTypeDescriptor);
      }
     }
     catch (JavaModelException jme)
     {
     }
    }
   }
  }
 }
 return beanSymbol;
}

  7. 加入注释(annotations)元数据

  我们的最终目标是加入如下的标签:

<t:locallyDeclaredBean var="x" classname="beans.MyBean"/>

  声明一个变量"x"来处理"beans.MyBean"。为了通过框架,我们必须使用无数据来注释t::locallyDeclaredBean。

  首先让我们在工程中建立一个metadata文件夹。在通过File->New->File来建立一个metadata.xml文件,如图4如示。


图4

  打开这个文件,在其中输入如下的内容。

<?xml version="1.0" encoding="UTF-8"?>
<grammar-annotation xmlns="http://org.eclipse.jsf.core/grammarAnnotationSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://org.eclipse.jsf.core/grammarAnnotationSchema D://EclipseWTPLatest//WTPWorkspace//org.eclipse.jst.jsf.core//schema//grammar-annotations.xsd ">
 <cm-element name="locallyDeclaredBean">
  <cm-attribute name="var">
   <property name="contributes-value-binding">
    < value>true</value>
   </property>
   <property name="value-binding-scope">
    <value>request</value>
   </property>
   <property name="value-binding-symbol-factory"> <value>tutorial.locallyDeclaredBean</value>
   </property>
  </cm-attribute>
 </cm-element>
</grammar-annotation>

 

 
 
说明
:本教程来源互联网或网友上传或出版商,仅为学习研究或媒体推广,wanshiok.com不保证资料的完整性。
 

上一篇:从XML到Java代码的数据绑定之对象  下一篇:使用JBuilder2007开发Web应用程序