Time for action: handling deletion

The incremental builder does not handle deletion in its current implementation. To handle deletion, the IResourceDelta instance needs to be inspected to find out what kind of delta took place and handle deleted resources accordingly.

  1. Run the target Eclipse instance, and delete a .minimark file. An exception is thrown and reported to the user:
    Time for action: handling deletion
  2. To fix this issue, modify the check in the MinimarkVisitor method processResource to see whether the resource exists or not:
    private void processResource(IResource resource) throws
     CoreException {
      if (resource instanceof IFile && resource.exists()) {
  3. This solves the NullPointerException, but the generated HTML file is left behind. If the .minimark file is deleted, and there is a corresponding .html file, that can be deleted as well. Modify the visit(IResourceDelta) method as follows:
    public boolean visit(IResourceDelta delta) throws CoreException {
      boolean deleted = (IResourceDelta.REMOVED & delta.getKind())!=0;
      IResource resource = delta.getResource();
      String name = resource.getName();
      if (deleted) {
        String htmlName = name.replace(".minimark",".html");
        IFile htmlFile = resource.getParent().
          getFile(new Path(htmlName));
        if (htmlFile.exists()) {
          htmlFile.delete(true, null);
        }
      } else {
        processResource(resource);
      }
      return true;
    }
  4. Run the target Eclipse instance, and create a new test.minimark file. Save it, and a corresponding test.html file will be created. Delete the test.minimark file, and the test.html file should also be deleted.
  5. Create the test.minimark file again, and the test.html file will be generated. Delete the test.html file, and it won't be regenerated automatically. To fix this, the IResourceDelta needs to track deletions of .html files as well and process the corresponding .minimark resource. Modify the visit(IResourceDelta) method as follows:
    public boolean visit(IResourceDelta delta) throws CoreException {
      boolean deleted = (IResourceDelta.REMOVED & delta.getKind())!=0;
      IResource resource = delta.getResource();
      String name = resource.getName();
      if (name.endsWith(".minimark")) {
        if (deleted) {
          String htmlName = name.replace(".minimark",".html");
          IFile htmlFile = resource.getParent().getFile(
              new Path(htmlName));
          if (htmlFile.exists()) {
            htmlFile.delete(true, null);
          }
        } else {
          processResource(resource);
        }
      } else if (name.endsWith(".html")) {
        String minimarkName = name.replace(".html",".minimark");
        IFile minimarkFile = resource.getParent().getFile(
         new Path(minimarkName));
        if (minimarkFile.exists()) {
          processResource(minimarkFile);
        }
      }
      return true;
    }
  6. Run the target Eclipse instance, and delete the generated test.html file. It should be automatically regenerated by the MinimarkBuilder and MinimarkVisitor classes. Now delete the test.minimark file, and the corresponding test.html file should be deleted as well.

What just happened?

When a .minimark file was deleted, a NullPointerException was seen. This was fixed by guarding the visit method with a check to see whether the resource existed or not.

For consistency, the deletion of associated resources was also handled. If the .html file is deleted, it is regenerated (but only if a corresponding .minimark file is present). When the .minimark file is deleted, the corresponding .html file is also deleted.

Have a go hero – builder upgrades

As well as reacting to changes and creating content, builders are also responsible for removing content when the project is cleaned. The IncrementalProjectBuilder interface defines a clean method, which is invoked when the user performs a Project | Clean on either that specific project or the workspace as a whole.

Implement a clean method in the MinimarkBuilder class which walks the project and, for each .minimark file, deletes any corresponding .html file. Note that not all .html files should be deleted as some may be legitimate source files in a project.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.248.159