Howto: Register Annotation Routers

From Eclipsepedia

Revision as of 13:21, 23 June 2011 by Nmehrega.cisco.com (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

< To: Tigerstripe_APIs
Annotation Framework provide configuration to setting where annotations can be saved. This configuration named Annotation Routers. Annotation Routers map annotation object to corresponding storage. If there is no one router specify for the annotation it will be saved in the default annotations location (located in the plug-in metadata). Annotation Router can be registered with the org.eclipse.tigerstripe.annotation.core.router extension point:

 <extension
       point="org.eclipse.tigerstripe.annotation.core.router">
    <router
          class="org.eclipse.tigerstripe.annotation.example.router.ResourceRouter"/>
 </extension>

ResourceRouter show how we can use router to save resource annotation to the project specific path:

 public class ResourceRouter implements EObjectRouter {
     private static final String ANNOTATIONS_FILE_NAME = ".ann";
     protected URI getUri(IResource res) {
         IPath path = res.getProject().getFullPath();
         if (path != null) {
             path = path.append(ANNOTATIONS_FILE_NAME);
             return URI.createPlatformResourceURI(path.toString(), false);
         }
         return null;
     }
     public URI route(EObject object) {
         if (object instanceof Annotation) {
             Annotation annotation = (Annotation)object;
             Object annotable =  AnnotationPlugin.getManager().getAnnotatedObject(annotation);
             if (annotable instanceof IResource) {
                 IResource resource = (IResource)annotable;
                 return getUri(resource);
             }
         }
         return null;
     }
 }

With the route(EObject) method we map object to some URI. If this object can not be routed (annotable object is not a resource), this method return null and will be saved to the default location or location specified with an another router. This URI will be used to get resource with ResourceSet.createResource(URI) and ResourceSet.getResource(URI, boolean) methods