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           
065            indentPrintln("</head>");
066            indentPrintln("<body bgcolor=\"#ffffff\" topmargin=0 marginheight=0>");
067        }
068    
069        public void writeHeaderBodyAndFooterOutToFile() {
070    
071            File dir = new File(this.directory);
072            //System.out.println ("Writing java class: " + fileName + " to " + dir.getAbsolutePath ());
073    
074            if (!dir.exists()) {
075                if (!dir.mkdirs()) {
076                    throw new DictionaryExecutionException("Could not create directory "
077                            + this.directory);
078                }
079            }
080            try {
081                PrintStream out = new PrintStream(new FileOutputStream(this.directory + "/" + fileName, false));
082                this.setOut(out);
083            } catch (FileNotFoundException ex) {
084                throw new DictionaryExecutionException(ex);
085            }
086            writeHeader();
087            indentPrintln(body.toString());
088            indentPrintln("</body>");
089            decrementIndent();
090            indentPrintln("</html>");
091        }
092    
093        public void writeTable(List<String> headers, List<List<String>> rows) {
094            this.indentPrintln("<table>");
095            incrementIndent();
096            this.indentPrintln("<tr>");
097            for (String header : headers) {
098                this.writeTag("th", header);
099            }
100            this.indentPrintln("</tr>");
101            for (List<String> row : rows) {
102                this.indentPrintln("<tr>");
103                for (String cell : row) {
104                    this.writeTag("td", cell);
105                }
106                this.indentPrintln("</tr>");
107            }
108            this.indentPrintln("</table>");
109        }
110    }