Develop and Download Open Source Software

Browse Subversion Repository

Contents of /tags/FeliCa2Money-2.6/CsvRules.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 189 - (show annotations) (download)
Tue Apr 8 13:03:07 2008 UTC (16 years ago) by tmurakam
File size: 6692 byte(s)
2.6 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.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 XmlElement root = doc.DocumentElement;
85
86 // Rule 子要素について処理
87 XmlNodeList list = root.GetElementsByTagName("Rule");
88
89 for (int i = 0; i < list.Count; i++)
90 {
91 // rule 生成
92 CsvRule rule = new CsvRule();
93
94 // rule の各メンバを設定
95 foreach (XmlElement e in list[i].ChildNodes)
96 {
97 string value = "";
98 if (e.FirstChild == null)
99 {
100 // 空要素
101 }
102 else
103 {
104 value = e.FirstChild.Value;
105 }
106
107 switch (e.Name)
108 {
109 case "Ident":
110 rule.Ident = value;
111 break;
112 case "Name":
113 rule.Name = value;
114 break;
115 case "BankId":
116 rule.BankId = int.Parse(value);
117 break;
118 case "FirstLine":
119 rule.FirstLine = value;
120 break;
121 case "Format":
122 rule.Format = value;
123 break;
124 case "Order":
125 rule.Order = value;
126 break;
127 case "Separator":
128 rule.Separator = value;
129 break;
130 default:
131 // ignore
132 break;
133 }
134 }
135
136 ruleList.Add(rule);
137 }
138 }
139
140 // 名前一覧を返す
141 public string[] names()
142 {
143 string[] names = new string[ruleList.Count];
144 int i = 0;
145 foreach (CsvRule rule in ruleList)
146 {
147 names[i] = rule.Name;
148 i++;
149 }
150 return names;
151 }
152
153 // 指定したインデックスのルールを返す
154 public CsvRule GetAt(int idx)
155 {
156 return ruleList[idx];
157 }
158
159 // 指定したルールのインデックスを返す
160 public int IndexOf(CsvRule rule)
161 {
162 return ruleList.IndexOf(rule);
163 }
164
165 // firstLine に一致するルールを探す
166 public CsvRule FindRule(string firstLine)
167 {
168 foreach (CsvRule rule in ruleList)
169 {
170 if (rule.FirstLine == firstLine)
171 {
172 return rule;
173 }
174 }
175 return null;
176 }
177
178 // 定義ファイルをダウンロードする
179 public static bool DownloadRule()
180 {
181 string path = getRulesPath() + "\\CsvRules.xml";
182 //string url = "http://moneyimport.sourceforge.jp/CsvRules.xml";
183 string url = "http://svn.sourceforge.jp/svnroot/moneyimport/trunk/FeliCa2Money.net/CsvRules.xml";
184
185 WebClient w = new WebClient();
186 try
187 {
188 w.DownloadFile(url, path);
189 }
190 catch (Exception ex)
191 {
192 MessageBox.Show(ex.Message, Properties.Resources.Error);
193 return false;
194 }
195 return true;
196 }
197
198 // 定義ファイルのディレクトリを返す
199 // UserAppDataPath からバージョン番号を除いたもの
200 private static string getRulesPath()
201 {
202 string path = Application.LocalUserAppDataPath;
203 path = System.IO.Path.GetDirectoryName(path);
204 return path;
205 }
206 }
207 }

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