]> err.no Git - sope/blob - sope-xml/XmlRpc/NSException+XmlRpcCoding.m
fixed a Tiger warning
[sope] / sope-xml / XmlRpc / NSException+XmlRpcCoding.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 "XmlRpcMethodResponse.h"
23 #include "common.h"
24 #include "XmlRpcCoder.h"
25
26 @implementation NSException(XmlRpcCoding)
27
28 - (NSString *)xmlRpcType {
29   return @"struct";
30 }
31
32 - (NSNumber *)xmlRpcFaultCode {
33   NSDictionary *ui;
34   id code;
35
36   ui = [self userInfo];
37   
38   if ((code = [ui objectForKey:@"XmlRpcFaultCode"]))
39     /* code is set */;
40   else if ((code = [ui objectForKey:@"faultCode"]))
41     /* code is set */;
42   else
43     code = [self name];
44   
45   return [NSNumber numberWithInt:[code intValue]];
46 }
47
48 - (void)encodeWithXmlRpcCoder:(XmlRpcEncoder *)_coder {
49   int      code;
50   NSString *str, *n, *r;
51   NSDictionary *ui;
52   
53   code = [[self xmlRpcFaultCode] intValue];
54   
55   n = [self name];
56   r = [self reason];
57   
58   if ([n length] == 0)
59     str = r;
60   else if ([r length] == 0)
61     str = n;
62   else
63     str = [NSString stringWithFormat:@"%@: %@", n, r];
64   
65   if ((ui = [self userInfo]))
66     str = [NSString stringWithFormat:@"%@ %@", str, ui];
67   
68   [_coder encodeInt:code   forKey:@"faultCode"];
69   [_coder encodeString:str forKey:@"faultString"];
70 }
71
72 + (NSString *)exceptionNameForXmlRpcFaultCode:(int)_code {
73   return [NSString stringWithFormat:@"XmlRpcFault<%i>", _code];
74 }
75 - (NSString *)exceptionNameForXmlRpcFaultCode:(int)_code {
76   return [[self class] exceptionNameForXmlRpcFaultCode:_code];
77 }
78
79 + (id)decodeObjectWithXmlRpcCoder:(XmlRpcDecoder *)_coder {
80   int          code;
81   NSString     *r;
82   NSDictionary *ui;
83   
84   code = [_coder decodeIntForKey:@"faultCode"];
85   r    = [_coder decodeStringForKey:@"faultString"];
86   ui   = [NSDictionary dictionaryWithObjectsAndKeys:
87                          [NSNumber numberWithInt:code], @"faultCode",
88                          r, @"faultString",
89                          nil];
90   
91   return [self exceptionWithName:
92                  [self exceptionNameForXmlRpcFaultCode:code]
93                reason:r
94                userInfo:ui];
95 }
96
97 @end /* NSException(XmlRpcCoding) */