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

de.mcs.jmeasurement.renderer.DefaultHTMLRenderer 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.DefaultMeasurePoint;
import de.mcs.jmeasurement.MeasureData;
import de.mcs.jmeasurement.MeasureFactory;
import de.mcs.jmeasurement.MeasurePoint;
import de.mcs.jmeasurement.MeasurementException;
import de.mcs.jmeasurement.MemoryHelper;
import de.mcs.jmeasurement.SnapShot;
import de.mcs.utils.Files;
import de.mcs.utils.StringUtils;

/**
 * This class is used to render the output of the MaesurementFactory into a
 * simple html-based report. Header, Footer and page oriented information are
 * used for creating the html side. The MeasureDataRenderer,
 * MeasureDataRendererColumnHeader and the
 * MeasureDataRendererPage interfaces are implemented.
 * 
 * @author w.klaas
 */
public class DefaultHTMLRenderer implements MeasureDataRenderer, MeasureDataRendererColumnHeader,
    MeasureDataRendererPage, MeasureDataRendererSnapshot {

  /** the CR LF characters. */
  private static final String CRLF = "\r\n";

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

  /** line counter to do some pretty background coloring in the table. */
  private int lineCount;

  /**
   * This methode will generate the string representation of the desired
   * renderer for one measure point.
   * 
   * @param point
   *            goes in.
   * @param jumpPrefix
   *            the prefix needed for jumping in the html file.
   * @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 jumpPrefix) {
    StringBuffer stringBuffer = new StringBuffer(BUFFER_SIZE);
    MeasureData[] datas = point.getData();
    // String[] values = new String[datas.length];
    if ((lineCount % 2) > 0) {
      stringBuffer.append("");

    } else {
      stringBuffer.append("");

    }
    stringBuffer.append(CRLF);
    String[] exceptions = new String[0];
    for (int j = 0; j < datas.length; j++) {
      MeasureData data = datas[j];
      stringBuffer.append("");
      if (data.getName().equals(DefaultMeasurePoint.DATA_KEY_EXCEPTION_LIST)) {
        exceptions = StringUtils.csvStringToArray(data.getAsString(), ',', '"');
        if (exceptions.length > 0) {
          stringBuffer.append("View");
        } else {
          stringBuffer.append(" ");
        }
      } else {
        String string = data.getAsString();
        if ((string == null) || string.equals("")) {
          string = " ";
        }
        stringBuffer.append(string);
      }
      stringBuffer.append("");
      stringBuffer.append(CRLF);
    }
    stringBuffer.append("");
    stringBuffer.append(CRLF);
    if (exceptions.length > 0) {
      stringBuffer.append("\r\n");
      stringBuffer.append("");
      stringBuffer.append("");
      stringBuffer.append("
");
      stringBuffer.append("Stacktraces: \r\n");
      for (int i = 0; i < exceptions.length; i++) {
        String string = exceptions[i];
        stringBuffer.append("Exception " + Integer.toString(i));
        stringBuffer.append(CRLF);
        stringBuffer.append(string);
        stringBuffer.append(CRLF);
      }
      stringBuffer.append("
"); stringBuffer.append(CRLF); stringBuffer.append(""); stringBuffer.append(CRLF); stringBuffer.append(""); stringBuffer.append(CRLF); } lineCount++; return stringBuffer.toString(); } /** * 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]; stringBuffer.append(""); stringBuffer.append(CRLF); stringBuffer.append(""); stringBuffer.append(CRLF); for (int j = 0; j < datas.length; j++) { MeasureData data = datas[j]; stringBuffer.append(""); stringBuffer.append(data.getName()); stringBuffer.append(""); stringBuffer.append(CRLF); } stringBuffer.append(""); stringBuffer.append(CRLF); stringBuffer.append(""); stringBuffer.append(CRLF); return stringBuffer.toString(); } /** * @return getting the header for an report * @see de.mcs.jmeasurement.renderer.MeasureDataRendererPage#getReportHeader() */ public final String getReportHeader() { StringBuffer stringBuffer = new StringBuffer(BUFFER_SIZE); stringBuffer.append(""); stringBuffer.append(CRLF); stringBuffer.append(""); stringBuffer.append(CRLF); stringBuffer.append("").append(CRLF); stringBuffer.append("measurement report for "); stringBuffer.append(MeasureFactory.getApplicationName()); stringBuffer.append(""); stringBuffer.append(CRLF); stringBuffer.append(""); stringBuffer.append(CRLF); stringBuffer.append(""); stringBuffer.append(CRLF); stringBuffer.append("

JMeasurement HTML Report for "); stringBuffer.append(MeasureFactory.getApplicationName()); stringBuffer.append("


"); return stringBuffer.toString(); } /** * @return getting the footer for an report * @see de.mcs.jmeasurement.renderer.MeasureDataRendererPage#getReportFooter() */ public final String getReportFooter() { StringBuffer stringBuffer = new StringBuffer(BUFFER_SIZE); stringBuffer.append(""); stringBuffer.append(CRLF); stringBuffer.append(""); return stringBuffer.toString(); } /** * @return getting a header for an page * @see de.mcs.jmeasurement.renderer.MeasureDataRendererPage#beginPage() */ public final String beginPage() { lineCount = 0; StringBuffer stringBuffer = new StringBuffer(BUFFER_SIZE); stringBuffer.append(""); stringBuffer.append(CRLF); return stringBuffer.toString(); } /** * @return getting a footer for an page * @see de.mcs.jmeasurement.renderer.MeasureDataRendererPage#endPage() */ public final String endPage() { StringBuffer stringBuffer = new StringBuffer(BUFFER_SIZE); stringBuffer.append("
"); stringBuffer.append(CRLF); return stringBuffer.toString(); } /** * @param snapShot * the snapshot to start with. * @return String * @see MeasureDataRendererSnapshot#startSnapShot(SnapShot) */ public final String startSnapShot(final SnapShot snapShot) { StringBuffer stringBuffer = new StringBuffer(BUFFER_SIZE); stringBuffer.append("

Snapshot: "); stringBuffer.append(snapShot.getName()); stringBuffer.append("

"); stringBuffer.append(CRLF); stringBuffer.append("max memory: "); stringBuffer.append(MemoryHelper.toGUIString(snapShot.getMaxMemory())); stringBuffer.append(", bytes: "); stringBuffer.append(Long.toString(snapShot.getMaxMemory())); stringBuffer.append("
"); stringBuffer.append(CRLF); stringBuffer.append("free memory: "); stringBuffer.append(MemoryHelper.toGUIString(snapShot.getFreeMemory())); stringBuffer.append(", bytes: "); stringBuffer.append(Long.toString(snapShot.getFreeMemory())); stringBuffer.append("
"); stringBuffer.append(CRLF); stringBuffer.append("total memory: "); stringBuffer.append(MemoryHelper.toGUIString(snapShot.getTotalMemory())); stringBuffer.append(", bytes: "); stringBuffer.append(Long.toString(snapShot.getTotalMemory())); stringBuffer.append("
"); stringBuffer.append(CRLF); return stringBuffer.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 DefaultHTMLRenderer()); Files.writeStringToFile(new File(outfile), report); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy