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