## Content Sensitive Object Contributions

Previously, the context menus of some applications were limited by the exclusion of certain menu options. This was based on the known peripheral information, such as the number of selected resources, the physical name of the file, the type of resource, and so on. In some instances, a restricted amount of information was known about the contents of a resource and this significantly reduced unusable menu options. Such information, could be of great use in an XML file as there are numerous situations in which an action is applicable for one type of XML file but not another, such as XML files that contain Ant scripts. The action Run Ant..., while logical in its context menu, is not an action that is applicable to an XML file that is used to define a plug-in.

With the addition of the extension point, org.eclipse.core.runtime.contentTypes, Eclipse now provides a 'content type', allowing plug-ins to contribute to the Platform content type catalog, further, classes called Content Type Describers are also available. For developers providing their own content types, there are two customizable, built-in content type describers available: BinarySignatureDescriber (for binary content types) and XMLRootElementContentDescriber (for text, XML based content types). Additionally, plug-in providers may create their own content describers, for detailed information see, the Platform Plug-in Developer Guide (Programmer's Guide > Runtime overview > Content types).

### XMLRootElementContentDescriber

It is now possible to define object contributions specific to an XML file with a given top level tag or that specifies a given DTD. To do this, define an extension to the org.eclipse.core.runtime.contentTypes extension point with a describer class of XMLRootElementContentDescriber and parameters indicating the top level tag name or the dtd name as follows:

       <extension
           point="org.eclipse.core.runtime.contentTypes">
           <content-type
               id=<id>
               name=<name>
               base-type="org.eclipse.core.runtime.xml">
               <describer class="org.eclipse.core.runtime.content.XMLRootElementContentDescriber">
                   <parameter name="element" value=tagValue />
               </describer>
           </content-type>
       </extension>

or

       <extension
           point="org.eclipse.core.runtime.contentTypes">
           <content-type
               id=<id>
               name=<name>
               base-type="org.eclipse.core.runtime.xml">
               <describer class="org.eclipse.core.runtime.content.XMLRootElementContentDescriber">
                   <parameter name="dtd" value=dtdValue />
               </describer>
           </content-type>
        </extension>

> where tagValue represents the name of the top level tag to match and

> dtdValue represents the name of the DTD as seen in the XML file.

Consider the following object contribution in a plugin.xml file:

       <extension
           point="org.eclipse.core.runtime.contentTypes">
           <content-type
               id="topElementContentType"
               name="Tests top-level element recognition"
               base-type="org.eclipse.core.runtime.xml"
               priority="high">
               <describer class="org.eclipse.core.runtime.content.XMLRootElementContentDescriber">
                   <parameter name="element" value="myTag" />
               </describer>
           </content-type>
       </extension>

       <extension
           point="org.eclipse.core.runtime.contentTypes">
           <content-type
               id="dtdContentType"
               name="Tests dtd element recognition"
               base-type="org.eclipse.core.runtime.xml"
               priority="high">
               <describer class="org.eclipse.core.runtime.content.XMLRootElementContentDescriber">
                   <parameter name="dtd" value="myDTD.xml" />
               </describer>
           </content-type>
       </extension>

       <extension point="org.eclipse.ui.popupMenus">
           <objectContribution
               id="org.eclipse.ui.examples.objectContributions"
               objectClass="org.eclipse.core.resources.IFile"
               nameFilter="*.xml">
               <visibility>
                   <or>
                       <objectState
                           name="contentTypeId"
                           value="org.eclipse.ui.examples.topElementContentType"/>
                       <objectState
                           name="contentTypeId"
                           value="org.eclipse.ui.examples.dtdContentType"/>
                   </or>
               </visibility>
               <action id="org.eclipse.ui.examples.objectContributions.action1"
                   label="%PopupMenus.action"
                   icon="icons/ctool16/openbrwsr.gif"
                   menubarPath="additions"
                   class="org.eclipse.ui.examples.objectContributions.PopupMenuActionDelegate"
                   enablesFor="1">
               </action>
           </objectContribution>
       </extension>

This will make action1 visible for any IFile with a name matching *.xml provided it contains myTag as the top level XML tag or it uses the DTD called myDTD.xml. So the following XML files will match:

>     <?xml version="1.0" encoding="UTF-8"?>
>     <myTag
>       id="org.eclipse.ui.workbench"
>       name="%pluginName"
>       version="3.0.0"
>       provider-name="%providerName"
>       class="org.eclipse.ui.internal.WorkbenchPlugin">
>     </myTag>

or

>     <?xml version="1.0" encoding="UTF-8"?>
>     <!DOCTYPE Book SYSTEM "myDTD.xml">
>     <fragment
>        id="org.eclipse.ui.workbench"
>        name="%pluginName"
>     	version="3.0.0"
>     	provider-name="%providerName"
>     	class="org.eclipse.ui.internal.WorkbenchPlugin">
>     	<runtime>
>     	   <library name="workbench.jar">
>     	      <export name="*"/>
>     		  <packages prefixes="org.eclipse.ui, org.eclipse.jface"/>
>     	   </library>
>     	</runtime>
>     </fragment>

### BinarySignatureDescriber

The BinarySignatureDescriber is a content describer that detects a specified binary 'signature' at a given offset within a file. This describer is used in the same fashion as the XMLRootElementContentDescriber with the exception that it takes parameters "signature", "offset" and "required" instead of "element" or "dtd". The Javadoc for BinarySignatureDescriber provides complete details on this content describer's class usage.