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