Using the Reporting Infrastructure

The interface ReportGenerator provides a method that is given a resource bundle and a ProjectCodeInfo and writes all information to the configured stream.

The AbstractOutputReportGenerator provides handles that may be overridden by subclasses to write certain information. Please refer to the type hierarchy to select the implementation most suitable to subclass.

The simple HTML Report

Running Main will generate a simple HTML report.

You may want to have a look at the sources to see how the information parts are generated to produce the report.

In the following fragment the configuration is build that provides the generation process with all relevant information.

    final String classPath = System.getProperty("java.class.path");
    final List<String> classRootDirectoryNames = Helper.split(classPath, ";");
    final String sourcePath = commandLine.getOptionValue(JAVA_SOURCE_PATH);
    final List<String> sourceRootDirectoryNames = Helper.split(sourcePath, ";");

    final String includesString = commandLine.getOptionValue(REPORT_INCLUDES,
        "**");
    final List<String> includes = Helper.split(includesString, ",");
    final String excludesString = commandLine.getOptionValue(REPORT_EXCLUDES);
    final List<String> excludes = Helper.split(excludesString, ",");

    final ProjectConfiguration<XMLStreamWriter> config = new DefaultProjectConfiguration<XMLStreamWriter>(
        commandLine.getOptionValue(PROJECT_NAME));
    config.setIncludes(includes);
    config.setExcludes(excludes);
    config.setSourceEncoding(commandLine.getOptionValue(JAVA_SOURCE_ENCODING,
        "UTF-8"));
    config.setClassRootDirectoryNames(classRootDirectoryNames);
    config.setSourceRootDirectoryNames(sourceRootDirectoryNames);

    config.setReportEncoding(commandLine.getOptionValue(REPORT_ENCODING,
        "UTF-8"));
    config.setReporter(commandLine.getOptionValue(REPORT_GENERATOR,
        "de.smartics.exceptions.report.generator.HtmlReportGenerator"));
    config.setReport(commandLine.getOptionValue(REPORT_FILE));

    final ResourceBundle bundle = ResourceBundle.getBundle(
        "de/smartics/exceptions/ExceptionCodeReportBundle", Locale.ENGLISH,
        Main.class.getClassLoader()); // NOPMD
    config.setBundle(bundle);
    ...
  }

Generating the report involves two steps:

  1. Fetching the information bean
  2. Writing the information as a report

Fetching the Information Bean

The information is fetched this way:

  public void run(
      final ProjectConfiguration<XMLStreamWriter> config,
      final XMLStreamWriter output) throws ReportException
  {
    final ParserFactoryLoader loader = new ParserFactoryLoader();
    final ParserFactory parserFactory = loader.load(config.getClass()
        .getClassLoader());
    config.setParserFactory(parserFactory);

    final CodeDiscoverer discoverer = new CodeDiscoverer();
    final ProjectCodeInfo codeInfo = discoverer.run(config);
    final JavadocCollector collector = new JavadocCollector();
    collector.run(config, codeInfo);
    ...

The code discovering finds the enumerations that implement the codes, the Javadoc collector fetches the Javadoc information for the enumeration constants found.

Writing the Information as a Report

And here the report is written:

...
   if (output != null)
    {
      final ReportGenerator<XMLStreamWriter> reporter = config
          .getReporterInstance();
      final String reportEncoding = config.getReportEncoding();
      final ResourceBundle bundle = config.getBundle();
      reporter.setOutput(output);
      reporter.setEncoding(reportEncoding);
      reporter.writeReport(bundle, codeInfo);
    }
    else
    {
      writeReport(config, codeInfo);
    }

The code reads the configured HTML reporter and sends the code information together with a resource bundle to write the report. The resource bundle contains labels presented in the report (table headers et al.).