]> err.no Git - sope/blob - sope-xml/samples/testqp.m
lF fixes
[sope] / sope-xml / samples / testqp.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 #include <SaxObjC/SaxObjC.h>
23 #include "common.h"
24 #include <DOM/DOMSaxBuilder.h>
25 #include <DOM/DOMXMLOutputter.h>
26 #include <DOM/DOMPYXOutputter.h>
27 #include <DOM/NSObject+QPEval.h>
28 #include <string.h>
29
30 /*
31   Usage
32   
33     testqp [options] files
34       -xml|-pyx         - output in XML or PYX format ...
35 */
36
37 int main(int argc, char **argv, char **env) {
38   NSEnumerator      *paths;
39   NSString          *path;
40   NSAutoreleasePool *pool;
41   Class             builderClass;
42   DOMSaxBuilder     *builder;
43   id                out;
44   
45 #if LIB_FOUNDATION_LIBRARY
46   [NSProcessInfo initializeWithArguments:argv count:argc environment:env];
47 #endif
48
49   pool = [[NSAutoreleasePool alloc] init];
50
51   out = nil;
52   
53   builderClass = Nil;
54   if ((builder = [[DOMSaxBuilder alloc] init]) == nil) {
55     fprintf(stderr, "could not load a SAX driver bundle !\n");
56     exit(2);
57   }
58   [builder autorelease];
59   
60   /* parse */
61
62   paths = [[[NSProcessInfo processInfo] arguments] objectEnumerator];
63   [paths nextObject]; // skip toolname
64   while ((path = [paths nextObject])) {
65     NSAutoreleasePool *pool;
66     NSDate            *date;
67     NSTimeInterval    duration;
68     id doc;
69     
70     if ([path hasPrefix:@"-"]) {
71       if ([path isEqualToString:@"-pyx"]) {
72         out = [[[NGDOMPYXOutputter alloc] init] autorelease];
73       }
74       else if ([path isEqualToString:@"-xml"]) {
75         out = [[[DOMXMLOutputter alloc] init] autorelease];
76       }
77       else {
78         // a default ? skip the value
79         [paths nextObject];
80       }
81       
82       continue;
83     }
84     
85     pool = [[NSAutoreleasePool alloc] init];
86
87     date = [NSDate date];
88     doc = [builder buildFromContentsOfFile:path];
89     duration = [[NSDate date] timeIntervalSinceDate:date];
90     
91     if (doc == nil) {
92       NSLog(@"couldn't build DOM from path '%@'", path);
93       [pool release]; pool = nil;
94     }
95     else {
96       [out outputDocument:doc to:nil];
97       NSLog(@"doc is %@, parsed in %.3fs", doc, duration);
98       
99       while (1) {
100         NSAutoreleasePool *pool;
101         char buf[4096];
102         
103         pool = [[NSAutoreleasePool alloc] init];
104         printf("enter query path: ");
105         fflush(stdout);
106         
107         fgets(buf, 4000, stdin);
108         
109         if (buf[0] == '\n' || buf[0] == 0) {
110           printf("... exit\n");
111           break;
112         }
113         else {
114           NSString *s;
115           volatile id res;
116
117           buf[strlen(buf) - 1] = '\0';
118           s = [NSString stringWithCString:buf];
119           NSLog(@"eval: '%@'", s);
120
121           NS_DURING
122             if ((res = [doc evaluateQueryPath:s])) 
123               NSLog(@"result: %@", res);
124             else
125               NSLog(@"no rresult ...");
126           NS_HANDLER {
127             fprintf(stderr, "%s\n", [[localException description] cString]);
128             abort();
129           }
130           NS_ENDHANDLER;
131         }
132         
133         [pool release];
134       }
135     }
136     
137     [pool release];
138   }
139   
140   [pool release];
141
142   exit(0);
143   return 0;
144 }