2 Copyright (C) 2000-2005 SKYRIX Software AG
4 This file is part of SOPE.
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
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.
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
22 #include <SaxObjC/SaxObjC.h>
23 #import <Foundation/Foundation.h>
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.
32 @interface PYXSaxHandler : SaxDefaultHandler
37 - (void)write:(NSString *)_s;
41 int main(int argc, char **argv, char **env) {
42 NSAutoreleasePool *pool;
43 id<NSObject,SaxXMLReader> parser;
48 #if LIB_FOUNDATION_LIBRARY
49 [NSProcessInfo initializeWithArguments:argv count:argc environment:env];
52 pool = [[NSAutoreleasePool alloc] init];
54 parser = [[SaxXMLReaderFactory standardXMLReaderFactory]
57 fprintf(stderr, "could not load a SAX driver bundle !\n");
61 sax = [[[PYXSaxHandler alloc] init] autorelease];
62 [parser setContentHandler:sax];
63 [parser setDTDHandler:sax];
64 [parser setErrorHandler:sax];
68 paths = [[[NSProcessInfo processInfo] arguments] objectEnumerator];
70 while ((path = [paths nextObject])) {
71 NSAutoreleasePool *pool;
73 if ([path hasPrefix:@"-"]) {
79 pool = [[NSAutoreleasePool alloc] init];
81 path = [@"file://" stringByAppendingString:path];
84 [parser parseFromSystemId:path];
86 fprintf(stderr, "xmln: catched: %s\n",
87 [[localException description] cString]);
90 [pool release]; pool = nil;
102 @implementation PYXSaxHandler
109 - (void)write:(NSString *)_s {
112 if ((len = [_s cStringLength]) == 0)
114 buf = malloc(len + 1);
116 fprintf(self->out, "%s", buf);
117 free(buf); buf = NULL;
119 - (void)tag:(unsigned char)_t key:(NSString *)_s value:(NSString *)_value {
123 fputc(_t, self->out);
125 len = [_s cStringLength];
126 vlen = [_value cStringLength];
128 buf = malloc((len>vlen ? len : vlen) + 1);
131 fprintf(self->out, "%s", buf);
132 [_value getCString:buf];
133 fprintf(self->out, " %s\n", buf);
136 fprintf(self->out, "%s\n", buf);
137 free(buf); buf = NULL;
139 - (void)tag:(unsigned char)_t key:(NSString *)_s {
140 [self tag:_t key:_s value:nil];
143 - (void)startElement:(NSString *)_localName
144 namespace:(NSString *)_ns
145 rawName:(NSString *)_rawName
146 attributes:(id<SaxAttributes>)_attrs
150 [self tag:'(' key:_rawName];
152 for (i = 0, c = [_attrs count]; i < c; i++) {
153 NSString *aname, *avalue;
155 aname = [_attrs rawNameAtIndex:i];
156 avalue = [_attrs valueAtIndex:i];
158 [self tag:'A' key:aname value:avalue];
162 - (void)endElement:(NSString *)_localName
163 namespace:(NSString *)_ns
164 rawName:(NSString *)_rawName
166 [self tag:')' key:_rawName];
169 - (void)characters:(unichar *)_chars length:(int)_len {
172 if (_len == 0) return;
174 fputc('-', self->out);
176 for (i = 0; i < _len; i++) {
177 if (_chars[i] > 255) {
178 fprintf(stderr, "found unichar, code 0x%04X\n", _chars[i]);
181 register unsigned char c = _chars[i];
185 fputc('\\', self->out);
186 fputc('n', self->out);
194 fputc('\n', self->out);
197 - (void)processingInstruction:(NSString *)_pi data:(NSString *)_data {
198 [self tag:'?' key:_pi value:_data];
201 - (void)warning:(SaxParseException *)_exception {
202 NSLog(@"WARNING: %@", [_exception reason]);
204 - (void)error:(SaxParseException *)_exception {
205 NSLog(@"ERROR: %@:%@: %@",
206 [[_exception userInfo] objectForKey:@"systemId"],
207 [[_exception userInfo] objectForKey:@"line"],
208 [_exception reason]);
210 - (void)fatalError:(SaxParseException *)_exception {
214 @end /* PYXSaxHandler */