2 Copyright (C) 2000-2003 SKYRIX Software AG
4 This file is part of OGo
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
24 A small demonstration program to show how to write a SAX handler
25 using SaxObjC. It goes over a RSS channel file and collects the
26 item information in a dictionary.
28 This example is almost the same like rss2plist1.m, but uses the
29 SaxMethodCallHandler which calls a method for each tag, so you
30 don't need to do manual tagname checks.
31 Note that we only process known content, all other tags are ignored.
33 As you will see it's quite a bit of work dealing with SAX ;-)
36 #import <Foundation/Foundation.h>
37 #include <SaxObjC/SaxObjC.h>
38 #include <SaxObjC/SaxMethodCallHandler.h>
40 /* ******************** the SAX handler ****************** */
42 @interface RSSSaxHandler : SaxMethodCallHandler
44 NSMutableArray *entries;
45 NSMutableDictionary *entry;
48 NSString *value; /* the (PCDATA) content of a tag */
51 - (NSArray *)rssEntries;
55 @implementation RSSSaxHandler
58 if ((self = [super init])) {
59 self->entries = [[NSMutableArray alloc] initWithCapacity:16];
60 self->entry = [[NSMutableDictionary alloc] initWithCapacity:8];
62 /* those are required for mapping the names */
63 [self registerNamespace:@"http://purl.org/rss/1.0/" withKey:@"rss"];
64 [self registerNamespace:@"http://www.w3.org/1999/02/22-rdf-syntax-ns#"
66 [self registerNamespace:@"http://purl.org/rss/1.0/modules/slash/"
68 [self registerNamespace:@"http://purl.org/rss/1.0/modules/syndication/"
74 [self->entry release];
75 [self->entries release];
79 /* accessing results */
81 - (NSArray *)rssEntries {
82 return [[self->entries copy] autorelease];
87 - (void)startDocument {
88 /* ensure consistent state */
89 [self->entries removeAllObjects];
94 - (void)start_rssitem:(id<SaxAttributes>)_attributes {
95 [self->entry removeAllObjects];
98 [self->entries addObject:[[self->entry copy] autorelease]];
101 /* the subtags of item ... */
103 - (void)start_rsstitle:(id<SaxAttributes>)_attributes {
104 [self->value release]; self->value = nil;
106 - (void)end_rsstitle {
108 [self->entry setObject:self->value forKey:@"title"];
111 - (void)start_rsslink:(id<SaxAttributes>)_attributes {
112 [self->value release]; self->value = nil;
114 - (void)end_rsslink {
116 [self->entry setObject:self->value forKey:@"link"];
119 - (void)start_rssdescription:(id<SaxAttributes>)_attributes {
120 [self->value release]; self->value = nil;
122 - (void)end_rssdescription {
124 [self->entry setObject:self->value forKey:@"info"];
127 - (void)characters:(unichar *)_chars length:(int)_len {
131 Note: The characters callback is allowed to be called multiple times
132 by the parser (makes writing parsers easier, but complicates the
135 s = [[NSString alloc] initWithCharacters:_chars length:_len];
137 self->value = [[self->value stringByAppendingString:s] copy];
144 @end /* RSSSaxHandler */
146 /* ******************** C main section ******************** */
148 int main(int argc, char **argv, char **env) {
149 NSAutoreleasePool *pool;
150 #if LIB_FOUNDATION_LIBRARY
151 [NSProcessInfo initializeWithArguments:argv count:argc environment:env];
154 pool = [[NSAutoreleasePool alloc] init];
156 if ([[[NSProcessInfo processInfo] arguments] count] < 2) {
157 fprintf(stderr, "usage: %s <rssfile>\n",
158 [[[[NSProcessInfo processInfo] arguments] lastObject] cString]);
162 /* the interesting section */
166 id<SaxXMLReader> parser;
169 /* step a, get a parser for XML */
170 parser = [[SaxXMLReaderFactory standardXMLReaderFactory]
171 createXMLReaderForMimeType:@"text/xml"];
173 /* step b, create a SAX handler and attach it to the parser */
174 sax = [[[RSSSaxHandler alloc] init] autorelease];
175 [parser setContentHandler:sax];
176 [parser setErrorHandler:sax];
178 /* step c, parse :-) */
180 args = [[[NSProcessInfo processInfo] arguments] objectEnumerator];
181 [args nextObject]; /* skip tool name */
183 while ((arg = [args nextObject])) {
186 /* the parser takes URLs, NSData's, NSString's */
187 arg = [[[NSURL alloc] initFileURLWithPath:arg] autorelease];
189 /* let the parser parse (it will report SAX events to the handler) */
190 [parser parseFromSource:arg];
192 /* now query the handler for the result */
193 entries = [sax rssEntries];
195 /* TODO: use NSPropertyListSerialization on OSX */
196 printf("%s\n", [[entries description] cString]);