001    /*
002     * Copyright 2011 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may      obtain a copy of the License at
007     *
008     *      http://www.osedu.org/licenses/ECL-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.student.contract.writer;
017    
018    import java.io.ByteArrayOutputStream;
019    import java.io.File;
020    import java.io.FileNotFoundException;
021    import java.io.FileOutputStream;
022    import java.io.PrintStream;
023    import java.util.List;
024    
025    import org.kuali.student.contract.exception.DictionaryExecutionException;
026    
027    /**
028     *
029     * @author nwright
030     */
031    public class HtmlWriter extends XmlWriter {
032    
033        private String directory;
034        private String fileName;
035        private String title;
036        private ByteArrayOutputStream body;
037    
038        public HtmlWriter(String directory, String fileName, String title) {
039            super();
040            this.body = new ByteArrayOutputStream(1000);
041            this.setOut(new PrintStream(body));
042            this.setIndent(0);
043            this.directory = directory;
044            this.fileName = fileName;
045            this.title = title;
046        }
047    
048        public ByteArrayOutputStream getBody() {
049            return body;
050        }
051    
052        public String getDirectory() {
053            return directory;
054        }
055    
056        public String getFileName() {
057            return fileName;
058        }
059    
060        public void writeHeader() {
061            indentPrintln("<html>");
062            indentPrintln("<head>");
063            this.writeTag("title", title);
064            indentPrintln("</head>");
065            indentPrintln("<body bgcolor=\"#ffffff\" topmargin=0 marginheight=0>");
066        }
067    
068        public void writeHeaderBodyAndFooterOutToFile() {
069    
070            File dir = new File(this.directory);
071            //System.out.println ("Writing java class: " + fileName + " to " + dir.getAbsolutePath ());
072    
073            if (!dir.exists()) {
074                if (!dir.mkdirs()) {
075                    throw new DictionaryExecutionException("Could not create directory "
076                            + this.directory);
077                }
078            }
079            try {
080                PrintStream out = new PrintStream(new FileOutputStream(this.directory + "/" + fileName, false));
081                this.setOut(out);
082            } catch (FileNotFoundException ex) {
083                throw new DictionaryExecutionException(ex);
084            }
085            writeHeader();
086            indentPrintln(body.toString());
087            indentPrintln("</body>");
088            decrementIndent();
089            indentPrintln("</html>");
090        }
091    
092        public void writeTable(List<String> headers, List<List<String>> rows) {
093            this.indentPrintln("<table>");
094            incrementIndent();
095            this.indentPrintln("<tr>");
096            for (String header : headers) {
097                this.writeTag("th", header);
098            }
099            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    }