1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.student.contract.writer;
17
18 import java.io.ByteArrayOutputStream;
19 import java.io.File;
20 import java.io.FileNotFoundException;
21 import java.io.FileOutputStream;
22 import java.io.PrintStream;
23 import java.util.List;
24
25 import org.kuali.student.contract.exception.DictionaryExecutionException;
26 import org.slf4j.LoggerFactory;
27 import org.slf4j.Logger;
28
29
30
31
32
33 public class HtmlWriter extends XmlWriter {
34
35 private static final Logger log = LoggerFactory.getLogger(HtmlWriter.class);
36
37 private String directory;
38 private String fileName;
39 private String title;
40 private ByteArrayOutputStream body;
41
42 public HtmlWriter(String directory, String fileName, String title) {
43 super();
44 this.body = new ByteArrayOutputStream(1000);
45 this.setOut(new PrintStream(body));
46 this.setIndent(0);
47 this.directory = directory;
48 this.fileName = fileName;
49 this.title = title;
50 }
51
52 public ByteArrayOutputStream getBody() {
53 return body;
54 }
55
56 public String getDirectory() {
57 return directory;
58 }
59
60 public String getFileName() {
61 return fileName;
62 }
63
64 public void writeHeader() {
65 indentPrintln("<html>");
66 indentPrintln("<head>");
67 this.writeTag("title", title);
68
69 indentPrintln("</head>");
70 indentPrintln("<body bgcolor=\"#ffffff\" topmargin=0 marginheight=0>");
71 }
72
73 public void writeHeaderBodyAndFooterOutToFile() {
74
75 File dir = new File(this.directory);
76 log.debug ("Writing java class: " + fileName + " to " + dir.getAbsolutePath ());
77
78 if (!dir.exists()) {
79 if (!dir.mkdirs()) {
80 throw new DictionaryExecutionException("Could not create directory "
81 + this.directory);
82 }
83 }
84 try {
85
86 String outputFileName = this.directory + "/"+ fileName;
87 log.info("opening file = " + outputFileName);
88
89 PrintStream out = new PrintStream(new FileOutputStream(outputFileName, false));
90 this.setOut(out);
91 } catch (FileNotFoundException ex) {
92 throw new DictionaryExecutionException(ex);
93 }
94 writeHeader();
95 indentPrintln(body.toString());
96 indentPrintln("</body>");
97 decrementIndent();
98 indentPrintln("</html>");
99
100 }
101
102 public void writeTable(List<String> headers, List<List<String>> rows) {
103 this.indentPrintln("<table>");
104 incrementIndent();
105 this.indentPrintln("<tr>");
106 for (String header : headers) {
107 this.writeTag("th", header);
108 }
109 this.indentPrintln("</tr>");
110 for (List<String> row : rows) {
111 this.indentPrintln("<tr>");
112 for (String cell : row) {
113 this.writeTag("td", cell);
114 }
115 this.indentPrintln("</tr>");
116 }
117 this.indentPrintln("</table>");
118 }
119 }