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

de.mcs.jmeasurement.renderer.DefaultTextRenderer 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 java.text.DateFormat;

import org.xml.sax.SAXException;

import de.mcs.jmeasurement.DefaultMeasurePoint;
import de.mcs.jmeasurement.MeasureData;
import de.mcs.jmeasurement.MeasureFactory;
import de.mcs.jmeasurement.MeasurePoint;
import de.mcs.jmeasurement.SnapShot;
import de.mcs.jmeasurement.exception.MeasurementException;
import de.mcs.jmeasurement.utils.MemoryHelper;
import de.mcs.utils.Files;
import de.mcs.utils.StringUtils;

/**
 * The default text renderer is a renderer which will render the data into a
 * normal text based format. Every line represent one measurepoint. The data
 * will be seperated with <TAB> or <SPACE> character. All fields
 * will be extactly fieldSize characters long. There is the
 * possibility to determine the fieldsize from the names of the data values, or
 * you can force the fieldsizes.
 * 
 * @author w.klaas
 */
public class DefaultTextRenderer
    implements MeasureDataRenderer, MeasureDataRendererColumnHeader, MeasureDataRendererSnapshot {

  /** default line count. */
  private static final int DEFAULT_LINE_COUNT = 0;

  /** default field size. */
  private static final int DEFAULT_FIELD_SIZE = 20;

  /** default page size. */
  private static final int DEFAULT_PAGE_SIZE = 80;

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

  /** CRLF Character. */
  private static final String CRLF = "\n";

  /** FF Character. */
  private static final String FF = "\f";

  /** FF Character. */
  private static final String TAB = "\t";

  /** SPCAE Character. */
  private static final String SPACE = " ";

  /**
   * page size in lines, after this count a FF will be added and a new header
   * will be written.
   */
  private int pageSize = DEFAULT_PAGE_SIZE;

  /** internal counter of lines. */
  private int lineCount = DEFAULT_LINE_COUNT;

  /** exact size of the field data. */
  private int fieldsize = DEFAULT_FIELD_SIZE;

  /** holding the empty measurepoint for adding header to page begins. */
  private MeasurePoint emptyPoint = null;

  /** array with all sizes of the fields. */
  private int[] fieldSizes;

  /** using the tab character for field separation. */
  private boolean useTab;

  /**
   * Construct this renderer with the default fieldsize of 20. Fieldseparation
   * will be set to TAB.
   * 
   * @param aPageSize
   *            count of lines until a FF is made.
   */
  public DefaultTextRenderer(final int aPageSize) {
    this(aPageSize, DEFAULT_FIELD_SIZE, true);
  }

  /**
   * Construct this renderer. If you set fieldSize to -1 then the fieldsize
   * will be evaluated from the length of the field name.
   * 
   * @param aPageSize
   *            count of lines until a FF is made.
   * @param aFieldSize
   *            count of characters for a field
   * @param aUseTab
   *            use tab character for field separation
   */
  public DefaultTextRenderer(final int aPageSize, final int aFieldSize, final boolean aUseTab) {
    this.pageSize = aPageSize;
    this.fieldsize = aFieldSize;
    this.fieldSizes = new int[0];
    this.useTab = aUseTab;
  }

  /**
   * Construct this renderer. The fieldsize will be evaluated from the
   * aFieldSizes array. Field separation will set to SPACE character.
   * 
   * @param aPageSize
   *            count of lines until a FF is made.
   * @param aFieldSizes
   *            array with values of fieldsizes
   */
  public DefaultTextRenderer(final int aPageSize, final int[] aFieldSizes) {
    this.pageSize = aPageSize;
    this.fieldSizes = aFieldSizes.clone();
    this.useTab = false;
  }

  /**
   * @see de.mcs.jmeasurement.renderer.MeasureDataRenderer#getDataAsString(MeasurePoint,
   *      String)
   * @param point
   *            goes in.
   * @param prefix
   *            not used in this renderer
   * @return String the string representation of the values
   */
  public final String getDataAsString(final MeasurePoint point, final String prefix) {
    StringBuilder builder = new StringBuilder(BUFFER_SIZE);
    builder.append(checkLineCount());
    MeasureData[] datas = point.getData();
    // String[] values = new String[datas.length];
    boolean first = true;
    for (int j = 0; j < datas.length; j++) {
      MeasureData data = datas[j];
      if (!first) {
        if (useTab) {
          builder.append(TAB);
        } else {
          builder.append(SPACE);
        }
      } else {
        first = false;
      }
      if (data.getName().equals(DefaultMeasurePoint.DATA_KEY_EXCEPTION_LIST)) {
        String[] exceptions = StringUtils.csvStringToArray(data.getAsString(), ',', '"');
        if (exceptions.length > 0) {
          builder.append(formatFieldData(exceptions[0], j));
        }
      } else {
        builder.append(formatFieldData(data.getAsString(), j));
      }
    }
    builder.append(CRLF);
    lineCount++;
    return builder.toString();
  }

  /**
   * Getting the value formattet to the actual fieldsize.
   * 
   * @param aValue
   *            value to format
   * @param pos
   *            array index of this field
   * @return String
   */
  private String formatFieldData(final String aValue, final int pos) {
    StringBuilder value = new StringBuilder(aValue);
    int size = fieldsize;
    if (fieldSizes.length > 0) {
      if (pos < fieldSizes.length) {
        size = fieldSizes[pos];
      }
    }
    while (value.length() < size) {
      value.append(SPACE);
    }
    if (value.length() > size) {
      value = new StringBuilder(value.substring(0, size));
    }
    return value.toString();
  }

  /**
   * checking if a new page has to begin.
   * 
   * @return String empty or the header line
   */
  private String checkLineCount() {
    if (lineCount >= pageSize) {
      lineCount = 0;
      return FF + getPageHeader();
    } else {
      return "";
    }
  }

  /**
   * @see MeasureDataRendererColumnHeader#getColumnHeaderAsString(de.mcs.jmeasurement.MeasurePoint)
   * @param point
   *            goes in.
   * @return String the string representation of the column headers
   */
  public final String getColumnHeaderAsString(final MeasurePoint point) {
    if (null == emptyPoint) {
      emptyPoint = point;
      if (fieldsize < 0) {
        // getting the column sizes
        MeasureData[] datas = emptyPoint.getData();
        fieldSizes = new int[datas.length];
        for (int j = 0; j < datas.length; j++) {
          MeasureData data = datas[j];
          fieldSizes[j] = data.getName().length();
        }
      }
    }
    return getPageHeader();
  }

  /**
   * writing the output.
   * 
   * @return String header information
   */
  private String getPageHeader() {
    StringBuilder builder = new StringBuilder(BUFFER_SIZE);
    MeasureData[] datas = emptyPoint.getData();
    // String[] values = new String[datas.length];
    boolean first = true;
    for (int j = 0; j < datas.length; j++) {
      MeasureData data = datas[j];
      if (!first) {
        if (useTab) {
          builder.append(TAB);
        } else {
          builder.append(SPACE);
        }
      } else {
        first = false;
      }
      builder.append(formatFieldData(data.getName(), j));
    }
    builder.append(CRLF);
    lineCount++;
    return builder.toString();
  }

  /**
   * @return Returns the fieldsize.
   */
  public final int getFieldsize() {
    return fieldsize;
  }

  /**
   * @param aFieldsize
   *            The fieldsize to set.
   */
  public final void setFieldsize(final int aFieldsize) {
    this.fieldsize = aFieldsize;
  }

  /**
   * @return Returns the pageSize.
   */
  public final int getPageSize() {
    return pageSize;
  }

  /**
   * @param aPageSize
   *            The pageSize to set.
   */
  public final void setPageSize(final int aPageSize) {
    this.pageSize = aPageSize;
  }

  /**
   * @return Returns the lineCount.
   */
  public final int getLineCount() {
    return lineCount;
  }

  /**
   * @param snapShot
   *            the snapshot to start with.
   * @return String
   * @see MeasureDataRendererSnapshot#startSnapShot(SnapShot)
   */
  public final String startSnapShot(final SnapShot snapShot) {
    StringBuilder builder = new StringBuilder(BUFFER_SIZE);
    builder.append("Snapshot: ");
    builder.append(snapShot.getName());
    builder.append(CRLF);
    builder.append("date: ");
    builder.append(DateFormat.getDateTimeInstance().format(snapShot.getDate()));
    builder.append(CRLF);
    builder.append("max memory: ");
    builder.append(MemoryHelper.toGUIString(snapShot.getMaxMemory()));
    builder.append(", bytes: ");
    builder.append(Long.toString(snapShot.getMaxMemory()));
    builder.append(CRLF);
    builder.append("free memory: ");
    builder.append(MemoryHelper.toGUIString(snapShot.getFreeMemory()));
    builder.append(", bytes: ");
    builder.append(Long.toString(snapShot.getFreeMemory()));
    builder.append(CRLF);
    builder.append("total memory: ");
    builder.append(MemoryHelper.toGUIString(snapShot.getTotalMemory()));
    builder.append(", bytes: ");
    builder.append(Long.toString(snapShot.getTotalMemory()));
    builder.append(CRLF);
    return builder.toString();
  }

  /**
   * @param snapShot
   *            the snapshot to start with.
   * @return String
   * @see MeasureDataRendererSnapshot#startSnapShot(SnapShot)
   */
  public final String endSnapShot(final SnapShot snapShot) {
    return "";
  }

  /**
   * 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 DefaultTextRenderer(80));
    Files.writeStringToFile(new File(outfile), report);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy