]> err.no Git - sope/blob - sope-xml/XmlRpc/XmlRpcRequestDecoder.m
fixed a header file
[sope] / sope-xml / XmlRpc / XmlRpcRequestDecoder.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/XmlRpcCoder.h>
24 #include <XmlRpc/XmlRpcSaxHandler.h>
25 #include <XmlRpc/XmlRpcMethodCall.h>
26 #include <XmlRpc/XmlRpcValue.h>
27 #include <SaxObjC/SaxXMLReaderFactory.h>
28 #include "common.h"
29
30 @implementation XmlRpcRequestDecoder
31
32 static id   saxRequestHandler = nil;
33 static id   requestParser     = nil;
34 static BOOL doDebug           = NO;
35
36 - (void)_ensureSaxAndParser {
37   if (saxRequestHandler == nil) {
38     static Class clazz = Nil;
39     
40     if (clazz == Nil)
41       clazz = NSClassFromString(@"XmlRpcSaxRequestHandler");
42     
43     if ((saxRequestHandler = [[clazz alloc] init]) == nil) {
44       NSLog(@"%s: did not find sax handler ...", __PRETTY_FUNCTION__);
45       return;
46     }
47   }
48   
49   if (requestParser != nil) return;
50   
51   requestParser =
52     [[SaxXMLReaderFactory standardXMLReaderFactory] createXMLReader];
53   
54   if (requestParser == nil) {
55     NSLog(@"%s: did not find an XML parser ...", __PRETTY_FUNCTION__);
56     return;
57   }
58   
59   [requestParser setContentHandler:saxRequestHandler];
60   [requestParser setDTDHandler:saxRequestHandler];
61   [requestParser setErrorHandler:saxRequestHandler];
62
63   [requestParser retain];
64 }
65
66 - (XmlRpcMethodCall *)decodeRootObject {
67   XmlRpcMethodCall *methodCall;
68   NSEnumerator     *paramEnum;
69   NSMutableArray   *params;
70   XmlRpcValue      *param;
71   
72   if (doDebug) NSLog(@"%s: begin", __PRETTY_FUNCTION__);
73   [self _ensureSaxAndParser];
74   
75   [saxRequestHandler reset];
76   
77   [requestParser parseFromSource:self->string systemId:nil];
78   
79   methodCall = [saxRequestHandler methodCall];
80   
81   // the methodCall's parameters is an array of XmlRpcValues!!!
82   
83   paramEnum = [[methodCall parameters] objectEnumerator];
84   params    = [[NSMutableArray alloc] initWithCapacity:
85                                       [[methodCall parameters] count]];
86
87   while ((param = [paramEnum nextObject])) {
88     Class objClass = Nil;
89     id    obj;
90     
91     [self->value autorelease];
92     self->value = [param retain];
93     
94     if ((objClass = NSClassFromString([param className])) != Nil) {
95       if ((obj = [objClass decodeObjectWithXmlRpcCoder:self])) {
96         [params addObject:obj];
97       }
98       else {
99         NSLog(@"%s: Warning: try to add 'nil' object to params (class='%@')",
100               __PRETTY_FUNCTION__,
101               [param className]);
102       }
103     }
104   }
105   [methodCall setParameters:params];
106   
107   [params release];
108
109   if (doDebug) NSLog(@"%s: done: %@", __PRETTY_FUNCTION__, methodCall);
110   return methodCall;
111 }
112
113 @end /* XmlRpcRequestDecoder */