]> err.no Git - sope/blob - sope-xml/XmlRpc/XmlRpcMethodCall.m
bumbed versions to 4.5
[sope] / sope-xml / XmlRpc / XmlRpcMethodCall.m
1 /*
2   Copyright (C) 2000-2004 SKYRIX Software AG
3
4   This file is part of OpenGroupware.org.
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
22 #include <XmlRpc/XmlRpcMethodCall.h>
23 #include <XmlRpc/XmlRpcCoder.h>
24 #include "common.h"
25
26 @interface XmlRpcMethodCall(PrivateMethodes)
27 - (NSString *)_encodeXmlRpcMethodCall;
28 @end
29
30 @implementation XmlRpcMethodCall
31
32 - (id)initWithXmlRpcString:(NSString *)_string {
33   XmlRpcDecoder    *coder;
34   XmlRpcMethodCall *baseCall;
35   
36   if ([_string length] == 0) {
37     [self release];
38     return nil;
39   }
40   
41   coder = [[XmlRpcDecoder alloc] initForReadingWithString:_string];
42   baseCall = [coder decodeMethodCall];
43   [coder release];
44
45   if (baseCall == nil) {
46     [self release];
47     return nil;
48   }
49   
50   self = [self initWithMethodName:[baseCall methodName]
51                parameters:[baseCall parameters]];
52   
53   return self;
54 }
55 - (id)initWithXmlRpcData:(NSData *)_xmlRpcData {
56   XmlRpcDecoder    *coder;
57   XmlRpcMethodCall *baseCall;
58   
59   if ([_xmlRpcData length] == 0) {
60     [self release];
61     return nil;
62   }
63   
64   coder = [[XmlRpcDecoder alloc] initForReadingWithData:_xmlRpcData];
65   baseCall = [coder decodeMethodCall];
66   [coder release];
67   
68   if (baseCall == nil) {
69     [self release];
70     return nil;
71   }
72   
73   self = [self initWithMethodName:[baseCall methodName]
74                parameters:[baseCall parameters]];
75   
76   return self;
77 }
78
79 - (id)initWithMethodName:(NSString *)_name parameters:(NSArray *)_params {
80   if ((self = [super init])) {
81     self->methodName = [_name copy];
82     [self setParameters:_params];
83   }
84   return self;
85 }
86
87 - (void)dealloc {
88   [self->methodName release];
89   [self->parameters release];
90   [super dealloc];
91 }
92
93 /* accessors */
94
95 - (void)setMethodName:(NSString *)_name {
96   [self->methodName autorelease];
97   self->methodName = [_name copy];
98 }
99 - (NSString *)methodName {
100   return self->methodName;
101 }
102
103 - (void)setParameters:(NSArray *)_params {
104   if (self->parameters != _params) {
105     unsigned i, cc;
106     id *objects;
107     
108     [self->parameters autorelease];
109     
110     /* 
111        shallow copy parameters, it is implemented here 'by-hand', since 
112        skyrix-xml is not dependend on EOControl
113     */
114     cc = [_params count];
115     objects = calloc(cc + 1, sizeof(id));
116     
117     for (i = 0; i < cc; i++)
118       objects[i] = [_params objectAtIndex:i];
119     self->parameters = [[NSArray alloc] initWithObjects:objects count:cc];
120     if (objects) free(objects);
121   }
122 }
123 - (NSArray *)parameters {
124   return self->parameters;
125 }
126
127 - (NSString *)xmlRpcString {
128   return [self _encodeXmlRpcMethodCall];
129 }
130
131 /* description */
132
133 - (NSString *)description {
134   NSMutableString *s;
135   
136   s = [NSMutableString stringWithFormat:@"<0x%08X[%@]: ",
137                          self, NSStringFromClass([self class])];
138   [s appendFormat:@"method=%@", [self methodName]];
139   [s appendFormat:@" #paras=%d", [self->parameters count]];
140   [s appendString:@">"];
141   return s;
142 }
143
144 @end /* XmlRpcMethodCall */
145
146
147 @implementation XmlRpcMethodCall(PrivateMethodes)
148
149 - (NSString *)_encodeXmlRpcMethodCall {
150   NSMutableString *str;
151   XmlRpcEncoder   *coder;
152
153 #if DEBUG
154   NSAssert1(self->methodName, @"%s, methodName is not allowed to be nil!",
155             __PRETTY_FUNCTION__);
156 #endif
157   
158   str   = [NSMutableString stringWithCapacity:512];
159   coder = [[XmlRpcEncoder alloc] initForWritingWithMutableString:str];
160   [coder encodeMethodCall:self];
161
162   [coder release]; coder = nil;
163   
164   return str;
165 }
166
167 @end /* XmlRpcMethodCall(PrivateMethodes) */