All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.mcs.jmeasurement.renderer.DefaultCSVDataRenderer Maven / Gradle / Ivy

There is a newer version: 1.1.226
Show newest version
/*
 * MCS Media Computer Software Copyright (c) 2005 by MCS
 * -------------------------------------- Created on 23.04.2005 by w.klaas
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package de.mcs.jmeasurement.renderer;

import java.io.File;
import java.io.IOException;

import org.xml.sax.SAXException;

import de.mcs.jmeasurement.MeasureData;
import de.mcs.jmeasurement.MeasureFactory;
import de.mcs.jmeasurement.MeasurePoint;
import de.mcs.jmeasurement.MeasurementException;
import de.mcs.utils.Files;
import de.mcs.utils.StringUtils;

/**
 * This class is used to render the output of the MaesurementFactory into a
 * simple csv-based report. No header, footer and page oriented information are
 * used. Only the MeasureDataRenderer and the
 * MeasureDataRendererColumnHeader interfaces are implemented.
 * 
 * @author w.klaas
 */
public class DefaultCSVDataRenderer implements MeasureDataRenderer, MeasureDataRendererColumnHeader {

  /** default buffer size. */
  private static final int BUFFER_SIZE = 1024;

  /** separator for this csv report. */
  private char localSeparator;

  /** delimiter for this csv report. */
  private char localDelimiter;

  /**
   * default constructor. Using <"> as field delimiter and
   * <,> as field separator.
   */
  public DefaultCSVDataRenderer() {
    this(',', '"');
  }

  /**
   * Constructor to define field separator and delimitter.
   * 
   * @param separator
   *            the field separator, default is <,>
   * @param delimiter
   *            the field delimiter, default is <">
   */
  public DefaultCSVDataRenderer(final char separator, final char delimiter) {
    localSeparator = separator;
    localDelimiter = delimiter;
  }

  /**
   * This methode will generate the string representation of the header of the
   * desired renderer for measure points.
   * 
   * @param point
   *            goes in.
   * @return String the string representation of the column headers
   * @see de.mcs.jmeasurement.renderer.MeasureDataRendererColumnHeader#getColumnHeaderAsString(MeasurePoint)
   */
  public final String getColumnHeaderAsString(final MeasurePoint point) {
    StringBuffer stringBuffer = new StringBuffer(BUFFER_SIZE);
    MeasureData[] datas = point.getData();
    String[] values = new String[datas.length];
    for (int j = 0; j < datas.length; j++) {
      MeasureData data = datas[j];
      values[j] = data.getName();
    }
    stringBuffer.append(StringUtils.arrayToCSVString(values, localSeparator, localDelimiter));
    stringBuffer.append("\r\n");
    return stringBuffer.toString();
  }

  /**
   * This methode will generate the string representation of the desired
   * renderer for one measure point.
   * 
   * @param point
   *            goes in.
   * @param prefix
   *            not used in this renderer
   * @return String the string representation of the values
   * @see de.mcs.jmeasurement.renderer.MeasureDataRenderer#getDataAsString(MeasurePoint,
   *      String)
   */
  public final String getDataAsString(final MeasurePoint point, final String prefix) {
    StringBuffer stringBuffer = new StringBuffer(BUFFER_SIZE);
    MeasureData[] datas = point.getData();
    String[] values = new String[datas.length];
    for (int j = 0; j < datas.length; j++) {
      MeasureData data = datas[j];
      values[j] = data.getAsString();
    }
    stringBuffer.append(StringUtils.arrayToCSVString(values, localSeparator, localDelimiter));
    stringBuffer.append("\r\n");
    return stringBuffer.toString();
  }

  /**
   * generating a report from persistence data.
   * 
   * @param args
   *            first argument is the file with the persistence data. Second
   *            must be the file the report should be exported to.
   * @throws IOException
   *             if something goes wrong.
   * @throws SAXException
   *             if something goes wrong.
   * @throws MeasurementException
   *             if something goes wrong.
   */

  public static void main(final String[] args) throws IOException, SAXException, MeasurementException {
    String infile = args[0];
    String outfile = args[1];
    MeasureFactory.loadFromXMLFile(infile, true);
    String report = MeasureFactory.getReport(new DefaultCSVDataRenderer());
    Files.writeStringToFile(new File(outfile), report);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy