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