2 Copyright (C) 2000-2004 SKYRIX Software AG
4 This file is part of OpenGroupware.org.
6 OGo 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 OGo 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 OGo; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 #include <SaxObjC/SaxObjC.h>
24 #import <Foundation/Foundation.h>
29 saxxml -XMLReader libxmlHTMLSAXDriver test.html
32 @interface MySAXHandler : SaxDefaultHandler
42 int main(int argc, char **argv, char **env) {
43 id<NSObject,SaxXMLReader> parser;
47 NSAutoreleasePool *pool;
50 pool = [[NSAutoreleasePool alloc] init];
51 #if LIB_FOUNDATION_LIBRARY
52 [NSProcessInfo initializeWithArguments:argv count:argc environment:env];
55 parser = [[SaxXMLReaderFactory standardXMLReaderFactory] createXMLReader];
56 cwd = [[NSFileManager defaultManager] currentDirectoryPath];
59 fprintf(stderr, "could not load a SAX driver bundle !\n");
63 sax = [[MySAXHandler alloc] init];
64 [parser setContentHandler:sax];
65 [parser setDTDHandler:sax];
66 [parser setErrorHandler:sax];
68 [parser setProperty:@"http://xml.org/sax/properties/declaration-handler"
71 [parser setProperty:@"http://xml.org/sax/properties/lexical-handler"
77 paths = [[[NSProcessInfo processInfo] arguments] objectEnumerator];
79 while ((path = [paths nextObject])) {
80 NSAutoreleasePool *pool;
82 if ([path hasPrefix:@"-"]) { /* consume defaults */
87 pool = [[NSAutoreleasePool alloc] init];
89 if (![path isAbsolutePath])
90 path = [cwd stringByAppendingPathComponent:path];
92 path = [@"file://" stringByAppendingString:path];
95 [parser parseFromSystemId:path];
114 @implementation MySAXHandler
119 for (i = 0; i < (self->indent * 4); i++)
123 @end /* MySAXHandler */
125 @implementation MySAXHandler(Documents)
128 [self->locator release];
132 - (void)setDocumentLocator:(id<NSObject,SaxLocator>)_loc {
133 [self->locator autorelease];
134 self->locator = [_loc retain];
137 - (void)startDocument {
138 puts("start document ..");
141 - (void)endDocument {
143 puts("end document.");
146 - (void)startPrefixMapping:(NSString *)_prefix uri:(NSString *)_uri {
148 printf("ns-map: %s=%s\n", [_prefix cString], [_uri cString]);
150 - (void)endPrefixMapping:(NSString *)_prefix {
152 printf("ns-unmap: %s\n", [_prefix cString]);
155 - (void)startElement:(NSString *)_localName
156 namespace:(NSString *)_ns
157 rawName:(NSString *)_rawName
158 attributes:(id<SaxAttributes>)_attrs
162 printf("<%s", [_localName cString]);
164 if ([_ns length] > 0)
165 printf(" (ns=%s)", [_ns cString]);
167 for (i = 0, c = [_attrs count]; i < c; i++) {
171 [[_attrs nameAtIndex:i] cString],
172 [[_attrs valueAtIndex:i] cString]);
174 if (![_ns isEqualToString:[_attrs uriAtIndex:i]])
175 printf("(ns=%s)", [[_attrs uriAtIndex:i] cString]);
177 type = [_attrs typeAtIndex:i];
178 if (![type isEqualToString:@"CDATA"] && (type != nil))
179 printf("[%s]", [type cString]);
184 - (void)endElement:(NSString *)_localName
185 namespace:(NSString *)_ns
186 rawName:(NSString *)_rawName
190 printf("</%s>\n", [_localName cString]);
193 - (void)characters:(unichar *)_chars length:(int)_len {
204 for (i = 0; i < (unsigned)_len; i++) {
205 if (_chars[i] > 255) {
206 NSLog(@"detected large char: o%04o d%03i h%04X",
207 _chars[i], _chars[i], _chars[i]);
211 str = [NSString stringWithCharacters:_chars length:_len];
214 tmp = [str componentsSeparatedByString:@"\n"];
215 str = [tmp componentsJoinedByString:@"\\n"];
216 tmp = [str componentsSeparatedByString:@"\r"];
217 str = [tmp componentsJoinedByString:@"\\r"];
220 printf("\"%s\"\n", [str cString]);
222 - (void)ignorableWhitespace:(unichar *)_chars length:(int)_len {
226 data = [NSString stringWithCharacters:_chars length:_len];
227 tmp = [data componentsSeparatedByString:@"\n"];
228 data = [tmp componentsJoinedByString:@"\\n"];
229 tmp = [data componentsSeparatedByString:@"\r"];
230 data = [tmp componentsJoinedByString:@"\\r"];
233 printf("whitespace: \"%s\"\n", [data cString]);
236 - (void)processingInstruction:(NSString *)_pi data:(NSString *)_data {
238 printf("PI: '%s' '%s'\n", [_pi cString], [_data cString]);
242 - (xmlEntityPtr)getEntity:(NSString *)_name {
243 NSLog(@"get entity %@", _name);
246 - (xmlEntityPtr)getParameterEntity:(NSString *)_name {
247 NSLog(@"get para entity %@", _name);
252 @end /* MySAXHandler(Documents) */
254 @implementation MySAXHandler(EntityResolver)
256 - (id)resolveEntityWithPublicId:(NSString *)_pubId
257 systemId:(NSString *)_sysId
260 printf("shall resolve entity with '%s' '%s'",
261 [_pubId cString], [_sysId cString]);
265 @end /* MySAXHandler(EntityResolver) */
267 @implementation MySAXHandler(Errors)
269 - (void)warning:(SaxParseException *)_exception {
270 NSLog(@"warning(%@:%i): %@",
271 [[_exception userInfo] objectForKey:@"publicId"],
272 [[[_exception userInfo] objectForKey:@"line"] intValue],
273 [_exception reason]);
276 - (void)error:(SaxParseException *)_exception {
277 NSLog(@"error(%@:%i): %@",
278 [[_exception userInfo] objectForKey:@"publicId"],
279 [[[_exception userInfo] objectForKey:@"line"] intValue],
280 [_exception reason]);
283 - (void)fatalError:(SaxParseException *)_exception {
284 NSLog(@"fatal error(%@:%i): %@",
285 [[_exception userInfo] objectForKey:@"publicId"],
286 [[[_exception userInfo] objectForKey:@"line"] intValue],
287 [_exception reason]);
291 @end /* MySAXHandler(Errors) */
293 @implementation MySAXHandler(DTD)
295 - (void)notationDeclaration:(NSString *)_name
296 publicId:(NSString *)_pubId
297 systemId:(NSString *)_sysId
299 NSLog(@"decl: notation %@ pub=%@ sys=%@", _name, _pubId, _sysId);
302 - (void)unparsedEntityDeclaration:(NSString *)_name
303 publicId:(NSString *)_pubId
304 systemId:(NSString *)_sysId
305 notationName:(NSString *)_notName
307 NSLog(@"decl: unparsed entity %@ pub=%@ sys=%@ not=%@",
308 _name, _pubId, _sysId, _notName);
311 @end /* MySAXHandler(DTD) */
313 @implementation MySAXHandler(Decl)
315 - (void)attributeDeclaration:(NSString *)_attributeName
316 elementName:(NSString *)_elementName
317 type:(NSString *)_type
318 defaultType:(NSString *)_defType
319 defaultValue:(NSString *)_defValue
321 NSLog(@"decl: attr %@[%@] type '%@' default '%@'[%@]",
322 _attributeName, _elementName, _type, _defValue, _defType);
325 - (void)elementDeclaration:(NSString *)_name contentModel:(NSString *)_model {
326 NSLog(@"decl: element %@ model %@", _name, _model);
329 - (void)externalEntityDeclaration:(NSString *)_name
330 publicId:(NSString *)_pub
331 systemId:(NSString *)_sys
333 NSLog(@"decl: e-entity %@ pub %@ sys %@", _name, _pub, _sys);
336 - (void)internalEntityDeclaration:(NSString *)_name value:(NSString *)_value {
337 NSLog(@"decl: i-entity %@ value %@", _name, _value);
340 @end /* MySAXHandler(Decl) */