2 Copyright (C) 2000-2005 SKYRIX Software AG
4 This file is part of SOPE.
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
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.
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
22 #include "DOMSaxBuilder.h"
23 #include "DOMSaxHandler.h"
24 #include "DOMAttribute.h"
25 #include "DOMDocument.h"
27 #include <SaxObjC/SaxObjC.h>
29 @implementation DOMSaxBuilder
31 - (id)initWithXMLReader:(id)_saxParser {
32 if (_saxParser == nil) {
37 ASSIGN(self->parser, _saxParser);
39 self->sax = [[DOMSaxHandler alloc] init];
41 [self->parser setContentHandler:self->sax];
42 [self->parser setDTDHandler:self->sax];
43 [self->parser setErrorHandler:self->sax];
45 [self->parser setProperty:@"http://xml.org/sax/properties/declaration-handler"
47 [self->parser setProperty:@"http://xml.org/sax/properties/lexical-handler"
52 - (id)initWithXMLReaderForMimeType:(id)_mimeType {
55 reader = [[SaxXMLReaderFactory standardXMLReaderFactory]
56 createXMLReaderForMimeType:_mimeType];
58 NSLog(@"%s: could not find a SAX driver bundle for type '%@' !\n",
59 __PRETTY_FUNCTION__, _mimeType);
64 return [self initWithXMLReader:reader];
70 reader = [[SaxXMLReaderFactory standardXMLReaderFactory] createXMLReader];
73 NSLog(@"%s: could not load a SAX driver bundle !\n",
79 return [self initWithXMLReader:reader];
82 [self->parser release];
89 - (id<NSObject,DOMDocument>)_docAfterParsing {
90 id<NSObject,DOMDocument> doc;
92 doc = [[self->sax document] retain];
94 [(id)doc addErrors:[self->sax errors]];
95 [(id)doc addWarnings:[self->sax warnings]];
98 return [doc autorelease];
101 - (id)buildFromData:(NSData *)_data {
102 NSAutoreleasePool *pool;
106 NSLog(@"missing data ..");
109 NSAssert(self->sax, @"missing sax handler ..");
110 NSAssert(self->parser, @"missing XML parser ..");
112 pool = [[NSAutoreleasePool alloc] init];
114 [self->parser parseFromSource:_data];
115 doc = [[self _docAfterParsing] retain];
120 NSAssert(self->sax, @"missing sax handler ..");
121 NSAssert(self->parser, @"missing XML parser ..");
124 return [doc autorelease];
127 - (id)buildFromContentsOfFile:(NSString *)_path {
128 NSAutoreleasePool *pool;
131 if ([_path length] == 0)
134 pool = [[NSAutoreleasePool alloc] init];
137 NSTimeInterval duration;
139 date = [NSDate date];
140 _path = [@"file://" stringByAppendingString:_path];
142 [self->parser parseFromSystemId:_path];
143 doc = [[self _docAfterParsing] retain];
145 duration = [[NSDate date] timeIntervalSinceDate:date];
149 return [doc autorelease];
152 - (id<NSObject,DOMDocument>)buildFromSource:(id)_source
153 systemId:(NSString *)_sysId
155 NSAutoreleasePool *pool;
161 pool = [[NSAutoreleasePool alloc] init];
164 NSTimeInterval duration;
166 date = [NSDate date];
168 [self->parser parseFromSource:_source systemId:_sysId];
169 doc = [[self _docAfterParsing] retain];
171 duration = [[NSDate date] timeIntervalSinceDate:date];
175 return [doc autorelease];
177 - (id<NSObject,DOMDocument>)buildFromSource:(id)_source {
178 return [self buildFromSource:_source systemId:nil];
181 - (id<NSObject,DOMDocument>)buildFromSystemId:(NSString *)_sysId {
182 NSAutoreleasePool *pool;
185 if ([_sysId length] == 0)
188 pool = [[NSAutoreleasePool alloc] init];
191 NSTimeInterval duration;
193 date = [NSDate date];
195 [self->parser parseFromSystemId:_sysId];
196 doc = [[self _docAfterParsing] retain];
198 duration = [[NSDate date] timeIntervalSinceDate:date];
202 return [doc autorelease];
205 @end /* DOMSaxBuilder */