Develop and Download Open Source Software

Browse Subversion Repository

Contents of /trunk/FeliCa2Money.net/CsvRule.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 229 - (show annotations) (download)
Mon Oct 19 16:04:50 2009 UTC (14 years, 5 months ago) by tmurakam
File size: 7659 byte(s)
取引ID付与手順を変更
CVS の Order に "Sort" を追加
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 public const int SortAscent = 0;
35 public const int SortDescent = 1;
36 public const int SortAuto = 2;
37
38 private string ident; // 識別子(組織名)
39 private int bankId; // 銀行ID
40 private string name; // 銀行名
41 private string firstLine = null; // 1行目
42 private int sortOrder; // ソートオーダー
43 private bool isTSV = false; // TSV かどうか
44
45 // 各 CSV カラムのマッピング規則
46 private Hashtable colHash = new Hashtable();
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 == "Sort") {
72 sortOrder = SortAuto;
73 } else if (value == "Descent") {
74 sortOrder = SortDescent;
75 } else {
76 sortOrder = SortAscent;
77 }
78 }
79 }
80 public int SortOrder
81 {
82 get { return sortOrder; }
83 }
84 public string Separator
85 {
86 set
87 {
88 if (value == "Tab")
89 {
90 isTSV = true;
91 }
92 else
93 {
94 isTSV = false;
95 }
96 }
97 }
98 public bool IsTSV
99 {
100 get { return isTSV; }
101 }
102
103
104 public string Format
105 {
106 set { SetFormat(value); }
107 }
108
109 // CSV 変換フォーマット文字列解析
110 public void SetFormat(string format)
111 {
112 string[] cols = format.Split(new Char[] { ',', '\t' });
113
114 for (int i = 0; i < cols.Length; i++)
115 {
116 colHash[cols[i].Trim()] = i;
117 }
118 }
119
120 // 指定したカラムを取得
121 private string getCol(string[] row, string key)
122 {
123 if (colHash[key] == null)
124 {
125 return null;
126 }
127 int col = (int)colHash[key];
128 return row[col];
129 }
130
131 // 指定したカラムを取得 (integer)
132 private int getColInt(string[] row, string key)
133 {
134 string v = getCol(row, key);
135 if (v == null) return 0;
136
137 // 空フィールドの場合は 0 を返す
138 if (v == "") return 0;
139
140 // 区切り文字を抜く
141 v = v.Replace(",", "");
142
143 // 先頭に '\' があるときは抜く
144 if (v.StartsWith("\\"))
145 {
146 v = v.Substring(1);
147 }
148
149 // 小数点が含まれることを考慮して、double でパース
150 return (int)double.Parse(v);
151 }
152
153 // 1行解析
154 // CSV の各カラムはすでに分解されているものとする
155 public Transaction parse(string[] row)
156 {
157 Transaction t = new Transaction();
158
159 // 日付
160 string date = getCol(row, "Date");
161 if (date != null)
162 {
163 t.date = parseDate(date);
164 }
165 else {
166 int year = getColInt(row, "Year");
167 int month = getColInt(row, "Month");
168 int day = getColInt(row, "Day");
169
170 if (year == 0 || month == 0 || day == 0) {
171 return null;
172 }
173
174 if (year < 100)
175 {
176 year += 2000;
177 }
178 t.date = new DateTime(year, month, day, 0, 0, 0);
179 }
180
181 // ID
182 string id = getCol(row, "Id");
183 if (id != null)
184 {
185 t.id = getColInt(row, "Id");
186 }
187 else
188 {
189 t.id = Transaction.UnassignedId;
190 }
191
192 // 金額
193 t.value = getColInt(row, "Income");
194 t.value -= getColInt(row, "Outgo");
195
196 // 残高
197 t.balance = getColInt(row, "Balance");
198
199 // 適用
200 t.desc = getCol(row, "Desc");
201
202 // 備考
203 t.memo = getCol(row, "Memo");
204
205 // トランザクションタイプを自動設定
206 t.GuessTransType(t.value >= 0);
207
208 return t;
209 }
210
211 // 日付文字列の解析
212 private DateTime parseDate(string date)
213 {
214 int year, month, day;
215
216 // 年月日で区切られている場合
217 string[] split = date.Split(new char[] { '年', '月', '日' });
218 if (split.Length < 3)
219 {
220 // '/' などで区切られている場合
221 split = date.Split(new Char[] { '/', '.', ' ' });
222 }
223
224 if (split.Length >= 3)
225 {
226 // 和暦の処理
227 // (三井住友銀行など)
228 if (split[0].StartsWith("H"))
229 {
230 year = int.Parse(split[0].Substring(1));
231 year += 1988;
232 } else {
233 year = int.Parse(split[0]);
234 }
235 month = int.Parse(split[1]);
236 day = int.Parse(split[2]);
237 }
238 else
239 {
240 date = split[0];
241
242 if (date.Length != 6 && date.Length != 8)
243 {
244 // パース不可能
245 // TBD
246 throw new Exception(Properties.Resources.CantParseDateStr + " (" + date + ")");
247 }
248
249 int n = int.Parse(date);
250 year = n / 10000;
251 month = (n / 100) % 100;
252 day = n % 100;
253 }
254
255 if (year < 100) {
256 year += 2000;
257 }
258 return new DateTime(year, month, day, 0, 0, 0);
259 }
260 }
261
262 }

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