2 Copyright (C) 2000-2005 SKYRIX Software AG
4 This file is part of SOPE.
6 SOPE 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 SOPE 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 SOPE; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
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.
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.
32 #import <Foundation/Foundation.h>
33 #include <SaxObjC/SaxObjC.h>
34 #include <SaxObjC/SaxMethodCallHandler.h>
36 /* ******************** the "business" objects ************ */
38 @interface RSSObject : NSObject
47 @interface RSSChannel : RSSObject
50 @interface RSSItem : RSSObject
53 @implementation RSSObject
56 [self->title release];
64 - (void)setTitle:(NSString *)_value {
65 [self->title autorelease];
66 self->title = [_value copy];
68 - (void)setLink:(NSString *)_value {
69 [self->link autorelease];
70 self->link = [_value copy];
72 - (void)setInfo:(NSString *)_value {
73 [self->info autorelease];
74 self->info = [_value copy];
79 - (NSString *)description {
80 NSMutableString *s = [NSMutableString stringWithCapacity:64];
81 [s appendFormat:@"<0x%p[%@]:", 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:@">"];
91 @implementation RSSChannel
94 @implementation RSSItem
97 /* ******************** C main section ******************** */
99 int main(int argc, char **argv, char **env) {
100 NSAutoreleasePool *pool;
102 pool = [[NSAutoreleasePool alloc] init];
103 #if LIB_FOUNDATION_LIBRARY
104 [NSProcessInfo initializeWithArguments:argv count:argc environment:env];
107 if ([[[NSProcessInfo processInfo] arguments] count] < 2) {
108 fprintf(stderr, "usage: %s <rssfile>\n",
109 [[[[NSProcessInfo processInfo] arguments] lastObject] cString]);
113 /* the interesting section */
117 id<SaxXMLReader> parser;
118 SaxObjectDecoder *sax;
120 /* step a, get a parser for XML */
121 parser = [[SaxXMLReaderFactory standardXMLReaderFactory]
122 createXMLReaderForMimeType:@"text/xml"];
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];
130 /* step c, parse :-) */
132 args = [[[NSProcessInfo processInfo] arguments] objectEnumerator];
133 [args nextObject]; /* skip tool name */
135 while ((arg = [args nextObject])) {
138 /* the parser takes URLs, NSData's, NSString's */
139 arg = [[[NSURL alloc] initFileURLWithPath:arg] autorelease];
141 /* let the parser parse (it will report SAX events to the handler) */
142 [parser parseFromSource:arg];
144 /* now query the handler for the result */
145 channel = [sax rootObject];
147 /* TODO: use NSPropertyListSerialization on OSX */
148 NSLog(@"parsed channel: %@", channel);