How to register an eclipse editor by file content

Sometimes you may want to register an eclipse editor by reading file content. As an example if we need to open Maven POM file with specificity packing type with your editor or if you need to use different files without file extension, so we have to write a custom ContentDescriber to handle it. 

We can write custom ContentDescriber by extending or implementing following classes/ interfaces.

ITextContentDescriber
XMLContentDescriber
IContentDescriber
BinarySignatureDescriber
 
So let's write a custom ContentDescriber by extending XMLContentDescriber

package org.my.editor.describer;

import java.io.IOException;
import java.io.InputStream;
import org.eclipse.core.runtime.content.IContentDescription;
import org.eclipse.core.runtime.content.XMLContentDescriber;

public class MyDescriber extends XMLContentDescriber {

 public int describe(InputStream input, IContentDescription description)
   throws IOException {
   /** Implement logics to validate content by reading input stream  
    *  if success, return VALID;   
    */
  return INVALID;
 }
 
} 

let's modify plugin.xml to register custom ContentDescriber
 
<extension
         point="org.eclipse.ui.editors">
 <editor
            class="org.my.editor.MyEditor"
            id="org.my.editor.myeditor"
            name="My Editor">
            <contentTypeBinding
    contentTypeId="org.my.contenttype.my">
   </contentTypeBinding>
      </editor>
   </extension>
   <extension point="org.eclipse.core.contenttype.contentTypes">
  <content-type base-type="org.eclipse.core.runtime.xml"
   id="org.my.contenttype.my"
   name="My File" priority="high">
   <describer
    class="org.my.editor.describer.MyDescriber">
   </describer>
  </content-type>
 </extension> 
   

Comments

Popular posts from this blog

Consuming a RESTful Web Service with C++ using QT libraries

How to develop CXF based JAX-WS with WSO2 Developer Studio

Quick Start Apache Stratos from EC2 images