com.ainfosec.macresponse.report.PdfGenerator.java Source code

Java tutorial

Introduction

Here is the source code for com.ainfosec.macresponse.report.PdfGenerator.java

Source

/*
    
 MacResponse: Incident Response Toolkit for Mac OS X
    
 Copyright (C) 2011 - Assured Information Security, Inc. All rights reserved.
     
 Authors:
 Paul Petzke <petzkep _at_ ainfosec.com>
    
 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, see <http://www.gnu.org/licenses/>.
    
*/

package com.ainfosec.macresponse.report;

import java.io.FileOutputStream;
import java.lang.reflect.Field;
import java.util.Date;

import org.eclipse.swt.SWT;

import com.ainfosec.macresponse.db.DisplayObject;
import com.ainfosec.macresponse.db.TreeObject;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chapter;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

public class PdfGenerator {

    private static int currentChapter = 1;

    private static Font chapterTitleFont = new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.BOLD);
    private static Font sectionTitleFont = new Font(Font.FontFamily.TIMES_ROMAN, 16, Font.BOLD);
    private static Font contentFont = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.NORMAL);

    // iText allows to add metadata to the PDF which can be viewed in your Adobe
    // Reader
    // under File -> Properties
    private static void addMetaData(String caseName, String authorName, Document document) {
        document.addTitle(caseName);
        document.addSubject("MacResponse Console");
        document.addKeywords("");
        document.addAuthor(authorName);
        document.addCreator(System.getProperty("user.name"));
    }

    private static void addTitlePage(Document document) throws DocumentException {
        Paragraph preface = new Paragraph();
        // We add one empty line
        preface.add(Chunk.NEWLINE);
        // Lets write a big header
        preface.add(new Paragraph("MAC Response", chapterTitleFont));

        preface.add(Chunk.NEWLINE);
        // Will create: Report generated by: _name, _date
        preface.add(new Paragraph("Report generated by: " + System.getProperty("user.name") + ", " + new Date(), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                contentFont));
        //      preface.add(Chunk.NEWLINE);
        //      preface.add(Chunk.NEWLINE);
        //      preface.add(Chunk.NEWLINE);
        //      preface.add(new Paragraph(
        //            "This document describes something which is very important ",
        //            contentFont));

        document.add(preface);
        // Start a new page
        document.newPage();
    }

    private static void addTables(Document document, TreeObject rootObject) throws DocumentException {
        // TODO Paul add table of contents/etc.?
        // Table of Contents
        Paragraph paragraph = new Paragraph();
        paragraph.add("Table of Contents\n\n");

        for (TreeObject treeObject : rootObject.getChildObjects()) {
            if (treeObject.isChecked()) {
                addTocSection(paragraph, treeObject, String.valueOf(currentChapter));
                currentChapter++;
            }
        }
        currentChapter = 1;

        document.add(paragraph);

        // Table of Figures
        // Table of Tables
    }

    private static void addTocSection(Paragraph paragraph, TreeObject treeObject, String sectionNumber) {
        int subsectionNumber = 1;

        if (treeObject.isChecked()) {
            StringBuffer sb = new StringBuffer();
            sb.append(sectionNumber);
            sb.append(". ");
            sb.append(treeObject.getTitle());
            sb.append("\n");
            paragraph.add(sb.toString());
        }

        // TODO Paul Print out the data sections

        // Print out the children
        if ((treeObject.getChildObjects() != null) && (treeObject.getChildObjects().size() > 0)) {
            for (TreeObject childObject : treeObject.getChildObjects()) {
                String currentSection = sectionNumber + "." + String.valueOf(subsectionNumber);
                if (treeObject.isChecked()) {
                    addTocSection(paragraph, childObject, currentSection);
                    subsectionNumber++;
                }
            }
        }
    }

    private static void addContent(Document document, TreeObject rootObject) throws DocumentException {
        for (TreeObject treeObject : rootObject.getChildObjects()) {
            if (treeObject.isChecked()) {
                Paragraph title = new Paragraph(treeObject.getTitle(), chapterTitleFont);
                Chapter chapter = new Chapter(title, currentChapter);
                populateChapter(treeObject, chapter);
                document.add(chapter);
                currentChapter++;
            }
        }
    }

    private static void populateChapter(TreeObject treeObject, Chapter chapter) throws DocumentException {
        // If there are children, don't display this object's data
        if ((treeObject.getChildObjects() == null) || (treeObject.getChildObjects().size() == 0)) {
            // Create the Data Paragraphs (displayObjects)
            if ((treeObject.getDisplayData() != null)
                    && (treeObject.getDisplayData().getDisplayObjects() != null)) {
                for (DisplayObject displayObject : treeObject.getDisplayData().getDisplayObjects()) {
                    Paragraph paragraph = new Paragraph(displayObject.getTitle(), sectionTitleFont);

                    paragraph.add("\n");
                    createDataSection(paragraph, displayObject);

                    chapter.addSection(paragraph);
                }
            }
        }
        // If there are children, display them
        else {
            for (TreeObject childTreeObject : treeObject.getChildObjects()) {
                Paragraph title = new Paragraph(childTreeObject.getTitle(), sectionTitleFont);
                Paragraph content = new Paragraph("\n");

                generateData(content, childTreeObject);

                chapter.addSection(title);
                chapter.add(content);
            }
        }
    }

    private static void generateData(Paragraph paragraph, TreeObject treeObject) {
        // TODO Paul take into consideration TreeObjects that have children (don't display current, make a section for each child)

        for (DisplayObject displayObject : treeObject.getDisplayData().getDisplayObjects()) {
            createDataSection(paragraph, displayObject);
            paragraph.add("\n");
        }
    }

    private static void createDataSection(Paragraph paragraph, DisplayObject displayObject) {
        if (displayObject == null || displayObject.getObjects() == null) {
            return;
        }

        // See if the DisplayObject has a list or a single object
        if (displayObject.getObjects().size() == 1) {
            TreeObject treeObject = displayObject.getObjects().get(0); // There's only 1 item

            // For each column, create/add a label with the title and the data
            int i = 0;
            for (String columnName : displayObject.getColumnNames()) {
                StringBuffer sb = new StringBuffer();

                // Add the column title
                sb.append(displayObject.getColumnTitles()[i]);
                sb.append(": ");

                // Get the value of the field
                try {
                    Field field = treeObject.getClass().getDeclaredField(columnName);
                    String val = (String) field.get(treeObject);
                    if (val == null) {
                        val = "";
                    }
                    // Add the value
                    sb.append(val);
                    sb.append("\n");
                } catch (NoSuchFieldException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                // Put the string into the paragraph
                paragraph.add(sb.toString());
                i++;
            }
        } else {
            PdfPTable table = new PdfPTable(displayObject.getColumnTitles().length);
            table.setWidthPercentage(100);
            table.setHorizontalAlignment(Element.ALIGN_CENTER);

            for (String columnName : displayObject.getColumnTitles()) {
                table.getDefaultCell().setBackgroundColor(BaseColor.CYAN);
                table.addCell(columnName);
                table.getDefaultCell().setBackgroundColor(BaseColor.WHITE);
            }

            for (TreeObject to1 : displayObject.getObjects()) {
                for (String columnName : displayObject.getColumnNames()) {
                    try {
                        Field field = to1.getClass().getDeclaredField(columnName);
                        String val = (String) field.get(to1);
                        if (val == null) {
                            val = "";
                        }
                        table.addCell(val);
                    } catch (NoSuchFieldException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IllegalAccessException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
            table.getDefaultCell().setColspan(displayObject.getColumnTitles().length);
            table.getDefaultCell().setBorder(SWT.NONE);
            table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);

            // TODO Paul number the tables
            table.addCell("Table: " + displayObject.getTitle());
            paragraph.add(table);
        }
    }

    public static void generateReport(String filename, TreeObject rootObject) {
        if ((rootObject == null) || (filename == null)) {
            return;
        }
        try {
            Document document = new Document();
            PdfWriter.getInstance(document, new FileOutputStream(filename));
            document.open();
            document.setMargins(20f, 20f, 20f, 20f);
            addMetaData("CASEID", "AUTHOR", document);
            addTitlePage(document);
            //         addTables(document, rootObject);
            addContent(document, rootObject);
            document.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}