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 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
public class HtmlWriter extends XmlWriter |
32 | |
{ |
33 | |
|
34 | |
private String directory; |
35 | |
private String fileName; |
36 | |
private String title; |
37 | |
private ByteArrayOutputStream body; |
38 | |
|
39 | |
public HtmlWriter (String directory, String fileName, String title) |
40 | |
{ |
41 | 0 | super (); |
42 | 0 | this.body = new ByteArrayOutputStream (1000); |
43 | 0 | this.setOut (new PrintStream (body)); |
44 | 0 | this.setIndent (0); |
45 | 0 | this.directory = directory; |
46 | 0 | this.fileName = fileName; |
47 | 0 | this.title = title; |
48 | 0 | } |
49 | |
|
50 | |
public ByteArrayOutputStream getBody () |
51 | |
{ |
52 | 0 | return body; |
53 | |
} |
54 | |
|
55 | |
public String getDirectory () |
56 | |
{ |
57 | 0 | return directory; |
58 | |
} |
59 | |
|
60 | |
public String getFileName () |
61 | |
{ |
62 | 0 | return fileName; |
63 | |
} |
64 | |
|
65 | |
public void writeHeader () |
66 | |
{ |
67 | 0 | indentPrintln ("<html>"); |
68 | 0 | indentPrintln ("<head>"); |
69 | 0 | this.writeTag ("title", title); |
70 | 0 | indentPrintln ("</head>"); |
71 | 0 | indentPrintln ("<body bgcolor=\"#ffffff\" topmargin=0 marginheight=0>"); |
72 | 0 | } |
73 | |
|
74 | |
public void writeHeaderBodyAndFooterOutToFile () |
75 | |
{ |
76 | |
|
77 | 0 | File dir = new File (this.directory); |
78 | |
|
79 | |
|
80 | 0 | if ( ! dir.exists ()) |
81 | |
{ |
82 | 0 | if ( ! dir.mkdirs ()) |
83 | |
{ |
84 | 0 | throw new DictionaryExecutionException ("Could not create directory " |
85 | |
+ this.directory); |
86 | |
} |
87 | |
} |
88 | |
try |
89 | |
{ |
90 | 0 | PrintStream out = new PrintStream (new FileOutputStream (this.directory + "/" + fileName, false)); |
91 | 0 | this.setOut (out); |
92 | |
} |
93 | 0 | catch (FileNotFoundException ex) |
94 | |
{ |
95 | 0 | throw new DictionaryExecutionException (ex); |
96 | 0 | } |
97 | 0 | writeHeader (); |
98 | 0 | indentPrintln (body.toString ()); |
99 | 0 | indentPrintln ("</body>"); |
100 | 0 | decrementIndent (); |
101 | 0 | indentPrintln ("</html>"); |
102 | 0 | } |
103 | |
|
104 | |
public void writeTable (List<String> headers, List<List<String>> rows) |
105 | |
{ |
106 | 0 | this.indentPrintln ("<table>"); |
107 | 0 | incrementIndent (); |
108 | 0 | this.indentPrintln ("<tr>"); |
109 | 0 | for (String header : headers) |
110 | |
{ |
111 | 0 | this.writeTag ("th", header); |
112 | |
} |
113 | 0 | this.indentPrintln ("</tr>"); |
114 | 0 | for (List<String> row : rows) |
115 | |
{ |
116 | 0 | this.indentPrintln ("<tr>"); |
117 | 0 | for (String cell : row) |
118 | |
{ |
119 | 0 | this.writeTag ("td", cell); |
120 | |
} |
121 | 0 | this.indentPrintln ("</tr>"); |
122 | |
} |
123 | 0 | this.indentPrintln ("</table>"); |
124 | 0 | } |
125 | |
} |