Time for action – error markers if file is empty

Errors and warning markers are used to indicate if there are problems in the source files. These are used by the Eclipse compiler to indicate Java compile errors, but they are also used for non-Java errors. For example, text editors also show warnings when words are misspelled. A warning can be shown if the .minimark file is empty and the title is missing.

There isn't a simple way of accessing the file's size in Eclipse, so one heuristic is that if the generated HTML file is less than about 100 bytes then there probably wasn't much to start with anyway.

  1. Open the MinimarkVisitor class and go to the processResource method.
  2. When the HTML file is generated, put in a test to determine if the size is less than 100 bytes:
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    MinimarkTranslator.convert(new InputStreamReader(in),
     new OutputStreamWriter(baos));
    ByteArrayInputStream contents = new ByteArrayInputStream(
     baos.toByteArray());
    if (baos.size() < 100) {
      System.out.println("Minimark file is empty");
    }
  3. The problem with printing out an error message is that it won't be seen by the user. Instead, it can represented with an IMarker object, created on the source resource, and with additional properties to set the type and location of the error:
    // System.out.println("Minimark file is empty");
    IMarker marker = resource.createMarker(IMarker.PROBLEM);
    marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_ERROR);
    marker.setAttribute(IMarker.MESSAGE, "Minimark file is empty");
    marker.setAttribute(IMarker.LINE_NUMBER, 1);
    marker.setAttribute(IMarker.CHAR_START, 0);
    marker.setAttribute(IMarker.CHAR_END, 0);
  4. Run the Eclipse instance and create a new empty .minimark file. When it is saved, an error will be reported in the Problems view. If it's not shown, it can be opened with Window | Show View | Other | General | Problems:
    Time for action – error markers if file is empty

What just happened?

A heuristic that detected when the input file was likely to be empty was used to generate a marker in the build view. When creating an empty file, the builder runs and the problem is generated in the problems view.

The marker also allows additional fields to be set if the location of the problem is known. These are optional fields but can be used to give a user more information as to the source of the problem. In the cases of a misspelled word, the CHAR_START and CHAR_END constants can be used to pinpoint the exact word on a line; in the cases of more general errors, the LINE_NUMBER constant can be used to indicate the approximate location in the file.

Each time the file is changed; additional markers are created in the problems view. Even if content is added, the existing errors aren't removed. That will be fixed in the next section.

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

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