| 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.Generic; |
| 25 |
using System.Text; |
| 26 |
using System.Xml; |
| 27 |
using System.Windows.Forms; |
| 28 |
using System.Net; |
| 29 |
using System.IO; |
| 30 |
|
| 31 |
namespace FeliCa2Money |
| 32 |
{ |
| 33 |
public class CsvRules |
| 34 |
{ |
| 35 |
private List<CsvRule> ruleList; |
| 36 |
|
| 37 |
public CsvRules() |
| 38 |
{ |
| 39 |
ruleList = new List<CsvRule>(); |
| 40 |
} |
| 41 |
|
| 42 |
public int Count |
| 43 |
{ |
| 44 |
get { return ruleList.Count; } |
| 45 |
} |
| 46 |
|
| 47 |
// 全 CSV 変換ルールを読み出す |
| 48 |
public bool LoadAllRules() |
| 49 |
{ |
| 50 |
// ユーザ設定フォルダのほうから読み出す |
| 51 |
String path = getRulesPath(); |
| 52 |
//String path = Path.GetDirectoryName(Application.ExecutablePath); |
| 53 |
|
| 54 |
string[] xmlFiles = Directory.GetFiles(path, "*.xml"); |
| 55 |
if (xmlFiles.Length == 0) |
| 56 |
{ |
| 57 |
if (MessageBox.Show(Properties.Resources.QueryCsvRuleDownload, Properties.Resources.Confirm, |
| 58 |
MessageBoxButtons.OKCancel) != DialogResult.OK) return false; |
| 59 |
|
| 60 |
if (!DownloadRule()) return false; |
| 61 |
|
| 62 |
xmlFiles = Directory.GetFiles(path, "*.xml"); |
| 63 |
} |
| 64 |
|
| 65 |
foreach (string xmlFile in xmlFiles) |
| 66 |
{ |
| 67 |
try |
| 68 |
{ |
| 69 |
LoadFromFile(xmlFile); |
| 70 |
} |
| 71 |
catch (Exception) |
| 72 |
{ |
| 73 |
MessageBox.Show(Properties.Resources.CsvRuleError + " in " + xmlFile, Properties.Resources.Error); |
| 74 |
} |
| 75 |
} |
| 76 |
return true; |
| 77 |
} |
| 78 |
|
| 79 |
// 定義ファイルを1つ読み込む |
| 80 |
public void LoadFromFile(string path) |
| 81 |
{ |
| 82 |
XmlDocument doc = new XmlDocument(); |
| 83 |
doc.Load(path); |
| 84 |
|
| 85 |
LoadFromXml(doc); |
| 86 |
} |
| 87 |
|
| 88 |
public void LoadFromXml(XmlDocument doc) |
| 89 |
{ |
| 90 |
XmlElement root = doc.DocumentElement; |
| 91 |
|
| 92 |
// Rule 子要素について処理 |
| 93 |
XmlNodeList list = root.GetElementsByTagName("Rule"); |
| 94 |
|
| 95 |
for (int i = 0; i < list.Count; i++) |
| 96 |
{ |
| 97 |
// rule 生成 |
| 98 |
CsvRule rule = new CsvRule(); |
| 99 |
|
| 100 |
// rule の各メンバを設定 |
| 101 |
foreach (XmlElement e in list[i].ChildNodes) |
| 102 |
{ |
| 103 |
string value = ""; |
| 104 |
if (e.FirstChild == null) |
| 105 |
{ |
| 106 |
// 空要素 |
| 107 |
} |
| 108 |
else |
| 109 |
{ |
| 110 |
value = e.FirstChild.Value; |
| 111 |
} |
| 112 |
|
| 113 |
switch (e.Name) |
| 114 |
{ |
| 115 |
case "Ident": |
| 116 |
rule.Ident = value; |
| 117 |
break; |
| 118 |
case "Name": |
| 119 |
rule.Name = value; |
| 120 |
break; |
| 121 |
case "BankId": |
| 122 |
rule.BankId = int.Parse(value); |
| 123 |
break; |
| 124 |
case "FirstLine": |
| 125 |
rule.FirstLine = value; |
| 126 |
break; |
| 127 |
case "Format": |
| 128 |
rule.Format = value; |
| 129 |
break; |
| 130 |
case "Order": |
| 131 |
rule.Order = value; |
| 132 |
break; |
| 133 |
case "Separator": |
| 134 |
rule.Separator = value; |
| 135 |
break; |
| 136 |
default: |
| 137 |
// ignore |
| 138 |
break; |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
ruleList.Add(rule); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
// 名前一覧を返す |
| 147 |
public string[] names() |
| 148 |
{ |
| 149 |
string[] names = new string[ruleList.Count]; |
| 150 |
int i = 0; |
| 151 |
foreach (CsvRule rule in ruleList) |
| 152 |
{ |
| 153 |
names[i] = rule.Name; |
| 154 |
i++; |
| 155 |
} |
| 156 |
return names; |
| 157 |
} |
| 158 |
|
| 159 |
// 指定したインデックスのルールを返す |
| 160 |
public CsvRule GetAt(int idx) |
| 161 |
{ |
| 162 |
return ruleList[idx]; |
| 163 |
} |
| 164 |
|
| 165 |
// 指定したルールのインデックスを返す |
| 166 |
public int IndexOf(CsvRule rule) |
| 167 |
{ |
| 168 |
return ruleList.IndexOf(rule); |
| 169 |
} |
| 170 |
|
| 171 |
// firstLine に一致するルールを探す |
| 172 |
public CsvRule FindRule(string firstLine) |
| 173 |
{ |
| 174 |
foreach (CsvRule rule in ruleList) |
| 175 |
{ |
| 176 |
if (rule.FirstLine == firstLine) |
| 177 |
{ |
| 178 |
return rule; |
| 179 |
} |
| 180 |
} |
| 181 |
return null; |
| 182 |
} |
| 183 |
|
| 184 |
// 定義ファイルをダウンロードする |
| 185 |
public static bool DownloadRule() |
| 186 |
{ |
| 187 |
string path = getRulesPath() + "\\CsvRules.xml"; |
| 188 |
//string url = "http://moneyimport.sourceforge.jp/CsvRules.xml"; |
| 189 |
string url = "http://svn.sourceforge.jp/svnroot/moneyimport/trunk/FeliCa2Money.net/CsvRules.xml"; |
| 190 |
|
| 191 |
WebClient w = new WebClient(); |
| 192 |
try |
| 193 |
{ |
| 194 |
w.DownloadFile(url, path); |
| 195 |
} |
| 196 |
catch (Exception ex) |
| 197 |
{ |
| 198 |
MessageBox.Show(ex.Message, Properties.Resources.Error); |
| 199 |
return false; |
| 200 |
} |
| 201 |
return true; |
| 202 |
} |
| 203 |
|
| 204 |
// 定義ファイルのディレクトリを返す |
| 205 |
// UserAppDataPath からバージョン番号を除いたもの |
| 206 |
private static string getRulesPath() |
| 207 |
{ |
| 208 |
string path = Application.LocalUserAppDataPath; |
| 209 |
path = System.IO.Path.GetDirectoryName(path); |
| 210 |
return path; |
| 211 |
} |
| 212 |
} |
| 213 |
} |