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 the filename from the root of the workspace. An IPath
with /project/folder/file.txt
refers to the file.txt
file in a folder called folder
contained within the project project
. The root path represents the IWorkspaceRoot
.
processResource
method of the MinimarkVisitor
class, calculate the new filename, and use it to get an IFile
object from the file's parent IContainer
object:try { IFile file = (IFile) resource; InputStream in = file.getContents(); String htmlName = file.getName().replace(".minimark", ".html"); IContainer container = file.getParent(); IFile htmlFile = container.getFile(new Path(htmlName));
InputStream
instance has to be passed to the setContents
method. The easiest way to create this is to pass a ByteArrayOutputStream
instance to the convert
method, and then use a ByteArrayInputStream
instance to set the contents of the file:ByteArrayOutputStream baos = new ByteArrayOutputStream(); MinimarkTranslator.convert(new InputStreamReader(in), // new OutputStreamWriter(System.out)); new OutputStreamWriter(baos)); ByteArrayInputStream contents = new ByteArrayInputStream(baos.toByteArray());
setContents
method, but if it doesn't, the create
method needs to be called instead:if (htmlFile.exists()) { htmlFile.setContents(contents, true, false, null); } else { htmlFile.create(contents, true, null); }
htmlFile.setDerived(true, null);
.minimark
file, do a clean again, and the HTML file should be regenerated.The build was modified to invoke the 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 purpose of setting the file's contents.
The exists
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. Paths 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.
3.142.153.224