]> err.no Git - sope/blob - sope-xml/samples/rss2plist1.m
removed linking against scripting libs
[sope] / sope-xml / samples / rss2plist1.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 // $Id$
22
23 /*
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.
27   
28   As you will see it's quite a bit of work dealing with SAX ;-)
29 */
30
31 #import <Foundation/Foundation.h>
32 #include <SaxObjC/SaxObjC.h>
33
34 /* ******************** the SAX handler ****************** */
35
36 @interface RSSSaxHandler : SaxDefaultHandler
37 {
38   NSMutableArray      *entries;
39   NSMutableDictionary *entry;
40
41   /* parsing state */
42   BOOL     isInItem; /* are we inside an 'item' tag ? */
43   NSString *value;   /* the (PCDATA) content of a tag */
44 }
45
46 - (NSArray *)rssEntries;
47
48 @end
49
50 @implementation RSSSaxHandler
51
52 - (id)init {
53   if ((self = [super init])) {
54     self->entries = [[NSMutableArray alloc] initWithCapacity:16];
55     self->entry   = [[NSMutableDictionary alloc] initWithCapacity:8];
56   }
57   return self;
58 }
59 - (void)dealloc {
60   [self->entry   release];
61   [self->entries release];
62   [super dealloc];
63 }
64
65 /* accessing results */
66
67 - (NSArray *)rssEntries {
68   return [[self->entries copy] autorelease];
69 }
70
71 /* setup/teardown */
72
73 - (void)startDocument {
74   /* ensure consistent state */
75   [self->entries removeAllObjects];
76   self->isInItem = NO;
77 }
78
79 /* parsing */
80
81 - (void)startElement:(NSString *)_localName
82   namespace:(NSString *)_ns
83   rawName:(NSString *)_rawName
84   attributes:(id<SaxAttributes>)_attributes
85 {
86   if ([_localName isEqualToString:@"item"]) {
87     [self->entry removeAllObjects];
88     self->isInItem = YES;
89   }
90   
91   /* always reset content when entering a new tag */
92   [self->value release]; self->value = nil;
93 }
94
95 - (void)endElement:(NSString *)_localName
96   namespace:(NSString *)_ns
97   rawName:(NSString *)_rawName
98 {
99   if ([_localName isEqualToString:@"item"]) {
100     /* found end of item */
101     self->isInItem = NO;
102     [self->entries addObject:[[self->entry copy] autorelease]];
103   }
104   else if (self->isInItem) {
105     /* any tag inside of an item is a key for the entry dict */
106     if (self->value) {
107       /* if we collected a PCDATA value, add it */
108       [self->entry setObject:self->value forKey:_localName];
109       [self->value release]; self->value = nil;
110     }
111   }
112 }
113
114 - (void)characters:(unichar *)_chars length:(int)_len {
115   NSString *s;
116   
117   if (!self->isInItem)
118     /* only track content if we are inside an item ... */
119     return;
120     
121   /* 
122      Note: The characters callback is allowed to be called multiple times
123            by the parser (makes writing parsers easier, but complicates the
124            handler ...).
125   */
126   s = [[NSString alloc] initWithCharacters:_chars length:_len];
127   if (self->value) {
128     self->value = [[self->value stringByAppendingString:s] copy];
129     [s release];
130   }
131   else
132     self->value = s;
133 }
134
135 @end /* RSSSaxHandler */
136
137 /* ******************** C main section ******************** */
138
139 int main(int argc, char **argv, char **env) {
140   NSAutoreleasePool *pool;
141 #if LIB_FOUNDATION_LIBRARY
142   [NSProcessInfo initializeWithArguments:argv count:argc environment:env];
143 #endif
144   
145   pool = [[NSAutoreleasePool alloc] init];
146
147   if ([[[NSProcessInfo processInfo] arguments] count] < 2) {
148     fprintf(stderr, "usage: %s <rssfile>\n",
149             [[[[NSProcessInfo processInfo] arguments] lastObject] cString]);
150     return 1;
151   }
152   
153   /* the interesting section */
154   {
155     NSEnumerator     *args;
156     NSString         *arg;
157     id<SaxXMLReader> parser;
158     RSSSaxHandler    *sax;
159     
160     /* step a, get a parser for XML */
161     parser = [[SaxXMLReaderFactory standardXMLReaderFactory]
162                                    createXMLReaderForMimeType:@"text/xml"];
163     
164     /* step b, create a SAX handler and attach it to the parser */
165     sax = [[[RSSSaxHandler alloc] init] autorelease];
166     [parser setContentHandler:sax];
167     [parser setErrorHandler:sax];
168     
169     /* step c, parse :-) */
170     
171     args = [[[NSProcessInfo processInfo] arguments] objectEnumerator];
172     [args nextObject]; /* skip tool name */
173     
174     while ((arg = [args nextObject])) {
175       NSArray *entries;
176       
177       /* the parser takes URLs, NSData's, NSString's */
178       arg = [[[NSURL alloc] initFileURLWithPath:arg] autorelease];
179       
180       /* let the parser parse (it will report SAX events to the handler) */
181       [parser parseFromSource:arg];
182       
183       /* now query the handler for the result */
184       entries = [sax rssEntries];
185       
186       /* TODO: use NSPropertyListSerialization on OSX */
187       printf("%s\n", [[entries description] cString]);
188     }
189     
190     return 0;
191   }
192   [pool release];
193   return 0;
194 }