View Javadoc

1   /*
2    * Copyright 2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may	obtain a copy of the License at
7    *
8    * 	http://www.osedu.org/licenses/ECL-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * @author nwright
30   */
31  public class HtmlWriter extends XmlWriter {
32  
33      private String directory;
34      private String fileName;
35      private String title;
36      private ByteArrayOutputStream body;
37  
38      public HtmlWriter(String directory, String fileName, String title) {
39          super();
40          this.body = new ByteArrayOutputStream(1000);
41          this.setOut(new PrintStream(body));
42          this.setIndent(0);
43          this.directory = directory;
44          this.fileName = fileName;
45          this.title = title;
46      }
47  
48      public ByteArrayOutputStream getBody() {
49          return body;
50      }
51  
52      public String getDirectory() {
53          return directory;
54      }
55  
56      public String getFileName() {
57          return fileName;
58      }
59  
60      public void writeHeader() {
61          indentPrintln("<html>");
62          indentPrintln("<head>");
63          this.writeTag("title", title);
64          indentPrintln("</head>");
65          indentPrintln("<body bgcolor=\"#ffffff\" topmargin=0 marginheight=0>");
66      }
67  
68      public void writeHeaderBodyAndFooterOutToFile() {
69  
70          File dir = new File(this.directory);
71          //System.out.println ("Writing java class: " + fileName + " to " + dir.getAbsolutePath ());
72  
73          if (!dir.exists()) {
74              if (!dir.mkdirs()) {
75                  throw new DictionaryExecutionException("Could not create directory "
76                          + this.directory);
77              }
78          }
79          try {
80              PrintStream out = new PrintStream(new FileOutputStream(this.directory + "/" + fileName, false));
81              this.setOut(out);
82          } catch (FileNotFoundException ex) {
83              throw new DictionaryExecutionException(ex);
84          }
85          writeHeader();
86          indentPrintln(body.toString());
87          indentPrintln("</body>");
88          decrementIndent();
89          indentPrintln("</html>");
90      }
91  
92      public void writeTable(List<String> headers, List<List<String>> rows) {
93          this.indentPrintln("<table>");
94          incrementIndent();
95          this.indentPrintln("<tr>");
96          for (String header : headers) {
97              this.writeTag("th", header);
98          }
99          this.indentPrintln("</tr>");
100         for (List<String> row : rows) {
101             this.indentPrintln("<tr>");
102             for (String cell : row) {
103                 this.writeTag("td", cell);
104             }
105             this.indentPrintln("</tr>");
106         }
107         this.indentPrintln("</table>");
108     }
109 }