]> err.no Git - sope/blob - sope-xml/samples/saxxml.m
added a select..X method which doesn't raise NSExceptions,
[sope] / sope-xml / samples / saxxml.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   Usage:
28
29     saxxml -XMLReader libxmlHTMLSAXDriver test.html
30 */
31
32 @interface MySAXHandler : SaxDefaultHandler
33 {
34   id  locator;
35   int indent;
36 }
37
38 - (void)indent;
39
40 @end
41
42 int main(int argc, char **argv, char **env) {
43   id<NSObject,SaxXMLReader> parser;
44   id           sax;
45   NSEnumerator *paths;
46   NSString     *path;
47   NSAutoreleasePool *pool;
48   NSString          *cwd;
49   
50   pool   = [[NSAutoreleasePool alloc] init];
51 #if LIB_FOUNDATION_LIBRARY
52   [NSProcessInfo initializeWithArguments:argv count:argc environment:env];
53 #endif
54   
55   parser = [[SaxXMLReaderFactory standardXMLReaderFactory] createXMLReader];
56   cwd    = [[NSFileManager defaultManager] currentDirectoryPath];
57   
58   if (parser == nil) {
59     fprintf(stderr, "could not load a SAX driver bundle !\n");
60     exit(2);
61   }
62   
63   sax = [[MySAXHandler alloc] init];
64   [parser setContentHandler:sax];
65   [parser setDTDHandler:sax];
66   [parser setErrorHandler:sax];
67   
68   [parser setProperty:@"http://xml.org/sax/properties/declaration-handler"
69           to:sax];
70 #if 0
71   [parser setProperty:@"http://xml.org/sax/properties/lexical-handler"
72           to:sax];
73 #endif
74   
75   /* parse */
76
77   paths = [[[NSProcessInfo processInfo] arguments] objectEnumerator];
78   [paths nextObject];
79   while ((path = [paths nextObject])) {
80     NSAutoreleasePool *pool;
81
82     if ([path hasPrefix:@"-"]) { /* consume defaults */
83       [paths nextObject];
84       continue;
85     }
86     
87     pool = [[NSAutoreleasePool alloc] init];
88     
89     if (![path isAbsolutePath])
90       path = [cwd stringByAppendingPathComponent:path];
91     
92     path = [@"file://" stringByAppendingString:path];
93     
94     NS_DURING
95       [parser parseFromSystemId:path];
96     NS_HANDLER
97       abort();
98     NS_ENDHANDLER;
99     
100     [pool release];
101   }
102   
103   /* cleanup */
104   
105   [sax release];
106   //[parser release];
107
108   [pool release];
109
110   exit(0);
111   return 0;
112 }
113
114 @implementation MySAXHandler
115
116 - (void)indent {
117   int i;
118   
119   for (i = 0; i < (self->indent * 4); i++)
120     fputc(' ', stdout);
121 }
122
123 @end /* MySAXHandler */
124
125 @implementation MySAXHandler(Documents)
126
127 - (void)dealloc {
128   [self->locator release];
129   [super dealloc];
130 }
131
132 - (void)setDocumentLocator:(id<NSObject,SaxLocator>)_loc {
133   [self->locator autorelease];
134   self->locator = [_loc retain];
135 }
136
137 - (void)startDocument {
138   puts("start document ..");
139   self->indent++;
140 }
141 - (void)endDocument {
142   self->indent--;
143   puts("end document.");
144 }
145
146 - (void)startPrefixMapping:(NSString *)_prefix uri:(NSString *)_uri {
147   [self indent];
148   printf("ns-map: %s=%s\n", [_prefix cString], [_uri cString]);
149 }
150 - (void)endPrefixMapping:(NSString *)_prefix {
151   [self indent];
152   printf("ns-unmap: %s\n", [_prefix cString]);
153 }
154
155 - (void)startElement:(NSString *)_localName
156   namespace:(NSString *)_ns
157   rawName:(NSString *)_rawName
158   attributes:(id<SaxAttributes>)_attrs
159 {
160   int i, c;
161   [self indent];
162   printf("<%s", [_localName cString]);
163   
164   if ([_ns length] > 0)
165     printf(" (ns=%s)", [_ns cString]);
166   
167   for (i = 0, c = [_attrs count]; i < c; i++) {
168     NSString *type;
169     
170     printf(" %s=\"%s\"",
171            [[_attrs nameAtIndex:i] cString],
172            [[_attrs valueAtIndex:i] cString]);
173
174     if (![_ns isEqualToString:[_attrs uriAtIndex:i]])
175       printf("(ns=%s)", [[_attrs uriAtIndex:i] cString]);
176     
177     type = [_attrs typeAtIndex:i];
178     if (![type isEqualToString:@"CDATA"] && (type != nil))
179       printf("[%s]", [type cString]);
180   }
181   puts(">");
182   self->indent++;
183 }
184 - (void)endElement:(NSString *)_localName
185   namespace:(NSString *)_ns
186   rawName:(NSString *)_rawName
187 {
188   self->indent--;
189   [self indent];
190   printf("</%s>\n", [_localName cString]);
191 }
192
193 - (void)characters:(unichar *)_chars length:(int)_len {
194   NSString *str;
195   id tmp;
196   unsigned i, len;
197
198   if (_len == 0) {
199     [self indent];
200     printf("\"\"\n");
201     return;
202   }
203   
204   for (i = 0; i < (unsigned)_len; i++) {
205     if (_chars[i] > 255) {
206       NSLog(@"detected large char: o%04o d%03i h%04X",
207             _chars[i], _chars[i], _chars[i]);
208     }
209   }
210   
211   str = [NSString stringWithCharacters:_chars length:_len];
212   len = [str length];
213   
214   tmp = [str componentsSeparatedByString:@"\n"];
215   str = [tmp componentsJoinedByString:@"\\n"];
216   tmp = [str componentsSeparatedByString:@"\r"];
217   str = [tmp componentsJoinedByString:@"\\r"];
218   
219   [self indent];
220   printf("\"%s\"\n", [str cString]);
221 }
222 - (void)ignorableWhitespace:(unichar *)_chars length:(int)_len {
223   NSString *data;
224   id tmp;
225
226   data = [NSString stringWithCharacters:_chars length:_len];
227   tmp  = [data componentsSeparatedByString:@"\n"];
228   data = [tmp componentsJoinedByString:@"\\n"];
229   tmp  = [data componentsSeparatedByString:@"\r"];
230   data = [tmp componentsJoinedByString:@"\\r"];
231   
232   [self indent];
233   printf("whitespace: \"%s\"\n", [data cString]);
234 }
235
236 - (void)processingInstruction:(NSString *)_pi data:(NSString *)_data {
237   [self indent];
238   printf("PI: '%s' '%s'\n", [_pi cString], [_data cString]);
239 }
240
241 #if 0
242 - (xmlEntityPtr)getEntity:(NSString *)_name {
243   NSLog(@"get entity %@", _name);
244   return NULL;
245 }
246 - (xmlEntityPtr)getParameterEntity:(NSString *)_name {
247   NSLog(@"get para entity %@", _name);
248   return NULL;
249 }
250 #endif
251
252 @end /* MySAXHandler(Documents) */
253
254 @implementation MySAXHandler(EntityResolver)
255
256 - (id)resolveEntityWithPublicId:(NSString *)_pubId
257   systemId:(NSString *)_sysId
258 {
259   [self indent];
260   printf("shall resolve entity with '%s' '%s'",
261          [_pubId cString], [_sysId cString]);
262   return nil;
263 }
264
265 @end /* MySAXHandler(EntityResolver) */
266
267 @implementation MySAXHandler(Errors)
268
269 - (void)warning:(SaxParseException *)_exception {
270   NSLog(@"warning(%@:%i): %@",
271         [[_exception userInfo] objectForKey:@"publicId"],
272         [[[_exception userInfo] objectForKey:@"line"] intValue],
273         [_exception reason]);
274 }
275
276 - (void)error:(SaxParseException *)_exception {
277   NSLog(@"error(%@:%i): %@",
278         [[_exception userInfo] objectForKey:@"publicId"],
279         [[[_exception userInfo] objectForKey:@"line"] intValue],
280         [_exception reason]);
281 }
282
283 - (void)fatalError:(SaxParseException *)_exception {
284   NSLog(@"fatal error(%@:%i): %@",
285         [[_exception userInfo] objectForKey:@"publicId"],
286         [[[_exception userInfo] objectForKey:@"line"] intValue],
287         [_exception reason]);
288   [_exception raise];
289 }
290
291 @end /* MySAXHandler(Errors) */
292
293 @implementation MySAXHandler(DTD)
294
295 - (void)notationDeclaration:(NSString *)_name
296   publicId:(NSString *)_pubId
297   systemId:(NSString *)_sysId
298 {
299   NSLog(@"decl: notation %@ pub=%@ sys=%@", _name, _pubId, _sysId);
300 }
301
302 - (void)unparsedEntityDeclaration:(NSString *)_name
303   publicId:(NSString *)_pubId
304   systemId:(NSString *)_sysId
305   notationName:(NSString *)_notName
306 {
307   NSLog(@"decl: unparsed entity %@ pub=%@ sys=%@ not=%@",
308         _name, _pubId, _sysId, _notName);
309 }
310
311 @end /* MySAXHandler(DTD) */
312
313 @implementation MySAXHandler(Decl)
314
315 - (void)attributeDeclaration:(NSString *)_attributeName
316   elementName:(NSString *)_elementName
317   type:(NSString *)_type
318   defaultType:(NSString *)_defType
319   defaultValue:(NSString *)_defValue
320 {
321   NSLog(@"decl: attr %@[%@] type '%@' default '%@'[%@]",
322         _attributeName, _elementName, _type, _defValue, _defType);
323 }
324
325 - (void)elementDeclaration:(NSString *)_name contentModel:(NSString *)_model {
326   NSLog(@"decl: element %@ model %@", _name, _model);
327 }
328
329 - (void)externalEntityDeclaration:(NSString *)_name
330   publicId:(NSString *)_pub
331   systemId:(NSString *)_sys
332 {
333   NSLog(@"decl: e-entity %@ pub %@ sys %@", _name, _pub, _sys);
334 }
335
336 - (void)internalEntityDeclaration:(NSString *)_name value:(NSString *)_value {
337   NSLog(@"decl: i-entity %@ value %@", _name, _value);
338 }
339
340 @end /* MySAXHandler(Decl) */