Time for action – creating resources

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.

  1. In the 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));
  2. To create the contents of the file, an 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());
  3. Now the contents need to be set on the file. If the file exists, the contents are set with the 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);
    }

    Note

    The second argument true forces the change; that is, the contents will be written even if the resource has been updated elsewhere. The third argument false indicates that changes should not be recorded, and the final argument null for both methods is for passing an optional ProgressMonitor.

  4. Finally the resource needs to be marked as derived, which tells Eclipse that this is an automatically generated file and not a user-edited one:
    htmlFile.setDerived(true, null);
  5. Run the target Eclipse instance, navigate to the Project | Clean menu, and the corresponding HTML file should be generated. Modify the .minimark file, do a clean again, and the HTML file should be regenerated.

What just happened?

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.153.224