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 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:

  1. In the 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));
  2. To create the contents of the file, an 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.

    Note

    PipedInput/OutputStream are inherently broken because they use a fixed width pipeline and will block when full.

    Make the following changes:

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

    Note

    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.

  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 Eclipse instance, do Project | Clean 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 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.

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

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