]> err.no Git - sope/blob - sope-xml/samples/rss2plist2.m
Id fixes
[sope] / sope-xml / samples / rss2plist2.m
1 /*
2   Copyright (C) 2000-2004 SKYRIX Software AG
3
4   This file is part of OpenGroupware.org.
5
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
9   later version.
10
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.
15
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
19   02111-1307, USA.
20 */
21
22 /*
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.
26   
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.
31   
32   As you will see it's quite a bit of work dealing with SAX ;-)
33 */
34
35 #import <Foundation/Foundation.h>
36 #include <SaxObjC/SaxObjC.h>
37 #include <SaxObjC/SaxMethodCallHandler.h>
38
39 /* ******************** the SAX handler ****************** */
40
41 @interface RSSSaxHandler : SaxMethodCallHandler
42 {
43   NSMutableArray      *entries;
44   NSMutableDictionary *entry;
45
46   /* parsing state */
47   NSString *value;   /* the (PCDATA) content of a tag */
48 }
49
50 - (NSArray *)rssEntries;
51
52 @end
53
54 @implementation RSSSaxHandler
55
56 - (id)init {
57   if ((self = [super init])) {
58     self->entries = [[NSMutableArray alloc] initWithCapacity:16];
59     self->entry   = [[NSMutableDictionary alloc] initWithCapacity:8];
60     
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#"
64           withKey:@"rdf"];
65     [self registerNamespace:@"http://purl.org/rss/1.0/modules/slash/"
66           withKey:@"slash"];
67     [self registerNamespace:@"http://purl.org/rss/1.0/modules/syndication/"
68           withKey:@"syn"];
69   }
70   return self;
71 }
72 - (void)dealloc {
73   [self->entry   release];
74   [self->entries release];
75   [super dealloc];
76 }
77
78 /* accessing results */
79
80 - (NSArray *)rssEntries {
81   return [[self->entries copy] autorelease];
82 }
83
84 /* setup/teardown */
85
86 - (void)startDocument {
87   /* ensure consistent state */
88   [self->entries removeAllObjects];
89 }
90
91 /* parsing */
92
93 - (void)start_rssitem:(id<SaxAttributes>)_attributes {
94   [self->entry removeAllObjects];
95 }
96 - (void)end_rssitem {
97   [self->entries addObject:[[self->entry copy] autorelease]];
98 }
99
100 /* the subtags of item ... */
101
102 - (void)start_rsstitle:(id<SaxAttributes>)_attributes {
103   [self->value release]; self->value = nil;
104 }
105 - (void)end_rsstitle {
106   if (self->value)
107     [self->entry setObject:self->value forKey:@"title"];
108 }
109
110 - (void)start_rsslink:(id<SaxAttributes>)_attributes {
111   [self->value release]; self->value = nil;
112 }
113 - (void)end_rsslink {
114   if (self->value)
115     [self->entry setObject:self->value forKey:@"link"];
116 }
117
118 - (void)start_rssdescription:(id<SaxAttributes>)_attributes {
119   [self->value release]; self->value = nil;
120 }
121 - (void)end_rssdescription {
122   if (self->value)
123     [self->entry setObject:self->value forKey:@"info"];
124 }
125
126 - (void)characters:(unichar *)_chars length:(int)_len {
127   NSString *s;
128   
129   /* 
130      Note: The characters callback is allowed to be called multiple times
131            by the parser (makes writing parsers easier, but complicates the
132            handler ...).
133   */
134   s = [[NSString alloc] initWithCharacters:_chars length:_len];
135   if (self->value) {
136     self->value = [[self->value stringByAppendingString:s] copy];
137     [s release];
138   }
139   else
140     self->value = s;
141 }
142
143 @end /* RSSSaxHandler */
144
145 /* ******************** C main section ******************** */
146
147 int main(int argc, char **argv, char **env) {
148   NSAutoreleasePool *pool;
149   
150   pool = [[NSAutoreleasePool alloc] init];
151 #if LIB_FOUNDATION_LIBRARY
152   [NSProcessInfo initializeWithArguments:argv count:argc environment:env];
153 #endif
154   
155   if ([[[NSProcessInfo processInfo] arguments] count] < 2) {
156     fprintf(stderr, "usage: %s <rssfile>\n",
157             [[[[NSProcessInfo processInfo] arguments] lastObject] cString]);
158     return 1;
159   }
160   
161   /* the interesting section */
162   {
163     NSEnumerator     *args;
164     NSString         *arg;
165     id<SaxXMLReader> parser;
166     RSSSaxHandler    *sax;
167     
168     /* step a, get a parser for XML */
169     parser = [[SaxXMLReaderFactory standardXMLReaderFactory]
170                                  createXMLReaderForMimeType:@"text/xml"];
171     
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];
176     
177     /* step c, parse :-) */
178     
179     args = [[[NSProcessInfo processInfo] arguments] objectEnumerator];
180     [args nextObject]; /* skip tool name */
181     
182     while ((arg = [args nextObject])) {
183       NSArray *entries;
184       
185       /* the parser takes URLs, NSData's, NSString's */
186       arg = [[[NSURL alloc] initFileURLWithPath:arg] autorelease];
187       
188       /* let the parser parse (it will report SAX events to the handler) */
189       [parser parseFromSource:arg];
190       
191       /* now query the handler for the result */
192       entries = [sax rssEntries];
193       
194       /* TODO: use NSPropertyListSerialization on OSX */
195       printf("%s\n", [[entries description] cString]);
196     }
197     
198     return 0;
199   }
200   [pool release];
201   return 0;
202 }