PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/Trunk/Source/Qif/QifApi/QifDom.cs

#
C# | 377 lines | 204 code | 58 blank | 115 comment | 6 complexity | 163409c141de8795d2f804ce0ac8b221 MD5 | raw file
  1. using System.Collections.Generic;
  2. using QifApi.Transactions;
  3. using System.IO;
  4. using QifApi.Logic;
  5. using System.Text.RegularExpressions;
  6. using System.Runtime.InteropServices;
  7. namespace QifApi
  8. {
  9. /// <summary>
  10. /// Represents a Document Object Model for a QIF file.
  11. /// </summary>
  12. [ClassInterface(ClassInterfaceType.None)]
  13. public class QifDom
  14. {
  15. /// <summary>
  16. /// Represents a collection of bank transactions.
  17. /// </summary>
  18. public List<BasicTransaction> BankTransactions
  19. {
  20. get;
  21. set;
  22. }
  23. /// <summary>
  24. /// Represents a collection of cash transactions.
  25. /// </summary>
  26. public List<BasicTransaction> CashTransactions
  27. {
  28. get;
  29. set;
  30. }
  31. /// <summary>
  32. /// Represents a collection of credit card transactions.
  33. /// </summary>
  34. public List<BasicTransaction> CreditCardTransactions
  35. {
  36. get;
  37. set;
  38. }
  39. /// <summary>
  40. /// Represents a collection of investment transactions.
  41. /// </summary>
  42. public List<InvestmentTransaction> InvestmentTransactions
  43. {
  44. get;
  45. set;
  46. }
  47. /// <summary>
  48. /// Represents a collection of asset transactions.
  49. /// </summary>
  50. public List<BasicTransaction> AssetTransactions
  51. {
  52. get;
  53. set;
  54. }
  55. /// <summary>
  56. /// Represents a collection of liability transactions.
  57. /// </summary>
  58. public List<BasicTransaction> LiabilityTransactions
  59. {
  60. get;
  61. set;
  62. }
  63. /// <summary>
  64. /// Represents a collection of account list transactions.
  65. /// </summary>
  66. public List<AccountListTransaction> AccountListTransactions
  67. {
  68. get;
  69. set;
  70. }
  71. /// <summary>
  72. /// Represents a collection of category list transactions.
  73. /// </summary>
  74. public List<CategoryListTransaction> CategoryListTransactions
  75. {
  76. get;
  77. set;
  78. }
  79. /// <summary>
  80. /// Represents a collection of class list transactions.
  81. /// </summary>
  82. public List<ClassListTransaction> ClassListTransactions
  83. {
  84. get;
  85. set;
  86. }
  87. /// <summary>
  88. /// Represents a collection of memorized transaction list transactions.
  89. /// </summary>
  90. public List<MemorizedTransactionListTransaction> MemorizedTransactionListTransactions
  91. {
  92. get;
  93. set;
  94. }
  95. /// <summary>
  96. /// Creates a new QIF DOM.
  97. /// </summary>
  98. public QifDom()
  99. {
  100. BankTransactions = new List<BasicTransaction>();
  101. CashTransactions = new List<BasicTransaction>();
  102. CreditCardTransactions = new List<BasicTransaction>();
  103. InvestmentTransactions = new List<InvestmentTransaction>();
  104. AssetTransactions = new List<BasicTransaction>();
  105. LiabilityTransactions = new List<BasicTransaction>();
  106. AccountListTransactions = new List<AccountListTransaction>();
  107. CategoryListTransactions = new List<CategoryListTransaction>();
  108. ClassListTransactions = new List<ClassListTransaction>();
  109. MemorizedTransactionListTransactions = new List<MemorizedTransactionListTransaction>();
  110. }
  111. /// <summary>
  112. /// Imports the specified file and replaces the current instance properties with details found in the import file.
  113. /// </summary>
  114. /// <param name="fileName">Name of the file to import.</param>
  115. public void Import(string fileName)
  116. {
  117. using (StreamReader reader = new StreamReader(fileName))
  118. {
  119. Import(reader);
  120. }
  121. }
  122. /// <summary>
  123. /// Imports a stream in a QIF format and replaces the current instance properties with details found in the import stream.
  124. /// </summary>
  125. /// <param name="reader">The import reader stream.</param>
  126. public void Import(StreamReader reader)
  127. {
  128. QifDom import = ImportFile(reader);
  129. this.AccountListTransactions = import.AccountListTransactions;
  130. this.AssetTransactions = import.AssetTransactions;
  131. this.BankTransactions = import.BankTransactions;
  132. this.CashTransactions = import.CashTransactions;
  133. this.CategoryListTransactions = import.CategoryListTransactions;
  134. this.ClassListTransactions = import.ClassListTransactions;
  135. this.CreditCardTransactions = import.CreditCardTransactions;
  136. this.InvestmentTransactions = import.InvestmentTransactions;
  137. this.LiabilityTransactions = import.LiabilityTransactions;
  138. this.MemorizedTransactionListTransactions = import.MemorizedTransactionListTransactions;
  139. }
  140. /// <summary>
  141. /// Exports the current instance properties to the specified file.
  142. /// </summary>
  143. /// <param name="fileName">Name of the file.</param>
  144. /// <remarks>This will overwrite an existing file.</remarks>
  145. public void Export(string fileName)
  146. {
  147. ExportFile(this, fileName);
  148. }
  149. /// <summary>
  150. /// Exports the specified instance properties to the specified file.
  151. /// </summary>
  152. /// <param name="qif">The <seealso cref="T:QifDom"/> to export.</param>
  153. /// <param name="fileName">Name of the file.</param>
  154. /// <remarks>This will overwrite an existing file.</remarks>
  155. public static void ExportFile(QifDom qif, string fileName)
  156. {
  157. if (File.Exists(fileName))
  158. {
  159. File.SetAttributes(fileName, FileAttributes.Normal);
  160. }
  161. using (StreamWriter writer = new StreamWriter(fileName))
  162. {
  163. writer.AutoFlush = true;
  164. AccountListLogic.Export(writer, qif.AccountListTransactions);
  165. AssetLogic.Export(writer, qif.AssetTransactions);
  166. BankLogic.Export(writer, qif.BankTransactions);
  167. CashLogic.Export(writer, qif.CashTransactions);
  168. CategoryListLogic.Export(writer, qif.CategoryListTransactions);
  169. ClassListLogic.Export(writer, qif.ClassListTransactions);
  170. CreditCardLogic.Export(writer, qif.CreditCardTransactions);
  171. InvestmentLogic.Export(writer, qif.InvestmentTransactions);
  172. LiabilityLogic.Export(writer, qif.LiabilityTransactions);
  173. MemorizedTransactionListLogic.Export(writer, qif.MemorizedTransactionListTransactions);
  174. }
  175. }
  176. /// <summary>
  177. /// Imports a QIF file and returns a QifDom object.
  178. /// </summary>
  179. /// <param name="fileName">The QIF file to import.</param>
  180. /// <returns>A QifDom object of transactions imported.</returns>
  181. public static QifDom ImportFile(string fileName)
  182. {
  183. QifDom result = null;
  184. // If the file doesn't exist
  185. if (File.Exists(fileName) == false)
  186. {
  187. // Identify the file doesn't exist
  188. throw new FileNotFoundException();
  189. }
  190. // Open the file
  191. using (StreamReader sr = new StreamReader(fileName))
  192. {
  193. result = ImportFile(sr);
  194. }
  195. return result;
  196. }
  197. /// <summary>
  198. /// Imports a QIF file stream reader and returns a QifDom object.
  199. /// </summary>
  200. /// <param name="reader">The stream reader pointing to an underlying QIF file to import.</param>
  201. /// <returns>A QifDom object of transactions imported.</returns>
  202. public static QifDom ImportFile(StreamReader reader)
  203. {
  204. QifDom result = new QifDom();
  205. // Read the entire file
  206. string input = reader.ReadToEnd();
  207. // Split the file by header types
  208. string[] transactionTypes = Regex.Split(input, @"^(!.*)$", RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace);
  209. // Loop through the transaction types
  210. for (int i = 0; i < transactionTypes.Length; i++)
  211. {
  212. // Get the exact transaction type
  213. string transactionType = transactionTypes[i].Replace("\r", "").Replace("\n", "").Trim();
  214. // If the string has a value
  215. if (transactionType.Length > 0)
  216. {
  217. // Check the transaction type
  218. switch (transactionType)
  219. {
  220. case Headers.Bank:
  221. // Increment the array counter
  222. i++;
  223. // Extract the transaction items
  224. string bankItems = transactionTypes[i];
  225. // Import all transaction types
  226. result.BankTransactions.AddRange(BankLogic.Import(bankItems));
  227. // All done
  228. break;
  229. case Headers.AccountList:
  230. // Increment the array counter
  231. i++;
  232. // Extract the transaction items
  233. string accountListItems = transactionTypes[i];
  234. // Import all transaction types
  235. result.AccountListTransactions.AddRange(AccountListLogic.Import(accountListItems));
  236. // All done
  237. break;
  238. case Headers.Asset:
  239. // Increment the array counter
  240. i++;
  241. // Extract the transaction items
  242. string assetItems = transactionTypes[i];
  243. // Import all transaction types
  244. result.AssetTransactions.AddRange(AssetLogic.Import(assetItems));
  245. // All done
  246. break;
  247. case Headers.Cash:
  248. // Increment the array counter
  249. i++;
  250. // Extract the transaction items
  251. string cashItems = transactionTypes[i];
  252. // Import all transaction types
  253. result.CashTransactions.AddRange(CashLogic.Import(cashItems));
  254. // All done
  255. break;
  256. case Headers.CategoryList:
  257. // Increment the array counter
  258. i++;
  259. // Extract the transaction items
  260. string catItems = transactionTypes[i];
  261. // Import all transaction types
  262. result.CategoryListTransactions.AddRange(CategoryListLogic.Import(catItems));
  263. // All done
  264. break;
  265. case Headers.ClassList:
  266. // Increment the array counter
  267. i++;
  268. // Extract the transaction items
  269. string classItems = transactionTypes[i];
  270. // Import all transaction types
  271. result.ClassListTransactions.AddRange(ClassListLogic.Import(classItems));
  272. // All done
  273. break;
  274. case Headers.CreditCard:
  275. // Increment the array counter
  276. i++;
  277. // Extract the transaction items
  278. string ccItems = transactionTypes[i];
  279. // Import all transaction types
  280. result.CreditCardTransactions.AddRange(CreditCardLogic.Import(ccItems));
  281. // All done
  282. break;
  283. case Headers.Investment:
  284. // Increment the array counter
  285. i++;
  286. // Extract the transaction items
  287. string investItems = transactionTypes[i];
  288. // Import all transaction types
  289. result.InvestmentTransactions.AddRange(InvestmentLogic.Import(investItems));
  290. // All done
  291. break;
  292. case Headers.Liability:
  293. // Increment the array counter
  294. i++;
  295. // Extract the transaction items
  296. string liabilityItems = transactionTypes[i];
  297. // Import all transaction types
  298. result.LiabilityTransactions.AddRange(LiabilityLogic.Import(liabilityItems));
  299. // All done
  300. break;
  301. case Headers.MemorizedTransactionList:
  302. // Increment the array counter
  303. i++;
  304. // Extract the transaction items
  305. string memItems = transactionTypes[i];
  306. // Import all transaction types
  307. result.MemorizedTransactionListTransactions.AddRange(MemorizedTransactionListLogic.Import(memItems));
  308. // All done
  309. break;
  310. default:
  311. // Don't do any processing
  312. break;
  313. }
  314. }
  315. }
  316. return result;
  317. }
  318. }
  319. }