PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/AODL/Document/Import/PlainText/CsvImporter.cs

https://bitbucket.org/chrisc/aodl
C# | 264 lines | 146 code | 30 blank | 88 comment | 4 complexity | 47bad5416567c29d7a39af7a83b27184 MD5 | raw file
  1. /*************************************************************************
  2. *
  3. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
  4. *
  5. * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
  6. *
  7. * Use is subject to license terms.
  8. *
  9. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  10. * use this file except in compliance with the License. You may obtain a copy
  11. * of the License at http://www.apache.org/licenses/LICENSE-2.0. You can also
  12. * obtain a copy of the License at http://odftoolkit.org/docs/license.txt
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  16. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. *
  18. * See the License for the specific language governing permissions and
  19. * limitations under the License.
  20. *
  21. ************************************************************************/
  22. using System;
  23. using System.IO;
  24. using System.Collections.Generic;
  25. using AODL.Document.Export;
  26. using AODL.Document.Exceptions;
  27. using AODL.Document.Content.Text;
  28. using AODL.Document.Content.Tables;
  29. using AODL.Document.SpreadsheetDocuments;
  30. using AODL.IO;
  31. namespace AODL.Document.Import.PlainText
  32. {
  33. /// <summary>
  34. /// CsvImporter, a class for importing csv files into
  35. /// OpenDocument spreadsheet documents.
  36. /// </summary>
  37. public class CsvImporter : IImporter, IPublisherInfo
  38. {
  39. /// <summary>
  40. /// The document to fill with content.
  41. /// </summary>
  42. private IDocument _document;
  43. /// <summary>
  44. /// Initializes a new instance of the <see cref="CsvImporter"/> class.
  45. /// </summary>
  46. public CsvImporter()
  47. {
  48. this._importError = new List<AODLWarning>();
  49. this._supportedExtensions = new List<DocumentSupportInfo>();
  50. this._supportedExtensions.Add(new DocumentSupportInfo(".csv", DocumentTypes.SpreadsheetDocument));
  51. this._author = "Lars Behrmann, lb@OpenDocument4all.com";
  52. this._infoUrl = "http://AODL.OpenDocument4all.com";
  53. this._description = "This the standard importer for comma seperated text files of the OpenDocument library AODL.";
  54. }
  55. #region IExporter Member
  56. private IList<DocumentSupportInfo> _supportedExtensions;
  57. /// <summary>
  58. /// Gets the document support infos.
  59. /// </summary>
  60. /// <value>The document support infos.</value>
  61. public IList<DocumentSupportInfo> DocumentSupportInfos
  62. {
  63. get { return this._supportedExtensions; }
  64. }
  65. /// <summary>
  66. /// Imports the specified filename.
  67. /// </summary>
  68. /// <param name="document">The TextDocument to fill.</param>
  69. /// <param name="filename">The filename.</param>
  70. /// <returns>The created TextDocument</returns>
  71. public void Import(IDocument document, string filename)
  72. {
  73. this._document = document;
  74. IList<string> lines = this.GetFileContent(filename);
  75. if (lines.Count > 0)
  76. this.CreateTables(lines);
  77. else
  78. {
  79. AODLWarning warning = new AODLWarning("Empty file. ["+filename+"]");
  80. this.ImportError.Add(warning);
  81. }
  82. }
  83. public Stream Open(string path)
  84. {
  85. return Stream.Null;
  86. }
  87. public IFile GetFile(string path)
  88. {
  89. return null;
  90. }
  91. private IList<AODLWarning> _importError;
  92. /// <summary>
  93. /// Gets the import errors as List of strings.
  94. /// </summary>
  95. /// <value>The import errors.</value>
  96. public IList<AODLWarning> ImportError
  97. {
  98. get
  99. {
  100. return this._importError;
  101. }
  102. }
  103. /// <summary>
  104. /// If the import file format isn't any OpenDocument
  105. /// format you have to return true and AODL will
  106. /// create a new one.
  107. /// </summary>
  108. /// <value></value>
  109. public bool NeedNewOpenDocument
  110. {
  111. get { return true; }
  112. }
  113. #endregion
  114. #region IPublisherInfo Member
  115. private string _author;
  116. /// <summary>
  117. /// The name the Author
  118. /// </summary>
  119. /// <value></value>
  120. public string Author
  121. {
  122. get
  123. {
  124. return this._author;
  125. }
  126. }
  127. private string _infoUrl;
  128. /// <summary>
  129. /// Url to a info site
  130. /// </summary>
  131. /// <value></value>
  132. public string InfoUrl
  133. {
  134. get
  135. {
  136. return this._infoUrl;
  137. }
  138. }
  139. private string _description;
  140. /// <summary>
  141. /// Description about the exporter resp. importer
  142. /// </summary>
  143. /// <value></value>
  144. public string Description
  145. {
  146. get
  147. {
  148. return this._description;
  149. }
  150. }
  151. #endregion
  152. public void DeleteUnpackedFiles()
  153. {
  154. }
  155. /// <summary>
  156. /// Creates the tables.
  157. /// </summary>
  158. /// <param name="lines">The lines.</param>
  159. private void CreateTables(IList<string> lines)
  160. {
  161. string unicodeDelimiter = "\u00BF"; // turned question mark
  162. if (lines != null)
  163. {
  164. Table table = TableBuilder.CreateSpreadsheetTable(
  165. (SpreadsheetDocument)this._document, "Table1", "table1");
  166. //First line must specify the used delimiter
  167. string delimiter = lines[0] as string;
  168. lines.RemoveAt(0);
  169. try
  170. {
  171. //Perform lines
  172. foreach(string line in lines)
  173. {
  174. string lineContent = line.Replace(delimiter, unicodeDelimiter);
  175. string[] cellContents = lineContent.Split(unicodeDelimiter.ToCharArray());
  176. Row row = new Row(table);
  177. foreach(string cellContent in cellContents)
  178. {
  179. Cell cell = new Cell(table.Document);
  180. Paragraph paragraph = ParagraphBuilder.CreateSpreadsheetParagraph(this._document);
  181. paragraph.TextContent.Add(new SimpleText(this._document, cellContent));
  182. cell.Content.Add(paragraph);
  183. row.InsertCellAt(row.Cells.Count, cell);
  184. }
  185. table.Rows.Add(row);
  186. }
  187. }
  188. catch(Exception ex)
  189. {
  190. throw new AODLException("Error while proccessing the csv file.", ex);
  191. }
  192. this._document.Content.Add(table);
  193. }
  194. }
  195. /// <summary>
  196. /// Gets the content of the file.
  197. /// </summary>
  198. /// <param name="fileName">Name of the file.</param>
  199. /// <returns>All text lines as an List of strings.</returns>
  200. private IList<string> GetFileContent(string fileName)
  201. {
  202. IList<string> lines = new List<string>();
  203. try
  204. {
  205. StreamReader sReader = File.OpenText(fileName);
  206. string currentLine = null;
  207. while((currentLine = sReader.ReadLine()) != null)
  208. {
  209. lines.Add(currentLine);
  210. }
  211. sReader.Close();
  212. }
  213. catch(Exception ex)
  214. {
  215. throw ex;
  216. }
  217. return lines;
  218. }
  219. }
  220. }
  221. /*
  222. * $Log: CsvImporter.cs,v $
  223. * Revision 1.2 2008/04/29 15:39:53 mt
  224. * new copyright header
  225. *
  226. * Revision 1.1 2007/02/25 08:58:46 larsbehr
  227. * initial checkin, import from Sourceforge.net to OpenOffice.org
  228. *
  229. * Revision 1.1 2006/02/02 21:55:59 larsbm
  230. * - Added Clone object support for many AODL object types
  231. * - New Importer implementation PlainTextImporter and CsvImporter
  232. * - New tests
  233. *
  234. */