PageRenderTime 37ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/AODL/Document/Import/PlainText/PlainTextImporter.cs

https://bitbucket.org/chrisc/aodl
C# | 237 lines | 119 code | 28 blank | 90 comment | 3 complexity | 70aff94860a31d2e2c8eb8a264717ab9 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.IO;
  29. namespace AODL.Document.Import.PlainText
  30. {
  31. /// <summary>
  32. /// Plain Text Importer.
  33. /// </summary>
  34. public class PlainTextImporter : IImporter, IPublisherInfo
  35. {
  36. /// <summary>
  37. /// The document to fill with content.
  38. /// </summary>
  39. private IDocument _document;
  40. /// <summary>
  41. /// Initializes a new instance of the <see cref="PlainTextImporter"/> class.
  42. /// </summary>
  43. public PlainTextImporter()
  44. {
  45. this._importError = new List<AODLWarning>();
  46. this._supportedExtensions = new List<DocumentSupportInfo>();
  47. this._supportedExtensions.Add(new DocumentSupportInfo(".txt", DocumentTypes.TextDocument));
  48. this._author = "Lars Behrmann, lb@OpenDocument4all.com";
  49. this._infoUrl = "http://AODL.OpenDocument4all.com";
  50. this._description = "This the standard importer for plain text files of the OpenDocument library AODL.";
  51. }
  52. #region IExporter Member
  53. private IList<DocumentSupportInfo> _supportedExtensions;
  54. /// <summary>
  55. /// Gets the document support infos.
  56. /// </summary>
  57. /// <value>The document support infos.</value>
  58. public IList<DocumentSupportInfo> DocumentSupportInfos
  59. {
  60. get { return this._supportedExtensions; }
  61. }
  62. /// <summary>
  63. /// Imports the specified filename.
  64. /// </summary>
  65. /// <param name="document">The TextDocument to fill.</param>
  66. /// <param name="filename">The filename.</param>
  67. /// <returns>The created TextDocument</returns>
  68. public void Import(IDocument document, string filename)
  69. {
  70. this._document = document;
  71. string text = this.ReadContentFromFile(filename);
  72. if (text.Length > 0)
  73. this.ReadTextToDocument(text);
  74. else
  75. {
  76. AODLWarning warning = new AODLWarning("Empty file. ["+filename+"]");
  77. this.ImportError.Add(warning);
  78. }
  79. }
  80. public Stream Open(string path)
  81. {
  82. return Stream.Null;
  83. }
  84. public IFile GetFile(string path)
  85. {
  86. return null;
  87. }
  88. private IList<AODLWarning> _importError;
  89. /// <summary>
  90. /// Gets the import errors as List of strings.
  91. /// </summary>
  92. /// <value>The import errors.</value>
  93. public IList<AODLWarning> ImportError
  94. {
  95. get
  96. {
  97. return this._importError;
  98. }
  99. }
  100. /// <summary>
  101. /// If the import file format isn't any OpenDocument
  102. /// format you have to return true and AODL will
  103. /// create a new one.
  104. /// </summary>
  105. /// <value></value>
  106. public bool NeedNewOpenDocument
  107. {
  108. get { return true; }
  109. }
  110. #endregion
  111. #region IPublisherInfo Member
  112. private string _author;
  113. /// <summary>
  114. /// The name the Author
  115. /// </summary>
  116. /// <value></value>
  117. public string Author
  118. {
  119. get
  120. {
  121. return this._author;
  122. }
  123. }
  124. private string _infoUrl;
  125. /// <summary>
  126. /// Url to a info site
  127. /// </summary>
  128. /// <value></value>
  129. public string InfoUrl
  130. {
  131. get
  132. {
  133. return this._infoUrl;
  134. }
  135. }
  136. private string _description;
  137. /// <summary>
  138. /// Description about the exporter resp. importer
  139. /// </summary>
  140. /// <value></value>
  141. public string Description
  142. {
  143. get
  144. {
  145. return this._description;
  146. }
  147. }
  148. #endregion
  149. public void DeleteUnpackedFiles()
  150. {
  151. }
  152. /// <summary>
  153. /// Reads the text to document.
  154. /// </summary>
  155. /// <param name="text">The text.</param>
  156. private void ReadTextToDocument(string text)
  157. {
  158. ParagraphCollection parCol = ParagraphBuilder.CreateParagraphCollection(
  159. this._document, text, false, ParagraphBuilder.ParagraphSeperator);
  160. if (parCol != null)
  161. foreach(Paragraph paragraph in parCol)
  162. this._document.Content.Add(paragraph);
  163. }
  164. /// <summary>
  165. /// Reads the content from file.
  166. /// </summary>
  167. /// <param name="fileName">Name of the file.</param>
  168. /// <returns></returns>
  169. private string ReadContentFromFile(string fileName)
  170. {
  171. string text = "";
  172. try
  173. {
  174. StreamReader sReader = File.OpenText(fileName);
  175. text = sReader.ReadToEnd();
  176. sReader.Close();
  177. }
  178. catch(Exception ex)
  179. {
  180. throw ex;
  181. }
  182. return this.SetConformLineBreaks(text);
  183. }
  184. /// <summary>
  185. /// Sets the conform line breaks.
  186. /// </summary>
  187. /// <param name="text">The text.</param>
  188. /// <returns></returns>
  189. private string SetConformLineBreaks(string text)
  190. {
  191. return text.Replace(
  192. ParagraphBuilder.ParagraphSeperator2, ParagraphBuilder.ParagraphSeperator);
  193. }
  194. }
  195. }
  196. /*
  197. * $Log: PlainTextImporter.cs,v $
  198. * Revision 1.2 2008/04/29 15:39:53 mt
  199. * new copyright header
  200. *
  201. * Revision 1.1 2007/02/25 08:58:46 larsbehr
  202. * initial checkin, import from Sourceforge.net to OpenOffice.org
  203. *
  204. * Revision 1.1 2006/02/02 21:55:59 larsbm
  205. * - Added Clone object support for many AODL object types
  206. * - New Importer implementation PlainTextImporter and CsvImporter
  207. * - New tests
  208. *
  209. */