Package juju.reattore.perfcap.reporter.impl

Source Code of juju.reattore.perfcap.reporter.impl.VelocityReporter

/*  Reattore HTTP Server

    Copyright (C) 2002 Michael Hope <michaelh@juju.net.nz>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    $Id: VelocityReporter.java,v 1.3 2003/03/05 04:31:56 michaelh Exp $
*/

package juju.reattore.perfcap.reporter.impl;

import java.util.*;
import java.io.*;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.exception.*;

import juju.reattore.perfcap.reporter.Reporter;
import juju.reattore.perfcap.var.Variable;
import juju.reattore.perfcap.tester.Results;

/** Generates an arbitrary report using a user defined Velocity
    template.  The available variables are:

    <ul>
    <li><tt>root</tt> - Top level of a tree of Maps containing all of
    the results.  The map is indexed by variable name.</li>
    <li><tt>names</tt> - List of all of the variable names in order.</li>
    <li><tt>values</tt> - List of Lists of all of the values a
    variable took in order received.</li>
    </ul>

    @tag velocityreporter
    @group Reporter
*/
public class VelocityReporter
    implements Reporter {

    private String filter;
    private String template = "report.vm";
    private String out;

    private Map root = new LinkedHashMap();
    private List names = new ArrayList();
    private List values = new ArrayList();

    /** The property from the test results.  If unset, the leaf is the
        whole Results object.

        @param filter  The Bean name of the property.
     */
    public void setFilter(String filter) {
        this.filter = filter;
    }

    /** The template file name to process.

        @param file File name.
        @default report.vm
    */
    public void setTemplate(String file) {
        this.template = file;
    }

    /** Name of the file to write to.  If unset, writes to stdout.

        @param out  Name of the file.
    */
    public void setOut(String out) {
        this.out = out;
    }

    /** @see Reporter */
    public void add(List ind, Results res)
        throws Exception {

        if (names.size() == 0) {
            for (Iterator i = ind.iterator(); i.hasNext();) {
                Variable var = (Variable)i.next();

                values.add(new ArrayList());
                names.add(var.getName());
            }
        }

        for (int i = 0; i < ind.size(); i++) {
            Variable var = (Variable)ind.get(i);
            List row = (List)values.get(i);

            Object value = var.getValue();
            if (row.contains(value) == false) {
                row.add(value);
            }
        }

        Map m = root;

        for (Iterator i = ind.iterator(); i.hasNext();) {
            Variable var = (Variable)i.next();

            if (i.hasNext()) {
                Map next = (Map)m.get(var.getValue());

                if (next == null) {
                    next = new LinkedHashMap();

                    m.put(var.getValue(), next);
                }
                m = next;
            }
            else {
                if (filter != null) {
                    m.put(var.getValue(), BeanUtils.getProperty(res, filter));
                }
                else {
                    m.put(var.getValue(), res);
                }
            }
        }
    }

    /** @see Reporter */
    public void end()
        throws Exception {

        VelocityEngine engine = new VelocityEngine();
        engine.init();

        VelocityContext ctx = new VelocityContext();

        ctx.put("root", root);
        ctx.put("names", names);
        ctx.put("values", values);

        StringWriter writer = new StringWriter();

        if (engine.mergeTemplate(template, ctx, writer) != true) {
            throw new Exception("Error processing template");
        }

        writer.flush();

        if (out != null) {
            PrintWriter pw = new PrintWriter(new FileWriter(out));
            pw.print(writer.toString());
            pw.close();
        }
        else {
            System.out.println(writer.toString());
        }
    }
}
TOP

Related Classes of juju.reattore.perfcap.reporter.impl.VelocityReporter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.