Commit 305e5250 authored by PhoeniX's avatar PhoeniX

XML parser without NSScanner

parent 125ceff6
......@@ -9,8 +9,8 @@
#import <UIKit/UIKit.h>
@interface pxSVGImage : NSObject
+ (instancetype) svgImageWithXML:(NSString*)data;
- (instancetype) initWithXML:(NSString*)data;
+ (instancetype) svgImageWithXML:(NSData*)data;
- (instancetype) initWithXML:(NSData*)data;
@property (nonatomic,readonly) CGRect bounds;
- (CALayer*)makeLayer;
@end
......@@ -15,14 +15,14 @@
@end
@implementation pxSVGImage
+ (instancetype)svgImageWithXML:(NSString *)xml
+ (instancetype)svgImageWithXML:(NSData *)xml
{
return [[self alloc] initWithXML:xml];
}
- (instancetype)initWithXML:(NSString *)xml
- (instancetype)initWithXML:(NSData *)xml
{
pxXMLNode *xmlTree =
[[pxXMLNode parseTree:[[NSScanner alloc] initWithString:xml]]
[[pxXMLNode parseTree:xml]
filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"tagName=%@",@"svg"]]
.firstObject;
if (!xmlTree) return nil;
......
......@@ -72,21 +72,20 @@
err = [NSError errorWithDomain:@"pxSVGLoader.httpStatus" code:((NSHTTPURLResponse*)resp).statusCode userInfo:nil];
if (!err && !data)
err = [NSError errorWithDomain:@"pxSVGLoader.emptyData" code:0 userInfo:nil];
NSString *str = data?[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]:nil;
data = nil, resp = nil;
resp = nil;
if ([op isCancelled]) {
op = nil, str = nil, err = nil;
op = nil, data = nil, err = nil;
return;
}
op = nil;
if (err) {
str = nil;
data = nil;
[weakself performSelectorOnMainThread:@selector(loadError:) withObject:err waitUntilDone:NO modes:@[NSRunLoopCommonModes]];
err = nil;
return;
}
[weakself performSelectorOnMainThread:@selector(loadString:) withObject:str waitUntilDone:NO modes:@[NSRunLoopCommonModes]];
str = nil, err = nil;
[weakself performSelectorOnMainThread:@selector(loadData:) withObject:data waitUntilDone:NO modes:@[NSRunLoopCommonModes]];
data = nil, err = nil;
} }];
op.name = url.absoluteString;
op.threadPriority = 0.1f;
......@@ -94,16 +93,11 @@
}
- (void)loadData:(NSData *)data
{
[self loadString:[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]];
}
- (void)loadString:(NSString *)string
{
[self clean];
__weak pxSVGLayer *weakself = self;
__block NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{ @autoreleasepool {
pxSVGImage *img = [pxSVGImage svgImageWithXML:string];
pxSVGImage *img = [pxSVGImage svgImageWithXML:data];
if ([op isCancelled]) {
op = nil, img = nil;
return;
......@@ -121,6 +115,11 @@
[[self.class parseQueue] addOperation:self.parseOperation=op];
}
- (void)loadString:(NSString *)string
{
[self loadData:[string dataUsingEncoding:NSUTF8StringEncoding]];
}
- (void)loadImage:(pxSVGImage*)image
{
[self clean];
......
......@@ -9,8 +9,7 @@
#import <Foundation/Foundation.h>
@interface pxXMLNode : NSObject
+ (NSArray*) parseTree:(NSScanner*)scanner;
+ (instancetype) parseNode:(NSString*)string childScanner:(NSScanner*)childScanner;
+ (NSArray*) parseTree:(NSData*)data;
@property (readonly) NSString *tagName;
@property (readonly) NSDictionary *attributes;
@property (readonly) NSArray *childNodes;
......
......@@ -16,47 +16,70 @@
@implementation pxXMLNode
+ (NSArray *)parseTree:(NSScanner *)scanner
+ (NSArray *)parseTree:(NSData *)data
{
NSMutableArray *nodes = [NSMutableArray new];
NSString *tag;
pxXMLNode *node;
while (!scanner.isAtEnd) {
[scanner scanUpToString:@"<" intoString:nil];
[scanner scanUpToString:@">" intoString:&tag];
tag = [tag stringByAppendingString:@">"]; [scanner scanString:@">" intoString:nil];
if ([tag hasPrefix:@"</"]) break;
if ([tag characterAtIndex:1] == '?') continue;
if ([tag characterAtIndex:1] == '!') continue;
node = [self parseNode:tag childScanner:scanner];
[nodes addObject:node];
}
return [NSArray arrayWithArray:nodes];
}
+ (instancetype)parseNode:(NSString *)string childScanner:(NSScanner *)childScanner
{
NSCharacterSet *seps = [NSCharacterSet whitespaceAndNewlineCharacterSet];
NSScanner *scan = [[NSScanner alloc] initWithString:string];
[scan scanString:@"<" intoString:nil];
static NSCharacterSet *seps; if (!seps) seps = [NSCharacterSet whitespaceAndNewlineCharacterSet];
NSMutableDictionary *attrs = [[NSMutableDictionary alloc] initWithCapacity:100];
NSMutableArray *nodes = [[NSMutableArray alloc] initWithCapacity:100];
NSMutableArray *nodeStack = [[NSMutableArray alloc] initWithCapacity:100];
NSString *tagName, *attrName, *attrValue;
NSMutableDictionary *attrs = [NSMutableDictionary new];
[scan scanUpToCharactersFromSet:seps intoString:&tagName]; [scan scanCharactersFromSet:seps intoString:nil];
while ([scan scanUpToString:@"=" intoString:&attrName]) {
if(![scan scanString:@"=\"" intoString:nil]) break;
[scan scanUpToString:@"\"" intoString:&attrValue];
[scan scanString:@"\"" intoString:nil];
[scan scanCharactersFromSet:seps intoString:nil];
[attrs setObject:attrValue?:@"" forKey:attrName];
pxXMLNode *node;
NSUInteger idx = 0, tidx;
const char* bytes = data.bytes;
NSRange tagRange, attrRange;
while (idx < data.length) {
while ((idx < data.length) && (bytes[idx] != '<')) idx++;
tagRange.location = idx;
while ((idx < data.length) && (bytes[idx] != '>')) idx++;
tagRange.length = idx-tagRange.location;
idx++;
if (tagRange.length < 2) break;
if (bytes[tagRange.location+1] == '/') {
if (nodeStack.count == 0) break;
nodes = nodeStack.lastObject;
[nodeStack removeLastObject];
continue;
}
if (bytes[tagRange.location+1] == '?') continue;
if (bytes[tagRange.location+1] == '!') continue;
{
tidx = 1;
attrRange.location = 1;
[attrs removeAllObjects];
while ((tidx < tagRange.length) && ![seps characterIsMember:bytes[tagRange.location+tidx]]) tidx++;
attrRange.length = tidx-attrRange.location;
tagName = [[NSString alloc] initWithBytes:&bytes[tagRange.location+attrRange.location] length:attrRange.length encoding:NSUTF8StringEncoding];
while ((tidx < tagRange.length) && [seps characterIsMember:bytes[tagRange.location+tidx]]) tidx++;
while (tidx < tagRange.length) {
attrRange.location = tidx;
while ((tidx < tagRange.length) && (bytes[tagRange.location+tidx] != '=') && (bytes[tagRange.location+tidx] != '/')) tidx++;
attrRange.length = tidx-attrRange.location;
if (attrRange.length == 0) break;
attrName = [[NSString alloc] initWithBytes:&bytes[tagRange.location+attrRange.location] length:attrRange.length encoding:NSUTF8StringEncoding];
if (tagRange.length-idx < 2) break;
if ((bytes[tagRange.location+tidx] != '=') || (bytes[tagRange.location+tidx+1] != '"')) break;
tidx += 2;
attrRange.location = tidx;
while ((tidx < tagRange.length) && (bytes[tagRange.location+tidx] != '"')) tidx++;
attrRange.length = tidx-attrRange.location;
attrValue = [[NSString alloc] initWithBytes:&bytes[tagRange.location+attrRange.location] length:attrRange.length encoding:NSUTF8StringEncoding];
tidx++;
while ((tidx < tagRange.length) && [seps characterIsMember:bytes[tagRange.location+tidx]]) tidx++;
[attrs setObject:attrValue?:@"" forKey:attrName];
}
BOOL selfClose = bytes[tagRange.location+tagRange.length-1] == '/';
node = [self new];
node.tagName = tagName;
node.attributes = [NSDictionary dictionaryWithDictionary:attrs];
[nodes addObject:node];
if (selfClose) node.childNodes = @[];
else {
[nodeStack addObject:nodes];
node.childNodes = nodes = [[NSMutableArray alloc] initWithCapacity:100];
}
}
}
if ([tagName hasSuffix:@">"]) tagName = [tagName substringToIndex:tagName.length-1];
BOOL selfClose = [[attrName stringByTrimmingCharactersInSet:seps] hasPrefix:@"/"];
pxXMLNode *node = [self new];
node.tagName = tagName;
node.attributes = attrs;
node.childNodes = selfClose?@[]:[self parseTree:childScanner];
return node;
return (nodeStack.count>0)?nodeStack.firstObject:[NSArray arrayWithArray:nodes];
}
- (NSString *)description
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment