Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Backend.Csv/CsvImportCommand.cs @ 6759

Last change on this file since 6759 was 6134, checked in by gkronber, 14 years ago

#1471: added plugin for DbExplorer interfaces, deleted .resx files, set svn:ignore properties, and added license header

File size: 3.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.IO;
25using System.Linq;
26using System.Text;
27using System.Xml;
28using System.Windows.Forms;
29using HeuristicLab.DataImporter.Data;
30using HeuristicLab.DataImporter.Data.Model;
31using HeuristicLab.DataImporter.Data.CommandBase;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33
34namespace HeuristicLab.DataImporter.Backend.Csv {
35  [StorableClass]
36  public class CsvImportCommand : DataSetCommandBase {
37    private ColumnGroup newColumnGroup;
38
39    private CsvImportCommand()
40      : base(null) {
41    }
42
43    public CsvImportCommand(DataSet dataSet)
44      : base(dataSet) {
45    }
46
47    [Storable]
48    private string fileName;
49    public string FileName {
50      get { return fileName; }
51      set { this.fileName = value; }
52    }
53
54    [Storable]
55    private char[] delimiter;
56    public char[] Delimiter {
57      get { return this.delimiter; }
58      set { this.delimiter = value; }
59    }
60
61    [Storable]
62    private bool columnNamesInFirstRow;
63    public bool ColumnNamesInFirstRow {
64      get { return this.columnNamesInFirstRow; }
65      set { this.columnNamesInFirstRow = value; }
66    }
67
68    public override string Description {
69      get { return "Import CSV file"; }
70    }
71
72    public override void Execute() {
73      base.Execute();
74      newColumnGroup = new ColumnGroup(fileName.Substring(fileName.LastIndexOf('\\') + 1));
75      StreamReader reader = new StreamReader(fileName);
76      string firstLine = reader.ReadLine();
77
78      int i = 1;
79      string[] values = firstLine.Split(delimiter);
80      foreach (string value in values) {
81        if (columnNamesInFirstRow)
82          newColumnGroup.AddColumn(new StringColumn(value));
83        else {
84          newColumnGroup.AddColumn(new StringColumn("Column " + i));
85        }
86        i++;
87      }
88      if (!columnNamesInFirstRow)
89        newColumnGroup.AddRow(values);
90      while (!reader.EndOfStream) {
91        newColumnGroup.AddRow(reader.ReadLine().Split(delimiter));
92      }
93      reader.Close();
94      DataSet.AddColumnGroup(newColumnGroup);
95      DataSet.FireChanged();
96    }
97
98    public override void UndoExecute() {
99      base.UndoExecute();
100      DataSet.RemoveColumnGroup(newColumnGroup);
101      newColumnGroup = null;
102      DataSet.FireChanged();
103    }
104  }
105}
Note: See TracBrowser for help on using the repository browser.