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 A small demonstration program to show how to write a SAX handler
24 using SaxObjC. It goes over a RSS channel file and collects the
25 item information in a dictionary.
27 This example is almost the same like rss2plist1.m, but uses the
28 SaxMethodCallHandler which calls a method for each tag, so you
29 don't need to do manual tagname checks.
30 Note that we only process known content, all other tags are ignored.
32 As you will see it's quite a bit of work dealing with SAX ;-)
35 #import <Foundation/Foundation.h>
36 #include <SaxObjC/SaxObjC.h>
37 #include <SaxObjC/SaxMethodCallHandler.h>
39 /* ******************** the SAX handler ****************** */
41 @interface RSSSaxHandler : SaxMethodCallHandler
43 NSMutableArray *entries;
44 NSMutableDictionary *entry;
47 NSString *value; /* the (PCDATA) content of a tag */
50 - (NSArray *)rssEntries;
54 @implementation RSSSaxHandler
57 if ((self = [super init])) {
58 self->entries = [[NSMutableArray alloc] initWithCapacity:16];
59 self->entry = [[NSMutableDictionary alloc] initWithCapacity:8];
61 /* those are required for mapping the names */
62 [self registerNamespace:@"http://purl.org/rss/1.0/" withKey:@"rss"];
63 [self registerNamespace:@"http://www.w3.org/1999/02/22-rdf-syntax-ns#"
65 [self registerNamespace:@"http://purl.org/rss/1.0/modules/slash/"
67 [self registerNamespace:@"http://purl.org/rss/1.0/modules/syndication/"
73 [self->entry release];
74 [self->entries release];
78 /* accessing results */
80 - (NSArray *)rssEntries {
81 return [[self->entries copy] autorelease];
86 - (void)startDocument {
87 /* ensure consistent state */
88 [self->entries removeAllObjects];
93 - (void)start_rssitem:(id<SaxAttributes>)_attributes {
94 [self->entry removeAllObjects];
97 [self->entries addObject:[[self->entry copy] autorelease]];
100 /* the subtags of item ... */
102 - (void)start_rsstitle:(id<SaxAttributes>)_attributes {
103 [self->value release]; self->value = nil;
105 - (void)end_rsstitle {
107 [self->entry setObject:self->value forKey:@"title"];
110 - (void)start_rsslink:(id<SaxAttributes>)_attributes {
111 [self->value release]; self->value = nil;
113 - (void)end_rsslink {
115 [self->entry setObject:self->value forKey:@"link"];
118 - (void)start_rssdescription:(id<SaxAttributes>)_attributes {
119 [self->value release]; self->value = nil;
121 - (void)end_rssdescription {
123 [self->entry setObject:self->value forKey:@"info"];
126 - (void)characters:(unichar *)_chars length:(int)_len {
130 Note: The characters callback is allowed to be called multiple times
131 by the parser (makes writing parsers easier, but complicates the
134 s = [[NSString alloc] initWithCharacters:_chars length:_len];
136 self->value = [[self->value stringByAppendingString:s] copy];
143 @end /* RSSSaxHandler */
145 /* ******************** C main section ******************** */
147 int main(int argc, char **argv, char **env) {
148 NSAutoreleasePool *pool;
150 pool = [[NSAutoreleasePool alloc] init];
151 #if LIB_FOUNDATION_LIBRARY
152 [NSProcessInfo initializeWithArguments:argv count:argc environment:env];
155 if ([[[NSProcessInfo processInfo] arguments] count] < 2) {
156 fprintf(stderr, "usage: %s <rssfile>\n",
157 [[[[NSProcessInfo processInfo] arguments] lastObject] cString]);
161 /* the interesting section */
165 id<SaxXMLReader> parser;
168 /* step a, get a parser for XML */
169 parser = [[SaxXMLReaderFactory standardXMLReaderFactory]
170 createXMLReaderForMimeType:@"text/xml"];
172 /* step b, create a SAX handler and attach it to the parser */
173 sax = [[[RSSSaxHandler alloc] init] autorelease];
174 [parser setContentHandler:sax];
175 [parser setErrorHandler:sax];
177 /* step c, parse :-) */
179 args = [[[NSProcessInfo processInfo] arguments] objectEnumerator];
180 [args nextObject]; /* skip tool name */
182 while ((arg = [args nextObject])) {
185 /* the parser takes URLs, NSData's, NSString's */
186 arg = [[[NSURL alloc] initFileURLWithPath:arg] autorelease];
188 /* let the parser parse (it will report SAX events to the handler) */
189 [parser parseFromSource:arg];
191 /* now query the handler for the result */
192 entries = [sax rssEntries];
194 /* TODO: use NSPropertyListSerialization on OSX */
195 printf("%s\n", [[entries description] cString]);