]> err.no Git - sope/blob - sope-xml/XmlRpc/XmlRpcResponseDecoder.m
fixed Xcode build
[sope] / sope-xml / XmlRpc / XmlRpcResponseDecoder.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 "XmlRpcCoder.h"
24 #include "XmlRpcSaxHandler.h"
25 #include <SaxObjC/SaxXMLReaderFactory.h>
26 #include "common.h"
27
28 @implementation XmlRpcResponseDecoder
29
30 static id saxResponseHandler = nil;
31 static id responseParser     = nil;
32
33 - (void)_ensureSaxAndParser {
34   if (saxResponseHandler == nil) {
35     if ((saxResponseHandler = [[XmlRpcSaxResponseHandler alloc] init])==nil) {
36       NSLog(@"%s: did not find sax handler ...", __PRETTY_FUNCTION__);
37       return;
38     }
39   }
40
41   if (responseParser != nil) return;
42   
43   responseParser =
44     [[SaxXMLReaderFactory standardXMLReaderFactory]
45                           createXMLReaderForMimeType:@"text/xml"];
46   
47   if (responseParser == nil) {
48     NSLog(@"%s: did not find an XML parser ...", __PRETTY_FUNCTION__);
49     return;
50   }
51   
52   responseParser = [responseParser retain];
53   [responseParser setContentHandler:saxResponseHandler];
54   [responseParser setDTDHandler:saxResponseHandler];
55   [responseParser setErrorHandler:saxResponseHandler];
56 }
57
58 - (id)decodeRootObject {
59   static Class ExceptionClass = Nil;
60   id     result;
61   
62   [self _ensureSaxAndParser];
63
64   if (ExceptionClass == Nil)
65     ExceptionClass = [NSException class];
66   
67   [saxResponseHandler reset];
68
69   [responseParser parseFromSource:self->string systemId:nil];
70   
71   result = [saxResponseHandler result];
72
73   if ([result isKindOfClass:ExceptionClass]) {
74     return result;
75   }
76   else { // => XmlRpcValue
77     XmlRpcValue *val     = result;
78     Class       objClass = Nil;
79     id          obj;
80
81     [self->value autorelease];
82     self->value = [val retain];
83
84     if ((objClass = NSClassFromString([val className])) != Nil) {
85       obj = [objClass decodeObjectWithXmlRpcCoder:self];
86       return obj;
87     }
88   }
89   
90   return nil;
91 }
92
93 @end /* XmlRpcResponseDecoder */