001package csv2html;
002
003import java.io.File;
004import java.net.URL;
005import java.util.List;
006import java.util.HashMap;
007import java.util.Map;
008import utility.StringUtility;
009
010/**
011 * 入出力:リーダ・ダウンローダ・ライタを抽象する。
012 */
013public abstract class IO extends Object
014{
015        /**
016         * テーブル(表:スプレッドシート)を記憶するフィールド。
017         */
018        private Table table;
019
020        /**
021         * 入出力のコンストラクタ。
022         * @param aTable テーブル
023         */
024        public IO(Table aTable)
025        {
026                super();
027
028                this.table = aTable;
029
030                return;
031        }
032
033        /**
034         * 属性リストを応答する。
035         * @return 属性リスト
036         */
037        public Attributes attributes()
038        {
039                return this.table().attributes();
040        }
041
042        /**
043         * ファイルやディレクトリを削除するクラスメソッド。
044         * @param aFile ファイルやディレクトリ
045         */
046        public static void deleteFileOrDirectory(File aFile)
047        {
048                if (!aFile.exists()) { return; }
049                if (aFile.isFile()) { aFile.delete(); }
050                if (aFile.isDirectory())
051                {
052                        File[] files = aFile.listFiles();
053                        for (File each : files) { IO.deleteFileOrDirectory(each); }
054                        aFile.delete();
055                }
056
057                return;
058        }
059
060        /**
061         * 指定された文字列をHTML内に記述できる正式な文字列に変換して応答する。
062         * @param aString 文字列
063         * @return HTML内に記述できる正式な文字列
064         */
065        public static String htmlCanonicalString(String aString)
066        {
067                Map<Character, String> aMap = new HashMap<Character, String>();
068                aMap.put('&', "&amp;");
069                aMap.put('>', "&gt;");
070                aMap.put('<', "&lt;");
071                aMap.put('"', "&quot;");
072                aMap.put(' ', "&nbsp;");
073                aMap.put('\t', "");
074                aMap.put('\r', "");
075                aMap.put('\n', "<br>");
076                aMap.put('\f', "");
077
078                StringBuffer aBuffer = new StringBuffer();
079                for (int index = 0; index < aString.length(); index++)
080                {
081                        Character keyCharacter = aString.charAt(index);
082                        String valueString = aMap.get(keyCharacter);
083                        if (valueString == null) { aBuffer.append(keyCharacter); }
084                        else { aBuffer.append(valueString); }
085                }
086                String theString = aBuffer.toString();
087
088                return theString;
089        }
090
091        /**
092         * 指定されたファイルからテキストを読み込んで、それを行リストにして応答するクラスメソッド。
093         * @param aFile ファイル
094         * @return 行リスト
095         */
096        public static List<String> readTextFromFile(File aFile)
097        {
098                return StringUtility.readTextFromFile(aFile);
099        }
100
101        /**
102         * 指定されたファイル文字列からテキストを読み込んで、それを行リストにして応答するクラスメソッド。
103         * @param fileString ファイル文字列
104         * @return 行リスト
105         */
106        public static List<String> readTextFromFile(String fileString)
107        {
108                return StringUtility.readTextFromFile(fileString);
109        }
110
111        /**
112         * 指定されたURL文字列からテキストを読み込んで、それを行リストにして応答するクラスメソッド。
113         * @param urlString URL文字列
114         * @return 行リスト
115         */
116        public static List<String> readTextFromURL(String urlString)
117        {
118                return StringUtility.readTextFromURL(urlString);
119        }
120
121        /**
122         * 指定されたURLからテキストを読み込んで、それを行リストにして応答するクラスメソッド。
123         * @param aURL URL
124         * @return 行リスト
125         */
126        public static List<String> readTextFromURL(URL aURL)
127        {
128                return StringUtility.readTextFromURL(aURL);
129        }
130
131        /**
132         * 文字列をセパレータで分割したトークン列を応答するクラスメソッド。
133         * @param string 文字列
134         * @param separators セパレータ文字列
135         * @return トークン列
136         */
137        public static List<String> splitString(String string, String separators)
138        {
139                return StringUtility.splitString(string, separators);
140        }
141
142        /**
143         * テーブルを応答する。
144         * @return テーブル
145         */
146        public Table table()
147        {
148                return this.table;
149        }
150
151        /**
152         * タプル群を応答する。
153         * @return タプル群
154         */
155        public List<Tuple> tuples()
156        {
157                return this.table().tuples();
158        }
159
160        /**
161         * 指定された行リストを、指定されたファイルに書き出すクラスメソッド。
162         * @param aCollection 行リスト
163         * @param aFile ファイル
164         */
165        public static void writeText(List<String> aCollection, File aFile)
166        {
167                StringUtility.writeText(aCollection, aFile);
168
169                return;
170        }
171
172        /**
173         * 指定された行リストを、指定されたファイル名のファイルに書き出すクラスメソッド。
174         * @param aCollection 行リスト
175         * @param fileString ファイル名
176         */
177        public static void writeText(List<String> aCollection, String fileString)
178        {
179                StringUtility.writeText(aCollection, fileString);
180
181                return;
182        }
183}