File: DOMSaxBuilder.m

package info (click to toggle)
sope 5.12.6-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 16,592 kB
  • sloc: objc: 169,229; sh: 3,823; ansic: 3,409; javascript: 446; python: 318; makefile: 185
file content (185 lines) | stat: -rw-r--r-- 4,459 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
  Copyright (C) 2000-2005 SKYRIX Software AG

  This file is part of SOPE.

  SOPE is free software; you can redistribute it and/or modify it under
  the terms of the GNU Lesser General Public License as published by the
  Free Software Foundation; either version 2, or (at your option) any
  later version.

  SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
  WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
  License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with SOPE; see the file COPYING.  If not, write to the
  Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  02111-1307, USA.
*/

#include "DOMSaxBuilder.h"
#include "DOMSaxHandler.h"
#include "DOMAttribute.h"
#include "DOMDocument.h"
#include "common.h"
#include <SaxObjC/SaxObjC.h>

@implementation DOMSaxBuilder

- (id)initWithXMLReader:(id)_saxParser {
  if (_saxParser == nil) {
    [self release];
    return nil;
  }
  
  ASSIGN(self->parser, _saxParser);
  
  self->sax = [[DOMSaxHandler alloc] init];
  
  [self->parser setContentHandler:self->sax];
  [self->parser setDTDHandler:self->sax];
  [self->parser setErrorHandler:self->sax];
  
  [self->parser setProperty:@"http://xml.org/sax/properties/declaration-handler"
                to:self->sax];
  [self->parser setProperty:@"http://xml.org/sax/properties/lexical-handler"
                to:self->sax];

  return self;
}
- (id)initWithXMLReaderForMimeType:(id)_mimeType {
  id reader;

  reader = [[SaxXMLReaderFactory standardXMLReaderFactory]
                                 createXMLReaderForMimeType:_mimeType];
  if (reader == nil) {
    NSLog(@"%s: could not find a SAX driver bundle for type '%@' !\n",
          __PRETTY_FUNCTION__, _mimeType);
    [self release];
    return nil;
  }
  
  return [self initWithXMLReader:reader];
}

- (id)init {
  id reader;

  reader = [[SaxXMLReaderFactory standardXMLReaderFactory] createXMLReader];

  if (reader == nil) {
    NSLog(@"%s: could not load a SAX driver bundle !\n",
          __PRETTY_FUNCTION__);
    [self release];
    return nil;
  }
  
  return [self initWithXMLReader:reader];
}
- (void)dealloc {
  [self->parser release];
  [self->sax    release];
  [super dealloc];
}

/* DOM building */

- (id<NSObject,DOMDocument>)_docAfterParsing {
  id<NSObject,DOMDocument> doc;

  doc = [[self->sax document] retain];

  [(id)doc addErrors:[self->sax errors]];
  [(id)doc addWarnings:[self->sax warnings]];
  
  [self->sax clear];
  return [doc autorelease];
}

- (id)buildFromData:(NSData *)_data {
  NSAutoreleasePool *pool;
  id doc;
  
  if (_data == nil) {
    NSLog(@"missing data ..");
    return nil;
  }
  NSAssert(self->sax,    @"missing sax handler ..");
  NSAssert(self->parser, @"missing XML parser ..");
  
  pool = [[NSAutoreleasePool alloc] init];
  {
    [self->parser parseFromSource:_data];
    doc = [[self _docAfterParsing] retain];
  }
  [pool release];

#if DEBUG
  NSAssert(self->sax,    @"missing sax handler ..");
  NSAssert(self->parser, @"missing XML parser ..");
#endif
  
  return [doc autorelease];
}

- (id)buildFromContentsOfFile:(NSString *)_path {
  NSAutoreleasePool *pool;
  id doc;
  
  if ([_path length] == 0)
    return nil;
  
  pool = [[NSAutoreleasePool alloc] init];
  {
    _path = [@"file://" stringByAppendingString:_path];
    
    [self->parser parseFromSystemId:_path];
    doc = [[self _docAfterParsing] retain];
  }
  [pool release];
  
  return [doc autorelease];
}

- (id<NSObject,DOMDocument>)buildFromSource:(id)_source
  systemId:(NSString *)_sysId
{
  NSAutoreleasePool *pool;
  id doc;
  
  if (_source == nil)
    return nil;
  
  pool = [[NSAutoreleasePool alloc] init];
  {
    [self->parser parseFromSource:_source systemId:_sysId];
    doc = [[self _docAfterParsing] retain];
  }
  [pool release];
  
  return [doc autorelease];
}
- (id<NSObject,DOMDocument>)buildFromSource:(id)_source {
  return [self buildFromSource:_source systemId:nil];
}

- (id<NSObject,DOMDocument>)buildFromSystemId:(NSString *)_sysId {
  NSAutoreleasePool *pool;
  id doc;
  
  if ([_sysId length] == 0)
    return nil;
  
  pool = [[NSAutoreleasePool alloc] init];
  {
    [self->parser parseFromSystemId:_sysId];
    doc = [[self _docAfterParsing] retain];
  }
  [pool release];
  
  return [doc autorelease];
}

@end /* DOMSaxBuilder */