]> err.no Git - sope/blob - sope-xml/samples/rssparse.m
9ac89ff64a8907685fff58bc1539626483f4a913
[sope] / sope-xml / samples / rssparse.m
1 /*
2   Copyright (C) 2000-2003 SKYRIX Software AG
3
4   This file is part of OGo
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 use SaxObjectDecoder
25   to parse XML files. This one parses a RSS channel file and collects
26   the information in a dictionary.
27   
28   This one is much easier and more high-level than rss2plist1 and rss2plist2.
29   Instead of writing a low level SAX event handler, you just define a mapping
30   model and the "enterprise" classes.
31 */
32
33 #import <Foundation/Foundation.h>
34 #include <SaxObjC/SaxObjC.h>
35 #include <SaxObjC/SaxMethodCallHandler.h>
36
37 /* ******************** the "business" objects ************ */
38
39 @interface RSSObject : NSObject
40 {
41   NSString *title;
42   NSString *link;
43   NSString *info;
44 }
45
46 @end
47
48 @interface RSSChannel : RSSObject
49 @end
50
51 @interface RSSItem : RSSObject
52 @end
53
54 @implementation RSSObject
55
56 - (void)dealloc {
57   [self->title release];
58   [self->link  release];
59   [self->info  release];
60   [super dealloc];
61 }
62
63 /* accessors */
64
65 - (void)setTitle:(NSString *)_value {
66   [self->title autorelease];
67   self->title = [_value copy];
68 }
69 - (void)setLink:(NSString *)_value {
70   [self->link autorelease];
71   self->link = [_value copy];
72 }
73 - (void)setInfo:(NSString *)_value {
74   [self->info autorelease];
75   self->info = [_value copy];
76 }
77
78 /* description */
79
80 - (NSString *)description {
81   NSMutableString *s = [NSMutableString stringWithCapacity:64];
82   [s appendFormat:@"<0x%08X[%@]:", self, NSStringFromClass([self class])];
83   if (self->title) [s appendFormat:@" title='%@'", self->title];
84   if (self->link)  [s appendFormat:@" link='%@'",  self->link];
85   //[s appendFormat:@" info='%@'", self->info];
86   [s appendString:@">"];
87   return s;
88 }
89
90 @end /* RSSObject */
91
92 @implementation RSSChannel
93 @end /* RSSChannel */
94
95 @implementation RSSItem
96 @end /* RSSItem */
97
98 /* ******************** C main section ******************** */
99
100 int main(int argc, char **argv, char **env) {
101   NSAutoreleasePool *pool;
102 #if LIB_FOUNDATION_LIBRARY
103   [NSProcessInfo initializeWithArguments:argv count:argc environment:env];
104 #endif
105   
106   pool = [[NSAutoreleasePool alloc] init];
107
108   if ([[[NSProcessInfo processInfo] arguments] count] < 2) {
109     fprintf(stderr, "usage: %s <rssfile>\n",
110             [[[[NSProcessInfo processInfo] arguments] lastObject] cString]);
111     return 1;
112   }
113   
114   /* the interesting section */
115   {
116     NSEnumerator     *args;
117     NSString         *arg;
118     id<SaxXMLReader> parser;
119     SaxObjectDecoder *sax;
120     
121     /* step a, get a parser for XML */
122     parser = [[SaxXMLReaderFactory standardXMLReaderFactory]
123                                    createXMLReaderForMimeType:@"text/xml"];
124     
125     /* step b, create a SAX handler and attach it to the parser */
126     sax = [[SaxObjectDecoder alloc] initWithMappingAtPath:@"./rssparse.xmap"];
127     [parser setContentHandler:sax];
128     [parser setErrorHandler:sax];
129     [sax autorelease];
130     
131     /* step c, parse :-) */
132     
133     args = [[[NSProcessInfo processInfo] arguments] objectEnumerator];
134     [args nextObject]; /* skip tool name */
135     
136     while ((arg = [args nextObject])) {
137       id channel;
138       
139       /* the parser takes URLs, NSData's, NSString's */
140       arg = [[[NSURL alloc] initFileURLWithPath:arg] autorelease];
141       
142       /* let the parser parse (it will report SAX events to the handler) */
143       [parser parseFromSource:arg];
144       
145       /* now query the handler for the result */
146       channel = [sax rootObject];
147       
148       /* TODO: use NSPropertyListSerialization on OSX */
149       NSLog(@"parsed channel: %@", channel);
150     }
151     
152     return 0;
153   }
154   [pool release];
155   return 0;
156 }