2 Copyright (C) 2000-2005 SKYRIX Software AG
4 This file is part of SOPE.
6 SOPE is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with SOPE; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 #include <XmlRpc/XmlRpcMethodResponse.h>
23 #include <XmlRpc/XmlRpcCoder.h>
24 #include <XmlRpc/NSObject+XmlRpc.h>
27 @interface NSObject(Misc)
28 - (id)initWithString:(NSString *)_s;
31 @interface NSString(XmlRpcParsing)
32 - (id)initWithXmlRpcType:(NSString *)_type
33 characters:(unichar *)_chars
37 @interface NSDate(XmlRpcParsing)
38 - (id)initWithXmlRpcType:(NSString *)_type
39 characters:(unichar *)_chars
43 @interface NSNumber(XmlRpcParsing)
44 - (id)initWithXmlRpcType:(NSString *)_type
45 characters:(unichar *)_chars
49 @interface NSData(XmlRpcParsing)
50 - (id)initWithXmlRpcType:(NSString *)_type
51 characters:(unichar *)_chars
55 @interface NSData(UsedNGExtensions)
56 - (NSData *)dataByDecodingBase64;
59 @implementation NSObject(XmlRpcParsing)
61 - (id)initWithXmlRpcCoder:(XmlRpcDecoder *)_coder {
62 NSClassDescription *cd;
64 if ((cd = [self classDescription])) {
68 if ((self = [self init])) {
69 e = [[cd attributeKeys] objectEnumerator];
70 while ((k = [e nextObject]))
71 [self takeValue:[_coder decodeObjectForKey:k] forKey:k];
73 e = [[cd toOneRelationshipKeys] objectEnumerator];
74 while ((k = [e nextObject]))
75 [self takeValue:[_coder decodeObjectForKey:k] forKey:k];
77 e = [[cd toManyRelationshipKeys] objectEnumerator];
78 while ((k = [e nextObject]))
79 [self takeValue:[_coder decodeArrayForKey:k] forKey:k];
82 else if ([self respondsToSelector:@selector(initWithString:)]) {
83 self = [(id)self initWithString:[_coder decodeString]];
86 [NSException raise:@"XmlRpcCodingException"
88 @"in initWithXmlRpcCoder: cannot decode class '%@'",
89 NSStringFromClass([self class])];
95 + (id)decodeObjectWithXmlRpcCoder:(XmlRpcDecoder *)_decoder {
96 return [[[self alloc] initWithXmlRpcCoder:_decoder] autorelease];
99 - (NSString *)xmlRpcType {
103 - (void)encodeWithXmlRpcCoder:(XmlRpcEncoder *)_coder {
104 NSClassDescription *cd;
106 if ((cd = [self classDescription])) {
110 e = [[cd attributeKeys] objectEnumerator];
111 while ((k = [e nextObject]))
112 [_coder encodeObject:[self valueForKey:k] forKey:k];
114 e = [[cd toOneRelationshipKeys] objectEnumerator];
115 while ((k = [e nextObject]))
116 [_coder encodeObject:[self valueForKey:k] forKey:k];
118 e = [[cd toManyRelationshipKeys] objectEnumerator];
119 while ((k = [e nextObject]))
120 [_coder encodeArray:[self valueForKey:k] forKey:k];
122 else if ([self respondsToSelector:@selector(initWithString:)]) {
123 [_coder encodeString:[self description]];
126 [NSException raise:@"XmlRpcCodingException"
128 @"in encodeWithXmlRpcCoder: "
129 @"cannot encode class '%@', object=%@B",
130 NSStringFromClass([self class]), self];
134 + (id)objectWithXmlRpcType:(NSString *)_type
135 characters:(unichar *)_chars length:(int)_len
137 static NSDictionary *typeToClass = nil;
138 Class ObjClass = Nil;
141 if (typeToClass == nil) {
142 typeToClass = [[NSDictionary alloc] initWithObjectsAndKeys:
143 [NSNumber class], @"i4",
144 [NSNumber class], @"int",
145 [NSNumber class], @"double",
146 [NSNumber class], @"boolean",
147 [NSString class], @"string",
148 [NSString class], @"value",
149 [NSData class], @"base64",
150 [NSCalendarDate class],
155 /* determine basetype class */
157 if ((ObjClass = [typeToClass objectForKey:_type]) == Nil) {
158 NSLog(@"WARNING(%s): unknown XML-RPC type '%@', using String ...",
159 __PRETTY_FUNCTION__, _type);
160 ObjClass = [NSString class];
163 /* construct object */
166 [[ObjClass alloc] initWithXmlRpcType:_type characters:_chars length:_len];
167 return [obj autorelease];
170 - (id)initWithXmlRpcType:(NSString *)_type
171 characters:(unichar *)_chars length:(int)_len
173 if ([self respondsToSelector:@selector(initWithString:)]) {
176 s = [[NSString alloc] initWithCharacters:_chars length:_len];
177 self = [self initWithString:s];
182 /* don't know how to init with given type ... */
187 @end /* NSObject(XmlRpc) */
189 @implementation NSData(XmlRpcParsing)
191 /* NSData represents the xml-rpc base type 'base64' */
193 - (id)initWithXmlRpcType:(NSString *)_type
194 characters:(unichar *)_chars length:(int)_len
198 [self release]; self = nil;
200 v = [NSString stringWithCharacters:_chars length:_len];
201 self = [v dataUsingEncoding:NSUTF8StringEncoding];
203 if ([_type isEqualToString:@"base64"])
204 self = [self dataByDecodingBase64];
209 @end /* NSData(XmlRpcParsing) */
211 @implementation NSDate(XmlRpcParsing)
213 /* NSDate represents the xml-rpc type dateTime.iso8601: */
214 - (id)initWithXmlRpcType:(NSString *)_type
215 characters:(unichar *)_chars length:(int)_len
217 /* eg 19980717T14:08:55 */
224 unsigned char buf[8];
225 int year, month, day, hour, min, sec;
227 buf[0] = _chars[0]; buf[1] = _chars[1];
228 buf[2] = _chars[2]; buf[3] = _chars[3];
230 year = atoi((char *)buf);
231 buf[0] = _chars[4]; buf[1] = _chars[5]; buf[2] = '\0';
232 month = atoi((char *)buf);
233 buf[0] = _chars[6]; buf[1] = _chars[7]; buf[2] = '\0';
234 day = atoi((char *)buf);
236 buf[0] = _chars[9]; buf[1] = _chars[10]; buf[2] = '\0';
237 hour = atoi((char *)buf);
238 buf[0] = _chars[12]; buf[1] = _chars[13]; buf[2] = '\0';
239 min = atoi((char *)buf);
240 buf[0] = _chars[15]; buf[1] = _chars[16]; buf[2] = '\0';
241 sec = atoi((char *)buf);
246 s = [[NSString alloc] initWithCharacters:_chars length:_len];
247 NSLog(@"WARNING: got a date value '%@' with year >2033, "
248 @"which cannot be represented, silently using 2033 ...",
253 else if (year < 1900) {
256 s = [[NSString alloc] initWithCharacters:_chars length:_len];
257 NSLog(@"WARNING: got a date value '%@' with year < 1900, "
258 @"which cannot be represented, silently using 1900 ...",
264 if (![self isKindOfClass:[NSCalendarDate class]]) {
266 self = [NSCalendarDate alloc];
269 return [(NSCalendarDate *)self
270 initWithYear:year month:month day:day
271 hour:hour minute:min second:sec
272 timeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
276 @end /* NSDate(XmlRpcParsing) */
278 @implementation NSNumber(XmlRpcParsing)
280 /* NSNumber represents the xml-rpc base types: 'int', 'double', 'boolean': */
282 - (id)initWithXmlRpcType:(NSString *)_type
283 characters:(unichar *)_chars length:(int)_len
285 if ([_type isEqualToString:@"boolean"]) {
289 ? ((_chars[0] == '1') ? YES : NO)
291 return [self initWithBool:v];
297 v = [NSString stringWithCharacters:_chars length:_len];
299 if ([_type isEqualToString:@"i4"] || [_type isEqualToString:@"int"])
301 else if ([_type isEqualToString:@"double"])
304 isInt = ([v rangeOfString:@"."].length == 0) ? YES : NO;
307 ? [self initWithInt:[v intValue]]
308 : [self initWithDouble:[v doubleValue]];
312 @end /* NSNumber(XmlRpcParsing */
315 @implementation NSString(XmlRpcParsing)
317 - (id)initWithXmlRpcType:(NSString *)_type
318 characters:(unichar *)_chars length:(int)_len
320 /* this is *never* called, since NSString+alloc returns a NSTemporaryString*/
321 return [self initWithCharacters:_chars length:_len];
324 @end /* NSString(XmlRpcParsing) */