Time for action – handling deletion

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

Perform the following steps:

  1. Run the 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 MinimarkVisitor class' processResource() method to see if the resource exists or not:
    private void processResource(IResource resource) throwsCoreException {
      if (resource instanceof IFile && resource.exists()) {
  3. This solves the NullPointerException, but the generated HTML file is left behind. To clean up the associated .html file if the .minimark file is being deleted, the resource delta's flags can be inspected to see if it is deleted, and if so, the corresponding HTML file 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 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, IResourceDelta needs to track deletions of .html files as well, and process the corresponding .minimark resource. Modify the visit(IResourceDelta) 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 Eclipse instance and delete the generated test.html file. It should be automatically regenerated by the MinimarkBuilder/MinimarkVisitor. Now, delete the test.html file and the corresponding test.minimark file should be deleted as well.

What just happened?

When a .minimark file was deleted, a NullPointerException was seen. This was fixed by guarding visit() with a check to see if 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

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

Implement a clean() method on the MinimarkBuilder 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