Skip navigation links
www.sdmetrics.com

Package com.sdmetrics.output

Provides data export to text files, HTML files, Excel, and OpenDocument Spreadsheets.

See: Description

Package com.sdmetrics.output Description

Provides data export to text files, HTML files, Excel, and OpenDocument Spreadsheets.

Tutorial - exporting metric data and rule violations

The following code snippets take you to the steps of exporting measurement data provided through the interface DataTables.

  1. Required imports
    import sdmetrics.app.ConsoleMessageHandler;
    import sdmetrics.app.MetricData;
    import sdmetrics.app.RuleData;
    import sdmetrics.output.DataExportConfiguration;
    import sdmetrics.output.DataExporter;
    import sdmetrics.output.ExportFormatter;
    import sdmetrics.output.ExportFormatterFactory;
    import sdmetrics.output.OverwriteConfirmation;
    
  2. Have the data tables you wish to export ready
    In this example, we continue to use the metrics and design rules obtained from the tutorial for package com.sdmetrics.app.
    MetricData metrics = ...; // Metric data, need not be pre-calculated
    RuleData rules = ...;     // Rule violations, must already have been checked
    
  3. Create a data exporter
    To create a data exporter, we need a data export configuration object and a message handler to report any problems that may arise during data export. Here, we create a default output configuration (output to a single file, using the platform's default character encoding, overwriting existing files), and write messages to the console:
    DataExportConfiguration config = new DataExportConfiguration();
    ConsoleMessageHandler msgHandler = new ConsoleMessageHandler();
    DataExporter exporter = new DataExporter(msgHandler, config);
    
  4. Choose a file format and export the data
    We obtain an output formatter to create tab-separated text files, and write the metrics data to file "sampleMetrics.txt" in the current directory:
    ExportFormatter textFormat = ExportFormatterFactory.getFormatter("txt");
    exporter.export(metrics, "sampleMetrics", textFormat);
    
  5. Adjust the output configuration if necessary
    To illustrate some of the export configuration features, the following example writes the rule violation data to separate HTML files, using UTF-8 character encoding, and have the user confirm file overwrites:
    config.setSingleOutputFile(false);
    config.setEncoding("UTF-8");
    config.setOverwriteConfirmer(new ConsoleOverwriteConfirmation());
    
    ExportFormatter htmlFormat = ExportFormatterFactory.getFormatter("html");
    exporter.export(rules, "sampleRules", htmlFormat);
    
    Note that class ConsoleOverwriteConfirmation is not provided by SDMetrics. The following implementation prompts the user at the console, and offers "no/yes/yes to all" options to confirm file overwrites.
    class ConsoleOverwriteConfirmation extends OverwriteConfirmation {
       boolean yesToAllGiven = false;
    
       public boolean okToOverwrite(String fileName, boolean oneFile) {
          if (!new java.io.File(fileName).exists() || yesToAllGiven)
             return true;
    
          System.out.println("OK to overwrite file " + fileName + "?");
          String reply = System.console().readLine(
                   "[N] for no, [Y] for yes%s",
                   (oneFile ? ": " : ", [A] for yes to all: "));
                   
          if ("Y".equalsIgnoreCase(reply))
             return true;
          if (!oneFile && "A".equalsIgnoreCase(reply)) {
             yesToAllGiven = true;
             return true;
          }
          return false;
       }
     }
    
Skip navigation links
www.sdmetrics.com