]> err.no Git - sope/blob - sope-appserver/NGObjWeb/wod.m
major improvements in resource/template lookup with SoProduct's
[sope] / sope-appserver / NGObjWeb / wod.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 "WODParser.h"
24
25 @interface MyWODHandler : NSObject < WODParserHandler >
26 @end
27
28 @implementation MyWODHandler
29
30 - (BOOL)parser:(id)_parser willParseDeclarationData:(NSData *)_data {
31   return YES;
32 }
33 - (void)parser:(id)_parser finishedParsingDeclarationData:(NSData *)_data
34   declarations:(NSDictionary *)_decls
35 {
36 }
37 - (void)parser:(id)_parser failedParsingDeclarationData:(NSData *)_data
38   exception:(NSException *)_exception
39 {
40   [_exception raise];
41 }
42
43 - (id)parser:(id)_parser makeAssociationWithValue:(id)_value {
44   return nil;
45 }
46 - (id)parser:(id)_parser makeAssociationWithKeyPath:(NSString *)_keyPath {
47   return nil;
48 }
49 - (id)parser:(id)_parser makeDefinitionForComponentNamed:(NSString *)_cname
50   associations:(id)_entry
51   elementName:(NSString *)_elemName
52 {
53   return nil;
54 }
55
56 @end /* MyWODHandler */
57
58 static void processFile(NSString *path) {
59   NSUserDefaults *ud;
60   NSData         *data;
61   NSDictionary   *mappings;
62   NSException    *e;
63   MyWODHandler   *wodHandler;
64
65   wodHandler = [[[MyWODHandler alloc] init] autorelease];
66   ud         = [NSUserDefaults standardUserDefaults];
67   mappings   = nil;
68   e          = nil;
69   
70   if ((data = [NSData dataWithContentsOfFile:path]) == nil) {
71     fprintf(stderr, "%s: could not open file.\n", [path cString]);
72     fflush(stderr);
73     return;
74   }
75   if ([data length] == 0)
76     /* no content */
77     return;
78   
79   NS_DURING {
80     id parser;
81
82     parser = [[[WODParser alloc] initWithHandler:(id)wodHandler] autorelease];
83     
84     mappings = [parser parseDeclarationData:data];
85   }
86   NS_HANDLER {
87     e = [localException retain];
88     //abort();
89   }
90   NS_ENDHANDLER;
91   
92   if (e) {
93     NSDictionary *ui;
94     int line = -1;
95     
96     if ((ui = [e userInfo]))
97       line = [[ui objectForKey:@"line"] intValue];
98
99     if (line > 0) {
100       fprintf(stderr, "%s:%i: %s\n",
101               [path cString], line,
102               [[e reason] cString]);
103     }
104     else {
105       fprintf(stderr, "%s: %s\n",
106               [path cString],
107               [[e reason] cString]);
108     }
109   }
110   
111   if ([ud objectForKey:@"print"]) {
112     NSEnumerator *e;
113     NSString *key;
114
115     e = [mappings keyEnumerator];
116     while ((key = [e nextObject])) {
117       id value;
118       
119       value = [mappings objectForKey:key];
120       printf("%s: %s\n", [key cString], [[value description] cString]);
121     }
122   }
123   
124   if ([ud objectForKey:@"list"]) {
125     id       keys;
126     NSString *key;
127
128     keys = [mappings allKeys];
129     keys = [keys sortedArrayUsingSelector:@selector(compare:)];
130     keys = [keys objectEnumerator];
131     
132     while ((key = [keys nextObject])) {
133       id value;
134       
135       value = [mappings objectForKey:key];
136       printf("%s: %s\n", [key cString], [[value description] cString]);
137     }
138   }
139 }
140
141 int main(int argc, char **argv, char **env) {
142   NSUserDefaults *ud;
143   NSAutoreleasePool *pool;
144   NSArray      *args;
145   id           paths;
146   NSString     *path;
147   int          i;
148   
149 #if LIB_FOUNDATION_LIBRARY
150   [NSProcessInfo initializeWithArguments:argv count:argc environment:env];
151 #endif
152
153   pool = [[NSAutoreleasePool alloc] init];
154   args = [[NSProcessInfo processInfo] arguments];
155
156   if (argc < 1)
157     exit(0);
158   
159   ud    = [NSUserDefaults standardUserDefaults];
160   *(&paths) = [NSMutableArray array];
161
162   for (*(&i) = 1; i < argc; i++) {
163     if (argv[i][0] == '-') {
164       NS_DURING 
165         [ud setObject:[NSNumber numberWithBool:YES]
166             forKey:[NSString stringWithCString:&(argv[i][1])]];
167       NS_HANDLER
168         abort();
169       NS_ENDHANDLER;
170     }
171     else {
172       [paths addObject:[NSString stringWithCString:argv[i]]];
173     }
174   }
175   paths = [paths objectEnumerator];
176   
177   while ((path = [paths nextObject])) {
178     NSAutoreleasePool *pool2;
179
180     pool2 = [[NSAutoreleasePool alloc] init];
181     processFile(path);
182     [pool2 release];
183   }
184   
185   [pool release];
186   exit(0);
187   return 0;
188 }