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 @interface SaxXMLReaderFactory(Pathes)
42 - (NSArray *)saxReaderSearchPathes;
45 static void usage(const char *n) {
47 "Usage: %s <file1> <file2> ...\n"
49 "Arguments (Defaults):\n"
50 " -XMLReader <classname> - select the SAX driver class\n"
51 " --dirs - just print the dirs containing drivers\n"
54 " %s /etc/X11/xkb/rules/xfree86.xml\n"
55 " %s -XMLReader STXSaxDriver structured-document.stx\n"
56 " %s -XMLReader VSiCalSaxDriver event.ics\n"
57 " %s -XMLReader VSvCardSaxDriver steve.vcf\n"
61 static void listSaxScanDirs(void) {
65 a = [[SaxXMLReaderFactory standardXMLReaderFactory] saxReaderSearchPathes];
66 for (i = 0, count = [a count]; i < count; i++)
67 printf("%s\n", [[a objectAtIndex:i] cString]);
70 fprintf(stderr, "found no search pathes!\n");
75 int main(int argc, char **argv, char **env) {
76 id<NSObject,SaxXMLReader> parser;
80 NSAutoreleasePool *pool;
84 pool = [[NSAutoreleasePool alloc] init];
85 #if LIB_FOUNDATION_LIBRARY
86 [NSProcessInfo initializeWithArguments:argv count:argc environment:env];
89 parser = [[SaxXMLReaderFactory standardXMLReaderFactory] createXMLReader];
90 cwd = [[NSFileManager defaultManager] currentDirectoryPath];
93 fprintf(stderr, "could not load a SAX driver bundle!\n");
97 sax = [[MySAXHandler alloc] init];
98 [parser setContentHandler:sax];
99 [parser setDTDHandler:sax];
100 [parser setErrorHandler:sax];
102 [parser setProperty:@"http://xml.org/sax/properties/declaration-handler"
105 [parser setProperty:@"http://xml.org/sax/properties/lexical-handler"
111 paths = [[[NSProcessInfo processInfo] arguments] objectEnumerator];
113 while ((path = [paths nextObject]) != nil) {
114 NSAutoreleasePool *pool;
116 if ([path isEqualToString:@"--help"]) {
121 if ([path isEqualToString:@"--dirs"]) {
126 if ([path hasPrefix:@"-"]) { /* consume defaults */
133 pool = [[NSAutoreleasePool alloc] init];
135 if (![path isAbsolutePath])
136 path = [cwd stringByAppendingPathComponent:path];
138 path = [@"file://" stringByAppendingString:path];
141 [parser parseFromSystemId:path];
165 @implementation MySAXHandler
170 for (i = 0; i < (self->indent * 4); i++)
174 @end /* MySAXHandler */
176 @implementation MySAXHandler(Documents)
179 [self->locator release];
183 - (void)setDocumentLocator:(id<NSObject,SaxLocator>)_loc {
184 [self->locator autorelease];
185 self->locator = [_loc retain];
188 - (void)startDocument {
189 puts("start document ..");
192 - (void)endDocument {
194 puts("end document.");
197 - (void)startPrefixMapping:(NSString *)_prefix uri:(NSString *)_uri {
199 printf("ns-map: %s=%s\n", [_prefix cString], [_uri cString]);
201 - (void)endPrefixMapping:(NSString *)_prefix {
203 printf("ns-unmap: %s\n", [_prefix cString]);
206 - (void)startElement:(NSString *)_localName
207 namespace:(NSString *)_ns
208 rawName:(NSString *)_rawName
209 attributes:(id<SaxAttributes>)_attrs
213 printf("<%s", [_localName cString]);
215 if ([_ns length] > 0)
216 printf(" (ns=%s)", [_ns cString]);
218 for (i = 0, c = [_attrs count]; i < c; i++) {
222 [[_attrs nameAtIndex:i] cString],
223 [[_attrs valueAtIndex:i] cString]);
225 if (![_ns isEqualToString:[_attrs uriAtIndex:i]])
226 printf("(ns=%s)", [[_attrs uriAtIndex:i] cString]);
228 type = [_attrs typeAtIndex:i];
229 if (![type isEqualToString:@"CDATA"] && (type != nil))
230 printf("[%s]", [type cString]);
235 - (void)endElement:(NSString *)_localName
236 namespace:(NSString *)_ns
237 rawName:(NSString *)_rawName
241 printf("</%s>\n", [_localName cString]);
244 - (void)characters:(unichar *)_chars length:(int)_len {
255 for (i = 0; i < (unsigned)_len; i++) {
256 if (_chars[i] > 255) {
257 NSLog(@"detected large char: o%04o d%03i h%04X",
258 _chars[i], _chars[i], _chars[i]);
262 str = [NSString stringWithCharacters:_chars length:_len];
265 tmp = [str componentsSeparatedByString:@"\n"];
266 str = [tmp componentsJoinedByString:@"\\n"];
267 tmp = [str componentsSeparatedByString:@"\r"];
268 str = [tmp componentsJoinedByString:@"\\r"];
271 printf("\"%s\"\n", [str cString]);
273 - (void)ignorableWhitespace:(unichar *)_chars length:(int)_len {
277 data = [NSString stringWithCharacters:_chars length:_len];
278 tmp = [data componentsSeparatedByString:@"\n"];
279 data = [tmp componentsJoinedByString:@"\\n"];
280 tmp = [data componentsSeparatedByString:@"\r"];
281 data = [tmp componentsJoinedByString:@"\\r"];
284 printf("whitespace: \"%s\"\n", [data cString]);
287 - (void)processingInstruction:(NSString *)_pi data:(NSString *)_data {
289 printf("PI: '%s' '%s'\n", [_pi cString], [_data cString]);
293 - (xmlEntityPtr)getEntity:(NSString *)_name {
294 NSLog(@"get entity %@", _name);
297 - (xmlEntityPtr)getParameterEntity:(NSString *)_name {
298 NSLog(@"get para entity %@", _name);
303 @end /* MySAXHandler(Documents) */
305 @implementation MySAXHandler(EntityResolver)
307 - (id)resolveEntityWithPublicId:(NSString *)_pubId
308 systemId:(NSString *)_sysId
311 printf("shall resolve entity with '%s' '%s'",
312 [_pubId cString], [_sysId cString]);
316 @end /* MySAXHandler(EntityResolver) */
318 @implementation MySAXHandler(Errors)
320 - (void)warning:(SaxParseException *)_exception {
321 NSLog(@"warning(%@:%i): %@",
322 [[_exception userInfo] objectForKey:@"publicId"],
323 [[[_exception userInfo] objectForKey:@"line"] intValue],
324 [_exception reason]);
327 - (void)error:(SaxParseException *)_exception {
328 NSLog(@"error(%@:%i): %@",
329 [[_exception userInfo] objectForKey:@"publicId"],
330 [[[_exception userInfo] objectForKey:@"line"] intValue],
331 [_exception reason]);
334 - (void)fatalError:(SaxParseException *)_exception {
335 NSLog(@"fatal error(%@:%i): %@",
336 [[_exception userInfo] objectForKey:@"publicId"],
337 [[[_exception userInfo] objectForKey:@"line"] intValue],
338 [_exception reason]);
342 @end /* MySAXHandler(Errors) */
344 @implementation MySAXHandler(DTD)
346 - (void)notationDeclaration:(NSString *)_name
347 publicId:(NSString *)_pubId
348 systemId:(NSString *)_sysId
350 NSLog(@"decl: notation %@ pub=%@ sys=%@", _name, _pubId, _sysId);
353 - (void)unparsedEntityDeclaration:(NSString *)_name
354 publicId:(NSString *)_pubId
355 systemId:(NSString *)_sysId
356 notationName:(NSString *)_notName
358 NSLog(@"decl: unparsed entity %@ pub=%@ sys=%@ not=%@",
359 _name, _pubId, _sysId, _notName);
362 @end /* MySAXHandler(DTD) */
364 @implementation MySAXHandler(Decl)
366 - (void)attributeDeclaration:(NSString *)_attributeName
367 elementName:(NSString *)_elementName
368 type:(NSString *)_type
369 defaultType:(NSString *)_defType
370 defaultValue:(NSString *)_defValue
372 NSLog(@"decl: attr %@[%@] type '%@' default '%@'[%@]",
373 _attributeName, _elementName, _type, _defValue, _defType);
376 - (void)elementDeclaration:(NSString *)_name contentModel:(NSString *)_model {
377 NSLog(@"decl: element %@ model %@", _name, _model);
380 - (void)externalEntityDeclaration:(NSString *)_name
381 publicId:(NSString *)_pub
382 systemId:(NSString *)_sys
384 NSLog(@"decl: e-entity %@ pub %@ sys %@", _name, _pub, _sys);
387 - (void)internalEntityDeclaration:(NSString *)_name value:(NSString *)_value {
388 NSLog(@"decl: i-entity %@ value %@", _name, _value);
391 @end /* MySAXHandler(Decl) */