-
Notifications
You must be signed in to change notification settings - Fork 64
/
FormatCSV.cs
255 lines (218 loc) · 8.88 KB
/
FormatCSV.cs
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
// Name: FormatCSV.cs
// Description: Text file import/export for localization
// Author: Tim Chipman
// Origination: Work performed for BuildingSmart by Constructivity.com LLC.
// Copyright: (c) 2012 BuildingSmart International Ltd.
// License: http://www.buildingsmart-tech.org/legal
using System;
using System.Collections.Generic;
using System.Text;
using IfcDoc.Schema.DOC;
namespace IfcDoc
{
internal class FormatCSV : IDisposable
{
string m_filename;
DocProject m_project;
string[] m_locales;
public FormatCSV(string filename)
{
this.m_filename = filename;
}
public DocProject Instance
{
get
{
return this.m_project;
}
set
{
this.m_project = value;
}
}
/// <summary>
/// Optional list of locales to import/export for names and descriptions.
/// </summary>
public string[] Locales
{
get
{
return this.m_locales;
}
set
{
this.m_locales = value;
}
}
public void Load()
{
// prepare map
Dictionary<string, DocObject> map = new Dictionary<string, DocObject>();
foreach (DocSection docSection in this.m_project.Sections)
{
foreach (DocSchema docSchema in docSection.Schemas)
{
foreach (DocEntity docEntity in docSchema.Entities)
{
map.Add(docEntity.Name, docEntity);
}
foreach (DocType docType in docSchema.Types)
{
map.Add(docType.Name, docType);
}
}
}
// use tabs for simplicity
using (System.IO.StreamReader reader = new System.IO.StreamReader(this.m_filename))
{
string headerline = reader.ReadLine();
string[] headercols = headerline.Split(new char[]{'\t'}, StringSplitOptions.RemoveEmptyEntries);
string blankline = reader.ReadLine(); // expect blank line
// first column is name that identifies definition
string[] locales = new string[headercols.Length];
string[] fields = new string[headercols.Length];
for(int icol = 0; icol < headercols.Length; icol++)
{
string col = headercols[icol];
int popen = col.IndexOf('(');
int pclos = col.IndexOf(')');
if(popen > 0 && pclos > popen)
{
locales[icol] = col.Substring(popen + 1, pclos - popen - 1);
fields[icol] = col.Substring(0, popen);
}
}
// now rows
while (!reader.EndOfStream)
{
string rowdata = reader.ReadLine();
string[] rowcells = rowdata.Split(new char[] { '\t' }, StringSplitOptions.RemoveEmptyEntries);
if (rowcells.Length > 1)
{
DocObject docObj = null;
string identifier = rowcells[0];
if (map.TryGetValue(identifier, out docObj))
{
for(int i = 1; i < rowcells.Length && i < headercols.Length; i++)
{
if (locales[i] != null)
{
// find existing
DocLocalization docLocalization = null;
foreach (DocLocalization docLocal in docObj.Localization)
{
if (docLocal.Locale.Equals(locales[i]))
{
docLocalization = docLocal;
break;
}
}
// create new
if (docLocalization == null)
{
docLocalization = new DocLocalization();
docLocalization.Locale = locales[i];
docObj.Localization.Add(docLocalization);
}
string value = rowcells[i];
if (value != null && value.StartsWith("\"") && value.EndsWith("\""))
{
// strip quotes
value = value.Substring(1, value.Length - 2);
}
// update info
switch (fields[i])
{
case "Name":
docLocalization.Name = value;
break;
case "Description":
docLocalization.Documentation = value;
break;
}
}
}
}
}
}
}
}
private void WriteList(System.IO.StreamWriter writer, SortedList<string, DocObject> sortlist)
{
foreach (string key in sortlist.Keys)
{
DocObject docEntity = sortlist[key];
writer.Write(docEntity.Name);
if (this.m_locales != null)
{
foreach (string locale in this.m_locales)
{
string localname = "";
string localdesc = "";
foreach (DocLocalization docLocal in docEntity.Localization)
{
if (docLocal.Locale.Equals(locale))
{
localname = docLocal.Name;
localdesc = docLocal.Documentation;
break;
}
}
writer.Write("\t");
writer.Write(localname);
writer.Write("\t");
writer.Write(localdesc);
}
}
writer.WriteLine();
}
}
public void Save()
{
using (System.IO.StreamWriter writer = new System.IO.StreamWriter(this.m_filename))
{
// header
writer.Write("Name");
if (this.m_locales != null)
{
foreach (string locale in this.m_locales)
{
writer.Write("\tName(");
writer.Write(locale);
writer.Write(")\tDescription(");
writer.Write(locale);
writer.Write(")");
}
}
writer.WriteLine();
// blank line separates row data
writer.WriteLine();
// split into two lists alphabetically for easier translation
SortedList<string, DocObject> sortlistEntity = new SortedList<string, DocObject>();
SortedList<string, DocObject> sortlistType = new SortedList<string, DocObject>();
// rows
foreach (DocSection docSection in this.m_project.Sections)
{
foreach (DocSchema docSchema in docSection.Schemas)
{
foreach (DocEntity docEntity in docSchema.Entities)
{
sortlistEntity.Add(docEntity.Name, docEntity);
}
foreach (DocType docEntity in docSchema.Types)
{
sortlistType.Add(docEntity.Name, docEntity);
}
}
}
WriteList(writer, sortlistEntity);
WriteList(writer, sortlistType);
}
}
#region IDisposable Members
public void Dispose()
{
}
#endregion
}
}