`
yianpuodiaotu
  • 浏览: 242788 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

Eclipse Tip: Define Custom Content Types to Identify Your Data Files

阅读更多

【转自】http://www.developer.com/java/data/article.php/3648736/Eclipse-Tip-Define-Custom-Content-Types-to-Identify-Your-Data-Files.htm

In desktop environments, the typical approach for identifying a file's content is to check its extension. You're familiar with the myriad of three-letter extensions used by the ever-growing number of desktop applications you run across. You also know that at times, the extension "gets it wrong"—even though its name ends in a ".doc", it may not be the text document format you expect.

In the Eclipse Workbench, the traditional approach also has been to assign a unique extension to each unique file format. The editor used to view and edit these files is then bound to that extension. This approach becomes problematic when a generic extension is used to represent an entire family of file formats. For example, the preferred extension for XML files is ".xml" regardless of what kind of data they contain. This way, generic applications capable of viewing XML files can immediately recognize the file as XML. However, this does not work for specialized Eclipse editors built to handle a specific type of XML content.
Defining Content Types

Listing 1: Declaring a content type extension for XML files containing the "apples" element.

<extension point="org.eclipse.core.contenttype.contentTypes">
   <content-type base-type="org.eclipse.core.runtime.xml"
                 file-extensions="xml"
                 id="ca.ecliptical.examples.contenttype.apples"
                 name="Apples File"
                 priority="normal">
      <describer class="org.eclipse.core.runtime.content
                        .XMLRootElementContentDescriber">
         <parameter name="element"
                    value="apples">
         </parameter>
      </describer>
   </content-type>
</extension>

Starting with version 3.0, Eclipse provides a mechanism to programmatically identify file types based on their actual content. The org. eclipse. core. contenttype. contentTypes extension point can be used to define a content type described by a "describer," which is a class capable of recognizing specific types of content given an input stream. Editors designed to work with a specific content type can be bound to it instead of a file extension.

Although it is possible to create and contribute custom content describers, the default ones often will do the job. Eclipse provides a BinarySignatureDescriber, which uses the file's binary signature to identify its content, and a XMLRootElementContentDescriber, which checks an XML document's root element (or its DTD). Both can be parameterized with application-specific values.

Listing 1 illustrates how to define a custom content type to represent XML files whose root element is "apples". See the Resources section for the entire source code for this example.

    Note: The content type is narrowed to files whose extension is ".xml". This makes the identification process more efficient—it allows the platform to exclude this content type from consideration for files whose extensions don't match.

Binding Editors to Content Types

Listing 2: Declaring the "ApplesEditor" bound to the "apples" content type.

<extension point="org.eclipse.ui.editors">
   <editor name="Apples Editor"
           icon="icons/sample.gif"
           contributorClass="org.eclipse.ui.texteditor
                             .BasicTextEditorActionContributor"
           class="ca.ecliptical.examples.contenttype.apples
                  .ApplesEditor"
           id="ca.ecliptical.examples.contenttype.apples
               .ApplesEditor">
      <contentTypeBinding
         contentTypeId="ca.ecliptical.examples.contenttype
                        .apples">
      </contentTypeBinding>
   </editor>
</extension>

Once you define a content type for your data format, you can bind the appropriate editor to it. In the attached example, you have two sample XML editors. The ApplesEditor is used for XML files whose root element is "apples"; the OrangesEditor handles XML files with "oranges" in their root. Listing 2 shows an extension declaring the ApplesEditor and binding it to the "apples" content type.

    Note: As far as the editors are concerned, the file extension no longer matters. Any file extension binding is relegated to the content type rather than the editor.

When you run the example as an Eclipse application, use the New XML file wizard in the Sample Wizards category to create a new file with either "apples" or "oranges" as its root element. Based on this, the workbench then opens either the ApplesEditor or the OrangesEditor, respectively. Also, when you right-click an XML file in the Resource Navigator, you will see the appropriate default editor in the "Open With" sub-menu.
Resources

    * Eclipse Platform Plug-in Developer Guide: Content types
    * Documentation for the org.eclipse.core.runtime.contentTypes extension point
    * Example plug-in source (Eclipse 3.2 project)

About the Author

Peter Nehrer is a software consultant specializing in Eclipse-based enterprise solutions and J2EE applications. He is the founder of Ecliptical Software Inc. and a contributor to several Eclipse-related Open Source projects. He holds an M.S. in Computer Science from the University of Massachusetts at Amherst, MA. Peter can be reached at pnehrer AT eclipticalsoftware DOT com.

 

由于时间问题,也没来得及整理,直接就copy过来了。我自己简单试了一下,挺简单。

plugin.xml

<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>

   <extension
         point="org.eclipse.ui.editors">
      <editor
            name="Sample XML Editor"
            extensions="test"
            icon="icons/sample.gif"
            contributorClass="org.eclipse.ui.texteditor.BasicTextEditorActionContributor"
            class="contest.editors.XMLEditor"
            id="contest.editors.XMLEditor">
         <contentTypeBinding
               contentTypeId="ca.ecliptical.examples.contenttype.apples">
         </contentTypeBinding>
            
      </editor>
   </extension>
   <extension
         point="org.eclipse.core.contenttype.contentTypes">
         <content-type base-type="org.eclipse.core.runtime.xml"
	                 file-extensions="test"
	                 id="ca.ecliptical.examples.contenttype.apples"
	                 name="Apples File"
	                 priority="normal">
	      <describer class="org.eclipse.core.runtime.content.XMLRootElementContentDescriber">
	         <parameter name="element"
	                    value="apples">
	         </parameter>
	      </describer>
	   </content-type>
   </extension>
   

</plugin>

 界面效果:

contents type



 file associations


  • 大小: 94.3 KB
  • 大小: 29.5 KB
分享到:
评论

相关推荐

    十字绣专业软件PCStitch.v10.00.017注册版 (Part 2)

    Custom Palette Sort: Define a custom order of palette entries on your printouts. Palette Symbol Enhancments: Select custom symbol colors for printouts. Choose symbol by stitch type. Export to ...

    十字绣专业软件PCStitch.v10.00.017注册版 (Part 1)

    Custom Palette Sort: Define a custom order of palette entries on your printouts. Palette Symbol Enhancments: Select custom symbol colors for printouts. Choose symbol by stitch type. Export to ...

    wechat_devtools_1.02.1907112_x64.zip 小程序解决ReferenceError: define is not defined

    1. 之前稳定版开发工具用的好好的,突然某天真机调试一直报错: ReferenceError: define is not defined 2. 找解决办法后发现,更新至开发版最新版可以解决,如附件,下载安装后,即可解决该问题。

    Excel.2013.Working.with.Data.Ranges.and.Tables

    Next you learn how to sort a data range on one column or on multiple columns and how to use a custom sort order, the Filter (AutoFilter) feature and the Advanced Filter feature to view data that meets...

    Microsoft SharePoint 2010 Developer Reference

    They can be customized to meet specific business needs and can be linked to content types to enforce data consistency. **Site Columns** These define fields that can be reused across multiple lists ...

    Building.Android.UIs.with.Custom.Views

    You will create custom styleable attributes that work with Android XML layouts, learn to process touch events, define custom attributes, and add properties and events to them. By the end of this book...

    QT CMake 3.3编译器

    QT CMake 3.3编译器 解决 使用Qt Creator 14.0.1 (Community) 创建项目时...报错的内容就是:error: The kit needs to define a CMake tool to parse this project. 译 错误:工具包需要定义一个CMake工具来解析这个项目

    Effective Java 3rd edition(Effective Java第三版英文原版)附第二版

    Item 22: Use interfaces only to define types Item 23: Prefer class hierarchies to tagged classes Item 24: Favor static member classes over nonstatic Item 25: Limit source files to a single top-level ...

    C Programming

    - **Defining Your Own Types**: Explanation of how to create custom data types using `typedef`. #### Files The document explains file handling in C, which involves reading from and writing to files: ...

    CSharp 3.0 With the .NET Framework 3.5 Unleashed(english)

    - **Overloading Operators**: Operator overloading enables you to define how built-in operators behave with custom types. - **Conversions and Conversion Operator Overloads**: Conversion operator ...

    Docker for Data Science: Building Scalable and Extensible Data Infrastructure

    Learn Docker "infrastructure as code" technology to define a system for performing standard but non-trivial data tasks on medium- to large-scale data sets, using Jupyter as the master controller. ...

    odoo themes

    ### Odoo Themes: A Comprehensive Guide to Creating Custom Themes for Odoo 9.0 #### Introduction to Odoo Themes In the vibrant world of content management systems (CMS), Odoo stands out as a powerful...

    Expert Data Visualization

    The book will also guide you through creating custom graphs and visualizations, and show you how to go from the raw data to beautiful visualizations. The extensive examples will include working with ...

    php提示Failed to write session data错误的解决方法

    当PHP运行时,如果遇到“Warning: Failed to write session data (files). Please verify that the current setting of session.save_path is correct() in Unknown on line 0”的警告,这说明PHP无法将session数据...

    Designing with Data: Improving the User Experience with A/B Testing

    Complete with real-world examples, this book shows you how to make data-driven design part of your product design workflow. Understand the relationship between data, business, and design Get a firm ...

    微软数据库考试70-768: Exam Ref 70-768 Developing SQL Data Models

    Course OutlineModule 1: Introduction to Business Intelligence and Data ModelingThis module introduces key BI concepts and the Microsoft BI product suite.Lessons Introduction to Business Intelligence...

    salesforce dev 501 Notes

    Custom controllers are used when you need complete control over the data retrieval and processing, while controller extensions provide a way to extend an existing controller without modifying its ...

Global site tag (gtag.js) - Google Analytics