]> err.no Git - sope/blob - sope-ical/samples/icalparsetest.m
renamed test tools
[sope] / sope-ical / samples / icalparsetest.m
1 /*
2   Copyright (C) 2000-2005 SKYRIX Software AG
3
4   This file is part of SOPE.
5
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
9   later version.
10
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.
15
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
19   02111-1307, USA.
20 */
21
22 #import <Foundation/Foundation.h>
23 #include <SaxObjC/SaxObjC.h>
24
25 @interface iCal2Tool : NSObject
26 {
27   id<NSObject,SaxXMLReader> parser;
28   SaxObjectDecoder *sax;
29 }
30
31 - (int)runWithArguments:(NSArray *)_args;
32
33 @end
34
35 @implementation iCal2Tool
36
37 - (id)init {
38   if ((self = [super init])) {
39     self->parser =
40       [[[SaxXMLReaderFactory standardXMLReaderFactory] 
41                              createXMLReaderForMimeType:@"text/calendar"]
42                              retain];
43     if (self->parser == nil) {
44       NSLog(@"%s: did not find a parser for text/calendar !",
45             __PRETTY_FUNCTION__);
46       [self release];
47       return nil;
48     }
49     
50     /* ensure that NGiCal.xmap can be found ! (Library/SaxMappings) */
51     self->sax = [[SaxObjectDecoder alloc] initWithMappingNamed:@"NGiCal"];
52     if (self->sax == nil) {
53       NSLog(@"could not create the iCal SAX handler !");
54       [self release];
55       return nil;
56     }
57     
58     [self->parser setContentHandler:self->sax];
59     [self->parser setErrorHandler:self->sax];
60   }
61   return self;
62 }
63 - (void)dealloc {
64   [self->sax    release];
65   [self->parser release];
66   [super dealloc];
67 }
68
69 /* parsing */
70
71 - (id)parseFile:(NSString *)_path {
72   if ([_path length] == 0) return nil;
73   
74   _path = [@"file://" stringByAppendingString:_path];
75   
76   [self->parser parseFromSystemId:_path];
77   
78   return [self->sax rootObject];
79 }
80
81 - (void)printParsedObject:(id)_object {
82   NSLog(@"component: %@",       _object);
83 #if 0
84   NSLog(@"  subcomponents: %@", [_object subComponents]);
85   
86   printf("%s", [[_object icalString] cString]);
87 #endif
88 }
89
90 /* run */
91
92 - (int)runWithArguments:(NSArray *)_args {
93   NSEnumerator      *args;
94   NSString          *arg;
95   
96   args = [_args objectEnumerator];
97   [args nextObject]; // process name ...
98
99   while ((arg = [args nextObject])) {
100     NSAutoreleasePool *pool2;
101
102     if ([arg hasPrefix:@"-"]) { /* consume defaults */
103       [args nextObject];
104       continue;
105     }
106     
107     pool2 = [[NSAutoreleasePool alloc] init];
108     {    
109       id component;
110       
111       NS_DURING
112         component = [self parseFile:arg];
113       NS_HANDLER
114         abort();
115       NS_ENDHANDLER;
116       
117       if (component == nil)
118         NSLog(@"could not parse file: '%@'", arg);
119       else
120         [self printParsedObject:component];
121     }
122     [pool2 release];
123   }
124   return 0;
125 }
126
127 @end /* iCal2Tool */
128
129 int main(int argc, char **argv, char **env)  {
130   NSAutoreleasePool *pool;
131   iCal2Tool *tool;
132   int rc;
133
134 #if LIB_FOUNDATION_LIBRARY  
135   [NSProcessInfo initializeWithArguments:argv count:argc environment:env];
136 #endif
137   
138   pool = [[NSAutoreleasePool alloc] init];
139   
140   if ((tool = [[iCal2Tool alloc] init])) {
141     NS_DURING
142       rc = [tool runWithArguments:[[NSProcessInfo processInfo] arguments]];
143     NS_HANDLER
144       abort();
145     NS_ENDHANDLER;
146     
147     [tool release];
148   }
149   else
150     rc = 1;
151   
152   [pool release];
153   return rc;
154 }