2 Copyright (C) 2005 Helge Hess
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 // Note: this does not yet produce valid XML output
24 #import <Foundation/NSObject.h>
25 #include <SaxObjC/SaxObjC.h>
27 @interface vcf2xml : NSObject
29 id<NSObject,SaxXMLReader> parser;
33 - (int)runWithArguments:(NSArray *)_args;
39 @interface MySAXHandler : SaxDefaultHandler
51 @implementation vcf2xml
54 if ((self = [super init]) != nil) {
55 self->parser = [[[SaxXMLReaderFactory standardXMLReaderFactory]
56 createXMLReaderForMimeType:@"text/x-vcard"] retain];
58 fprintf(stderr, "Error: could not load a vCard SAX driver bundle!\n");
61 //NSLog(@"Using parser: %@", self->parser);
63 self->sax = [[MySAXHandler alloc] init];
64 [parser setContentHandler:self->sax];
65 [parser setErrorHandler:self->sax];
72 [self->parser release];
78 - (void)processFile:(NSString *)_path {
79 [self->parser parseFromSystemId:_path];
84 - (NSException *)handleException:(NSException *)_exc onPath:(NSString *)_p {
85 fprintf(stderr, "Error: catched exception on path '%s': %s\n",
86 [_p cString], [[_exc description] cString]);
92 - (int)runWithArguments:(NSArray *)_args {
96 /* begin processing */
98 args = [_args objectEnumerator];
99 [args nextObject]; // skip tool name ...
101 while ((arg = [args nextObject]) != nil) {
102 NSAutoreleasePool *pool2;
104 if ([arg hasPrefix:@"-"]) { /* consume defaults */
109 pool2 = [[NSAutoreleasePool alloc] init];
112 if (![arg isAbsolutePath]) {
113 arg = [[[NSFileManager defaultManager] currentDirectoryPath]
114 stringByAppendingPathComponent:arg];
118 [self->parser parseFromSystemId:arg];
120 [[self handleException:localException onPath:arg] raise];
131 @implementation MySAXHandler
134 [self->lastNS release];
135 [self->locator release];
144 for (i = 0; i < (self->indent * 4); i++)
150 - (void)setDocumentLocator:(id<NSObject,SaxLocator>)_loc {
151 [self->locator autorelease];
152 self->locator = [_loc retain];
155 - (void)startDocument {
156 //puts("start document ..");
159 - (void)endDocument {
161 //puts("end document.");
164 - (void)startPrefixMapping:(NSString *)_prefix uri:(NSString *)_uri {
166 //printf("ns-map: %s=%s\n", [_prefix cString], [_uri cString]);
168 - (void)endPrefixMapping:(NSString *)_prefix {
170 //printf("ns-unmap: %s\n", [_prefix cString]);
173 - (void)startElement:(NSString *)_localName
174 namespace:(NSString *)_ns
175 rawName:(NSString *)_rawName
176 attributes:(id<SaxAttributes>)_attrs
180 printf("<%s", [_localName cString]);
182 if ([_ns length] > 0) {
183 if ([_ns isEqualToString:self->lastNS])
186 printf(" xmlns='%s'", [_ns cString]);
187 ASSIGNCOPY(self->lastNS, _ns);
191 for (i = 0, c = [_attrs count]; i < c; i++) {
195 ans = [_attrs uriAtIndex:i];
198 [[_attrs nameAtIndex:i] cString],
199 [[_attrs valueAtIndex:i] cString]);
201 if (![_ns isEqualToString:ans])
202 printf("(ns=%s)", [ans cString]);
204 type = [_attrs typeAtIndex:i];
205 if (![type isEqualToString:@"CDATA"] && (type != nil))
206 printf("[%s]", [type cString]);
211 - (void)endElement:(NSString *)_localName
212 namespace:(NSString *)_ns
213 rawName:(NSString *)_rawName
217 printf("</%s>\n", [_localName cString]);
220 - (void)characters:(unichar *)_chars length:(int)_len {
231 for (i = 0; i < (unsigned)_len; i++) {
232 if (_chars[i] > 255) {
233 NSLog(@"detected large char: o%04o d%03i h%04X",
234 _chars[i], _chars[i], _chars[i]);
238 str = [NSString stringWithCharacters:_chars length:_len];
241 tmp = [str componentsSeparatedByString:@"\n"];
242 str = [tmp componentsJoinedByString:@"\\n"];
243 tmp = [str componentsSeparatedByString:@"\r"];
244 str = [tmp componentsJoinedByString:@"\\r"];
247 printf("\"%s\"\n", [str cString]);
249 - (void)ignorableWhitespace:(unichar *)_chars length:(int)_len {
253 data = [NSString stringWithCharacters:_chars length:_len];
254 tmp = [data componentsSeparatedByString:@"\n"];
255 data = [tmp componentsJoinedByString:@"\\n"];
256 tmp = [data componentsSeparatedByString:@"\r"];
257 data = [tmp componentsJoinedByString:@"\\r"];
260 printf("whitespace: \"%s\"\n", [data cString]);
263 - (void)processingInstruction:(NSString *)_pi data:(NSString *)_data {
265 printf("PI: '%s' '%s'\n", [_pi cString], [_data cString]);
269 - (xmlEntityPtr)getEntity:(NSString *)_name {
270 NSLog(@"get entity %@", _name);
273 - (xmlEntityPtr)getParameterEntity:(NSString *)_name {
274 NSLog(@"get para entity %@", _name);
281 - (id)resolveEntityWithPublicId:(NSString *)_pubId
282 systemId:(NSString *)_sysId
285 printf("shall resolve entity with '%s' '%s'",
286 [_pubId cString], [_sysId cString]);
292 - (void)warning:(SaxParseException *)_exception {
293 NSLog(@"warning(%@:%i): %@",
294 [[_exception userInfo] objectForKey:@"publicId"],
295 [[[_exception userInfo] objectForKey:@"line"] intValue],
296 [_exception reason]);
299 - (void)error:(SaxParseException *)_exception {
300 NSLog(@"error(%@:%i): %@",
301 [[_exception userInfo] objectForKey:@"publicId"],
302 [[[_exception userInfo] objectForKey:@"line"] intValue],
303 [_exception reason]);
306 - (void)fatalError:(SaxParseException *)_exception {
307 NSLog(@"fatal error(%@:%i): %@",
308 [[_exception userInfo] objectForKey:@"publicId"],
309 [[[_exception userInfo] objectForKey:@"line"] intValue],
310 [_exception reason]);
314 @end /* MySAXHandler */
318 int main(int argc, char **argv, char **env) {
319 NSAutoreleasePool *pool;
323 pool = [[NSAutoreleasePool alloc] init];
324 #if LIB_FOUNDATION_LIBRARY
325 [NSProcessInfo initializeWithArguments:argv count:argc environment:env];
328 if ((tool = [[vcf2xml alloc] init])) {
330 rc = [tool runWithArguments:[[NSProcessInfo processInfo] arguments]];