// // XMLParseManager.m // XMLParseDome // // Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved. // // THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW // AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT. // UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES. // This notice may not be removed from this file. // #import "XMLParseManager.h" #define dispatch_main_safe_async(block) if ([NSThread isMainThread]) {\ block();\ }else{\ dispatch_async(dispatch_get_main_queue(), ^{\ block();\ });\ } @interface XMLParseManager() @property (nonatomic, strong) NSXMLParser *parser; @property(nonatomic,copy)ParseXMLCompletion block; @property (nonatomic, strong) XPDXMLElement *rootElement; @property (nonatomic, strong) XPDXMLElement *currentElement; @end @implementation XMLParseManager -(void)parseXMLWithURL:(NSURL *)url completion:(ParseXMLCompletion)block{ if (url) { self.block=block; __weak typeof(self) weakSelf = self; NSInputStream *fileInput=[[NSInputStream alloc]initWithURL:url]; _parser = [[NSXMLParser alloc]initWithStream:fileInput]; _parser.delegate = self; __strong typeof(weakSelf) strongSelf = weakSelf; [strongSelf.parser parse]; if (weakSelf.parser.parserError) { dispatch_main_safe_async(^{ if (block) { block(NO,nil,strongSelf.parser.parserError); } }); } } } -(void)parserDidStartDocument:(NSXMLParser *)parser{ self.rootElement=nil; self.currentElement = nil; } -(void)parserDidEndDocument:(NSXMLParser *)parser{ __weak typeof(self) weakSelf = self; if (self&&self.block) { dispatch_main_safe_async(^{ __strong typeof(weakSelf) strongSelf = weakSelf; strongSelf.block(YES, strongSelf.rootElement, nil); }); } } -(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{ if (!self.rootElement) { self.rootElement = [[XPDXMLElement alloc]init]; self.currentElement = self.rootElement; }else{ XPDXMLElement *element = [[XPDXMLElement alloc]init]; element.parent = self.currentElement; [self.currentElement.childElement addObject:element]; element.name = elementName; self.currentElement = element; } self.currentElement.name = elementName; self.currentElement.attribute = attributeDict; } - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { [self.currentElement.text appendString:string]; } -(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{ self.currentElement = self.currentElement.parent; } - (void)parser:(NSXMLParser *)parser validationErrorOccurred:(NSError *)validationError{ if (self.block) { __weak typeof(self) weakSelf = self; dispatch_main_safe_async(^{ __strong typeof(weakSelf) strongSelf = weakSelf; strongSelf.block(NO, nil,validationError); }); } } @end