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