co.turnus.profiling.io.XlsHalsteadAnalysisWriter.java Source code

Java tutorial

Introduction

Here is the source code for co.turnus.profiling.io.XlsHalsteadAnalysisWriter.java

Source

/* 
 * TURNUS, the co-exploration framework
 * 
 * Copyright (C) 2014 EPFL SCI STI MM
 *
 * This file is part of TURNUS.
 *
 * TURNUS 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 3 of the License, or
 * (at your option) any later version.
 *
 * TURNUS 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 TURNUS.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Additional permission under GNU GPL version 3 section 7
 * 
 * If you modify this Program, or any covered work, by linking or combining it
 * with Eclipse (or a modified version of Eclipse or an Eclipse plugin or 
 * an Eclipse library), containing parts covered by the terms of the 
 * Eclipse Public License (EPL), the licensors of this Program grant you 
 * additional permission to convey the resulting work.  Corresponding Source 
 * for a non-source form of such a combination shall include the source code 
 * for the parts of Eclipse libraries used as well as that of the  covered work.
 * 
 */
package co.turnus.profiling.io;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;

import co.turnus.TurnusRuntimeException;
import co.turnus.dataflow.Network;
import co.turnus.profiling.ActionStaticData;
import co.turnus.profiling.ActorClassStaticData;
import co.turnus.profiling.BasicStaticData;
import co.turnus.profiling.ProfilingFactory;
import co.turnus.profiling.SourceCodeData;
import co.turnus.profiling.util.HalsteadAnalyser;
import co.turnus.profiling.util.StaticDataUtil;

public class XlsHalsteadAnalysisWriter {

    private HSSFFont titleFont;
    private CellStyle cellStyle;

    public void write(SourceCodeData report, File file) {
        try {
            HSSFWorkbook workbook = new HSSFWorkbook();

            titleFont = workbook.createFont();
            titleFont.setFontName("Arial");
            titleFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

            cellStyle = workbook.createCellStyle();
            cellStyle.setDataFormat(workbook.getCreationHelper().createDataFormat().getFormat("m/d/yy h:mm"));

            writeSummary(workbook, report.getNetwork(), report);
            writeActorClassesTable(workbook, report);

            OutputStream out = new FileOutputStream(file);
            out = new BufferedOutputStream(out);
            workbook.write(out);
            out.close();
        } catch (Exception e) {
            throw new TurnusRuntimeException("Error writing the excel file " + file, e.getCause());
        }

    }

    private void writeActorClassesTable(HSSFWorkbook workbook, SourceCodeData report) {
        int sheetNumber = 0;
        for (ActorClassStaticData aData : report.getActorsData()) {
            HalsteadAnalyser a = new HalsteadAnalyser(aData);
            String aClass = aData.getActorClass().getName();

            HSSFSheet sheet = workbook.createSheet("Class n." + sheetNumber++);
            // Action Actor Class Results
            Cell cell = sheet.createRow(0).createCell(0);
            HSSFRichTextString title = new HSSFRichTextString("Halstead Analysis for the Actor Class: " + aClass);
            title.applyFont(titleFont);
            cell.setCellValue(title);

            Row row = sheet.createRow(2);
            row.createCell(0).setCellValue("Summary");
            row.createCell(1).setCellValue("n1");
            row.createCell(2).setCellValue("n2");
            row.createCell(3).setCellValue("n");
            row.createCell(4).setCellValue("N1");
            row.createCell(5).setCellValue("N2");
            row.createCell(6).setCellValue("N");
            row.createCell(7).setCellValue("cN");
            row.createCell(8).setCellValue("V");
            row.createCell(9).setCellValue("D");
            row.createCell(10).setCellValue("E");
            row.createCell(11).setCellValue("T");
            row.createCell(12).setCellValue("B");
            row.createCell(13).setCellValue("I");
            row.createCell(14).setCellValue("NoL");

            row = sheet.createRow(3);
            row.createCell(1).setCellValue(a.getn1());
            row.createCell(2).setCellValue(a.getn2());
            row.createCell(3).setCellValue(a.getn());
            row.createCell(4).setCellValue(a.getN1());
            row.createCell(5).setCellValue(a.getN2());
            row.createCell(6).setCellValue(a.getN());
            row.createCell(7).setCellValue(a.getcN());
            row.createCell(8).setCellValue(a.getV());
            row.createCell(9).setCellValue(a.getD());
            row.createCell(10).setCellValue(a.getE());
            row.createCell(11).setCellValue(a.getT());
            row.createCell(12).setCellValue(a.getB());
            row.createCell(13).setCellValue(a.getI());
            row.createCell(14).setCellValue(aData.getNumberOfLines());

            int i = 5;
            row = sheet.createRow(i++);
            row.createCell(0).setCellValue("Action");
            row.createCell(1).setCellValue("n1");
            row.createCell(2).setCellValue("n2");
            row.createCell(3).setCellValue("n");
            row.createCell(4).setCellValue("N1");
            row.createCell(5).setCellValue("N2");
            row.createCell(6).setCellValue("N");
            row.createCell(7).setCellValue("cN");
            row.createCell(8).setCellValue("V");
            row.createCell(9).setCellValue("D");
            row.createCell(10).setCellValue("E");
            row.createCell(11).setCellValue("T");
            row.createCell(12).setCellValue("B");
            row.createCell(13).setCellValue("I");
            for (ActionStaticData actionData : aData.getActionsData()) {
                a = new HalsteadAnalyser(actionData);
                row = sheet.createRow(i++);
                row.createCell(0).setCellValue(actionData.getAction().getId());
                row.createCell(1).setCellValue(a.getn1());
                row.createCell(2).setCellValue(a.getn2());
                row.createCell(3).setCellValue(a.getn());
                row.createCell(4).setCellValue(a.getN1());
                row.createCell(5).setCellValue(a.getN2());
                row.createCell(6).setCellValue(a.getN());
                row.createCell(7).setCellValue(a.getcN());
                row.createCell(8).setCellValue(a.getV());
                row.createCell(9).setCellValue(a.getD());
                row.createCell(10).setCellValue(a.getE());
                row.createCell(11).setCellValue(a.getT());
                row.createCell(12).setCellValue(a.getB());
                row.createCell(13).setCellValue(a.getI());
            }

            /*
             * i++; row = sheet.createRow(i++);
             * row.createCell(0).setCellValue("Procedure");
             * row.createCell(1).setCellValue("n1");
             * row.createCell(2).setCellValue("n2");
             * row.createCell(3).setCellValue("n");
             * row.createCell(4).setCellValue("N1");
             * row.createCell(5).setCellValue("N2");
             * row.createCell(6).setCellValue("N");
             * row.createCell(7).setCellValue("cN");
             * row.createCell(8).setCellValue("V");
             * row.createCell(9).setCellValue("D");
             * row.createCell(10).setCellValue("E");
             * row.createCell(11).setCellValue("T");
             * row.createCell(12).setCellValue("B");
             * row.createCell(13).setCellValue("I");
             * 
             * for (ProcedureStaticData pData : aData.getProceduresData()) { a =
             * new HalsteadAnalyser(pData); row = sheet.createRow(i++);
             * row.createCell(0).setCellValue(pData.getProcedure().getName());
             * row.createCell(1).setCellValue(a.getn1());
             * row.createCell(2).setCellValue(a.getn2());
             * row.createCell(3).setCellValue(a.getn());
             * row.createCell(4).setCellValue(a.getN1());
             * row.createCell(5).setCellValue(a.getN2());
             * row.createCell(6).setCellValue(a.getN());
             * row.createCell(7).setCellValue(a.getcN());
             * row.createCell(8).setCellValue(a.getV());
             * row.createCell(9).setCellValue(a.getD());
             * row.createCell(10).setCellValue(a.getE());
             * row.createCell(11).setCellValue(a.getT());
             * row.createCell(12).setCellValue(a.getB());
             * row.createCell(13).setCellValue(a.getI()); }
             */
        }

    }

    private void writeSummary(HSSFWorkbook workbook, Network network, SourceCodeData report) {
        BasicStaticData networkData = ProfilingFactory.eINSTANCE.createBasicStaticData();

        for (ActorClassStaticData aData : report.getActorsData()) {
            StaticDataUtil.merge(networkData, aData);
        }

        HalsteadAnalyser a = new HalsteadAnalyser(networkData);
        HSSFSheet sheet = workbook.createSheet("Halstead Summary");
        // Action Actor Class Results
        Cell cell = sheet.createRow(0).createCell(0);
        HSSFRichTextString title = new HSSFRichTextString("Halstead Analysis: Network and Actor Classes");
        title.applyFont(titleFont);
        cell.setCellValue(title);

        Row row = sheet.createRow(1);
        row.createCell(0).setCellValue("Network");
        row = sheet.createRow(2);
        row.createCell(0).setCellValue("n1");
        row.createCell(1).setCellValue("n2");
        row.createCell(2).setCellValue("n");
        row.createCell(3).setCellValue("N1");
        row.createCell(4).setCellValue("N2");
        row.createCell(5).setCellValue("N");
        row.createCell(6).setCellValue("cN");
        row.createCell(7).setCellValue("V");
        row.createCell(8).setCellValue("D");
        row.createCell(9).setCellValue("E");
        row.createCell(10).setCellValue("T");
        row.createCell(11).setCellValue("B");
        row.createCell(12).setCellValue("I");
        row.createCell(13).setCellValue("NoL");
        row = sheet.createRow(3);
        row.createCell(0).setCellValue(a.getn1());
        row.createCell(1).setCellValue(a.getn2());
        row.createCell(2).setCellValue(a.getn());
        row.createCell(3).setCellValue(a.getN1());
        row.createCell(4).setCellValue(a.getN2());
        row.createCell(5).setCellValue(a.getN());
        row.createCell(6).setCellValue(a.getcN());
        row.createCell(7).setCellValue(a.getV());
        row.createCell(8).setCellValue(a.getD());
        row.createCell(9).setCellValue(a.getE());
        row.createCell(10).setCellValue(a.getT());
        row.createCell(11).setCellValue(a.getB());
        row.createCell(12).setCellValue(a.getI());
        row.createCell(13).setCellValue(networkData.getNumberOfLines());

        row = sheet.createRow(5);
        row.createCell(0).setCellValue("Actor Class");
        row.createCell(1).setCellValue("n1");
        row.createCell(2).setCellValue("n2");
        row.createCell(3).setCellValue("n");
        row.createCell(4).setCellValue("N1");
        row.createCell(5).setCellValue("N2");
        row.createCell(6).setCellValue("N");
        row.createCell(7).setCellValue("cN");
        row.createCell(8).setCellValue("V");
        row.createCell(9).setCellValue("D");
        row.createCell(10).setCellValue("E");
        row.createCell(11).setCellValue("T");
        row.createCell(12).setCellValue("B");
        row.createCell(13).setCellValue("I");
        row.createCell(14).setCellValue("NoL");

        int i = 6;
        for (ActorClassStaticData aData : report.getActorsData()) {
            a = new HalsteadAnalyser(aData);
            row = sheet.createRow(i);
            row.createCell(0).setCellValue(aData.getActorClass().getName());
            row.createCell(1).setCellValue(a.getn1());
            row.createCell(2).setCellValue(a.getn2());
            row.createCell(3).setCellValue(a.getn());
            row.createCell(4).setCellValue(a.getN1());
            row.createCell(5).setCellValue(a.getN2());
            row.createCell(6).setCellValue(a.getN());
            row.createCell(7).setCellValue(a.getcN());
            row.createCell(8).setCellValue(a.getV());
            row.createCell(9).setCellValue(a.getD());
            row.createCell(10).setCellValue(a.getE());
            row.createCell(11).setCellValue(a.getT());
            row.createCell(12).setCellValue(a.getB());
            row.createCell(13).setCellValue(a.getI());
            row.createCell(14).setCellValue(aData.getNumberOfLines());

            i++;
        }
    }

}