2 Copyright (C) 2000-2003 SKYRIX Software AG
4 This file is part of OGo
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
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.
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.
33 #import <Foundation/Foundation.h>
34 #include <SaxObjC/SaxObjC.h>
35 #include <SaxObjC/SaxMethodCallHandler.h>
37 /* ******************** the "business" objects ************ */
39 @interface RSSObject : NSObject
48 @interface RSSChannel : RSSObject
51 @interface RSSItem : RSSObject
54 @implementation RSSObject
57 [self->title release];
65 - (void)setTitle:(NSString *)_value {
66 [self->title autorelease];
67 self->title = [_value copy];
69 - (void)setLink:(NSString *)_value {
70 [self->link autorelease];
71 self->link = [_value copy];
73 - (void)setInfo:(NSString *)_value {
74 [self->info autorelease];
75 self->info = [_value copy];
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:@">"];
92 @implementation RSSChannel
95 @implementation RSSItem
98 /* ******************** C main section ******************** */
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];
106 pool = [[NSAutoreleasePool alloc] init];
108 if ([[[NSProcessInfo processInfo] arguments] count] < 2) {
109 fprintf(stderr, "usage: %s <rssfile>\n",
110 [[[[NSProcessInfo processInfo] arguments] lastObject] cString]);
114 /* the interesting section */
118 id<SaxXMLReader> parser;
119 SaxObjectDecoder *sax;
121 /* step a, get a parser for XML */
122 parser = [[SaxXMLReaderFactory standardXMLReaderFactory]
123 createXMLReaderForMimeType:@"text/xml"];
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];
131 /* step c, parse :-) */
133 args = [[[NSProcessInfo processInfo] arguments] objectEnumerator];
134 [args nextObject]; /* skip tool name */
136 while ((arg = [args nextObject])) {
139 /* the parser takes URLs, NSData's, NSString's */
140 arg = [[[NSURL alloc] initFileURLWithPath:arg] autorelease];
142 /* let the parser parse (it will report SAX events to the handler) */
143 [parser parseFromSource:arg];
145 /* now query the handler for the result */
146 channel = [sax rootObject];
148 /* TODO: use NSPropertyListSerialization on OSX */
149 NSLog(@"parsed channel: %@", channel);