]> err.no Git - sope/blob - sope-xml/DOM/DOMSaxBuilder.m
lF fixes
[sope] / sope-xml / DOM / DOMSaxBuilder.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 "DOMSaxBuilder.h"
23 #include "DOMSaxHandler.h"
24 #include "DOMAttribute.h"
25 #include "DOMDocument.h"
26 #include "common.h"
27 #include <SaxObjC/SaxObjC.h>
28
29 @implementation DOMSaxBuilder
30
31 - (id)initWithXMLReader:(id)_saxParser {
32   if (_saxParser == nil) {
33     [self release];
34     return nil;
35   }
36   
37   ASSIGN(self->parser, _saxParser);
38   
39   self->sax = [[DOMSaxHandler alloc] init];
40   
41   [self->parser setContentHandler:self->sax];
42   [self->parser setDTDHandler:self->sax];
43   [self->parser setErrorHandler:self->sax];
44   
45   [self->parser setProperty:@"http://xml.org/sax/properties/declaration-handler"
46                 to:self->sax];
47   [self->parser setProperty:@"http://xml.org/sax/properties/lexical-handler"
48                 to:self->sax];
49
50   return self;
51 }
52 - (id)initWithXMLReaderForMimeType:(id)_mimeType {
53   id reader;
54
55   reader = [[SaxXMLReaderFactory standardXMLReaderFactory]
56                                  createXMLReaderForMimeType:_mimeType];
57   if (reader == nil) {
58     NSLog(@"%s: could not find a SAX driver bundle for type '%@' !\n",
59           __PRETTY_FUNCTION__, _mimeType);
60     [self release];
61     return nil;
62   }
63   
64   return [self initWithXMLReader:reader];
65 }
66
67 - (id)init {
68   id reader;
69
70   reader = [[SaxXMLReaderFactory standardXMLReaderFactory] createXMLReader];
71
72   if (reader == nil) {
73     NSLog(@"%s: could not load a SAX driver bundle !\n",
74           __PRETTY_FUNCTION__);
75     [self release];
76     return nil;
77   }
78   
79   return [self initWithXMLReader:reader];
80 }
81 - (void)dealloc {
82   [self->parser release];
83   [self->sax    release];
84   [super dealloc];
85 }
86
87 /* DOM building */
88
89 - (id<NSObject,DOMDocument>)_docAfterParsing {
90   id<NSObject,DOMDocument> doc;
91
92   doc = [[self->sax document] retain];
93
94   [(id)doc addErrors:[self->sax errors]];
95   [(id)doc addWarnings:[self->sax warnings]];
96   
97   [self->sax clear];
98   return [doc autorelease];
99 }
100
101 - (id)buildFromData:(NSData *)_data {
102   NSAutoreleasePool *pool;
103   id doc;
104   
105   if (_data == nil) {
106     NSLog(@"missing data ..");
107     return nil;
108   }
109   NSAssert(self->sax,    @"missing sax handler ..");
110   NSAssert(self->parser, @"missing XML parser ..");
111   
112   pool = [[NSAutoreleasePool alloc] init];
113   {
114     [self->parser parseFromSource:_data];
115     doc = [[self _docAfterParsing] retain];
116   }
117   [pool release];
118
119 #if DEBUG
120   NSAssert(self->sax,    @"missing sax handler ..");
121   NSAssert(self->parser, @"missing XML parser ..");
122 #endif
123   
124   return [doc autorelease];
125 }
126
127 - (id)buildFromContentsOfFile:(NSString *)_path {
128   NSAutoreleasePool *pool;
129   id doc;
130   
131   if ([_path length] == 0)
132     return nil;
133   
134   pool = [[NSAutoreleasePool alloc] init];
135   {
136     NSDate *date;
137     NSTimeInterval duration;
138
139     date = [NSDate date];
140     _path = [@"file://" stringByAppendingString:_path];
141     
142     [self->parser parseFromSystemId:_path];
143     doc = [[self _docAfterParsing] retain];
144     
145     duration = [[NSDate date] timeIntervalSinceDate:date];
146   }
147   [pool release];
148   
149   return [doc autorelease];
150 }
151
152 - (id<NSObject,DOMDocument>)buildFromSource:(id)_source
153   systemId:(NSString *)_sysId
154 {
155   NSAutoreleasePool *pool;
156   id doc;
157   
158   if (_source == nil)
159     return nil;
160   
161   pool = [[NSAutoreleasePool alloc] init];
162   {
163     NSDate *date;
164     NSTimeInterval duration;
165
166     date = [NSDate date];
167     
168     [self->parser parseFromSource:_source systemId:_sysId];
169     doc = [[self _docAfterParsing] retain];
170     
171     duration = [[NSDate date] timeIntervalSinceDate:date];
172   }
173   [pool release];
174   
175   return [doc autorelease];
176 }
177 - (id<NSObject,DOMDocument>)buildFromSource:(id)_source {
178   return [self buildFromSource:_source systemId:nil];
179 }
180
181 - (id<NSObject,DOMDocument>)buildFromSystemId:(NSString *)_sysId {
182   NSAutoreleasePool *pool;
183   id doc;
184   
185   if ([_sysId length] == 0)
186     return nil;
187   
188   pool = [[NSAutoreleasePool alloc] init];
189   {
190     NSDate *date;
191     NSTimeInterval duration;
192
193     date = [NSDate date];
194     
195     [self->parser parseFromSystemId:_sysId];
196     doc = [[self _docAfterParsing] retain];
197     
198     duration = [[NSDate date] timeIntervalSinceDate:date];
199   }
200   [pool release];
201   
202   return [doc autorelease];
203 }
204
205 @end /* DOMSaxBuilder */