2 Copyright (C) 2000-2004 SKYRIX Software AG
4 This file is part of OpenGroupware.org.
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
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.
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
23 #include <SaxObjC/SaxObjC.h>
24 #import <Foundation/Foundation.h>
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.
33 @interface PYXSaxHandler : SaxDefaultHandler
38 - (void)write:(NSString *)_s;
42 int main(int argc, char **argv, char **env) {
43 NSAutoreleasePool *pool;
44 id<NSObject,SaxXMLReader> parser;
49 #if LIB_FOUNDATION_LIBRARY
50 [NSProcessInfo initializeWithArguments:argv count:argc environment:env];
53 pool = [[NSAutoreleasePool alloc] init];
55 parser = [[SaxXMLReaderFactory standardXMLReaderFactory]
58 fprintf(stderr, "could not load a SAX driver bundle !\n");
62 sax = [[[PYXSaxHandler alloc] init] autorelease];
63 [parser setContentHandler:sax];
64 [parser setDTDHandler:sax];
65 [parser setErrorHandler:sax];
69 paths = [[[NSProcessInfo processInfo] arguments] objectEnumerator];
71 while ((path = [paths nextObject])) {
72 NSAutoreleasePool *pool;
74 if ([path hasPrefix:@"-"]) {
80 pool = [[NSAutoreleasePool alloc] init];
82 path = [@"file://" stringByAppendingString:path];
85 [parser parseFromSystemId:path];
87 fprintf(stderr, "xmln: catched: %s\n",
88 [[localException description] cString]);
91 [pool release]; pool = nil;
103 @implementation PYXSaxHandler
110 - (void)write:(NSString *)_s {
113 if ((len = [_s cStringLength]) == 0)
115 buf = malloc(len + 1);
117 fprintf(self->out, "%s", buf);
118 free(buf); buf = NULL;
120 - (void)tag:(unsigned char)_t key:(NSString *)_s value:(NSString *)_value {
124 fputc(_t, self->out);
126 len = [_s cStringLength];
127 vlen = [_value cStringLength];
129 buf = malloc((len>vlen ? len : vlen) + 1);
132 fprintf(self->out, "%s", buf);
133 [_value getCString:buf];
134 fprintf(self->out, " %s\n", buf);
137 fprintf(self->out, "%s\n", buf);
138 free(buf); buf = NULL;
140 - (void)tag:(unsigned char)_t key:(NSString *)_s {
141 [self tag:_t key:_s value:nil];
144 - (void)startElement:(NSString *)_localName
145 namespace:(NSString *)_ns
146 rawName:(NSString *)_rawName
147 attributes:(id<SaxAttributes>)_attrs
151 [self tag:'(' key:_rawName];
153 for (i = 0, c = [_attrs count]; i < c; i++) {
154 NSString *aname, *avalue;
156 aname = [_attrs rawNameAtIndex:i];
157 avalue = [_attrs valueAtIndex:i];
159 [self tag:'A' key:aname value:avalue];
163 - (void)endElement:(NSString *)_localName
164 namespace:(NSString *)_ns
165 rawName:(NSString *)_rawName
167 [self tag:')' key:_rawName];
170 - (void)characters:(unichar *)_chars length:(int)_len {
173 if (_len == 0) return;
175 fputc('-', self->out);
177 for (i = 0; i < _len; i++) {
178 if (_chars[i] > 255) {
179 fprintf(stderr, "found unichar, code 0x%04X\n", _chars[i]);
182 register unsigned char c = _chars[i];
186 fputc('\\', self->out);
187 fputc('n', self->out);
195 fputc('\n', self->out);
198 - (void)processingInstruction:(NSString *)_pi data:(NSString *)_data {
199 [self tag:'?' key:_pi value:_data];
202 - (void)warning:(SaxParseException *)_exception {
203 NSLog(@"WARNING: %@", [_exception reason]);
205 - (void)error:(SaxParseException *)_exception {
206 NSLog(@"ERROR: %@:%@: %@",
207 [[_exception userInfo] objectForKey:@"systemId"],
208 [[_exception userInfo] objectForKey:@"line"],
209 [_exception reason]);
211 - (void)fatalError:(SaxParseException *)_exception {
215 @end /* PYXSaxHandler */