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    import org.slf4j.LoggerFactory;
027    import org.slf4j.Logger;
028    
029    /**
030     *
031     * @author nwright
032     */
033    public class HtmlWriter extends XmlWriter {
034    
035            private static final Logger log = LoggerFactory.getLogger(HtmlWriter.class);
036            
037        private String directory;
038        private String fileName;
039        private String title;
040        private ByteArrayOutputStream body;
041    
042        public HtmlWriter(String directory, String fileName, String title) {
043            super();
044            this.body = new ByteArrayOutputStream(1000);
045            this.setOut(new PrintStream(body));
046            this.setIndent(0);
047            this.directory = directory;
048            this.fileName = fileName;
049            this.title = title;
050        }
051    
052        public ByteArrayOutputStream getBody() {
053            return body;
054        }
055    
056        public String getDirectory() {
057            return directory;
058        }
059    
060        public String getFileName() {
061            return fileName;
062        }
063    
064        public void writeHeader() {
065            indentPrintln("<html>");
066            indentPrintln("<head>");
067            this.writeTag("title", title);
068           
069            indentPrintln("</head>");
070            indentPrintln("<body bgcolor=\"#ffffff\" topmargin=0 marginheight=0>");
071        }
072    
073        public void writeHeaderBodyAndFooterOutToFile() {
074    
075            File dir = new File(this.directory);
076            log.debug ("Writing java class: " + fileName + " to " + dir.getAbsolutePath ());
077    
078            if (!dir.exists()) {
079                if (!dir.mkdirs()) {
080                    throw new DictionaryExecutionException("Could not create directory "
081                            + this.directory);
082                }
083            }
084            try {
085                    
086                    String outputFileName = this.directory + "/"+ fileName;
087                    log.info("opening file = " + outputFileName);
088                    
089                PrintStream out = new PrintStream(new FileOutputStream(outputFileName, false));
090                this.setOut(out);
091            } catch (FileNotFoundException ex) {
092                throw new DictionaryExecutionException(ex);
093            }
094            writeHeader();
095            indentPrintln(body.toString());
096            indentPrintln("</body>");
097            decrementIndent();
098            indentPrintln("</html>");
099            
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    }