| 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 |
year = int.Parse(split[0]); |
| 210 |
month = int.Parse(split[1]); |
| 211 |
day = int.Parse(split[2]); |
| 212 |
} |
| 213 |
else |
| 214 |
{ |
| 215 |
date = split[0]; |
| 216 |
|
| 217 |
if (date.Length != 6 && date.Length != 8) |
| 218 |
{ |
| 219 |
// パース不可能 |
| 220 |
// TBD |
| 221 |
throw new Exception("日付文字列解析失敗 (" + date + ")"); |
| 222 |
} |
| 223 |
|
| 224 |
int n = int.Parse(date); |
| 225 |
year = n / 10000; |
| 226 |
month = (n / 100) % 100; |
| 227 |
day = n % 100; |
| 228 |
} |
| 229 |
|
| 230 |
if (year < 100) { |
| 231 |
year += 2000; |
| 232 |
} |
| 233 |
return new DateTime(year, month, day, 0, 0, 0); |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
} |