Live a simple life

          沉默(zhu_xing@live.cn)
          隨筆 - 48, 文章 - 0, 評論 - 132, 引用 - 0
          數據加載中……

          【Eclipse插件開發】基于WTP開發自定義的JSP編輯器(十):WTP TLD內容模型介紹

                     前面的系列文章中,我們已經分析了WTP的語法Document(IStructuredDocument)和語義Documnt(ICSSDocument或者IDOMDocument)以及和二者密切相關的IStructuredModel,并在這基礎之上對WTP默認提供的StructuredTextEditor進行了部分功能定制。


                      
                      問題出現了,我們想要的信息全部包含在IStructuredDocument、IDOMDocument(ICSSDocument)或IStructuredModel中嗎? 沒有。例如,如果我們需要訪問上圖JSP文檔TLD相關信息(例如:判斷當前JSP文檔中使用的特定標簽在TLD中是如何聲明的、和當前JSP文檔想關聯的TLD是怎樣定義的、、、),這些信息并不是直接放置于語法Document(IStructuredDocument)或者語義Document(IDOMDocument或者ICSSDocument)中的。除了TLD相關的信息外,我們需要的還有其他的描述信息,所有這些信息可以看做元數據信息,WTP將其稱為content model(直譯為內容模型吧^_^)。在本節中我們就先介紹一種內容模型:TLD內容模型(TLD Content Model),在后面緊接下來的章節中,我們會基于本節介紹的TLD內容模型開發一個自動編輯策略(auto edit strategy)。

                  【TLD Content Document】
                    所謂的TLD Content Document,從字面上就可以猜測出來是對某一TLD的描述文檔。那我們就先看一個TLD定義文件:
          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
          <taglib>
              
          <tlibversion>1.0</tlibversion>
              
          <jspversion>1.0</jspversion>
              
          <shortname>test1</shortname>
              
          <uri>http://www.aygfsteel.com/zhuxing/tags/test1</uri>
              
          <tag>
                  
          <name>test</name>
                  
          <tagclass>any</tagclass>
                  
          <bodycontent>empty</bodycontent>
                  
          <attribute>
                      
          <name>scope</name>
                      
          <required>true</required>
                      
          <rtexprvalue>true</rtexprvalue>
                  
          </attribute>
              
          </tag>
          </taglib>

                    
                      從結構上看,我們的一個TLD可以看做一個樹形結構的對象,具體節點種類可以分為:taglib、tag、attribute,示意圖如下:
                      
                      對應于上圖,我們定義了一個uri為http://www.aygfsteel.com/zhuxing/tags/test1的TLD(TLD Document),內含一個名為test的標簽(TLD Element),該標簽中含有一個名為scope的屬性(TLD Attribute)。如果我們能夠拿到這樣的一個樹形結構文檔對象,我們就可以獲取到我們想獲取的有關特定TLD的信息了。上圖中,其實也列舉了幾個相關的重要接口,這也是我們以后在操作TLD Content Model的時候需要經常打交道的:
                  org.eclipse.jst.jsp.core.internal.contentmodel.tld.provisional.TLDDocument
                  org.eclipse.jst.jsp.core.internal.contentmodel.tld.provisional.TLDElementDeclaration
                  org.eclipse.jst.jsp.core.internal.contentmodel.tld.provisional.TLDAttributeDeclaration

                【WTP Content Model】
                     我們在本節開頭部分就說了,TLD Content Model只是一種WTP Content Model,WTP為所有類型的content model設計了統一的類型接口(樹形結構composite的接口實現):
                      
                      從上面的類型體系圖可以看出,CMNode就是WTP內容模型樹的統一頂級接口,相關接口定義在org.eclipse.wst.xml.core插件中,大家可以去看一下。我們在使用WTP content model的時候常用的接口有:
                      1、org.eclipse.wst.xml.core.internal.contentmodel.CMNode
                      2、org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap
                      3、org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration
                      4、org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration
                      5、org.eclipse.wst.xml.core.internal.contentmodel.CMDocument
                      6、org.eclipse.wst.xml.core.internal.contentmodel.CMEntityDeclaration
                      7、org.eclipse.wst.xml.core.internal.provisional.contentmodel.CMNodeWrapper(CMNode的適配接口)

                      我們上面列舉的三個關于TLD Content Model的接口都是CMNode子接口:
                      1、TLDDocument接口是CMDocument的子接口
                      2、TLDElementDeclaration接口是CMElementDeclaration(CMContent)的子接口
                      3、TLDAttributeDeclaration接口是CMAttributeDeclaration的子接口
                  
                      PS:有興趣可以看一下另外兩種常用的content model:HTMLCMDocument和JSPCMDocument。
                      
                      【使用TLD Content Model】
                         WTP content model的創建不需要我們負責,我們只是讀取content model里的數據,常用接口有兩類:
                         1、org.eclipse.wst.xml.core插件中定義的CMNode系列抽象接口
                         2、特定類型content model相關的擴展接口
                        上面說的第一類接口我們已經列舉過,有關第二類接口也非常常用,它幫助我們更方便的訪問特定content model相關的信息。例如,相對于TLD Content Model,相關的擴展接口就是我們上面提到的TLDDocument、TLDElementDeclaration和TLDAttributeDeclaration。
                      
                          【TLDDocument、TLDElementDeclaration和TLDAttributeDeclaration】
                          1、TLDDocument接口提供的操作:
                          
                          我們可以很方便地借助TLDDocument接口訪問到的信息:URI、version、shortname等,通過其父接口CMDocument.getElements操作獲取tld element列表(TLDTLDElementDeclaration列表)。

                          2、TLDElementDeclaration接口提供的操作:
                          
                          我們可以很方便地借助TLDElementDeclaration接口訪問到的信息:tag class、body content等,通過其父接口CMElementDeclaration.getAttributes操作獲取tld attribute列表(TLDAttributeDeclaration列表)。

                          3、TLDAttributeDeclaration接口提供的操作:
                          
                          我們可以很方便地借助TLDAttributeDeclaration接口訪問到的信息:是否是必填屬性等
                          
                          【TaglibTracker:taglib條目】
                  
                            如上圖所示,每一個tld條目可以被表示為一個taglib tracker,這里的taglib tracker是位置相關的!!!我們接著看一下TaglibTracker(org.eclipse.jst.jsp.core.internal.contentmodel.tld.TaglibTracker)的實現代碼:   

          /*******************************************************************************
           * Copyright (c) 2004 IBM Corporation and others.
           * All rights reserved. This program and the accompanying materials
           * are made available under the terms of the Eclipse Public License v1.0
           * which accompanies this distribution, and is available at
           * 
          http://www.eclipse.org/legal/epl-v10.html
           * 
           * Contributors:
           *     IBM Corporation - initial API and implementation
           ******************************************************************************
          */
          package org.eclipse.jst.jsp.core.internal.contentmodel.tld;



          import org.eclipse.jst.jsp.core.internal.contentmodel.CMDocumentWrapperImpl;
          import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion;
          import org.eclipse.wst.xml.core.internal.contentmodel.CMDocument;
          import org.eclipse.wst.xml.core.internal.provisional.contentmodel.CMDocumentTracker;

          /**
           * TaglibTracker class
           
          */
          public class TaglibTracker extends CMDocumentWrapperImpl implements CMDocumentTracker {

              
          private IStructuredDocumentRegion fStructuredDocumentRegion;

              
          public TaglibTracker(String newURI, String newPrefix, CMDocument tld, IStructuredDocumentRegion aStructuredDocumentRegion) {
                  
          super(newURI, newPrefix, tld);
                  fStructuredDocumentRegion 
          = aStructuredDocumentRegion;
              }

              
          public IStructuredDocumentRegion getStructuredDocumentRegion() {
                  
          return fStructuredDocumentRegion;
              }

              
          public String toString() {
                  
          if (getStructuredDocumentRegion() != null)
                      
          return getPrefix() + "@" + getStructuredDocumentRegion().getStartOffset(); //$NON-NLS-1$
                  return super.toString();
              }
          }

                          分析以上代碼實現,我們看到兩點:
                          1、TaglibTracker是和特定IStructuredDocumentRegion綁定的,間接是位置相關的
                          2、TaglibTracker實現了CMNodeWrapper接口,可以適配為對應的original CMNode,可以通過TaglibTracker獲取對應的CMDocument(TLDDocument)。

                           附加說明,taglib tracker的收集通過兩種途徑:一是通過taglib指令直接引用;二是include指令間接導入(被include文件中的tld將被加入到當前JSP 文檔對應的taglib tracker列表中)

                      【TLDCMDocumentManager】
                      
          問題出來了:前面講了這么多,怎么獲取TLD Content Model實例呢?TLDCMDocumentManager(org.eclipse.jst.jsp.core.internal.contentmodel.tld.TLDCMDocumentManager),TLDCMDocumentManager的獲取需要借助于TaglibController(org.eclipse.jst.jsp.core.internal.contentmodel.TaglibController)中定義的getTLDCMDocumentManager(IDocument document)接口。   
                    
                          TLDCMDocumentManager中定義了幾個非常重要的操作,我們使用的時候要區分對待:
                          List  getTaglibTrackers():當前文檔相關的taglib tracker條目,隨著對應IStructuredDocument的變化而變化
                          Hashtable  getDocuments():當前文檔相關的taglib tracker條目,只增不減,不會隨著IStructuredDocument的變化而即時刪除掉對應的無效taglib tracker條目
                          【說明】:如果想獲取實時準確地taglib tracker信息,請使用getTaglibTrackers操作!!!                

                          List getCMDocumentTrackers(int offset):特定位置之前引入的taglib tracker條目
                          List getCMDocumentTrackers(String prefix, int offset):特定位置之前引入的指定prefix的taglib tracker條目

                          【說明】:前面我們介紹TaglibTracker的時候已經說過了TaglibTracker是位置相關的,一個TaglibTracker是和特定的IStructuredDocumentRegion綁定的,由于IStructuredDocumentRegion本身持有準確的位置信息,所以查詢特定位置之前引入的taglib tracker條目成為可能。聯系實際更加直觀,在特定taglib指令之前使用對應的標簽,則會出現標簽不識別的問題。
                          
                          【TLD Content Model使用的一般流程】
                                          

                          【代碼示例】
                           示例代碼一:獲取特定JSP文檔對應的TLD Content Model(TLD CMDocument實現)列表

          /**
               * 獲取特定JSP文檔相關的TLD列表
               * 
               * 
          @param structuredDocument
               * 
          @return
               
          */
              
          public static TLDDocument[] getTLDDoucments(IStructuredDocument structuredDocument) {
                  
          //獲取本structuredDocument對應的TLDCMDocumentManager實例
                  TLDCMDocumentManager tldDocumentManager = TaglibController.getTLDCMDocumentManager(structuredDocument);
                  
                  
          //獲取當前文檔中的taglib tracker條目
                  List taglibTrackers = tldDocumentManager.getTaglibTrackers();
                  
                  
          //獲取taglib tracker條目對應的TLDDocument
                  TLDDocument[] tldDocuments = new TLDDocument[taglibTrackers.size()];
                  
          for (int i = 0; i < taglibTrackers.size(); i++) {
                      TaglibTracker taglibTracker 
          = (TaglibTracker)taglibTrackers.get(i);
                      tldDocuments[i] 
          = (TLDDocument)taglibTracker.getDocument();
                  }
                  
          return tldDocuments;
              }
                          
                          示例代碼二:分析一個特定的TLDDocument(分析tld element,分析tld attribute)  
          /**
               * 獲取特定TLDDocument中的tag列表(tld element列表)
               * 
               * 
          @param tldDocument
               * 
          @return
               
          */
              
          public static TLDElementDeclaration[] getTags(TLDDocument tldDocument) {
                  
          //獲取tld element列表
                  CMNamedNodeMap children = tldDocument.getElements();
                  
                  TLDElementDeclaration[] tags 
          = new TLDElementDeclaration[children.getLength()];
                  
          for (int i = 0; i < tags.length; i++) {
                      tags[i] 
          = (TLDElementDeclaration)children.item(i);
                  }
                  
          return tags;
              }
              
              
          /**
               * 獲取特定tag中(tld element)定義的屬性列表
               * 
               * 
          @param tagElement
               * 
          @return
               
          */
              
          public static TLDAttributeDeclaration[] getAttributes(TLDElementDeclaration tagElement) {
                  
          //獲取tld attribute列表
                  CMNamedNodeMap children = tagElement.getAttributes();
                  
                  TLDAttributeDeclaration[] attributes 
          = new TLDAttributeDeclaration[children.getLength()];
                  
          for (int i = 0; i < attributes.length; i++) {
                      attributes[i] 
          = (TLDAttributeDeclaration)children.item(i);
                  }
                  
          return attributes;
              }

                      
                     PS:說明,有關TLDCMDocumentManager中提供的位置相關的taglib tracker查詢接口大家可以自己編寫相應的測試代碼,著重理解taglib tracker位置相關的特性。

                  

                      下一節中,我們會開發一個TLD Content Model分析視圖,之后一節會定制WTP編輯器默認的自動編輯策略(auto edit strategy)。



          本博客中的所有文章、隨筆除了標題中含有引用或者轉載字樣的,其他均為原創。轉載請注明出處,謝謝!

          posted on 2008-10-13 17:50 zhuxing 閱讀(2437) 評論(3)  編輯  收藏 所屬分類: Eclipse Plug-in & OSGIWTP(Web Tools Platform)

          評論

          # re: 【Eclipse插件開發】基于WTP開發自定義的JSP編輯器(十):WTP TLD內容模型介紹  回復  更多評論   

          朱星大哥,這一系列的文章簡直太經典了,頂一個!
          前面的StructrueModel的分析視圖代碼,好像放錯了,放成StructuredDocument的分析視圖代碼了,能不能更新一下啊,小弟自己跟著步驟,沒弄出來,好像treeviewer.serInput 沒有調用,我自己把 structureModel傳進去,缺又老是報錯,求救啊!
          2008-10-15 21:17 | 飛揚的麥子

          # re: 【Eclipse插件開發】基于WTP開發自定義的JSP編輯器(十):WTP TLD內容模型介紹  回復  更多評論   

          好了,我問題解決了,是自己的一些代碼沒寫全呵呵,不過最好還是把 代碼換成正確的,方便后來人啊!不好意思,您的名字打錯了,應該是朱興!
          2008-10-15 22:39 | 飛揚的麥子

          # re: 【Eclipse插件開發】基于WTP開發自定義的JSP編輯器(十):WTP TLD內容模型介紹[未登錄]  回復  更多評論   

          請問 wtp怎么加載的這個TLD文件,是怎么找到的? 怎么確定是哪一個tld文件,從哪里指定???你程序里面這個TLD都沒加載到。。。
          2012-04-26 16:51 | john
          主站蜘蛛池模板: 永和县| 长治县| 渝中区| 河津市| 彭阳县| 东海县| 呼玛县| 娄烦县| 拉萨市| 淮安市| 裕民县| 万年县| 嘉峪关市| 青阳县| 自贡市| 两当县| 容城县| 白山市| 泽州县| 富宁县| 嘉禾县| 安阳市| 徐州市| 满洲里市| 凤山市| 湖州市| 苗栗县| 岳阳市| 延吉市| 玉龙| 滁州市| 元氏县| 富顺县| 专栏| 汉川市| 双辽市| 上高县| 临桂县| 元朗区| 天气| 伊宁市|