001package csv2html;
002
003import java.io.BufferedWriter;
004import java.io.File;
005import java.io.FileNotFoundException;
006import java.io.FileOutputStream;
007import java.io.IOException;
008import java.io.OutputStreamWriter;
009import java.io.UnsupportedEncodingException;
010import java.util.Calendar;
011import utility.StringUtility;
012
013/**
014 * ライタ:情報のテーブルをHTMLページとして書き出す。
015 */
016public class Writer extends IO
017{
018        /**
019         * ライタのコンストラクタ。
020         * @param aTable テーブル
021         */
022        public Writer(Table aTable)
023        {
024                super(aTable);
025
026                return;
027        }
028
029        /**
030         * HTMLページを基にするテーブルからインデックスファイル(index.html)に書き出す。
031         */
032        public void perform()
033        {
034                try
035                {
036                        Attributes attributes = this.attributes();
037                        String fileStringOfHTML = attributes.baseDirectory() + attributes.indexHTML();
038                        File aFile = new File(fileStringOfHTML);
039                        FileOutputStream outputStream = new FileOutputStream(aFile);
040                        OutputStreamWriter outputWriter = new OutputStreamWriter(outputStream, StringUtility.encodingSymbol());
041                        BufferedWriter aWriter = new BufferedWriter(outputWriter);
042
043                        this.writeHeaderOn(aWriter);
044                        this.writeTableBodyOn(aWriter);
045                        this.writeFooterOn(aWriter);
046
047                        aWriter.close();
048                }
049                catch (UnsupportedEncodingException | FileNotFoundException anException) { anException.printStackTrace(); }
050                catch (IOException anException) { anException.printStackTrace(); }
051
052                return;
053        }
054
055        /**
056         * 属性リストを書き出す。
057         * @param aWriter ライタ
058         */
059        public void writeAttributesOn(BufferedWriter aWriter)
060        {
061                try
062                {
063                        aWriter.write("\t\t\t\t\t\t");
064                        aWriter.write("<tr>\n");
065
066                        for (String aString : this.attributes().names())
067                        {
068                                aWriter.write("\t\t\t\t\t\t\t");
069                                aWriter.write("<td class=\"center-pink\"><strong>");
070                                aWriter.write(aString);
071                                aWriter.write("</strong></td>\n");
072                        }
073
074                        aWriter.write("\t\t\t\t\t\t");
075                        aWriter.write("</tr>\n");
076                }
077                catch (IOException anException) { anException.printStackTrace(); }
078
079                return;
080        }
081
082        /**
083         * フッタを書き出す。
084         * @param aWriter ライタ
085         */
086        public void writeFooterOn(BufferedWriter aWriter)
087        {
088                Class aClass = Translator.class;
089                String nameString = aClass.getName();
090
091                Calendar aCalendar = Calendar.getInstance();
092                int year = aCalendar.get(aCalendar.YEAR);
093                int month = aCalendar.get(aCalendar.MONTH) + 1;
094                int day = aCalendar.get(aCalendar.DATE);
095                int hour = aCalendar.get(aCalendar.HOUR_OF_DAY);
096                int minute = aCalendar.get(aCalendar.MINUTE);
097                int second = aCalendar.get(aCalendar.SECOND);
098
099                StringBuffer aBuffer = new StringBuffer();
100                aBuffer.append(" on ");
101                aBuffer.append(String.format("%1$04d", year));
102                aBuffer.append("/");
103                aBuffer.append(String.format("%1$02d", month));
104                aBuffer.append("/");
105                aBuffer.append(String.format("%1$02d", day));
106                String dateString = aBuffer.toString();
107
108                aBuffer = new StringBuffer();
109                aBuffer.append(" at ");
110                aBuffer.append(String.format("%1$02d", hour));
111                aBuffer.append(":");
112                aBuffer.append(String.format("%1$02d", minute));
113                aBuffer.append(":");
114                aBuffer.append(String.format("%1$02d", second));
115                String timeString = aBuffer.toString();
116
117                try
118                {
119                        aWriter.write("\t\t\t\t\t</tbody>\n\t\t\t\t</table>\n\t\t\t</td>\n\t\t</tr>\n\t</tbody>\n</table>\n<hr>\n<div class=\"right-small\">Created by " + nameString + dateString + timeString + "</div>\n</body>\n</html>\n");
120                }
121                catch (IOException anException) { anException.printStackTrace(); }
122
123                return;
124        }
125
126        /**
127         * ヘッダを書き出す。
128         * @param aWriter ライタ
129         */
130        public void writeHeaderOn(BufferedWriter aWriter)
131        {
132                try
133                {
134                        aWriter.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n<html lang=\"ja\">\n<head>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n<meta http-equiv=\"Content-Style-Type\" content=\"text/css\">\n<meta http-equiv=\"Content-Script-Type\" content=\"text/javascript\">\n<meta name=\"keywords\" content=\"Smalltalk,Oriented,Programming\">\n<meta name=\"description\" content=\"");
135                        aWriter.write(this.attributes().titleString());
136                        aWriter.write("\">\n<meta name=\"author\" content=\"AOKI Atsushi\">\n<link rev=\"made\" href=\"http://www.cc.kyoto-su.ac.jp/~atsushi/\">\n<link rel=\"index\" href=\"index.html\">\n<style type=\"text/css\">\n<!--\nbody {\n\tbackground-color : #ffffff;\n\tmargin : 20px;\n\tpadding : 10px;\n\tfont-family : serif;\n\tfont-size : 10pt;\n}\na {\n\ttext-decoration : underline;\n\tcolor : #000000;\n}\na:link {\n\tbackground-color : #ffddbb;\n}\na:visited {\n\tbackground-color : #ccffcc;\n}\na:hover {\n\tbackground-color : #dddddd;\n}\na:active {\n\tbackground-color : #dddddd;\n}\ndiv.belt {\n\tbackground-color : #eeeeee;\n\tpadding : 0px 4px;\n}\ndiv.right-small {\n\ttext-align : right;\n\tfont-size : 8pt;\n}\nimg.borderless {\n\tborder-width : 0px;\n\tvertical-align : middle;\n}\ntable.belt {\n\tborder-style : solid;\n\tborder-width : 0px;\n\tborder-color : #000000;\n\tbackground-color : #ffffff;\n\tpadding : 0px 0px;\n\twidth : 100%;\n}\ntable.content {\n\tborder-style : solid;\n\tborder-width : 0px;\n\tborder-color : #000000;\n\tpadding : 2px 2px;\n}\ntd.center-blue {\n\tpadding : 2px 2px;\n\ttext-align : center;\n\tbackground-color : #ddeeff;\n}\ntd.center-pink {\n\tpadding : 2px 2px;\n\ttext-align : center;\n\tbackground-color : #ffddee;\n}\ntd.center-yellow {\n\tpadding : 2px 2px;\n\ttext-align : center;\n\tbackground-color : #ffffcc;\n}\n-->\n</style>\n<title>");
137                        aWriter.write(this.attributes().titleString());
138                        aWriter.write("</title>\n</head>\n<body>\n<div class=\"belt\">\n<h2>");
139                        aWriter.write(this.attributes().captionString());
140                        aWriter.write("</h2>\n</div>\n<table class=\"belt\" summary=\"table\">\n\t<tbody>\n\t\t<tr>\n\t\t\t<td>\n\t\t\t\t<table class=\"content\" summary=\"table\">\n\t\t\t\t\t<tbody>\n");
141                }
142                catch (IOException anException) { anException.printStackTrace(); }
143
144                return;
145        }
146
147        /**
148         * ボディを書き出す。
149         * @param aWriter ライタ
150         */
151        public void writeTableBodyOn(BufferedWriter aWriter)
152        {
153                this.writeAttributesOn(aWriter);
154                this.writeTuplesOn(aWriter);
155
156                return;
157        }
158
159        /**
160         * タプル群を書き出す。
161         * @param aWriter ライタ
162         */
163        public void writeTuplesOn(BufferedWriter aWriter)
164        {
165                try
166                {
167                        int no = 0;
168                        for (Tuple aTuple : this.tuples())
169                        {
170                                aWriter.write("\t\t\t\t\t\t");
171                                aWriter.write("<tr>\n");
172
173                                for (String aString : aTuple.values())
174                                {
175                                        aWriter.write("\t\t\t\t\t\t\t");
176                                        if (no % 2 == 0) { aWriter.write("<td class=\"center-blue\">");  }
177                                        else { aWriter.write("<td class=\"center-yellow\">"); }
178                                        aWriter.write(aString);
179                                        aWriter.write("</td>\n");
180                                }
181
182                                aWriter.write("\t\t\t\t\t\t");
183                                aWriter.write("</tr>\n");
184                                no++;
185                        }
186                }
187                catch (IOException anException) { anException.printStackTrace(); }
188
189                return;
190        }
191}