XMLParseManager.m 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //
  2. // XMLParseManager.m
  3. // XMLParseDome
  4. //
  5. // Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved.
  6. //
  7. // THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  8. // AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  9. // UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  10. // This notice may not be removed from this file.
  11. //
  12. #import "XMLParseManager.h"
  13. #define dispatch_main_safe_async(block) if ([NSThread isMainThread]) {\
  14. block();\
  15. }else{\
  16. dispatch_async(dispatch_get_main_queue(), ^{\
  17. block();\
  18. });\
  19. }
  20. @interface XMLParseManager()<NSXMLParserDelegate>
  21. @property (nonatomic, strong) NSXMLParser *parser;
  22. @property(nonatomic,copy)ParseXMLCompletion block;
  23. @property (nonatomic, strong) XPDXMLElement *rootElement;
  24. @property (nonatomic, strong) XPDXMLElement *currentElement;
  25. @end
  26. @implementation XMLParseManager
  27. -(void)parseXMLWithURL:(NSURL *)url completion:(ParseXMLCompletion)block{
  28. if (url) {
  29. self.block=block;
  30. __weak typeof(self) weakSelf = self;
  31. NSInputStream *fileInput=[[NSInputStream alloc]initWithURL:url];
  32. _parser = [[NSXMLParser alloc]initWithStream:fileInput];
  33. _parser.delegate = self;
  34. __strong typeof(weakSelf) strongSelf = weakSelf;
  35. [strongSelf.parser parse];
  36. if (weakSelf.parser.parserError) {
  37. dispatch_main_safe_async(^{
  38. if (block) {
  39. block(NO,nil,strongSelf.parser.parserError);
  40. }
  41. });
  42. }
  43. }
  44. }
  45. -(void)parserDidStartDocument:(NSXMLParser *)parser{
  46. self.rootElement=nil;
  47. self.currentElement = nil;
  48. }
  49. -(void)parserDidEndDocument:(NSXMLParser *)parser{
  50. __weak typeof(self) weakSelf = self;
  51. if (self&&self.block) {
  52. dispatch_main_safe_async(^{
  53. __strong typeof(weakSelf) strongSelf = weakSelf;
  54. strongSelf.block(YES, strongSelf.rootElement, nil);
  55. });
  56. }
  57. }
  58. -(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary<NSString *,NSString *> *)attributeDict{
  59. if (!self.rootElement) {
  60. self.rootElement = [[XPDXMLElement alloc]init];
  61. self.currentElement = self.rootElement;
  62. }else{
  63. XPDXMLElement *element = [[XPDXMLElement alloc]init];
  64. element.parent = self.currentElement;
  65. [self.currentElement.childElement addObject:element];
  66. element.name = elementName;
  67. self.currentElement = element;
  68. }
  69. self.currentElement.name = elementName;
  70. self.currentElement.attribute = attributeDict;
  71. }
  72. - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
  73. {
  74. [self.currentElement.text appendString:string];
  75. }
  76. -(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
  77. self.currentElement = self.currentElement.parent;
  78. }
  79. - (void)parser:(NSXMLParser *)parser validationErrorOccurred:(NSError *)validationError{
  80. if (self.block) {
  81. __weak typeof(self) weakSelf = self;
  82. dispatch_main_safe_async(^{
  83. __strong typeof(weakSelf) strongSelf = weakSelf;
  84. strongSelf.block(NO, nil,validationError);
  85. });
  86. }
  87. }
  88. @end