DomainToolCore.Report.XLSSubDomains.java Source code

Java tutorial

Introduction

Here is the source code for DomainToolCore.Report.XLSSubDomains.java

Source

/*
This file is part of Domain Tool.
    
Domain Tool 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.
    
Domain Tool 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 Domain Tool.  If not, see <http://www.gnu.org/licenses/>.
 */

package DomainToolCore.Report;

import DomainToolCore.Core.Utils;
import java.io.*;
import java.util.Vector;
import java.util.Date;
import java.util.Calendar;
import java.util.GregorianCalendar;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Font;

/**
 *
 * @author Luca Magistrelli
 */
public class XLSSubDomains {
    private static Vector subdxls, ipxls;
    private static String domainxls, ip_domainxls;
    public static String FILEXls;
    private String xlsDir = System.getProperty("user.dir") + "\\Report\\xls\\";
    private Calendar c = new GregorianCalendar();
    private FileOutputStream fos = null;
    private HSSFWorkbook workbook;

    public XLSSubDomains(String domain, Vector v1, Vector v2, String ipdo) {
        subdxls = (Vector) v1.clone();
        ipxls = (Vector) v2.clone();
        this.domainxls = domain;
        this.ip_domainxls = ipdo;
        Utils u1 = new Utils();
        /*****Check if the Report folder exist, if not, it will be created***/
        if (!u1.FolderExist("Report")) {
            u1.MakeReportFolder();
            u1.CreateFolder("Report\\xls");
        }
        if (!u1.FolderExist("\\Report\\xls"))
            u1.CreateFolder("Report\\xls");
        /********************************************************************/
        workbook = new HSSFWorkbook();
        HSSFSheet firstSheet = workbook.createSheet("Domain Tool Report");
        SetDomain(firstSheet);
        SetDomainIP(firstSheet);
        WriteSubDomain(firstSheet);
        WriteIp(firstSheet);
        GeneratedBy(firstSheet);
        try {
            FILEXls = xlsDir + domainxls + "_" + c.get(Calendar.DAY_OF_MONTH) + "_" + c.get(Calendar.MONTH) + "_"
                    + c.get(Calendar.YEAR) + "_" + c.get(Calendar.HOUR_OF_DAY) + "_" + c.get(Calendar.MINUTE) + "_"
                    + c.get(Calendar.SECOND) + ".xls";
            fos = new FileOutputStream(new File(FILEXls));
            workbook.write(fos);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    //HACK HACK, method called after the constructor, for retrieving the path of pdf
    public String GetXlsPath() {
        return FILEXls;
    }

    private HSSFCellStyle setBold() {
        HSSFCellStyle cellStyle = workbook.createCellStyle();
        HSSFFont font = workbook.createFont();
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        cellStyle.setFont(font);
        return cellStyle;

    }

    private void SetDomain(HSSFSheet sh) {
        sh.createRow(1).createCell(0).setCellValue("Domain: " + domainxls);
        sh.getRow(1).getCell(0).setCellStyle(setBold());

    }

    private void SetDomainIP(HSSFSheet sh2) {
        sh2.createRow(2).createCell(0).setCellValue("Domain IP: " + ip_domainxls);
        sh2.getRow(2).getCell(0).setCellStyle(setBold());
    }

    private void WriteSubDomain(HSSFSheet firstSheet) {
        firstSheet.createRow(5).createCell(0).setCellValue("SubDomain");
        firstSheet.getRow(5).getCell(0).setCellStyle(setBold());
        firstSheet.setColumnWidth(0, 10000);
        for (int i = 1, j = 6; i < subdxls.size(); i++) {
            firstSheet.createRow(j).createCell(0).setCellValue((String) subdxls.get(i));
            j++;
        }
    }

    private void WriteIp(HSSFSheet sheet) {
        sheet.getRow(5).createCell(1).setCellValue("IP Address");
        sheet.getRow(5).getCell(1).setCellStyle(setBold());
        sheet.setColumnWidth(1, 10000);
        for (int i = 1, j = 6; i < subdxls.size(); i++) {
            sheet.getRow(j).createCell(1).setCellValue((String) ipxls.get(i));
            j++;
        }
    }

    private void GeneratedBy(HSSFSheet sheet1) {
        int casella = subdxls.size() + 10;
        sheet1.createRow(casella).createCell(0).setCellValue("Generated by Domain Tool on "
                + c.get(Calendar.DAY_OF_MONTH) + "/" + c.get(Calendar.MONTH) + "/" + c.get(Calendar.YEAR));
        sheet1.getRow(casella).getCell(0).setCellStyle(BackColor());
    }

    private HSSFCellStyle BackColor() {
        HSSFCellStyle style = workbook.createCellStyle();
        Font font = workbook.createFont();
        font.setColor(HSSFColor.RED.index);
        font.setItalic(true);
        style.setFont(font);
        return style;
    }

}