-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDBFileWriter.m
More file actions
321 lines (257 loc) · 7.53 KB
/
Copy pathDBFileWriter.m
File metadata and controls
321 lines (257 loc) · 7.53 KB
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
/* -*- mode: objc -*-
Project: DataBasin
Copyright (C) 2009-2021 Free Software Foundation
Author: Riccardo Mottola
Created: 2017-09-20 Riccardo Mottola
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 31 Milk Street #960789 Boston, MA 02196 USA.
*/
#import "DBFileWriter.h"
#import "DBSObject.h"
#import "DBLoggerProtocol.h"
#import "DBSFTypeWrappers.h"
#import <WebServices/GWSConstants.h>
@implementation DBFileWriter
- (id)initWithHandle:(NSFileHandle *)fileHandle
{
if ((self = [super init]))
{
file = fileHandle;
writeOrdered = NO;
fieldNames = nil;
}
return self;
}
- (void)dealloc
{
[fieldNames release];
[super dealloc];
}
- (void)setFileFormat:(NSString *)f
{
}
- (void)setLogger:(id<DBLoggerProtocol>)l
{
logger = l;
}
- (void)setStringEncoding: (NSStringEncoding) enc
{
NSData *tempData;
encoding = enc;
bomLength = 0;
/* BOM heuristics */
tempData = [@"" dataUsingEncoding: encoding];
bomLength = [tempData length];
NSLog(@"bom length: %u", bomLength);
}
- (void)setWriteFieldsOrdered:(BOOL)flag
{
writeOrdered = flag;
}
- (BOOL)writeFieldsOrdered
{
return writeOrdered;
}
/*
This methods sets the internal field names for the header when using ordered object writeout.
*/
- (void)setFieldNames:(id)obj andWriteThem:(BOOL)flag
{
NSArray *array;
[logger log: LogDebug :@"[DBFileWriter setFieldNames] for Object: %@:\n", obj];
/* if we have no data, we return */
if (obj == nil)
return;
/* if we have just a single object, we fake an array */
if([obj isKindOfClass: [NSArray class]])
array = obj;
else
array = [NSArray arrayWithObject: obj];
if ([array count] == 0)
return;
if (fieldNames != array)
{
[fieldNames release];
fieldNames = array;
[array retain];
}
[logger log: LogDebug :@"[DBFileWriter setFieldNames] Names: %@:\n", array];
/* if we write the header, fine, else we write at least the BOM */
if (flag == YES)
{
NSString *theLine;
theLine = [self formatOneLine:array forHeader:YES];
[file writeData: [theLine dataUsingEncoding: encoding]];
}
else
{
NSData *tempData;
tempData = [@" "dataUsingEncoding: encoding];
tempData = [tempData subdataWithRange: NSMakeRange(0, bomLength)];
[file writeData: tempData];
}
}
- (void)writeStart
{
}
- (void)writeEnd
{
}
- (void)writeDataSet:(NSArray *)array
{
NSUInteger i;
NSUInteger setCount;
NSAutoreleasePool *arp;
if (array == nil)
return;
arp = [[NSAutoreleasePool alloc] init];
setCount = [array count];
for (i = 0; i < setCount; i++)
{
NSString *oneLine;
NSData *data;
NSData *data2;
id o;
o = [array objectAtIndex:i];
if ([o isKindOfClass: [DBSObject class]])
o = [NSArray arrayWithObject: o];
oneLine = [self formatOneLine:o forHeader:NO];
data = [oneLine dataUsingEncoding: encoding];
if (bomLength > 0)
data2 = [NSData dataWithBytesNoCopy: (void *)[data bytes]+bomLength length: [data length]-bomLength freeWhenDone: NO];
else
data2 = data;
[file writeData: data2];
}
[arp release];
}
- (void)formatComplexObject:(NSMutableDictionary *)d withRoot:(NSString *)root inDict:(NSMutableDictionary *)dict inOrder:(NSMutableArray *)order
{
NSMutableArray *keys;
NSUInteger i;
if (!d)
return;
keys = [NSMutableArray arrayWithArray:[d allKeys]];
[keys removeObject:GWSOrderKey];
if ([root hasSuffix:@"Address"])
{
NSMutableString *str;
str = [[NSMutableString alloc] init];
for (i = 0; i < [keys count]; i++)
{
NSString *s;
s = [d objectForKey:[keys objectAtIndex:i]];
if (s && [s length])
{
if ([str length])
[str appendString: @" - "];
[str appendString:s];
}
}
[dict setObject:str forKey:root];
[order addObject:root];
[str release];
}
/* remove some fields which get added automatically by salesforce even if not asked for */
[keys removeObject:@"type"];
for (i = 0; i < [keys count]; i++)
{
id obj;
NSString *key;
NSMutableString *extendedFieldName;
key = [keys objectAtIndex: i];
obj = [d objectForKey: key];
if ([key isEqualToString:@"Id"])
{
if ([obj isKindOfClass: [NSArray class]])
obj = [obj objectAtIndex: 0];
else if ([obj isKindOfClass: [NSString class]] && [obj length] == 0)
continue;
}
if (root)
{
extendedFieldName = [NSMutableString stringWithString:root];
[extendedFieldName appendString:@"."];
[extendedFieldName appendString:key];
}
else
{
extendedFieldName = [NSMutableString stringWithString:key];
}
if ([obj isKindOfClass: [NSDictionary class]])
{
[self formatComplexObject: obj withRoot:extendedFieldName inDict:dict inOrder:order];
}
else if ([obj isKindOfClass: [DBSFDataType class]])
{
[dict setObject:obj forKey:extendedFieldName];
[order addObject:extendedFieldName];
}
else if ([obj isKindOfClass: [NSString class]] || [obj isKindOfClass: [NSNumber class]])
{
[dict setObject:obj forKey:extendedFieldName];
[order addObject:extendedFieldName];
}
else
NSLog(@"[DBFileWriter formatComplexObject] unknown class of value: %@, object: %@", [obj class], obj);
}
}
- (void)formatSObject:(DBSObject *)so withRoot:(NSString *)root inDict:(NSMutableDictionary *)dict inOrder:(NSMutableArray *)order
{
NSArray *fields;
NSUInteger i;
if (!so)
return;
fields = [so fieldNames];
for (i = 0; i < [fields count]; i++)
{
id obj;
NSString *field;
NSMutableString *extendedFieldName;
field = [fields objectAtIndex: i];
obj = [so valueForField: field];
if (root)
{
extendedFieldName = [NSMutableString stringWithString:root];
[extendedFieldName appendString:@"."];
[extendedFieldName appendString:field];
}
else
{
extendedFieldName = [NSMutableString stringWithString:field];
}
if ([obj isKindOfClass: [DBSFDataType class]])
{
[dict setObject:obj forKey:extendedFieldName];
[order addObject:extendedFieldName];
}
else if ([obj isKindOfClass: [NSString class]] || [obj isKindOfClass: [NSNumber class]])
{
[dict setObject:obj forKey:extendedFieldName];
[order addObject:extendedFieldName];
}
else if ([obj isKindOfClass: [NSDictionary class]])
{
[self formatComplexObject: obj withRoot:extendedFieldName inDict:dict inOrder:order];
}
else
{
NSLog(@"[DBFileWriter formatSObject] unknown class of value: %@, object: %@", [obj class], obj);
}
}
}
- (NSString *)formatOneLine:(id)data forHeader:(BOOL) headerFlag
{
NSLog(@"DBFileWriter - formatOneLine: forHeader: - method should be subclassed");
return nil;
}
@end