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