Coverage Report - org.homeunix.thecave.buddi.plugin.api.util.HtmlPage
 
Classes in this File Line Coverage Branch Coverage Complexity
HtmlPage
0%
0/48
0%
0/28
5.75
 
 1  
 /*
 2  
  * Created on Aug 24, 2007 by wyatt
 3  
  */
 4  
 package org.homeunix.thecave.buddi.plugin.api.util;
 5  
 
 6  
 import java.awt.image.BufferedImage;
 7  
 import java.io.BufferedOutputStream;
 8  
 import java.io.File;
 9  
 import java.io.FileOutputStream;
 10  
 import java.io.IOException;
 11  
 import java.io.OutputStreamWriter;
 12  
 import java.util.Map;
 13  
 import java.util.logging.Logger;
 14  
 
 15  
 import javax.imageio.ImageIO;
 16  
 
 17  
 import org.homeunix.thecave.buddi.Buddi;
 18  
 import org.homeunix.thecave.buddi.util.FileFunctions;
 19  
 
 20  
 /**
 21  
  * A small class which defines an HTML page.  There are two parts to this: the HTML text,
 22  
  * and a map of images.  The HTML text is constructed manually (optionally using HTMLHelper
 23  
  * to create the standard report look and layout).  If you are going to add images, include
 24  
  * them in the HTML text normally (with no path - just the image name), and map that name
 25  
  * to a buffered image.  The buffered images will be stored to disk, to the file name
 26  
  * given in the map.
 27  
  * 
 28  
  * For an example on how to do this, please refer to some of the built in reports in 
 29  
  * the Buddi SVN repository.
 30  
  * 
 31  
  * @author wyatt
 32  
  *
 33  
  */
 34  
 public class HtmlPage {
 35  
         private String html;
 36  
         private Map<String, BufferedImage> images;
 37  
 
 38  
         /**
 39  
          * Creates a new HtmlPage, with the given text and images.
 40  
          * @param html
 41  
          * @param images
 42  
          */
 43  0
         public HtmlPage(String html, Map<String, BufferedImage> images) {
 44  0
                 this.html = html;
 45  0
                 this.images = images;
 46  0
         }
 47  
         public String getHtml() {
 48  0
                 return html;
 49  
         }
 50  
         public Map<String, BufferedImage> getImages() {
 51  0
                 return images;
 52  
         }
 53  
         
 54  
         /**
 55  
          * Saves the HTML and associated images to disk.  Returns the
 56  
          * file object of the associated HTML index on success, or null
 57  
          * on failure. 
 58  
          */
 59  
         public File createHTML(String name){
 60  0
                 final int countMax = 100;
 61  
 
 62  0
                 File dataFolder = Buddi.getReportsFolder();
 63  0
                 File htmlFolder = null;
 64  0
                 int counter = 0;
 65  
 
 66  
                 //Do some sanity checks, logging results
 67  0
                 if (dataFolder == null)
 68  0
                         Logger.getLogger(this.getClass().getName()).warning("Data folder is null (HtmlPage.createHTML()).");
 69  0
                 if (html == null)
 70  0
                         Logger.getLogger(this.getClass().getName()).warning("HTML is null (HtmlPage.createHTML()).");
 71  0
                 if (name == null)
 72  0
                         Logger.getLogger(this.getClass().getName()).warning("Name is null (HtmlPage.createHTML()).");
 73  
 
 74  
                 //Get a unique folder name which has not yet been used.
 75  0
                 while (counter < countMax && (htmlFolder == null || htmlFolder.exists())){
 76  0
                         htmlFolder = new File(dataFolder.getAbsolutePath() + File.separator + name + (counter > 0 ? "_" + counter : ""));
 77  0
                         counter++;
 78  
                 }
 79  
                 //We could not create a folder, after 1000 tries.  Exit.
 80  0
                 if (counter == countMax){
 81  0
                         Logger.getLogger(this.getClass().getName()).warning("Could not find a folder to use in the data folder, after 1000 tries.  Cancelling HTML export.  Please verify if you really have this many folders in your data drectory, and if so, clean up a bit.");
 82  
 
 83  0
                         return null;
 84  
                 }
 85  
 
 86  0
                 if (!FileFunctions.isFolderWritable(htmlFolder.getParentFile())){
 87  0
                         Logger.getLogger(this.getClass().getName()).warning("Cannot write to '" + htmlFolder.getParentFile().getAbsolutePath() + "'.  This may cause problems shortly...");
 88  
                 }
 89  
 
 90  
                 //Try to create a folder.  If this doesn't work, we return with an error.
 91  0
                 if (!htmlFolder.mkdirs()){
 92  0
                         Logger.getLogger(this.getClass().getName()).warning("Could not create folder '" + htmlFolder.getAbsolutePath() + "'.  Please check that you have write permission on this folder and its sub folders.");
 93  0
                         return null;
 94  
                 }
 95  
 
 96  0
                 File htmlFile = new File(htmlFolder.getAbsolutePath() + File.separator + "index.html");
 97  0
                 htmlFolder.deleteOnExit();
 98  0
                 htmlFile.deleteOnExit();
 99  
                 try{
 100  0
                         OutputStreamWriter out = new OutputStreamWriter(
 101  
                                         new BufferedOutputStream(
 102  
                                                         new FileOutputStream(htmlFile)), "UTF-8");
 103  0
                         out.write(this.getHtml());
 104  0
                         out.close();
 105  
 //                        BufferedWriter bw = new BufferedWriter(
 106  
 //                        new FileWriter(htmlFile));
 107  
 //                        bw.write(html.getHtml());
 108  
 //                        bw.close();
 109  
                 }
 110  0
                 catch (IOException ioe){
 111  0
                         Logger.getLogger(this.getClass().getName()).warning("Could not write HTML file.");
 112  0
                         return null;
 113  0
                 }
 114  
 
 115  0
                 if (this.getImages() != null){
 116  0
                         for (String imgName : this.getImages().keySet()) {
 117  0
                                 if (imgName != null && this.getImages().get(imgName) != null){
 118  0
                                         File f = new File(htmlFolder.getAbsolutePath() + File.separator + imgName);
 119  
                                         try {
 120  0
                                                 ImageIO.write(this.getImages().get(imgName), "png", f);
 121  0
                                                 f.deleteOnExit();
 122  
                                         }
 123  0
                                         catch (IOException ioe){
 124  0
                                                 Logger.getLogger(this.getClass().getName()).warning("Error writing file " + f.getAbsolutePath());
 125  0
                                         }
 126  0
                                 }
 127  
                         }
 128  
                 }
 129  
 
 130  0
                 return htmlFile;
 131  
         }
 132  
 }