This repository has been archived by the owner on Apr 15, 2019. It is now read-only.
forked from eczarny/xmlrpc
-
Notifications
You must be signed in to change notification settings - Fork 29
/
XMLRPCDefaultEncoder.m
256 lines (183 loc) · 6.92 KB
/
XMLRPCDefaultEncoder.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
#import "XMLRPCDefaultEncoder.h"
#import "NSStringAdditions.h"
#import "NSData+Base64.h"
@interface XMLRPCDefaultEncoder (XMLRPCEncoderPrivate)
- (NSString *)valueTag: (NSString *)tag value: (NSString *)value;
#pragma mark -
- (NSString *)replaceTarget: (NSString *)target withValue: (NSString *)value inString: (NSString *)string;
#pragma mark -
- (NSString *)encodeObject: (id)object;
#pragma mark -
- (NSString *)encodeArray: (NSArray *)array;
- (NSString *)encodeDictionary: (NSDictionary *)dictionary;
#pragma mark -
- (NSString *)encodeBoolean: (CFBooleanRef)boolean;
- (NSString *)encodeNumber: (NSNumber *)number;
- (NSString *)encodeString: (NSString *)string omitTag: (BOOL)omitTag;
- (NSString *)encodeDate: (NSDate *)date;
- (NSString *)encodeData: (NSData *)data;
@end
#pragma mark -
@implementation XMLRPCDefaultEncoder
- (id)init {
if (self = [super init]) {
myMethod = [[NSString alloc] init];
myParameters = [[NSArray alloc] init];
}
return self;
}
#pragma mark -
- (NSString *)encode {
NSMutableString *buffer = [NSMutableString stringWithString: @"<?xml version=\"1.0\"?><methodCall>"];
[buffer appendFormat: @"<methodName>%@</methodName>", [self encodeString: myMethod omitTag: YES]];
[buffer appendString: @"<params>"];
if (myParameters) {
NSEnumerator *enumerator = [myParameters objectEnumerator];
id parameter = nil;
while ((parameter = [enumerator nextObject])) {
[buffer appendString: @"<param>"];
[buffer appendString: [self encodeObject: parameter]];
[buffer appendString: @"</param>"];
}
}
[buffer appendString: @"</params>"];
[buffer appendString: @"</methodCall>"];
return buffer;
}
#pragma mark -
- (void)setMethod: (NSString *)method withParameters: (NSArray *)parameters {
#if ! __has_feature(objc_arc)
if (myMethod) {
[myMethod release];
}
if (!method) {
myMethod = nil;
} else {
myMethod = [method retain];
}
if (myParameters) {
[myParameters release];
}
if (!parameters) {
myParameters = nil;
} else {
myParameters = [parameters retain];
}
#else
myMethod = method;
myParameters = parameters;
#endif
}
#pragma mark -
- (NSString *)method {
return myMethod;
}
- (NSArray *)parameters {
return myParameters;
}
#pragma mark -
- (void)dealloc {
#if ! __has_feature(objc_arc)
[myMethod release];
[myParameters release];
[super dealloc];
#endif
}
@end
#pragma mark -
@implementation XMLRPCDefaultEncoder (XMLRPCEncoderPrivate)
- (NSString *)valueTag: (NSString *)tag value: (NSString *)value {
return [NSString stringWithFormat: @"<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 -
- (NSString *)encodeObject: (id)object {
if (!object) {
return nil;
}
if ([object isKindOfClass: [NSArray class]]) {
return [self encodeArray: object];
} else if ([object isKindOfClass: [NSDictionary class]]) {
return [self encodeDictionary: object];
#if ! __has_feature(objc_arc)
} else if (((CFBooleanRef)object == kCFBooleanTrue) || ((CFBooleanRef)object == kCFBooleanFalse)) {
#else
} else if (((__bridge CFBooleanRef)object == kCFBooleanTrue) || ((__bridge CFBooleanRef)object == kCFBooleanFalse)) {
#endif
return [self encodeBoolean: (CFBooleanRef)object];
} else if ([object isKindOfClass: [NSNumber class]]) {
return [self encodeNumber: object];
} else if ([object isKindOfClass: [NSString class]]) {
return [self encodeString: object omitTag: NO];
} else if ([object isKindOfClass: [NSDate class]]) {
return [self encodeDate: object];
} else if ([object isKindOfClass: [NSData class]]) {
return [self encodeData: object];
} else {
return [self encodeString: object omitTag: NO];
}
}
#pragma mark -
- (NSString *)encodeArray: (NSArray *)array {
NSMutableString *buffer = [NSMutableString string];
NSEnumerator *enumerator = [array objectEnumerator];
[buffer appendString: @"<value><array><data>"];
id object = nil;
while (object = [enumerator nextObject]) {
[buffer appendString: [self encodeObject: object]];
}
[buffer appendString: @"</data></array></value>"];
return (NSString *)buffer;
}
- (NSString *)encodeDictionary: (NSDictionary *)dictionary {
NSMutableString * buffer = [NSMutableString string];
NSEnumerator *enumerator = [dictionary keyEnumerator];
[buffer appendString: @"<value><struct>"];
NSString *key = nil;
NSObject *val;
while (key = [enumerator nextObject]) {
[buffer appendString: @"<member>"];
[buffer appendFormat: @"<name>%@</name>", [self encodeString: key omitTag: YES]];
val = [dictionary objectForKey: key];
if (val != [NSNull null]) {
[buffer appendString: [self encodeObject: val]];
} else {
[buffer appendString:@"<value><nil/></value>"];
}
[buffer appendString: @"</member>"];
}
[buffer appendString: @"</struct></value>"];
return (NSString *)buffer;
}
#pragma mark -
- (NSString *)encodeBoolean: (CFBooleanRef)boolean {
if (boolean == kCFBooleanTrue) {
return [self valueTag: @"boolean" value: @"1"];
} else {
return [self valueTag: @"boolean" value: @"0"];
}
}
- (NSString *)encodeNumber: (NSNumber *)number {
NSString *numberType = [NSString stringWithCString: [number objCType] encoding: NSUTF8StringEncoding];
if ([numberType isEqualToString: @"d"]) {
return [self valueTag: @"double" value: [number stringValue]];
} else {
return [self valueTag: @"i4" value: [number stringValue]];
}
}
- (NSString *)encodeString: (NSString *)string omitTag: (BOOL)omitTag {
return omitTag ? [string escapedString] : [self valueTag: @"string" value: [string escapedString]];
}
- (NSString *)encodeDate: (NSDate *)date {
unsigned components = kCFCalendarUnitYear | kCFCalendarUnitMonth | kCFCalendarUnitDay | kCFCalendarUnitHour | kCFCalendarUnitMinute | kCFCalendarUnitSecond;
NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components: components fromDate: date];
NSString *buffer = [NSString stringWithFormat: @"%.4ld%.2ld%.2ldT%.2ld:%.2ld:%.2ld", (long)[dateComponents year], (long)[dateComponents month], (long)[dateComponents day], (long)[dateComponents hour], (long)[dateComponents minute], (long)[dateComponents second], nil];
return [self valueTag: @"dateTime.iso8601" value: buffer];
}
- (NSString *)encodeData: (NSData *)data {
return [self valueTag: @"base64" value: [data base64EncodedString]];
}
@end