]> err.no Git - sope/blob - sope-xml/XmlRpc/NSObject+XmlRpc.m
fixed a Tiger warning
[sope] / sope-xml / XmlRpc / NSObject+XmlRpc.m
1 /*
2   Copyright (C) 2000-2005 SKYRIX Software AG
3
4   This file is part of SOPE.
5
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
9   later version.
10
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.
15
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
19   02111-1307, USA.
20 */
21
22 #include <XmlRpc/XmlRpcMethodResponse.h>
23 #include <XmlRpc/XmlRpcCoder.h>
24 #include <XmlRpc/NSObject+XmlRpc.h>
25 #include "common.h"
26
27 @interface NSObject(Misc)
28 - (id)initWithString:(NSString *)_s;
29 @end
30
31 @interface NSString(XmlRpcParsing)
32 - (id)initWithXmlRpcType:(NSString *)_type
33   characters:(unichar *)_chars
34   length:(int)_len;
35 @end
36
37 @interface NSDate(XmlRpcParsing)
38 - (id)initWithXmlRpcType:(NSString *)_type
39   characters:(unichar *)_chars
40   length:(int)_len;
41 @end
42
43 @interface NSNumber(XmlRpcParsing)
44 - (id)initWithXmlRpcType:(NSString *)_type
45   characters:(unichar *)_chars
46   length:(int)_len;
47 @end
48
49 @interface NSData(XmlRpcParsing)
50 - (id)initWithXmlRpcType:(NSString *)_type
51   characters:(unichar *)_chars
52   length:(int)_len;
53 @end
54
55 @interface NSData(UsedNGExtensions)
56 - (NSData *)dataByDecodingBase64;
57 @end
58
59 @implementation NSObject(XmlRpcParsing)
60
61 - (id)initWithXmlRpcCoder:(XmlRpcDecoder *)_coder {
62   NSClassDescription *cd;
63   
64   if ((cd = [self classDescription])) {
65     NSEnumerator *e;
66     NSString     *k;
67
68     if ((self = [self init])) {
69       e = [[cd attributeKeys] objectEnumerator];
70       while ((k = [e nextObject]))
71         [self takeValue:[_coder decodeObjectForKey:k] forKey:k];
72     
73       e = [[cd toOneRelationshipKeys] objectEnumerator];
74       while ((k = [e nextObject]))
75         [self takeValue:[_coder decodeObjectForKey:k] forKey:k];
76     
77       e = [[cd toManyRelationshipKeys] objectEnumerator];
78       while ((k = [e nextObject]))
79         [self takeValue:[_coder decodeArrayForKey:k] forKey:k];
80     }
81   }
82   else if ([self respondsToSelector:@selector(initWithString:)]) {
83     self = [(id)self initWithString:[_coder decodeString]];
84   }
85   else {
86     [NSException raise:@"XmlRpcCodingException"
87                  format:
88                  @"in initWithXmlRpcCoder: cannot decode class '%@'",
89                  NSStringFromClass([self class])];
90     [self release];
91     return nil;
92   }
93   return self;
94 }
95 + (id)decodeObjectWithXmlRpcCoder:(XmlRpcDecoder *)_decoder {
96   return [[[self alloc] initWithXmlRpcCoder:_decoder] autorelease];
97 }
98
99 - (NSString *)xmlRpcType {
100   return @"struct";
101 }
102
103 - (void)encodeWithXmlRpcCoder:(XmlRpcEncoder *)_coder {
104   NSClassDescription *cd;
105   
106   if ((cd = [self classDescription])) {
107     NSEnumerator *e;
108     NSString     *k;
109     
110     e = [[cd attributeKeys] objectEnumerator];
111     while ((k = [e nextObject]))
112       [_coder encodeObject:[self valueForKey:k] forKey:k];
113     
114     e = [[cd toOneRelationshipKeys] objectEnumerator];
115     while ((k = [e nextObject]))
116       [_coder encodeObject:[self valueForKey:k] forKey:k];
117     
118     e = [[cd toManyRelationshipKeys] objectEnumerator];
119     while ((k = [e nextObject]))
120       [_coder encodeArray:[self valueForKey:k] forKey:k];
121   }
122   else if ([self respondsToSelector:@selector(initWithString:)]) {
123     [_coder encodeString:[self description]];
124   }
125   else {
126     [NSException raise:@"XmlRpcCodingException"
127                  format:
128                    @"in encodeWithXmlRpcCoder: "
129                    @"cannot encode class '%@', object=%@B",
130                  NSStringFromClass([self class]), self];
131   }
132 }
133
134 + (id)objectWithXmlRpcType:(NSString *)_type
135   characters:(unichar *)_chars length:(int)_len
136 {
137   static NSDictionary *typeToClass = nil;
138   Class ObjClass = Nil;
139   id obj;
140   
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],
151                                           @"dateTime.iso8601",
152                                           nil];
153   }
154   
155   /* determine basetype class */
156   
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];
161   }
162   
163   /* construct object */
164   
165   obj =
166     [[ObjClass alloc] initWithXmlRpcType:_type characters:_chars length:_len];
167   return [obj autorelease];
168 }
169
170 - (id)initWithXmlRpcType:(NSString *)_type
171   characters:(unichar *)_chars length:(int)_len
172 {
173   if ([self respondsToSelector:@selector(initWithString:)]) {
174     NSString *s;
175
176     s = [[NSString alloc] initWithCharacters:_chars length:_len];
177     self = [self initWithString:s];
178     [s release];
179     return self;
180   }
181   
182   /* don't know how to init with given type ... */
183   [self release];
184   return nil;
185 }
186
187 @end /* NSObject(XmlRpc) */
188
189 @implementation NSData(XmlRpcParsing)
190
191 /* NSData represents the xml-rpc base type 'base64' */
192
193 - (id)initWithXmlRpcType:(NSString *)_type
194   characters:(unichar *)_chars length:(int)_len
195 {
196   NSString *v;
197
198   [self release]; self = nil;
199   
200   v    = [NSString stringWithCharacters:_chars length:_len];
201   self = [v dataUsingEncoding:NSUTF8StringEncoding];
202   
203   if ([_type isEqualToString:@"base64"])
204     self = [self dataByDecodingBase64];
205   
206   return [self copy];
207 }
208
209 @end /* NSData(XmlRpcParsing) */
210
211 @implementation NSDate(XmlRpcParsing)
212
213 /* NSDate represents the xml-rpc type dateTime.iso8601: */
214 - (id)initWithXmlRpcType:(NSString *)_type
215   characters:(unichar *)_chars length:(int)_len
216 {
217   /* eg 19980717T14:08:55 */
218   if (_len < 17) {
219     [self release];
220     return nil;
221   }
222   
223   {
224     unsigned char buf[8];
225     int year, month, day, hour, min, sec;
226         
227     buf[0] = _chars[0]; buf[1] = _chars[1];
228     buf[2] = _chars[2]; buf[3] = _chars[3];
229     buf[4] = '\0';
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);
235
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);
242     
243     if (year > 2033) {
244       NSString *s;
245
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  ...",
249             s);
250       [s release];
251       year = 2033;
252     }
253     else if (year < 1900) {
254       NSString *s;
255
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  ...",
259             s);
260       [s release];
261       year = 1900;
262     }
263     
264     if (![self isKindOfClass:[NSCalendarDate class]]) {
265       [self release];
266       self = [NSCalendarDate alloc];
267     }
268     
269     return [(NSCalendarDate *)self
270                               initWithYear:year month:month day:day
271                               hour:hour minute:min second:sec
272                               timeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
273   }
274 }
275
276 @end /* NSDate(XmlRpcParsing) */
277
278 @implementation NSNumber(XmlRpcParsing)
279
280 /* NSNumber represents the xml-rpc base types: 'int', 'double', 'boolean': */
281
282 - (id)initWithXmlRpcType:(NSString *)_type
283   characters:(unichar *)_chars length:(int)_len
284 {
285   if ([_type isEqualToString:@"boolean"]) {
286     BOOL v;
287       
288     v = (_len > 0)
289       ? ((_chars[0] == '1') ? YES : NO)
290       : NO;
291     return [self initWithBool:v];
292   }
293   else {
294     NSString *v;
295     BOOL isInt = NO;
296     
297     v = [NSString stringWithCharacters:_chars length:_len];
298     
299     if ([_type isEqualToString:@"i4"] || [_type isEqualToString:@"int"])
300       isInt = YES;
301     else if ([_type isEqualToString:@"double"])
302       isInt = NO;
303     else
304       isInt = ([v rangeOfString:@"."].length == 0) ? YES : NO;
305     
306     return isInt
307       ? [self initWithInt:[v intValue]]
308       : [self initWithDouble:[v doubleValue]];
309   }
310 }
311
312 @end /* NSNumber(XmlRpcParsing */
313
314
315 @implementation NSString(XmlRpcParsing)
316
317 - (id)initWithXmlRpcType:(NSString *)_type
318   characters:(unichar *)_chars length:(int)_len
319 {
320   /* this is *never* called, since NSString+alloc returns a NSTemporaryString*/
321   return [self initWithCharacters:_chars length:_len];
322 }
323
324 @end /* NSString(XmlRpcParsing) */