-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSVGParser.m
More file actions
514 lines (404 loc) · 14.4 KB
/
SVGParser.m
File metadata and controls
514 lines (404 loc) · 14.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
//
// SVGParser.m
// SVGKit
//
// Copyright Matt Rajca 2010-2011. All rights reserved.
//
#import "SVGParser.h"
#import <libxml/parser.h>
#import "SVGDocument.h"
@interface SVGParserStackItem : NSObject
@property(nonatomic,retain) NSObject<SVGParserExtension>* parserForThisItem;
@property(nonatomic,retain) NSObject* item;
@end
@implementation SVGParserStackItem
@synthesize item;
@synthesize parserForThisItem;
- (void) dealloc
{
self.item = nil;
self.parserForThisItem = nil;
[super dealloc];
}
@end
@implementation SVGParser
@synthesize sourceURL;
@synthesize parserExtensions;
@synthesize parseWarnings;
static xmlSAXHandler SAXHandler;
static void startElementSAX(void *ctx, const xmlChar *localname, const xmlChar *prefix, const xmlChar *URI, int nb_namespaces, const xmlChar **namespaces, int nb_attributes, int nb_defaulted, const xmlChar **attributes);
static void endElementSAX(void *ctx, const xmlChar *localname, const xmlChar *prefix, const xmlChar *URI);
static void charactersFoundSAX(void * ctx, const xmlChar * ch, int len);
static void errorEncounteredSAX(void * ctx, const char * msg, ...);
static NSString *NSStringFromLibxmlString (const xmlChar *string);
static NSMutableDictionary *NSDictionaryFromLibxmlAttributes (const xmlChar **attrs, int attr_ct);
#define READ_CHUNK_SZ 1024*10
- (id)initWithPath:(NSString *)aPath document:(SVGDocument *)document {
self = [super init];
if (self) {
self.parseWarnings = [NSMutableArray array];
self.parserExtensions = [NSMutableArray array];
_path = [aPath copy];
self.sourceURL = nil;
_document = document;
_storedChars = [NSMutableString new];
_elementStack = [NSMutableArray new];
_failed = NO;
}
return self;
}
- (id) initWithURL:(NSURL*)aURL document:(SVGDocument *)document {
self = [super init];
if( self) {
self.parseWarnings = [NSMutableArray array];
self.parserExtensions = [NSMutableArray array];
self.sourceURL = aURL;
_document = document;
_storedChars = [NSMutableString new];
_elementStack = [NSMutableArray new];
_failed = NO;
}
return self;
}
- (void)dealloc {
[_path release];
[_storedChars release];
[_elementStack release];
self.parserExtensions = nil;
[super dealloc];
}
- (BOOL)parse:(NSError **)outError {
errorForCurrentParse = nil;
[self.parseWarnings removeAllObjects];
/**
Is this file being loaded from disk?
Or from network?
*/
NSURLResponse* response;
NSData* httpData = nil;
if( self.sourceURL != nil )
{
NSURLRequest* request = [NSURLRequest requestWithURL:self.sourceURL];
NSError* error = nil;
httpData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if( error != nil )
{
NSLog( @"[%@] ERROR: failed to parse SVG from URL, because failed to download file at URL = %@, error = %@", [self class], self.sourceURL, error );
return false;
}
}
FILE *file;
if( self.sourceURL == nil )
{
const char *cPath = [_path fileSystemRepresentation];
file = fopen(cPath, "r");
if (!file)
return NO;
}
xmlParserCtxtPtr ctx = xmlCreatePushParserCtxt(&SAXHandler, self, NULL, 0, NULL);
if( self.sourceURL == nil )
{
if (!ctx) {
fclose(file);
return NO;
}
}
if( self.sourceURL == nil )
{
size_t read = 0;
char buff[READ_CHUNK_SZ];
while ((read = fread(buff, 1, READ_CHUNK_SZ, file)) > 0) {
if (xmlParseChunk(ctx, buff, read, 0) != 0) {
_failed = YES;
NSLog(@"An error occured while parsing the current XML chunk");
break;
}
}
fclose(file);
}
else
{
if (xmlParseChunk(ctx, (const char*) [httpData bytes], [httpData length], 0) != 0) {
_failed = YES;
// NSLog(@"An error occured while parsing the current XML chunk");
}
}
if (!_failed)
xmlParseChunk(ctx, NULL, 0, 1); // EOF
xmlFreeParserCtxt(ctx);
if( errorForCurrentParse != nil )
{
*outError = errorForCurrentParse;
_failed = TRUE;
}
return !_failed;
}
/** ADAM: use this for a higher-performance, *non-blocking* parse
(when someone upgrades this class and the interface to support non-blocking parse)
// Called when a chunk of data has been downloaded.
- (void)connection:(NSURLConnection *)connection
didReceiveData:(NSData *)data
{
// Process the downloaded chunk of data.
xmlParseChunk(_xmlParserContext, (const char *)[data bytes], [data length], 0);//....Getting Exception at this line.
}
*/
- (void)handleStartElement:(NSString *)name xmlns:(NSString*) prefix attributes:(NSMutableDictionary *)attributes {
for( NSObject<SVGParserExtension>* subParser in self.parserExtensions )
{
if( [[subParser supportedNamespaces] containsObject:prefix]
&& [[subParser supportedTags] containsObject:name] )
{
NSObject* subParserResult = nil;
if( nil != (subParserResult = [subParser handleStartElement:name document:_document xmlns:prefix attributes:attributes]) )
{
NSLog(@"[%@] tag: <%@:%@> -- handled by subParser: %@", [self class], prefix, name, subParser );
SVGParserStackItem* stackItem = [[[SVGParserStackItem alloc] init] autorelease];;
stackItem.parserForThisItem = subParser;
stackItem.item = subParserResult;
[_elementStack addObject:stackItem];
if ([subParser createdItemShouldStoreContent:stackItem.item]) {
[_storedChars setString:@""];
_storingChars = YES;
}
else {
_storingChars = NO;
}
return;
}
}
}
NSLog(@"[%@] ERROR: could not find a parser for tag: <%@:%@>; adding empty placeholder", [self class], prefix, name );
SVGParserStackItem* emptyItem = [[[SVGParserStackItem alloc] init] autorelease];
[_elementStack addObject:emptyItem];
}
static void startElementSAX (void *ctx, const xmlChar *localname, const xmlChar *prefix,
const xmlChar *URI, int nb_namespaces, const xmlChar **namespaces,
int nb_attributes, int nb_defaulted, const xmlChar **attributes) {
SVGParser *self = (SVGParser *) ctx;
NSString *name = NSStringFromLibxmlString(localname);
NSMutableDictionary *attrs = NSDictionaryFromLibxmlAttributes(attributes, nb_attributes);
//NSString *url = NSStringFromLibxmlString(URI);
NSString *prefix2 = nil;
if( prefix != NULL )
prefix2 = NSStringFromLibxmlString(prefix);
NSString *objcURIString = nil;
if( URI != NULL )
objcURIString = NSStringFromLibxmlString(URI);
#if DEBUG_VERBOSE_LOG_EVERY_TAG
NSLog(@"[%@] DEBUG_VERBOSE: <%@%@> (namespace URL:%@), attributes: %i", [self class], (prefix2==nil)?@"":[NSString stringWithFormat:@"%@:",prefix2], name, (URI==NULL)?@"n/a":objcURIString, nb_attributes );
#endif
#if DEBUG_VERBOSE_LOG_EVERY_TAG
if( prefix2 == nil )
{
/* The XML library allows this, although it's very unhelpful when writing application code */
/* Let's find out what namespaces DO exist... */
/*
TODO / DEVELOPER WARNING: the library says nb_namespaces is the number of elements in the array,
but it keeps returning nil pointer (not always, but often). WTF? Not sure what we're doing wrong
here, but commenting it out for now...
if( nb_namespaces > 0 )
{
for( int i=0; i<nb_namespaces; i++ )
{
NSLog(@"[%@] DEBUG: found namespace [%i] : %@", [self class], i, namespaces[i] );
}
}
else
NSLog(@"[%@] DEBUG: there are ZERO namespaces!", [self class] );
*/
}
#endif
[self handleStartElement:name xmlns:objcURIString attributes:attrs];
}
- (void)handleEndElement:(NSString *)name {
//DELETE DEBUG NSLog(@"ending element, name = %@", name);
SVGParserStackItem* stackItem = [_elementStack lastObject];
[_elementStack removeLastObject];
if( stackItem.parserForThisItem == nil )
{
/*! this was an unmatched tag - we have no parser for it, so we're pruning it from the tree */
NSLog(@"[%@] WARN: ended non-parsed tag (</%@>) - this will NOT be added to the output tree", [self class], name );
}
else
{
SVGParserStackItem* parentStackItem = [_elementStack lastObject];
NSObject<SVGParserExtension>* parserHandlingTheParentItem = parentStackItem.parserForThisItem;
if( parentStackItem.item == nil )
{
/**
Special case: we've hit the closing of the root tag.
Because each parser-extension MIGHT need to do cleanup / post-processing on the end tag,
we need to ensure that whichever class parsed the root tag gets one final callback to tell it that the end
tag has been reached
*/
parserHandlingTheParentItem = stackItem.parserForThisItem;
}
NSLog(@"[%@] DEBUG-PARSER: ended tag (</%@>): telling parser (%@) to add that item to tree-parent = %@", [self class], name, parserHandlingTheParentItem, parentStackItem.item );
[parserHandlingTheParentItem addChildObject:stackItem.item toObject:parentStackItem.item inDocument:_document];
if ( [stackItem.parserForThisItem createdItemShouldStoreContent:stackItem.item]) {
[stackItem.parserForThisItem parseContent:_storedChars forItem:stackItem.item];
[_storedChars setString:@""];
_storingChars = NO;
}
}
}
static void endElementSAX (void *ctx, const xmlChar *localname, const xmlChar *prefix, const xmlChar *URI) {
SVGParser *self = (SVGParser *) ctx;
[self handleEndElement:NSStringFromLibxmlString(localname)];
}
- (void)handleFoundCharacters:(const xmlChar *)chars length:(int)len {
if (_storingChars) {
char value[len + 1];
strncpy(value, (const char *) chars, len);
value[len] = '\0';
[_storedChars appendString:[NSString stringWithUTF8String:value]];
}
}
static void charactersFoundSAX (void *ctx, const xmlChar *chars, int len) {
SVGParser *self = (SVGParser *) ctx;
[self handleFoundCharacters:chars length:len];
}
-(void) setParseError:(NSError*) error
{
errorForCurrentParse = error;
}
-(void) addParseWarning:(NSError*) error
{
errorForCurrentParse = error;
}
- (void)handleError {
_failed = YES;
}
static void errorEncounteredSAX (void *ctx, const char *msg, ...) {
NSLog(@"Error encountered during parse: %s", msg);
[ (SVGParser *) ctx handleError];
}
static void unparsedEntityDeclaration(void * ctx,
const xmlChar * name,
const xmlChar * publicId,
const xmlChar * systemId,
const xmlChar * notationName)
{
NSLog(@"ERror: unparsed entity Decl");
}
static void structuredError (void * userData,
xmlErrorPtr error)
{
/**
XML_ERR_WARNING = 1 : A simple warning
XML_ERR_ERROR = 2 : A recoverable error
XML_ERR_FATAL = 3 : A fatal error
*/
xmlErrorLevel errorLevel = error->level;
NSError* objcError = [NSError errorWithDomain:[[NSNumber numberWithInt:error->domain] stringValue] code:error->code userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
[NSString stringWithCString:error->message encoding:NSUTF8StringEncoding], NSLocalizedDescriptionKey,
[NSNumber numberWithInt:error->line], @"lineNumber",
nil]
];
switch( errorLevel )
{
case XML_ERR_WARNING:
{
NSLog(@"Warning: parser reports: %@", objcError );
}break;
case XML_ERR_ERROR: /** FIXME: ADAM: "non-fatal" errors should be reported as warnings, but SVGDocument + this class need rewriting to return something better than "TRUE/FALSE" on parse finishing */
case XML_ERR_FATAL:
{
NSLog(@"Error: parser reports: %@", objcError );
[(SVGParser*) userData setParseError:objcError];
}
default:
break;
}
}
static xmlSAXHandler SAXHandler = {
NULL, /* internalSubset */
NULL, /* isStandalone */
NULL, /* hasInternalSubset */
NULL, /* hasExternalSubset */
NULL, /* resolveEntity */
NULL, /* getEntity */
NULL, /* entityDecl */
NULL, /* notationDecl */
NULL, /* attributeDecl */
NULL, /* elementDecl */
unparsedEntityDeclaration, /* unparsedEntityDecl */
NULL, /* setDocumentLocator */
NULL, /* startDocument */
NULL, /* endDocument */
NULL, /* startElement*/
NULL, /* endElement */
NULL, /* reference */
charactersFoundSAX, /* characters */
NULL, /* ignorableWhitespace */
NULL, /* processingInstruction */
NULL, /* comment */
NULL, /* warning */
errorEncounteredSAX, /* error */
NULL, /* fatalError //: unused error() get all the errors */
NULL, /* getParameterEntity */
NULL, /* cdataBlock */
NULL, /* externalSubset */
XML_SAX2_MAGIC,
NULL,
startElementSAX, /* startElementNs */
endElementSAX, /* endElementNs */
structuredError, /* serror */
};
#pragma mark -
#pragma mark Utility
static NSString *NSStringFromLibxmlString (const xmlChar *string) {
return [NSString stringWithUTF8String:(const char *) string];
}
static NSMutableDictionary *NSDictionaryFromLibxmlAttributes (const xmlChar **attrs, int attr_ct) {
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
for (int i = 0; i < attr_ct * 5; i += 5) {
const char *begin = (const char *) attrs[i + 3];
const char *end = (const char *) attrs[i + 4];
int vlen = strlen(begin) - strlen(end);
char val[vlen + 1];
strncpy(val, begin, vlen);
val[vlen] = '\0';
[dict setObject:[NSString stringWithUTF8String:val]
forKey:NSStringFromLibxmlString(attrs[i])];
}
return [dict autorelease];
}
#define MAX_ACCUM 256
#define MAX_NAME 256
+(NSDictionary *) NSDictionaryFromCSSAttributes: (NSString *)css {
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
const char *cstr = [css UTF8String];
size_t len = strlen(cstr);
char name[MAX_NAME];
bzero(name, MAX_NAME);
char accum[MAX_ACCUM];
bzero(accum, MAX_ACCUM);
size_t accumIdx = 0;
for (size_t n = 0; n <= len; n++) {
char c = cstr[n];
if (c == '\n' || c == '\t' || c == ' ') {
continue;
}
if (c == ':') {
strcpy(name, accum);
name[accumIdx] = '\0';
bzero(accum, MAX_ACCUM);
accumIdx = 0;
continue;
}
else if (c == ';' || c == '\0') {
accum[accumIdx] = '\0';
[dict setObject:[NSString stringWithUTF8String:accum]
forKey:[NSString stringWithUTF8String:name]];
bzero(name, MAX_NAME);
bzero(accum, MAX_ACCUM);
accumIdx = 0;
continue;
}
accum[accumIdx++] = c;
}
return [dict autorelease];
}
@end