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