首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> JSP 程式設計 >> 關於JSP

Eclipse 插件開發實戰:基於 WTP 打造自定義 JSP 編輯器(一)— 整體架構與核心概念

     編輯:關於JSP

Web Tools Platform (WTP) 是 Eclipse 生態中 J2EE 開發的基石,眾多商業 IDE(MyEclipse、RAD、JBoss Tools)均基於 WTP 進行二次封裝。本系列從插件開發者視角,以「自定義 JSP 編輯器」為實戰載體,系統講解 WTP 結構化文檔模型(SDM)、結構化文本編輯器、內容助手、驗證器、超鏈接檢測等核心擴展點的定制實現。首篇建立整體認知框架。

WTP 核心組件架構

WTP = WST (Web Standard Tools) + JST (J2EE Standard Tools)
├── WST: 通用 Web 技術支援 (HTML, CSS, JavaScript, XML, JSP, WS...)
│   ├── SSE (Structured Source Editing) 結構化源碼編輯框架 — 核心
│   │   ├── Structured Document Model (DOM) — 語法層樹狀模型
│   │   ├── Structured Model — 語義層模型 (XML DOM, CSS OM)
│   │   ├── StructuredTextEditor — 編輯器基類
│   │   ├── Content Assist / Validation / Hyperlink / Formatter — 功能擴展點
│   │   └── Partitioning / Reconciling — 增量解析協調機制
│   └── Server Tools, Browser Tools, ...
└── JST: J2EE 專用擴展 (JSP, Servlet, EJB, JPA, JSF, Facelets...)

系列規劃與知識地圖

篇章核心主題關鍵接口/類實戰產出
一WTP 整體介紹、SSE 架構、開發環境搭建IEditorPart, IDocumentProvider, SourceViewerConfiguration可運行的空白 JSP 編輯器插件
二Structured Document (IStructuredDocument) — 語法樹模型IStructuredDocument, ITextRegion, Node, Composite PatternDocument 結構分析視圖插件
三Structured Model (IStructuredModel) — 語義模型與 XML DOMIStructuredModel, IDOMDocument, ModelManager, ModelBuilderModel 結構分析視圖插件
四StructuredTextEditor 定制:資源定位、內容助手、驗證器ISourceViewer, IContentAssistant, IValidator, IHyperlinkDetector具備智能提示、實時校驗的 JSP 編輯器
五進階定制:Auto Edit Strategy、折疊、標記、重構IAutoEditStrategy, IProjectionListener, IQuickFixProcessor生產級編輯器體驗
六WTP 進階:分區協調、模型同步、性能優化、測試IDocumentPartitioner, IReconciler, ModelSynchronizer穩健的插件發布包

開發環境搭建(基線版本)

  • Eclipse Platform: 4.3 (Kepler) / 4.4 (Luna) / 4.5 (Mars) — 對應 WTP 3.5 / 3.6 / 3.7
  • WTP SDK: 必須安裝 SDK 版(含源碼),而非僅 Runtime。
  • 必要依賴: GEF (Graphical Editing Framework), EMF (Eclipse Modeling Framework) — 版本必須與 Eclipse Platform/WTP 嚴格對應。
  • Target Platform: 建議配置 Target Definition,引入 WTP SDK、GEF SDK、EMF SDK,隔離開發環境與運行環境。
  • 導入核心插件源碼 (二進制亦可,但源碼利於調試):
    • org.eclipse.wst.sse.core / .ui
    • org.eclipse.wst.xml.core / .ui
    • org.eclipse.wst.html.core / .ui
    • org.eclipse.wst.css.core / .ui
    • org.eclipse.wst.javascript.core / .ui
    • org.eclipse.jst.jsp.core / .ui
    • org.eclipse.jst.web.core / .ui

JFace Text Framework 與 Eclipse Editor Framework 基石

WTP 編輯器建立在 JFace Text 之上,三大核心角色:

  1. IEditorPart:編輯器部件接口,生命周期管理(init, createPartControl, setInput, doSave, isDirty)。
  2. IDocumentProvider:文檔提供者,負責輸入元素(IEditorInput)到文檔模型(IDocument)的映射、緩存、脏狀態管理。
  3. SourceViewerConfiguration:編輯器行為配置總線,註冊:
- 文檔分區器 (IDocumentPartitioner) — 如 JSP 分區:HTML, JSP_SCRIPTLET, JSP_EXPRESSION, JSP_DIRECTIVE, JSP_COMMENT
- 內容助手 (IContentAssistant) — 完成提示、參數提示、信息悬停
- 驗證器 (IAnnotationModel + IValidator) — 實時錯誤/警告標記
- 超鏈接檢測器 (IHyperlinkDetector) — Ctrl+Click 跳轉
- 格式化器 (IContentFormatter) — 代碼格式化
- 自動編輯策略 (IAutoEditStrategy) — 括號補全、引號閉合、智能縮進
- 摺疊提供者 (IProjectionListener) — 代碼折疊
- 雙擊選擇策略 — 詞元選擇邏輯

第一步:創建最小化 JSP 編輯器插件

  1. 新建 Plug-in Project:`com.example.jsp.editor`,選擇 "This plug-in will make contributions to the UI"。
  2. 依賴:`org.eclipse.ui`, `org.eclipse.ui.editors`, `org.eclipse.ui.workbench.texteditor`, `org.eclipse.wst.sse.ui`, `org.eclipse.jst.jsp.ui`。
  3. 擴展點 `org.eclipse.ui.editors` 註冊編輯器:
    <extension point="org.eclipse.ui.editors">
      <editor
          class="com.example.jsp.editor.JSPEditor"
          contributorClass="org.eclipse.ui.texteditor.BasicTextEditorActionContributor"
          default="true"
          extensions="jsp,jspf"
          icon="icons/jsp_editor.gif"
          id="com.example.jsp.editor.JSPEditor"
          name="Custom JSP Editor">
      </editor>
    </extension>
  4. 實現 `JSPEditor` 繼承 `StructuredTextEditor`:
    public class JSPEditor extends StructuredTextEditor {
        public JSPEditor() {
            super();
            setSourceViewerConfiguration(new JSPConfiguration()); // 關鍵:注入配置
            setDocumentProvider(new JSPDocumentProvider());       // 可選:自定義文檔提供者
        }
    }
  5. 實現 `JSPConfiguration` 繼承 `XMLConfiguration` (或 `JSPConfiguration` 基類),覆寫需定制的方法。

關鍵認知:為什麼要深入理解 Document 與 Model?

所有高級功能(內容助手、驗證、超鏈接、重構)的實現,**本質上都是對 Document (語法樹) 或 Model (語義樹) 的遍歷、查詢、修改**。不熟悉數據模型結構,任何定制都是盲人摸象。下兩篇將分別深入剖析 `IStructuredDocument` 與 `IStructuredModel`。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved