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