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

Language C# Lines 264
MD5 Hash 47bad5416567c29d7a39af7a83b27184
Repository https://bitbucket.org/chrisc/aodl View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 * 
 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
 * 
 * Use is subject to license terms.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy
 * of the License at http://www.apache.org/licenses/LICENSE-2.0. You can also
 * obtain a copy of the License at http://odftoolkit.org/docs/license.txt
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * 
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ************************************************************************/

using System;
using System.IO;
using System.Collections.Generic;
using AODL.Document.Export;
using AODL.Document.Exceptions;
using AODL.Document.Content.Text;
using AODL.Document.Content.Tables;
using AODL.Document.SpreadsheetDocuments;
using AODL.IO;

namespace AODL.Document.Import.PlainText
{
	/// <summary>
	/// CsvImporter, a class for importing csv files into
	/// OpenDocument spreadsheet documents.
	/// </summary>
	public class CsvImporter : IImporter, IPublisherInfo
	{
		/// <summary>
		/// The document to fill with content.
		/// </summary>
		private IDocument _document;

		/// <summary>
		/// Initializes a new instance of the <see cref="CsvImporter"/> class.
		/// </summary>
		public CsvImporter()
		{
			this._importError					= new List<AODLWarning>();
			
			this._supportedExtensions			= new List<DocumentSupportInfo>();
			this._supportedExtensions.Add(new DocumentSupportInfo(".csv", DocumentTypes.SpreadsheetDocument));

			this._author						= "Lars Behrmann, lb@OpenDocument4all.com";
			this._infoUrl						= "http://AODL.OpenDocument4all.com";
			this._description					= "This the standard importer for comma seperated text files of the OpenDocument library AODL.";
		}

		#region IExporter Member

		private IList<DocumentSupportInfo> _supportedExtensions;
		/// <summary>
		/// Gets the document support infos.
		/// </summary>
		/// <value>The document support infos.</value>
		public IList<DocumentSupportInfo> DocumentSupportInfos
		{
			get { return this._supportedExtensions; }
		}

		/// <summary>
		/// Imports the specified filename.
		/// </summary>
		/// <param name="document">The TextDocument to fill.</param>
		/// <param name="filename">The filename.</param>
		/// <returns>The created TextDocument</returns>
		public void Import(IDocument document, string filename)
		{
			this._document			= document;
			IList<string> lines			= this.GetFileContent(filename);
			
			if (lines.Count > 0)
				this.CreateTables(lines);
			else
			{
				AODLWarning warning	= new AODLWarning("Empty file. ["+filename+"]");
				this.ImportError.Add(warning);
			}
		}

        public Stream Open(string path)
        {
            return Stream.Null;
        }

        public IFile GetFile(string path)
        {
            return null;
        }
        
        private IList<AODLWarning> _importError;
		/// <summary>
		/// Gets the import errors as List of strings.
		/// </summary>
		/// <value>The import errors.</value>
        public IList<AODLWarning> ImportError
		{
			get
			{
				return this._importError;
			}
		}

		/// <summary>
		/// If the import file format isn't any OpenDocument
		/// format you have to return true and AODL will
		/// create a new one.
		/// </summary>
		/// <value></value>
		public bool NeedNewOpenDocument
		{
			get { return true; }
		}

		#endregion

		#region IPublisherInfo Member

		private string _author;
		/// <summary>
		/// The name the Author
		/// </summary>
		/// <value></value>
		public string Author
		{
			get
			{
				return this._author;
			}
		}

		private string _infoUrl;
		/// <summary>
		/// Url to a info site
		/// </summary>
		/// <value></value>
		public string InfoUrl
		{
			get
			{
				return this._infoUrl;
			}
		}

		private string _description;
		/// <summary>
		/// Description about the exporter resp. importer
		/// </summary>
		/// <value></value>
		public string Description
		{
			get
			{
				return this._description;
			}
		}

		#endregion

		public void DeleteUnpackedFiles()
		{
			
		}
		
		/// <summary>
		/// Creates the tables.
		/// </summary>
		/// <param name="lines">The lines.</param>
		private void CreateTables(IList<string> lines)
		{
			string unicodeDelimiter				= "\u00BF"; // turned question mark

			if (lines != null)
			{
				Table table						= TableBuilder.CreateSpreadsheetTable(
					(SpreadsheetDocument)this._document, "Table1", "table1");
				//First line must specify the used delimiter
				string delimiter				= lines[0] as string;
				lines.RemoveAt(0);

				try
				{
					//Perform lines
					foreach(string line in lines)
					{
						string lineContent			= line.Replace(delimiter, unicodeDelimiter);
						string[] cellContents		= lineContent.Split(unicodeDelimiter.ToCharArray());
						Row row						= new Row(table);
						foreach(string cellContent in cellContents)
						{
							Cell cell				= new Cell(table.Document);
							Paragraph paragraph		= ParagraphBuilder.CreateSpreadsheetParagraph(this._document);
							paragraph.TextContent.Add(new SimpleText(this._document, cellContent));
							cell.Content.Add(paragraph);
							row.InsertCellAt(row.Cells.Count, cell);
						}
						table.Rows.Add(row);
					}
				}
				catch(Exception ex)
				{
					throw new AODLException("Error while proccessing the csv file.", ex);
				}

				this._document.Content.Add(table);
			}
		}

		/// <summary>
		/// Gets the content of the file.
		/// </summary>
		/// <param name="fileName">Name of the file.</param>
		/// <returns>All text lines as an List of strings.</returns>
		private IList<string> GetFileContent(string fileName)
		{
			IList<string> lines						= new List<string>();

			try
			{
				StreamReader sReader	= File.OpenText(fileName);
				string currentLine		= null;

				while((currentLine = sReader.ReadLine()) != null)
				{
					lines.Add(currentLine);
				}
				sReader.Close();
			}
			catch(Exception ex)
			{
				throw ex;
			}

			return lines;
		}
	}
}

/*
 * $Log: CsvImporter.cs,v $
 * Revision 1.2  2008/04/29 15:39:53  mt
 * new copyright header
 *
 * Revision 1.1  2007/02/25 08:58:46  larsbehr
 * initial checkin, import from Sourceforge.net to OpenOffice.org
 *
 * Revision 1.1  2006/02/02 21:55:59  larsbm
 * - Added Clone object support for many AODL object types
 * - New Importer implementation PlainTextImporter and CsvImporter
 * - New tests
 *
 */
Back to Top