Procházet zdrojové kódy

PDFViewer(iOS) - 移动XML到指定位置

xiaochen před 1 rokem
rodič
revize
f82e69990b

+ 25 - 0
SDKLicense/XMLParseManager.h

@@ -0,0 +1,25 @@
+//
+//  XMLParseManager.h
+//  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 <Foundation/Foundation.h>
+
+#import "XPDXMLElement.h"
+
+@interface XMLParseManager : NSObject
+
+typedef void(^ParseXMLCompletion)(BOOL SUC,XPDXMLElement *data,NSError *error);
+
+- (void)parseXMLWithURL:(NSURL *)url completion:(ParseXMLCompletion)block;
+
+@end
+
+

+ 103 - 0
SDKLicense/XMLParseManager.m

@@ -0,0 +1,103 @@
+//
+//  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()<NSXMLParserDelegate>
+@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<NSString *,NSString *> *)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

+ 32 - 0
SDKLicense/XPDXMLElement.h

@@ -0,0 +1,32 @@
+//
+//  XPDXMLElement.h
+//  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 <Foundation/Foundation.h>
+
+NS_ASSUME_NONNULL_BEGIN
+
+@interface XPDXMLElement : NSObject
+
+@property (nonatomic, strong) NSString *name;
+
+@property (nonatomic, strong) NSMutableString *text;
+
+@property (nonatomic, strong) NSDictionary *attribute;
+
+@property (nonatomic, strong) XPDXMLElement *parent;
+
+@property (nonatomic, strong) NSMutableArray<XPDXMLElement *> *childElement;
+
+-(NSDictionary *)convertToDic;
+@end
+
+NS_ASSUME_NONNULL_END

+ 84 - 0
SDKLicense/XPDXMLElement.m

@@ -0,0 +1,84 @@
+//
+//  XPDXMLElement.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 "XPDXMLElement.h"
+
+@implementation XPDXMLElement
+- (instancetype)init
+{
+    self = [super init];
+    if (self) {
+        _text=[[NSMutableString alloc]init];
+    }
+    return self;
+}
+-(NSMutableArray<XPDXMLElement *> *)childElement{
+    if (!_childElement) {
+        _childElement = [[NSMutableArray alloc]init];
+    }
+    return _childElement;
+}
+-(NSString *)description{
+    
+    NSDictionary *dic = [self convertToDic];
+  
+    
+    if ([NSJSONSerialization isValidJSONObject:dic]) {
+        NSError *error;
+        NSData *jsonData=[NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&error];
+        if (error) {
+            return @"{}";
+        }
+        if (jsonData) {
+            @try {
+                NSString *jsonStr = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
+                return jsonStr;
+            } @catch (NSException *exception) {
+                
+            }
+        }
+    }
+    
+    return @"{}";
+    
+}
+-(NSDictionary *)convertToDic{
+    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
+    if (self.name) {
+        
+        dic[self.name]=self.text;
+    }
+    if (self.attribute&&self.attribute.count>0) {
+        [dic addEntriesFromDictionary:self.attribute];
+    }
+    if (self.childElement&&self.childElement.count>0) {
+        NSMutableArray *child=[[NSMutableArray alloc]init];
+        for (XPDXMLElement *element in self.childElement) {
+            NSDictionary *childDic = [element convertToDic];
+            if (element.childElement&&element.childElement.count>0) {
+                [child addObject:childDic];
+            }else{
+                [dic addEntriesFromDictionary:childDic];
+            }
+            
+            
+            
+        }
+        if (child.count>0) {
+            dic[@"child"]=child;
+        }
+        
+        
+    }
+    return dic;
+}
+@end

+ 12 - 12
viewer-ctrl-demo/viewer-ctrl-demo.xcodeproj/project.pbxproj

@@ -8,8 +8,8 @@
 
 /* Begin PBXBuildFile section */
 		4FBCB52629E817B900F86483 /* CPDFMoreListViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 4FBCB52429E817B900F86483 /* CPDFMoreListViewController.m */; };
-		4FD5D46329E65C82004BE40B /* XMLParseManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 4FD5D46229E65C82004BE40B /* XMLParseManager.m */; };
-		4FD5D46629E65CA9004BE40B /* XPDXMLElement.m in Sources */ = {isa = PBXBuildFile; fileRef = 4FD5D46529E65CA9004BE40B /* XPDXMLElement.m */; };
+		4FBCB53A29E8253D00F86483 /* XPDXMLElement.m in Sources */ = {isa = PBXBuildFile; fileRef = 4FBCB53729E8253D00F86483 /* XPDXMLElement.m */; };
+		4FBCB53B29E8253D00F86483 /* XMLParseManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 4FBCB53829E8253D00F86483 /* XMLParseManager.m */; };
 		C9413B0D29E420C700F2CBF6 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = C9413B0C29E420C700F2CBF6 /* AppDelegate.m */; };
 		C9413B1029E420C700F2CBF6 /* SceneDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = C9413B0F29E420C700F2CBF6 /* SceneDelegate.m */; };
 		C9413B1829E420C900F2CBF6 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = C9413B1729E420C900F2CBF6 /* Assets.xcassets */; };
@@ -44,10 +44,10 @@
 /* Begin PBXFileReference section */
 		4FBCB52429E817B900F86483 /* CPDFMoreListViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CPDFMoreListViewController.m; sourceTree = "<group>"; };
 		4FBCB52529E817B900F86483 /* CPDFMoreListViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CPDFMoreListViewController.h; sourceTree = "<group>"; };
-		4FD5D46129E65C82004BE40B /* XMLParseManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = XMLParseManager.h; sourceTree = "<group>"; };
-		4FD5D46229E65C82004BE40B /* XMLParseManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = XMLParseManager.m; sourceTree = "<group>"; };
-		4FD5D46429E65CA9004BE40B /* XPDXMLElement.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = XPDXMLElement.h; sourceTree = "<group>"; };
-		4FD5D46529E65CA9004BE40B /* XPDXMLElement.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = XPDXMLElement.m; sourceTree = "<group>"; };
+		4FBCB53629E8253D00F86483 /* XPDXMLElement.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = XPDXMLElement.h; sourceTree = "<group>"; };
+		4FBCB53729E8253D00F86483 /* XPDXMLElement.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = XPDXMLElement.m; sourceTree = "<group>"; };
+		4FBCB53829E8253D00F86483 /* XMLParseManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = XMLParseManager.m; sourceTree = "<group>"; };
+		4FBCB53929E8253D00F86483 /* XMLParseManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = XMLParseManager.h; sourceTree = "<group>"; };
 		C9413B0829E420C700F2CBF6 /* viewer-ctrl-demo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "viewer-ctrl-demo.app"; sourceTree = BUILT_PRODUCTS_DIR; };
 		C9413B0B29E420C700F2CBF6 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = "<group>"; };
 		C9413B0C29E420C700F2CBF6 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = "<group>"; };
@@ -106,10 +106,6 @@
 				C9413B0C29E420C700F2CBF6 /* AppDelegate.m */,
 				C9413B0E29E420C700F2CBF6 /* SceneDelegate.h */,
 				C9413B0F29E420C700F2CBF6 /* SceneDelegate.m */,
-				4FD5D46129E65C82004BE40B /* XMLParseManager.h */,
-				4FD5D46229E65C82004BE40B /* XMLParseManager.m */,
-				4FD5D46429E65CA9004BE40B /* XPDXMLElement.h */,
-				4FD5D46529E65CA9004BE40B /* XPDXMLElement.m */,
 				F313657F29E4378800EE6BBB /* CPDFViewController.h */,
 				F313658029E4378800EE6BBB /* CPDFViewController.m */,
 				4FBCB52529E817B900F86483 /* CPDFMoreListViewController.h */,
@@ -145,6 +141,10 @@
 		F3F6322129E5909100D23F77 /* SDKLicense */ = {
 			isa = PBXGroup;
 			children = (
+				4FBCB53929E8253D00F86483 /* XMLParseManager.h */,
+				4FBCB53829E8253D00F86483 /* XMLParseManager.m */,
+				4FBCB53629E8253D00F86483 /* XPDXMLElement.h */,
+				4FBCB53729E8253D00F86483 /* XPDXMLElement.m */,
 				F3F6322229E5909100D23F77 /* SDKLicense.xml */,
 			);
 			name = SDKLicense;
@@ -228,10 +228,10 @@
 				C9413B0D29E420C700F2CBF6 /* AppDelegate.m in Sources */,
 				F313658129E4378800EE6BBB /* CPDFViewController.m in Sources */,
 				C9413B1E29E420C900F2CBF6 /* main.m in Sources */,
-				4FD5D46629E65CA9004BE40B /* XPDXMLElement.m in Sources */,
+				4FBCB53B29E8253D00F86483 /* XMLParseManager.m in Sources */,
+				4FBCB53A29E8253D00F86483 /* XPDXMLElement.m in Sources */,
 				4FBCB52629E817B900F86483 /* CPDFMoreListViewController.m in Sources */,
 				C9413B1029E420C700F2CBF6 /* SceneDelegate.m in Sources */,
-				4FD5D46329E65C82004BE40B /* XMLParseManager.m in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};