-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXMLRPCEncoder.m
379 lines (274 loc) · 9.97 KB
/
XMLRPCEncoder.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
368
369
370
371
372
373
374
375
376
377
378
379
#import "XMLRPCEncoder.h"
#import "NSStringAdditions.h"
#import "NSData+Base64.h"
// If you change this, be sure it's a multiple of 90.
// The base 64 encoding adds a new line every 90 characters.
#define CHUNK_SIZE 18000
@interface XMLRPCEncoder (XMLRPCEncoderPrivate)
- (void)valueTag: (NSString *)tag value: (NSString *)value;
#pragma mark -
- (NSString *)replaceTarget: (NSString *)target withValue: (NSString *)value inString: (NSString *)string;
#pragma mark -
- (void)encodeObject: (id)object;
#pragma mark -
- (void)encodeArray: (NSArray *)array;
- (void)encodeDictionary: (NSDictionary *)dictionary;
#pragma mark -
- (void)encodeBoolean: (CFBooleanRef)boolean;
- (void)encodeNumber: (NSNumber *)number;
- (void)encodeString: (NSString *)string omitTag: (BOOL)omitTag;
- (void)encodeDate: (NSDate *)date;
- (void)encodeData: (NSData *)data;
- (void)encodeInputStream: (NSInputStream *)stream;
- (void)encodeFileHandle: (NSFileHandle *)handle;
#pragma mark -
- (void)appendString:(NSString *)aString;
- (void)appendFormat:(NSString *)format, ...;
#pragma mark -
- (void)openStreamingCache;
@end
#pragma mark -
@implementation XMLRPCEncoder
- (id)init {
self = [super init];
if (self) {
myMethod = [[NSString alloc] init];
myParameters = [[NSArray alloc] init];
}
return self;
}
#pragma mark -
- (NSString *)encode {
if (streamingCacheFilePath == nil) {
[self encodeForStreaming];
}
NSInputStream *stream = [self encodedStream];
NSMutableData *encodedData = [NSMutableData data];
[stream open];
while ([stream hasBytesAvailable]) {
uint8_t buf[1024];
unsigned int len = 0;
len = [stream read:buf maxLength:1024];
if (len) {
[encodedData appendBytes:buf length:len];
}
}
[stream close];
return [[[NSString alloc] initWithData:encodedData encoding:NSUTF8StringEncoding] autorelease];
}
- (void)encodeForStreaming {
[self openStreamingCache];
[self appendString: @"<?xml version=\"1.0\"?><methodCall><methodName>"];
[self encodeString: myMethod omitTag: YES];
[self appendString: @"</methodName><params>"];
if (myParameters) {
NSEnumerator *enumerator = [myParameters objectEnumerator];
id parameter = nil;
while ((parameter = [enumerator nextObject])) {
[self appendString: @"<param>"];
[self encodeObject: parameter];
[self appendString: @"</param>"];
}
}
[self appendString: @"</params>"];
[self appendString: @"</methodCall>"];
[streamingCacheFile synchronizeFile];
}
- (NSInputStream *)encodedStream {
if (streamingCacheFilePath == nil) {
[self encodeForStreaming];
}
return [NSInputStream inputStreamWithFileAtPath:streamingCacheFilePath];
}
- (NSNumber *)encodedLength {
if (streamingCacheFilePath == nil) {
[self encodeForStreaming];
}
NSError *error = nil;
NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:streamingCacheFilePath error:&error];
if (error) {
return nil;
}
return [attributes objectForKey:NSFileSize];
}
#pragma mark -
- (void)setMethod: (NSString *)method withParameters: (NSArray *)parameters {
if (myMethod) {
[myMethod release];
}
if (!method) {
myMethod = nil;
} else {
myMethod = [method retain];
}
if (myParameters) {
[myParameters release];
}
if (!parameters) {
myParameters = nil;
} else {
myParameters = [parameters retain];
}
}
#pragma mark -
- (NSString *)method {
return myMethod;
}
- (NSArray *)parameters {
return myParameters;
}
#pragma mark -
- (void)dealloc {
[myMethod release];
[myParameters release];
if (streamingCacheFile != nil) {
[streamingCacheFile closeFile];
[streamingCacheFile release];
[[NSFileManager defaultManager] removeItemAtPath:streamingCacheFilePath error:nil];
}
[streamingCacheFilePath release];
[super dealloc];
}
@end
#pragma mark -
@implementation XMLRPCEncoder (XMLRPCEncoderPrivate)
- (void)valueTag: (NSString *)tag value: (NSString *)value {
[self appendFormat: @"<value><%@>%@</%@></value>", tag, value, tag];
}
#pragma mark -
- (NSString *)replaceTarget: (NSString *)target withValue: (NSString *)value inString: (NSString *)string {
return [[string componentsSeparatedByString: target] componentsJoinedByString: value];
}
#pragma mark -
- (void)encodeObject: (id)object {
if (!object) {
return;
}
if ([object isKindOfClass: [NSArray class]]) {
[self encodeArray: object];
} else if ([object isKindOfClass: [NSDictionary class]]) {
[self encodeDictionary: object];
} else if (((CFBooleanRef)object == kCFBooleanTrue) || ((CFBooleanRef)object == kCFBooleanFalse)) {
[self encodeBoolean: (CFBooleanRef)object];
} else if ([object isKindOfClass: [NSNumber class]]) {
[self encodeNumber: object];
} else if ([object isKindOfClass: [NSString class]]) {
[self encodeString: object omitTag: NO];
} else if ([object isKindOfClass: [NSDate class]]) {
[self encodeDate: object];
} else if ([object isKindOfClass: [NSData class]]) {
[self encodeData: object];
} else if ([object isKindOfClass: [NSInputStream class]]) {
[self encodeInputStream: object];
} else if ([object isKindOfClass: [NSFileHandle class]]) {
[self encodeFileHandle: object];
} else {
[self encodeString: object omitTag: NO];
}
}
#pragma mark -
- (void)encodeArray: (NSArray *)array {
NSEnumerator *enumerator = [array objectEnumerator];
[self appendString: @"<value><array><data>"];
id object = nil;
while (object = [enumerator nextObject]) {
[self encodeObject: object];
}
[self appendString: @"</data></array></value>"];
}
- (void)encodeDictionary: (NSDictionary *)dictionary {
NSEnumerator *enumerator = [dictionary keyEnumerator];
[self appendString: @"<value><struct>"];
NSString *key = nil;
while (key = [enumerator nextObject]) {
[self appendString: @"<member>"];
[self appendString: @"<name>"];
[self encodeString: key omitTag: YES];
[self appendString: @"</name>"];
[self encodeObject: [dictionary objectForKey: key]];
[self appendString: @"</member>"];
}
[self appendString: @"</struct></value>"];
}
#pragma mark -
- (void)encodeBoolean: (CFBooleanRef)boolean {
if (boolean == kCFBooleanTrue) {
[self valueTag: @"boolean" value: @"1"];
} else {
[self valueTag: @"boolean" value: @"0"];
}
}
- (void)encodeNumber: (NSNumber *)number {
NSString *numberType = [NSString stringWithCString: [number objCType] encoding: NSUTF8StringEncoding];
if ([numberType isEqualToString: @"d"]) {
[self valueTag: @"double" value: [number stringValue]];
} else {
[self valueTag: @"i4" value: [number stringValue]];
}
}
- (void)encodeString: (NSString *)string omitTag: (BOOL)omitTag {
if (omitTag)
[self appendString: [string escapedString]];
else
[self valueTag: @"string" value: [string escapedString]];
}
- (void)encodeDate: (NSDate *)date {
unsigned components = kCFCalendarUnitYear | kCFCalendarUnitMonth | kCFCalendarUnitDay | kCFCalendarUnitHour | kCFCalendarUnitMinute | kCFCalendarUnitSecond;
NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components: components fromDate: date];
NSString *buffer = [NSString stringWithFormat: @"%.4d%.2d%.2dT%.2d:%.2d:%.2d", [dateComponents year], [dateComponents month], [dateComponents day], [dateComponents hour], [dateComponents minute], [dateComponents second], nil];
[self valueTag: @"dateTime.iso8601" value: buffer];
}
- (void)encodeData: (NSData *)data {
[self valueTag: @"base64" value: [data base64EncodedString]];
}
- (void)encodeInputStream: (NSInputStream *)stream {
[self appendString:@"<value><base64>"];
[stream open];
while ([stream hasBytesAvailable]) {
uint8_t buf[CHUNK_SIZE];
unsigned int len = 0;
len = [stream read:buf maxLength:CHUNK_SIZE];
if (len) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSData *chunk = [NSData dataWithBytes:buf length:len];
NSString *encodedChunk = [chunk base64EncodedString];
[self appendString:encodedChunk];
[pool release];
}
}
[stream close];
[self appendString:@"</base64></value>"];
}
- (void)encodeFileHandle: (NSFileHandle *)handle {
[self appendString:@"<value><base64>"];
NSData *chunk = [handle readDataOfLength:CHUNK_SIZE];
while ([chunk length] > 0) {
NSString *encodedChunk = [chunk base64EncodedString];
[self appendString:encodedChunk];
chunk = [handle readDataOfLength:CHUNK_SIZE];
}
[self appendString:@"</base64></value>"];
}
#pragma mark -
- (void)appendString:(NSString *)aString {
[streamingCacheFile writeData:[aString dataUsingEncoding:NSUTF8StringEncoding]];
}
- (void)appendFormat:(NSString *)format, ... {
va_list ap;
va_start(ap, format);
NSString *message = [[[NSString alloc] initWithFormat:format arguments:ap] autorelease];
[self appendString:message];
}
#pragma mark -
- (void)openStreamingCache {
if (streamingCacheFile != nil)
return;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *directory = [paths objectAtIndex:0];
NSString *guid = [[NSProcessInfo processInfo] globallyUniqueString];
streamingCacheFilePath = [[directory stringByAppendingPathComponent:guid] retain];
[fileManager createFileAtPath:streamingCacheFilePath contents:nil attributes:nil];
streamingCacheFile = [[NSFileHandle fileHandleForWritingAtPath:streamingCacheFilePath] retain];
}
@end