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

de.mcs.jmeasurement.renderer.CssHTMLRenderer 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.SnapShot;
import de.mcs.jmeasurement.exception.MeasurementException;
import de.mcs.jmeasurement.utils.MemoryHelper;
import de.mcs.utils.Files;
import de.mcs.utils.StringUtils;

/**
 * This class is used to render the output of the MaesurementFactory into a
 * html-based report using css styles. 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 CssHTMLRenderer 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;

  private String stylesheetname;

  private boolean fullHtml = true;

  public CssHTMLRenderer() {
  }

  /**
   * Constructs a new CssHTMLRender with the fullHtml option. 
   * @param fullHtml true build a full html site
   */
  public CssHTMLRenderer(boolean fullHtml) {
    this.fullHtml = fullHtml;
  }

  /**
   * Construct a new CssHtmlRenderer with the defined stylesheet name. 
   * @param stylesheetName name and path for the stylesheet file.
   */
  public CssHTMLRenderer(String stylesheetName) {
    this.stylesheetname = stylesheetName;
  }

  /**
   * 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) {
    StringBuilder sb = new StringBuilder(BUFFER_SIZE);
    MeasureData[] datas = point.getData();

    if ((lineCount % 2) > 0) {
      sb.append("\r\n");
    } else {
      sb.append("\r\n");
    }

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

JMeasurement HTML Report for %s", MeasureFactory.getApplicationName())) .append("


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

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

").append(CRLF); sb.append("
").append(CRLF); sb.append(String.format("max memory: %s, bytes: %d
", MemoryHelper.toGUIString(snapShot.getMaxMemory()), snapShot.getMaxMemory())).append(CRLF); sb.append(String.format("free memory: %s, bytes: %d
", MemoryHelper.toGUIString(snapShot.getFreeMemory()), snapShot.getFreeMemory())).append(CRLF); sb.append(String.format("total memory: %s, bytes: %d
", MemoryHelper.toGUIString(snapShot.getTotalMemory()), snapShot.getTotalMemory())).append(CRLF); sb.append("
").append(CRLF); return sb.toString(); } /** * @param snapShot * the snapshot to start with. * @return String * @see MeasureDataRendererSnapshot#startSnapShot(SnapShot) */ public final String endSnapShot(final SnapShot snapShot) { return ""; } public String getStylesheetname() { return stylesheetname; } public void setStylesheetname(String stylesheetname) { this.stylesheetname = stylesheetname; } /** * 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 CssHTMLRenderer("style.css")); Files.writeStringToFile(new File(outfile), report); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy