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 <SaxObjC/SaxObjC.h>
23 #import <Foundation/Foundation.h>
28 saxxml -XMLReader libxmlHTMLSAXDriver test.html
31 @interface MySAXHandler : SaxDefaultHandler
41 int main(int argc, char **argv, char **env) {
42 id<NSObject,SaxXMLReader> parser;
46 NSAutoreleasePool *pool;
49 pool = [[NSAutoreleasePool alloc] init];
50 #if LIB_FOUNDATION_LIBRARY
51 [NSProcessInfo initializeWithArguments:argv count:argc environment:env];
54 parser = [[SaxXMLReaderFactory standardXMLReaderFactory] createXMLReader];
55 cwd = [[NSFileManager defaultManager] currentDirectoryPath];
58 fprintf(stderr, "could not load a SAX driver bundle!\n");
62 sax = [[MySAXHandler alloc] init];
63 [parser setContentHandler:sax];
64 [parser setDTDHandler:sax];
65 [parser setErrorHandler:sax];
67 [parser setProperty:@"http://xml.org/sax/properties/declaration-handler"
70 [parser setProperty:@"http://xml.org/sax/properties/lexical-handler"
76 paths = [[[NSProcessInfo processInfo] arguments] objectEnumerator];
78 while ((path = [paths nextObject])) {
79 NSAutoreleasePool *pool;
81 if ([path hasPrefix:@"-"]) { /* consume defaults */
86 pool = [[NSAutoreleasePool alloc] init];
88 if (![path isAbsolutePath])
89 path = [cwd stringByAppendingPathComponent:path];
91 path = [@"file://" stringByAppendingString:path];
94 [parser parseFromSystemId:path];
113 @implementation MySAXHandler
118 for (i = 0; i < (self->indent * 4); i++)
122 @end /* MySAXHandler */
124 @implementation MySAXHandler(Documents)
127 [self->locator release];
131 - (void)setDocumentLocator:(id<NSObject,SaxLocator>)_loc {
132 [self->locator autorelease];
133 self->locator = [_loc retain];
136 - (void)startDocument {
137 puts("start document ..");
140 - (void)endDocument {
142 puts("end document.");
145 - (void)startPrefixMapping:(NSString *)_prefix uri:(NSString *)_uri {
147 printf("ns-map: %s=%s\n", [_prefix cString], [_uri cString]);
149 - (void)endPrefixMapping:(NSString *)_prefix {
151 printf("ns-unmap: %s\n", [_prefix cString]);
154 - (void)startElement:(NSString *)_localName
155 namespace:(NSString *)_ns
156 rawName:(NSString *)_rawName
157 attributes:(id<SaxAttributes>)_attrs
161 printf("<%s", [_localName cString]);
163 if ([_ns length] > 0)
164 printf(" (ns=%s)", [_ns cString]);
166 for (i = 0, c = [_attrs count]; i < c; i++) {
170 [[_attrs nameAtIndex:i] cString],
171 [[_attrs valueAtIndex:i] cString]);
173 if (![_ns isEqualToString:[_attrs uriAtIndex:i]])
174 printf("(ns=%s)", [[_attrs uriAtIndex:i] cString]);
176 type = [_attrs typeAtIndex:i];
177 if (![type isEqualToString:@"CDATA"] && (type != nil))
178 printf("[%s]", [type cString]);
183 - (void)endElement:(NSString *)_localName
184 namespace:(NSString *)_ns
185 rawName:(NSString *)_rawName
189 printf("</%s>\n", [_localName cString]);
192 - (void)characters:(unichar *)_chars length:(int)_len {
203 for (i = 0; i < (unsigned)_len; i++) {
204 if (_chars[i] > 255) {
205 NSLog(@"detected large char: o%04o d%03i h%04X",
206 _chars[i], _chars[i], _chars[i]);
210 str = [NSString stringWithCharacters:_chars length:_len];
213 tmp = [str componentsSeparatedByString:@"\n"];
214 str = [tmp componentsJoinedByString:@"\\n"];
215 tmp = [str componentsSeparatedByString:@"\r"];
216 str = [tmp componentsJoinedByString:@"\\r"];
219 printf("\"%s\"\n", [str cString]);
221 - (void)ignorableWhitespace:(unichar *)_chars length:(int)_len {
225 data = [NSString stringWithCharacters:_chars length:_len];
226 tmp = [data componentsSeparatedByString:@"\n"];
227 data = [tmp componentsJoinedByString:@"\\n"];
228 tmp = [data componentsSeparatedByString:@"\r"];
229 data = [tmp componentsJoinedByString:@"\\r"];
232 printf("whitespace: \"%s\"\n", [data cString]);
235 - (void)processingInstruction:(NSString *)_pi data:(NSString *)_data {
237 printf("PI: '%s' '%s'\n", [_pi cString], [_data cString]);
241 - (xmlEntityPtr)getEntity:(NSString *)_name {
242 NSLog(@"get entity %@", _name);
245 - (xmlEntityPtr)getParameterEntity:(NSString *)_name {
246 NSLog(@"get para entity %@", _name);
251 @end /* MySAXHandler(Documents) */
253 @implementation MySAXHandler(EntityResolver)
255 - (id)resolveEntityWithPublicId:(NSString *)_pubId
256 systemId:(NSString *)_sysId
259 printf("shall resolve entity with '%s' '%s'",
260 [_pubId cString], [_sysId cString]);
264 @end /* MySAXHandler(EntityResolver) */
266 @implementation MySAXHandler(Errors)
268 - (void)warning:(SaxParseException *)_exception {
269 NSLog(@"warning(%@:%i): %@",
270 [[_exception userInfo] objectForKey:@"publicId"],
271 [[[_exception userInfo] objectForKey:@"line"] intValue],
272 [_exception reason]);
275 - (void)error:(SaxParseException *)_exception {
276 NSLog(@"error(%@:%i): %@",
277 [[_exception userInfo] objectForKey:@"publicId"],
278 [[[_exception userInfo] objectForKey:@"line"] intValue],
279 [_exception reason]);
282 - (void)fatalError:(SaxParseException *)_exception {
283 NSLog(@"fatal error(%@:%i): %@",
284 [[_exception userInfo] objectForKey:@"publicId"],
285 [[[_exception userInfo] objectForKey:@"line"] intValue],
286 [_exception reason]);
290 @end /* MySAXHandler(Errors) */
292 @implementation MySAXHandler(DTD)
294 - (void)notationDeclaration:(NSString *)_name
295 publicId:(NSString *)_pubId
296 systemId:(NSString *)_sysId
298 NSLog(@"decl: notation %@ pub=%@ sys=%@", _name, _pubId, _sysId);
301 - (void)unparsedEntityDeclaration:(NSString *)_name
302 publicId:(NSString *)_pubId
303 systemId:(NSString *)_sysId
304 notationName:(NSString *)_notName
306 NSLog(@"decl: unparsed entity %@ pub=%@ sys=%@ not=%@",
307 _name, _pubId, _sysId, _notName);
310 @end /* MySAXHandler(DTD) */
312 @implementation MySAXHandler(Decl)
314 - (void)attributeDeclaration:(NSString *)_attributeName
315 elementName:(NSString *)_elementName
316 type:(NSString *)_type
317 defaultType:(NSString *)_defType
318 defaultValue:(NSString *)_defValue
320 NSLog(@"decl: attr %@[%@] type '%@' default '%@'[%@]",
321 _attributeName, _elementName, _type, _defValue, _defType);
324 - (void)elementDeclaration:(NSString *)_name contentModel:(NSString *)_model {
325 NSLog(@"decl: element %@ model %@", _name, _model);
328 - (void)externalEntityDeclaration:(NSString *)_name
329 publicId:(NSString *)_pub
330 systemId:(NSString *)_sys
332 NSLog(@"decl: e-entity %@ pub %@ sys %@", _name, _pub, _sys);
335 - (void)internalEntityDeclaration:(NSString *)_name value:(NSString *)_value {
336 NSLog(@"decl: i-entity %@ value %@", _name, _value);
339 @end /* MySAXHandler(Decl) */