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