| 1 | |
|
| 2 | |
|
| 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 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
public class HtmlPage { |
| 35 | |
private String html; |
| 36 | |
private Map<String, BufferedImage> images; |
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 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 | |
|
| 56 | |
|
| 57 | |
|
| 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 | |
|
| 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 | |
|
| 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 | |
|
| 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 | |
|
| 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 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 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 | |
} |