Develop and Download Open Source Software

Browse Subversion Repository

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 166 - (show annotations) (download)
Thu Mar 20 14:04:13 2008 UTC (16 years ago) by tmurakam
File size: 7187 byte(s)
2.5 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
40 // 各 CSV カラムのマッピング規則
41 private Hashtable colHash = new Hashtable();
42
43 // シリアル番号採番用
44 private DateTime prevDate;
45 private int idSerial;
46
47 // プロパティ
48 public string Ident
49 {
50 get { return ident; }
51 set { ident = value; }
52 }
53 public int BankId {
54 get { return bankId; }
55 set { bankId = value; }
56 }
57 public string Name {
58 get { return name; }
59 set { name = value; }
60 }
61 public string FirstLine
62 {
63 get { return firstLine; }
64 set { firstLine = value; }
65 }
66 public string Order
67 {
68 set
69 {
70 if (value == "Descent") isAscent = false;
71 else isAscent = true;
72 }
73 }
74 public bool IsAscent
75 {
76 get { return isAscent; }
77 }
78 public string Format
79 {
80 set { SetFormat(value); }
81 }
82
83 // CSV 変換フォーマット文字列解析
84 public void SetFormat(string format)
85 {
86 string[] cols = format.Split(new Char[] { ',' });
87
88 for (int i = 0; i < cols.Length; i++)
89 {
90 colHash[cols[i].Trim()] = i;
91 }
92 }
93
94 // シリアル番号リセット
95 public void Reset()
96 {
97 prevDate = new DateTime(1900, 1, 1, 0, 0, 0);
98 idSerial = 0;
99 }
100
101 // 指定したカラムを取得
102 private string getCol(string[] row, string key)
103 {
104 if (colHash[key] == null)
105 {
106 return null;
107 }
108 int col = (int)colHash[key];
109 return row[col];
110 }
111
112 // 指定したカラムを取得 (integer)
113 private int getColInt(string[] row, string key)
114 {
115 string v = getCol(row, key);
116 if (v == null) return 0;
117
118 // 空フィールドの場合は 0 を返す
119 if (v == "") return 0;
120
121 // 区切り文字を抜く
122 v = v.Replace(",", "");
123 return int.Parse(v);
124 }
125
126 // 1行解析
127 // CSV の各カラムはすでに分解されているものとする
128 public Transaction parse(string[] row)
129 {
130 Transaction t = new Transaction();
131
132 // 日付
133 string date = getCol(row, "Date");
134 if (date != null)
135 {
136 t.date = parseDate(date);
137 }
138 else {
139 int year = getColInt(row, "Year");
140 int month = getColInt(row, "Month");
141 int day = getColInt(row, "Day");
142
143 if (year == 0 || month == 0 || day == 0) {
144 return null;
145 }
146
147 if (year < 100)
148 {
149 year += 2000;
150 }
151 t.date = new DateTime(year, month, day, 0, 0, 0);
152 }
153
154 // ID
155 string id = getCol(row, "Id");
156 if (id != null)
157 {
158 t.id = getColInt(row, "Id");
159 }
160 else
161 {
162 // 日付毎に ID を振る
163 if (t.date == prevDate)
164 {
165 idSerial++;
166 }
167 else
168 {
169 prevDate = t.date;
170 idSerial = 0;
171 }
172 t.id = idSerial;
173 }
174
175 // 金額
176 t.value = getColInt(row, "Income");
177 t.value -= getColInt(row, "Outgo");
178
179 // 残高
180 t.balance = getColInt(row, "Balance");
181
182 // 適用
183 t.desc = getCol(row, "Desc");
184
185 // 備考
186 t.memo = getCol(row, "Memo");
187
188 // トランザクションタイプを自動設定
189 t.GuessTransType(t.value >= 0);
190
191 return t;
192 }
193
194 // 日付文字列の解析
195 private DateTime parseDate(string date)
196 {
197 int year, month, day;
198
199 // 年月日で区切られている場合
200 string[] split = date.Split(new char[] { '年', '月', '日' });
201 if (split.Length < 3)
202 {
203 // '/' などで区切られている場合
204 split = date.Split(new Char[] { '/', '.', ' ' });
205 }
206
207 if (split.Length >= 3)
208 {
209 // 和暦の処理
210 // (三井住友銀行など)
211 if (split[0].Substring(1, 1) == "H")
212 {
213 year = int.Parse(split[0].Substring(2));
214 year += 1988;
215 } else {
216 year = int.Parse(split[0]);
217 }
218 month = int.Parse(split[1]);
219 day = int.Parse(split[2]);
220 }
221 else
222 {
223 date = split[0];
224
225 if (date.Length != 6 && date.Length != 8)
226 {
227 // パース不可能
228 // TBD
229 throw new Exception("日付文字列解析失敗 (" + date + ")");
230 }
231
232 int n = int.Parse(date);
233 year = n / 10000;
234 month = (n / 100) % 100;
235 day = n % 100;
236 }
237
238 if (year < 100) {
239 year += 2000;
240 }
241 return new DateTime(year, month, day, 0, 0, 0);
242 }
243 }
244
245 }

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