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:
.minimark
file. An exception is thrown and reported to the user:MinimarkVisitor
class' processResource()
method to see if the resource exists or not:private void processResource(IResource resource) throwsCoreException {
if (resource instanceof IFile && resource.exists()) {
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; }
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.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; }
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.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.
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.