The next step is to create an IFile
resource for the .html
file (based on the name of the .minimark
file). Eclipse uses an IPath
object to represent a filename from the root of the workspace. An IPath
of /project/folder/file.txt
refers to the file.txt
file in a folder called folder
contained within the project called project
. The root path represents the IWorkspaceRoot
. Perform the following steps:
processResource()
method of MinimarkVisitor
, calculate the new filename, and use it to create an IFile
from the file's parent IContainer
:try { IFile file = (IFile) resource; String htmlName = file.getName().replace(".minimark", ".html"); IContainer container = file.getParent(); IFile htmlFile = container.getFile(new Path(htmlName));
InputStream
has to be passed to the setContents()
method. The easiest way to create this is to pass a ByteArrayOutputStream
to MinimarkTranslator
, and then use a ByteArrayInputStream
to get the contents to set on the file.ByteArrayOutputStream baos = new ByteArrayOutputStream(); MinimarkTranslator.convert(new InputStreamReader(in),new OutputStreamWriter(System.out)); //The following commented line needs to be removed /*new OutputStreamWriter(baos));*/ ByteArrayInputStream contents = new ByteArrayInputStream(baos.toByteArray());
setContents()
, but if it doesn't create()
needs to be called instead:if (htmlFile.exists()) { htmlFile.setContents(contents, true, false, null); } else { htmlFile.create(contents, true, null); }
The true
parameter is to force the change; that is, the contents will be written even if the resource has been updated elsewhere. The false
parameter for the setContents()
method is to indicate that changes should not be recorded, and the final null
parameter for both methods is to pass an optional ProgressMonitor
.
htmlFile.setDerived(true,null);
.minimark
file, do a clean again, and the HTML file should be regenerated.The build was modified to invoke MinimarkTranslator
and create a resource in the filesystem. Since it uses a stream to set the contents a ByteArrayOutputStream
is used to build up the translated contents, and a ByteArrayInputStream
is used to read it back for the purposes of setting the file's contents.
The exists()
method check is necessary because setting the contents on a non-existent file throws a CoreException
.
The files are represented as a generic IPath
object, which is concretely implemented with the Path
class. Path
classes are represented as slash (/) separated filenames, regardless of the operating system, but each path component needs to obey the local filesystem's constraints, such as not allowing colons on Windows.