]> err.no Git - sope/blob - sope-xml/samples/xmln.m
added a select..X method which doesn't raise NSExceptions,
[sope] / sope-xml / samples / xmln.m
1 /*
2   Copyright (C) 2000-2004 SKYRIX Software AG
3
4   This file is part of OpenGroupware.org.
5
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
9   later version.
10
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.
15
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
19   02111-1307, USA.
20 */
21 // $Id$
22
23 #include <SaxObjC/SaxObjC.h>
24 #import <Foundation/Foundation.h>
25
26 /*
27   This tool uses a SAX parser to generate a PYX representation
28   of an XML file. PYX is a simplified XML syntax for line oriented
29   processing of XML files.
30   See 'XML Processing with Python' for an explanation of PYX.
31 */
32
33 @interface PYXSaxHandler : SaxDefaultHandler
34 {
35   FILE *out;
36 }
37
38 - (void)write:(NSString *)_s;
39
40 @end
41
42 int main(int argc, char **argv, char **env) {
43   NSAutoreleasePool         *pool;
44   id<NSObject,SaxXMLReader> parser;
45   id                        sax;
46   NSEnumerator              *paths;
47   NSString                  *path;
48
49 #if LIB_FOUNDATION_LIBRARY
50   [NSProcessInfo initializeWithArguments:argv count:argc environment:env];
51 #endif
52
53   pool = [[NSAutoreleasePool alloc] init];
54   
55   parser = [[SaxXMLReaderFactory standardXMLReaderFactory]
56                                  createXMLReader];
57   if (parser == nil) {
58     fprintf(stderr, "could not load a SAX driver bundle !\n");
59     exit(2);
60   }
61   
62   sax = [[[PYXSaxHandler alloc] init] autorelease];
63   [parser setContentHandler:sax];
64   [parser setDTDHandler:sax];
65   [parser setErrorHandler:sax];
66   
67   /* parse */
68
69   paths = [[[NSProcessInfo processInfo] arguments] objectEnumerator];
70   [paths nextObject];
71   while ((path = [paths nextObject])) {
72     NSAutoreleasePool *pool;
73
74     if ([path hasPrefix:@"-"]) {
75       /* skip defaults */
76       [paths nextObject];
77       continue;
78     }
79     
80     pool = [[NSAutoreleasePool alloc] init];
81
82     path = [@"file://" stringByAppendingString:path];
83     
84     NS_DURING
85       [parser parseFromSystemId:path];
86     NS_HANDLER
87       fprintf(stderr, "xmln: catched: %s\n", 
88               [[localException description] cString]);
89     NS_ENDHANDLER;
90     
91     [pool release]; pool = nil;
92   }
93   
94   /* cleanup */
95   [pool release];
96
97   exit(0);
98   return 0;
99 }
100
101 #include <stdio.h>
102
103 @implementation PYXSaxHandler
104
105 - (id)init {
106   self->out = stdout;
107   return self;
108 }
109
110 - (void)write:(NSString *)_s {
111   unsigned len;
112   char *buf;
113   if ((len = [_s cStringLength]) == 0)
114     return;
115   buf = malloc(len + 1);
116   [_s getCString:buf];
117   fprintf(self->out, "%s", buf);
118   free(buf); buf = NULL;
119 }
120 - (void)tag:(unsigned char)_t key:(NSString *)_s value:(NSString *)_value {
121   unsigned len, vlen;
122   char *buf;
123   
124   fputc(_t, self->out);
125   
126   len  = [_s cStringLength];
127   vlen = [_value cStringLength];
128   
129   buf = malloc((len>vlen ? len : vlen) + 1);
130   [_s getCString:buf];
131   if (_value) {
132     fprintf(self->out, "%s", buf);
133     [_value getCString:buf];
134     fprintf(self->out, " %s\n", buf);
135   }
136   else
137     fprintf(self->out, "%s\n", buf);
138   free(buf); buf = NULL;
139 }
140 - (void)tag:(unsigned char)_t key:(NSString *)_s {
141   [self tag:_t key:_s value:nil];
142 }
143
144 - (void)startElement:(NSString *)_localName
145   namespace:(NSString *)_ns
146   rawName:(NSString *)_rawName
147   attributes:(id<SaxAttributes>)_attrs
148 {
149   int i, c;
150   
151   [self tag:'(' key:_rawName];
152   
153   for (i = 0, c = [_attrs count]; i < c; i++) {
154     NSString *aname, *avalue;
155     
156     aname  = [_attrs rawNameAtIndex:i];
157     avalue = [_attrs valueAtIndex:i];
158     
159     [self tag:'A' key:aname value:avalue];
160   }
161 }
162
163 - (void)endElement:(NSString *)_localName
164   namespace:(NSString *)_ns
165   rawName:(NSString *)_rawName
166 {
167   [self tag:')' key:_rawName];
168 }
169
170 - (void)characters:(unichar *)_chars length:(int)_len {
171   int i;
172   
173   if (_len == 0) return;
174   
175   fputc('-', self->out);
176   
177   for (i = 0; i < _len; i++) {
178     if (_chars[i] > 255) {
179       fprintf(stderr, "found unichar, code 0x%04X\n", _chars[i]);
180     }
181     else {
182       register unsigned char c = _chars[i];
183
184       switch (c) {
185       case '\n':
186         fputc('\\', self->out);
187         fputc('n', self->out);
188         break;
189       default:
190         fputc(c, self->out);
191         break;
192       }
193     }
194   }
195   fputc('\n', self->out);
196 }
197
198 - (void)processingInstruction:(NSString *)_pi data:(NSString *)_data {
199   [self tag:'?' key:_pi value:_data];
200 }
201
202 - (void)warning:(SaxParseException *)_exception {
203   NSLog(@"WARNING: %@", [_exception reason]);
204 }
205 - (void)error:(SaxParseException *)_exception {
206   NSLog(@"ERROR: %@:%@: %@",
207         [[_exception userInfo] objectForKey:@"systemId"],
208         [[_exception userInfo] objectForKey:@"line"],
209         [_exception reason]);
210 }
211 - (void)fatalError:(SaxParseException *)_exception {
212   [_exception raise];
213 }
214
215 @end /* PYXSaxHandler */