-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBCDataRowsValidator.m
367 lines (243 loc) · 9.2 KB
/
BCDataRowsValidator.m
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
//
// BCDataRowsValidator.m
// Xynk
//
// Created by Tom Houpt on 21/9/23.
//
#import "BCDataRowsValidator.h"
#import "XYConstants.h"
@interface BCDataRowsValidator (Private)
-(DataValidationError)trimCells;
-(DataValidationError)trimRows;
-(DataValidationError)numberOfColumns:(NSInteger *)num;
-(DataValidationError)trimColumns;
-(DataValidationError)checkRowNumber;
-(DataValidationError)checkColumnNumber;
-(DataValidationError)checkHeaderRow;
-(void)setMissingValues;
@end
@implementation BCDataRowsValidator
/* -------------------------------------------------------------- */
-(id)initWithRows:(NSMutableArray *)theRows; {
self = [super init];
if (self) {
_rows = theRows;
_errorStrings = @[
@"Non-Equal Row Lengths",
@"Not Enough Columns",
@"Not Enough Rows",
@"Some Rows Longer Than Header",
@"Rows Shorter Than Header",
@"Empty Header Cell",
@"No Data on Pasteboard",
@"Problem reading subject"
];
}
return self;
}
/* -------------------------------------------------------------- */
/**
given an DataValidationError, concatenate error strings corresponding to
the error bits which are set into a single error string message
*/
-(NSString *)errorString:(DataValidationError)error; {
NSMutableString *errorString = [NSMutableString string];
BOOL firstString = YES;
for (NSInteger bitIndex = 0; bitIndex < [_errorStrings count];bitIndex++) {
if (error & 1 << bitIndex) {
if (!firstString) {
[errorString appendString:@"; "];
}
[errorString appendString:_errorStrings[bitIndex]];
firstString = NO;
}
}
return errorString;
}
/* -------------------------------------------------------------- */
/** trim Cells
_rows is an array of arrays of NSStrings
then delete any _rows that contain only empty cells
*/
-(DataValidationError)trimCells; {
NSCharacterSet *whitespace = [NSCharacterSet whitespaceAndNewlineCharacterSet];
for (NSMutableArray *row in _rows) {
for (NSInteger c =0; c < [row count];c++) {
NSString *cell = [row objectAtIndex:c];
[row replaceObjectAtIndex:c withObject:[cell stringByTrimmingCharactersInSet:whitespace]];
}
}
return kDataValidationErrorNoError;
}
/* -------------------------------------------------------------- */
/** trim _rows
_rows is an array of arrays of NSStrings
delete any _rows that contain only empty cells, or which begin with an octothorpe in first cell
*/
-(DataValidationError)trimRows; {
NSMutableArray *rowsToRemove = [NSMutableArray array];
for (NSArray *row in [_rows reverseObjectEnumerator]) {
NSInteger sum_cell_length = 0;
for (NSString *cell in row) {
sum_cell_length += [cell length];
}
if (0 == sum_cell_length || 0 == [row count]) {
[rowsToRemove addObject: row];
}
else { // check for comment row
NSString *firstCell = [row firstObject];
if ([firstCell hasPrefix:@"#"]) {
[rowsToRemove addObject: row];
}
}
}
for (NSArray *row in rowsToRemove) {
[_rows removeObject: row];
}
return kDataValidationErrorNoError;
}
/* -------------------------------------------------------------- */
/** number of Columns
find the maximum number of columns (ie. number of cells in a row)
@return TRUE if all _rows have the same number of cells; otherwise false if there are _rows with disparate number of _rows
*/
-(DataValidationError)numberOfColumns:(NSInteger *)num; {
DataValidationError error = kDataValidationErrorNoError;
NSInteger headerColumns = [(NSMutableArray *)[_rows firstObject] count];
NSInteger maxColumns = headerColumns;
for (NSArray *row in _rows) {
NSInteger rowColumns = [row count];
if (rowColumns != maxColumns) {
error |= kDataValidationErrorNonEqualRowLengths;
}
if (rowColumns > headerColumns) {
error |= kDataValidationErrorRowsLongerThanHeader;
}
if (rowColumns < headerColumns) {
error |= kDataValidationErrorRowsShorterThanHeader;
}
if (rowColumns > maxColumns) {
maxColumns = rowColumns;
}
}
(*num) = maxColumns;
return error;
}
/* -------------------------------------------------------------- */
/** trim Columns
if a column (ie. all cells at row[columnIndex]) are empty,
then the cells at columnIndex are removed from all _rows
Note that a column may have a non-empty header cell (in row[0]) with empty cells underneath the header string
*/
-(DataValidationError)trimColumns; {
DataValidationError error = kDataValidationErrorNoError;
NSInteger numColumns;
error |= [self numberOfColumns:&numColumns];
if (error) {
return kDataValidationErrorNonEqualRowLengths;
}
for (NSInteger c = numColumns - 1; c >= 0; c--) {
BOOL emptyColumn = YES;
for (NSArray *row in _rows) {
NSString *cellString = [row objectAtIndex:c];
NSInteger cellLength = [cellString length];
if (0 < cellLength) {
emptyColumn = NO;
}
}
if (emptyColumn) {
for (NSMutableArray *row in _rows) {
[row removeObjectAtIndex: c];
}
}
}
return error;
}
/* -------------------------------------------------------------- */
/**
make sure we have the minimum number of rows (at least header and 1 subject)
minimum is defined by constant kDataRowsMinimum
@return kDataValidationErrorNoError if sufficient rows otherwise error kDataValidationErrorInsufficientRows
*/
-(DataValidationError)checkRowNumber; {
NSInteger numRows = [_rows count];
if (kDataRowsMinimum > numRows) {
return kDataValidationErrorInsufficientRows;
}
return kDataValidationErrorNoError;
}
/* -------------------------------------------------------------- */
/**
make sure we have the minimum number of columns (at least subject, group, and 1 measure)
minimum is defined by constant kDataColumnsMinimum
@return kDataValidationErrorNoError if sufficient columns otherwise error kDataValidationErrorInsufficientColumns
*/
-(DataValidationError)checkColumnNumber; {
NSInteger numColumns = [[_rows firstObject] count];
if (kDataColumnsMinimum > numColumns) {
return kDataValidationErrorInsufficientColumns;
}
return kDataValidationErrorNoError;
}
/* -------------------------------------------------------------- */
/**
make sure we have a a header row that contains all non-empty cells
@return kDataValidationErrorNoError if valid header row, otherwise return error kDataValidationErrorEmptyHeaderCell
*/
-(DataValidationError)checkHeaderRow; {
NSMutableArray *headerRow = [_rows firstObject];
DataValidationError error = kDataValidationErrorNoError;
for (NSInteger c =0; c < [headerRow count];c++) {
NSString *cell = [headerRow objectAtIndex:c];
if ([cell length] == 0) {
error |= kDataValidationErrorEmptyHeaderCell;
}
}
return error;
}
/* -------------------------------------------------------------- */
/** if there are any empty cells (ie cells with string length 0
set the cell contents to kMissingDataOrNAString
*/
-(void)setMissingValues; {
for (NSMutableArray *row in _rows) {
for (NSInteger c =0; c < [row count];c++) {
NSString *cell = [row objectAtIndex:c];
if ([cell length] == 0) {
[row replaceObjectAtIndex:c withObject:kMissingDataOrNAString];
}
}
}
}
/* -------------------------------------------------------------- */
/** validate _rows
trim and validate data _rows
trimming:
- will remove white space from start and end of each cell string
- will remove empty rows (rows that contain only cells with 0-length string and comment rows that begin with "#" in first cell)
- will remove columns that consist of only empty cells
validating:
- check row number (must be at least kDataRowsMinimum, with header and 1 subject)
- check column number (must be at least kDataColumnsMinimum, with subject, group, and 1 measure)
- make sure all cells in header row contain non-empty strings
- set any remaining empty cells to kMissingDataOrNAString
@return a DataValidationError error code that can be used to signal user of problem
*/
-(DataValidationError)validateRows; {
DataValidationError error = kDataValidationErrorNoError;
error |= [self trimCells];
error |= [self trimRows];
error |= [self trimColumns];
if (error) {
return error;
}
error |= [self checkRowNumber];
error |= [self checkColumnNumber];
error |= [self checkHeaderRow];
if (error) {
return error;
}
[self setMissingValues];
return error;
}
@end