Develop and Download Open Source Software

Browse Subversion Repository

Contents of /tags/FeliCa2Money-2.8/CsvRule.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 202 - (show annotations) (download)
Sat May 17 09:58:27 2008 UTC (15 years, 10 months ago) by tmurakam
File size: 7836 byte(s)
ver 2.8 tag

1 /*
2 * FeliCa2Money
3 *
4 * Copyright (C) 2001-2008 Takuya Murakami
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21 // CSV 変換ルール
22
23 using System;
24 using System.Collections;
25 using System.Collections.Generic;
26 using System.Text;
27 using System.IO;
28 using System.Xml;
29
30 namespace FeliCa2Money
31 {
32 public class CsvRule
33 {
34 private string ident; // 識別子(組織名)
35 private int bankId; // 銀行ID
36 private string name; // 銀行名
37 private string firstLine = null; // 1行目
38 private bool isAscent; // 昇順かどうか
39 private bool isTSV = false; // TSV かどうか
40
41 // 各 CSV カラムのマッピング規則
42 private Hashtable colHash = new Hashtable();
43
44 // シリアル番号採番用
45 private DateTime prevDate;
46 private int idSerial;
47
48 // プロパティ
49 public string Ident
50 {
51 get { return ident; }
52 set { ident = value; }
53 }
54 public int BankId {
55 get { return bankId; }
56 set { bankId = value; }
57 }
58 public string Name {
59 get { return name; }
60 set { name = value; }
61 }
62 public string FirstLine
63 {
64 get { return firstLine; }
65 set { firstLine = value; }
66 }
67 public string Order
68 {
69 set
70 {
71 if (value == "Descent") isAscent = false;
72 else isAscent = true;
73 }
74 }
75 public bool IsAscent
76 {
77 get { return isAscent; }
78 }
79 public string Separator
80 {
81 set
82 {
83 if (value == "Tab")
84 {
85 isTSV = true;
86 }
87 else
88 {
89 isTSV = false;
90 }
91 }
92 }
93 public bool IsTSV
94 {
95 get { return isTSV; }
96 }
97
98
99 public string Format
100 {
101 set { SetFormat(value); }
102 }
103
104 // CSV 変換フォーマット文字列解析
105 public void SetFormat(string format)
106 {
107 string[] cols = format.Split(new Char[] { ',', '\t' });
108
109 for (int i = 0; i < cols.Length; i++)
110 {
111 colHash[cols[i].Trim()] = i;
112 }
113 }
114
115 // シリアル番号リセット
116 public void Reset()
117 {
118 prevDate = new DateTime(1900, 1, 1, 0, 0, 0);
119 idSerial = 0;
120 }
121
122 // 指定したカラムを取得
123 private string getCol(string[] row, string key)
124 {
125 if (colHash[key] == null)
126 {
127 return null;
128 }
129 int col = (int)colHash[key];
130 return row[col];
131 }
132
133 // 指定したカラムを取得 (integer)
134 private int getColInt(string[] row, string key)
135 {
136 string v = getCol(row, key);
137 if (v == null) return 0;
138
139 // 空フィールドの場合は 0 を返す
140 if (v == "") return 0;
141
142 // 区切り文字を抜く
143 v = v.Replace(",", "");
144
145 // 先頭に '\' があるときは抜く
146 if (v.StartsWith("\\"))
147 {
148 v = v.Substring(1);
149 }
150
151 return int.Parse(v);
152 }
153
154 // 1行解析
155 // CSV の各カラムはすでに分解されているものとする
156 public Transaction parse(string[] row)
157 {
158 Transaction t = new Transaction();
159
160 // 日付
161 string date = getCol(row, "Date");
162 if (date != null)
163 {
164 t.date = parseDate(date);
165 }
166 else {
167 int year = getColInt(row, "Year");
168 int month = getColInt(row, "Month");
169 int day = getColInt(row, "Day");
170
171 if (year == 0 || month == 0 || day == 0) {
172 return null;
173 }
174
175 if (year < 100)
176 {
177 year += 2000;
178 }
179 t.date = new DateTime(year, month, day, 0, 0, 0);
180 }
181
182 // ID
183 string id = getCol(row, "Id");
184 if (id != null)
185 {
186 t.id = getColInt(row, "Id");
187 }
188 else
189 {
190 // 日付毎に ID を振る
191 if (t.date == prevDate)
192 {
193 idSerial++;
194 }
195 else
196 {
197 prevDate = t.date;
198 idSerial = 0;
199 }
200 t.id = idSerial;
201 }
202
203 // 金額
204 t.value = getColInt(row, "Income");
205 t.value -= getColInt(row, "Outgo");
206
207 // 残高
208 t.balance = getColInt(row, "Balance");
209
210 // 適用
211 t.desc = getCol(row, "Desc");
212
213 // 備考
214 t.memo = getCol(row, "Memo");
215
216 // トランザクションタイプを自動設定
217 t.GuessTransType(t.value >= 0);
218
219 return t;
220 }
221
222 // 日付文字列の解析
223 private DateTime parseDate(string date)
224 {
225 int year, month, day;
226
227 // 年月日で区切られている場合
228 string[] split = date.Split(new char[] { '年', '月', '日' });
229 if (split.Length < 3)
230 {
231 // '/' などで区切られている場合
232 split = date.Split(new Char[] { '/', '.', ' ' });
233 }
234
235 if (split.Length >= 3)
236 {
237 // 和暦の処理
238 // (三井住友銀行など)
239 if (split[0].StartsWith("H"))
240 {
241 year = int.Parse(split[0].Substring(1));
242 year += 1988;
243 } else {
244 year = int.Parse(split[0]);
245 }
246 month = int.Parse(split[1]);
247 day = int.Parse(split[2]);
248 }
249 else
250 {
251 date = split[0];
252
253 if (date.Length != 6 && date.Length != 8)
254 {
255 // パース不可能
256 // TBD
257 throw new Exception(Properties.Resources.CantParseDateStr + " (" + date + ")");
258 }
259
260 int n = int.Parse(date);
261 year = n / 10000;
262 month = (n / 100) % 100;
263 day = n % 100;
264 }
265
266 if (year < 100) {
267 year += 2000;
268 }
269 return new DateTime(year, month, day, 0, 0, 0);
270 }
271 }
272
273 }

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26