Guest User

Untitled

a guest
Feb 21st, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. // FileSystemManager.h
  3. //
  4.  
  5. #import <Foundation/Foundation.h>
  6. #import "xml.h"
  7. #import "MindMap.h"
  8. #import "MindMapController.h"
  9.  
  10. @interface FileSystemManager : NSObject <xml> {
  11.     NSFileManager *fileManager;
  12.     MindMapController *mapController;
  13.    
  14.     // check filestatus
  15.     BOOL fileExisting;
  16.     BOOL contentIsEqual;
  17.     BOOL isWriteable;
  18.    
  19.     // fileinformation
  20.     NSString    *filePath;
  21.     NSString    *fileName;
  22.    
  23.     // filereading & writeing
  24.     NSData      *databuffer;
  25.     NSString    *fileContent;
  26.    
  27.     // parsing
  28.     MindMap         *mindMap;
  29.     NSMutableArray  *array;
  30. }
  31.  
  32. // properties
  33. @property(nonatomic, assign) BOOL       fileExisting;
  34. @property(nonatomic, assign) BOOL       contentIsEqual;
  35. @property(nonatomic, assign) BOOL       isWriteable;
  36. @property(nonatomic, retain) NSString   *filePath;
  37. @property(nonatomic, retain) NSString   *fileName;
  38. @property(nonatomic, retain) NSData     *databuffer;
  39. @property(nonatomic, retain) NSString   *fileContent;
  40.  
  41. // init
  42. -(id)initWithPath:(NSString *)path andFileName:(NSString *)name;
  43.  
  44. // check & compare
  45. -(void)checkFileExisting:(NSString *)path;
  46. -(void)compareContent:(NSString *)firstPath andSecondPath:(NSString *)secondPath;
  47. -(void)checkIsWriteable:(NSString *)path;
  48.  
  49. // methods
  50. -(void)movingOrRenameFile:(NSString *)atPath toPath:(NSString *)toPath;
  51. -(void)copyFile:(NSString *)atPath toPath:(NSString *)toPath;
  52. -(void)removeFile:(NSString *)atPath;
  53. -(void)createSymbolicLink:(NSString *)atPath destPath:(NSString *)destPath;
  54. -(void)readFile:(NSString *)atPath;
  55. -(void)writeFile:(NSString *)atPath;
  56. -(void)convertBufferToString;
  57. -(void)printFileContent;
  58. -(void)showCurrentDepth;
  59. -(NSMutableArray *)returnControllerArray;
  60.  
  61. @end
  62.  
  63.  
  64.  
  65. //
  66. // FileSystemManager.m
  67. //
  68.  
  69. #import "FileSystemManager.h"
  70.  
  71. @interface FileSystemManager () <NSXMLParserDelegate>
  72.  
  73. @end
  74.  
  75.  
  76. @implementation FileSystemManager
  77.  
  78. // FileSystemManager.h
  79. @synthesize fileExisting;
  80. @synthesize contentIsEqual;
  81. @synthesize fileName;
  82. @synthesize filePath;
  83. @synthesize isWriteable;
  84. @synthesize databuffer;
  85. @synthesize fileContent;
  86.  
  87. // xml.h
  88. @synthesize depth;
  89. @synthesize currentName;
  90. @synthesize currentElement;
  91. @synthesize adressParser;
  92.  
  93. #pragma GCC diagnostic ignored "-Wformat-security"
  94.  
  95. -(id)initWithPath:(NSString *)path andFileName:(NSString *)name {
  96.     fileManager = [NSFileManager defaultManager];
  97.    
  98.     self.filePath = [path stringByAppendingFormat:name];
  99.     self.fileName = name;
  100.     self.fileExisting = NO;
  101.     self.contentIsEqual = NO;
  102.    
  103.     return self;
  104. }
  105.  
  106.  
  107. -(void)dealloc {
  108.     [filePath release];
  109.     [fileName release];
  110.     [fileManager release];
  111.     [databuffer release];
  112.     [fileContent release];
  113.     [currentElement release];
  114.     [currentName release];
  115.     [super dealloc];
  116. }
  117.  
  118. #pragma mark -
  119. #pragma mark FileSystem methods
  120.  
  121. -(void)checkFileExisting:(NSString *)path {
  122.     if ([path length] != 0) {
  123.         if([fileManager fileExistsAtPath:path] == YES) {
  124.             self.fileExisting = YES;
  125.         }
  126.         else {
  127.             self.fileExisting = NO;
  128.         }
  129.     }
  130.     else {
  131.         self.fileExisting = NO;
  132.         NSLog(@"There is an empty path to the file! - existing");
  133.     }
  134. }
  135.  
  136.  
  137. -(void)compareContent:(NSString *)firstPath andSecondPath:(NSString *)secondPath {
  138.     if([firstPath length] != 0 && [secondPath length] != 0) {
  139.         if ([fileManager contentsEqualAtPath:firstPath andPath:secondPath] == YES) {
  140.             self.contentIsEqual = YES;
  141.         }
  142.         else {
  143.             self.contentIsEqual = NO;
  144.         }
  145.     }
  146.     else {
  147.         self.contentIsEqual = NO;
  148.         NSLog(@"There is an empty path to the file! - compare");
  149.     }
  150. }
  151.  
  152.  
  153. -(void)checkIsWriteable:(NSString *)path {
  154.     if([path length] != 0) {
  155.         if([fileManager isWritableFileAtPath:path] == YES) {
  156.             self.isWriteable = YES;
  157.         }
  158.         else {
  159.             self.isWriteable = NO;
  160.         }
  161.     }
  162.     else {
  163.         self.isWriteable = NO;
  164.         NSLog(@"There is an empty path to the file! - writeable");
  165.     }
  166. }
  167.  
  168.  
  169. -(void)movingOrRenameFile:(NSString *)atPath toPath:(NSString *)toPath {
  170.     if([atPath length] != 0 && [toPath length] != 0) {
  171.         [fileManager moveItemAtPath:atPath toPath:toPath error:nil];
  172.     }
  173.     else {
  174.         NSLog(@"There is an empty path to the files! - move_rename");
  175.     }
  176. }
  177.  
  178.  
  179. -(void)copyFile:(NSString *)atPath toPath:(NSString *)toPath {
  180.     if([atPath length] != 0 && [toPath length] != 0) {
  181.         [fileManager copyItemAtPath:atPath toPath:toPath error:nil];
  182.     }
  183.     else {
  184.         NSLog(@"There is an empty path to the files! - copy");
  185.     }
  186. }
  187.  
  188.  
  189. -(void)removeFile:(NSString *)atPath {
  190.     if([atPath length] != 0) {
  191.         [fileManager removeItemAtPath:atPath error:nil];
  192.     }
  193.     else {
  194.         NSLog(@"There is an empty path to the files! - remove");
  195.     }
  196. }
  197.  
  198.  
  199. -(void)createSymbolicLink:(NSString *)atPath destPath:(NSString *)destPath {
  200.     if([atPath length] != 0 && [destPath length] != 0) {
  201.         [fileManager createSymbolicLinkAtPath:atPath withDestinationPath:destPath error:nil];
  202.     }
  203.     else {
  204.         NSLog(@"There is an empty path to the files! - symbolic");
  205.     }
  206. }
  207.  
  208.  
  209. -(void)readFile:(NSString *)atPath {
  210.     if([atPath length] != 0) {
  211.         self.databuffer = [fileManager contentsAtPath:atPath];
  212.         [self parseXMLFile:atPath];
  213.     }
  214.     else {
  215.         NSLog(@"There is an empty Path! - read_file");
  216.     }
  217. }
  218.  
  219.  
  220. -(void)writeFile:(NSString *)atPath {
  221.     if([atPath length] != 0) {
  222.         [fileManager createFileAtPath:atPath contents:self.databuffer attributes:nil];
  223.     }
  224.     else {
  225.         NSLog(@"There is an empty Path! - write_file");
  226.     }
  227. }
  228.  
  229.  
  230. -(void)convertBufferToString {
  231.     if([databuffer length] != 0) {
  232.         self.fileContent = [[[NSString alloc] initWithData:self.databuffer encoding:NSUTF8StringEncoding] autorelease];
  233.     }
  234.     else {
  235.         NSLog(@"There is an empty buffer! - convert_buffer");
  236.     }
  237. }
  238.  
  239.  
  240. -(void)printFileContent {
  241.      NSLog(@"Dateiinhalt: - %@", fileContent);
  242. }
  243.  
  244.  
  245. -(void)parseXMLFile:(NSString *)pathToFile {
  246.     @try {
  247.         BOOL success;
  248.         NSURL *xmlURL = [NSURL fileURLWithPath:pathToFile];
  249.         if(adressParser) {
  250.             [adressParser release];
  251.         }
  252.         adressParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
  253.         [adressParser setDelegate:self];
  254.         [adressParser setShouldResolveExternalEntities:YES];
  255.         success = [adressParser parse];
  256.     }
  257.     @catch (NSException *exception) {
  258.         NSLog(@"Exception - %@", exception);
  259.     }
  260. }
  261.  
  262. #pragma mark -
  263. #pragma mark NSXMLParserDelegate methods
  264.  
  265. -(void)parserDidStartDocument:(NSXMLParser *)parser{
  266.     NSLog(@"Document started - %@ - Path: %@", fileName, filePath);
  267.     depth = 0;
  268.     currentElement = nil;
  269. }
  270.  
  271.  
  272. -(void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
  273.     NSLog(@"There was an error during parsing - %@ - Reason : %@", parseError, [parseError localizedFailureReason]);
  274. }
  275.  
  276.  
  277. - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
  278.     [currentElement release];
  279.     currentElement = [elementName copy];
  280.    
  281.     if ([currentElement isEqualToString:@"CATALOG"])
  282.     {
  283.         ++depth;
  284.         [self showCurrentDepth];
  285.         array = [[[NSMutableArray alloc] init] autorelease];
  286.     }
  287.     else if ([currentElement isEqualToString:@"CD"])
  288.     {
  289.         ++depth;
  290.         [self showCurrentDepth];
  291.         mindMap = [[MindMap alloc] init];
  292.     }
  293. }
  294.  
  295.  
  296.  
  297. - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
  298.     @try {
  299.         if ([elementName isEqualToString:@"CATALOG"])
  300.         {
  301.             --depth;
  302.             [self showCurrentDepth];
  303.         }
  304.         else if ([elementName isEqualToString:@"CD"]) {
  305.         }
  306.         else if ([elementName isEqualToString:@"TITLE"] || [elementName isEqualToString:@"ARTIST"] || [elementName isEqualToString:@"COUNTRY"] || [elementName isEqualToString:@"COMPANY"] || [elementName isEqualToString:@"PRICE"] || [elementName isEqualToString:@"YEAR"])
  307.         {
  308.             if (depth == 1)
  309.             {
  310.                 NSLog(@"Outer name tag: %@", currentName);
  311.             }
  312.             else
  313.             {
  314.                 NSLog(@"Inner name tag: %@", currentName);
  315.                 if([elementName isEqualToString:@"TITLE"]) {
  316.                     [mindMap release];
  317.                     mindMap = [[MindMap alloc] init];
  318.                     [mindMap setTitle:currentName];
  319.                 }
  320.                 else if([elementName isEqualToString:@"ARTIST"]) {
  321.                     [mindMap setArtist:currentName];
  322.                 }
  323.                 else if([elementName isEqualToString:@"COUNTRY"]) {
  324.                     [mindMap setCountry:currentName];
  325.                 }
  326.                 else if([elementName isEqualToString:@"COMPANY"]) {
  327.                     [mindMap setCompany:currentName];
  328.                 }
  329.                 else if([elementName isEqualToString:@"PRICE"]) {
  330.                     [mindMap setPrice:currentName];
  331.                 }
  332.                 else if([elementName isEqualToString:@"YEAR"]) {
  333.                     [mindMap setYear:currentName];
  334.                     if([currentElement isEqualToString: @"YEAR"]) {
  335.                         [array addObject:mindMap];
  336.                         [mindMap release];
  337.                     }
  338.                 }
  339.                 else {
  340.                     NSLog(@"There was an error while parsing!");
  341.                 }
  342.             }
  343.         }
  344.     }
  345.     @catch (NSException *exception) {
  346.         NSLog(@"Exception - %@", exception);
  347.     }
  348. }
  349.  
  350.  
  351. -(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
  352.     [currentName release];
  353.     currentName = [[NSMutableString alloc] init];
  354.     [currentName appendString:string];
  355. }
  356.  
  357.  
  358. -(void)parserDidEndDocument:(NSXMLParser *)parser {
  359.     @try {
  360.         NSLog(@"Document finished", nil);
  361.         NSLog(@"There are %i entries", [array count]);
  362.         MindMap *newMap = [[MindMap alloc] init];
  363.         int i;
  364.         for (i = 0; i < [array count]; i++) {
  365.             newMap =[array objectAtIndex:i];
  366.             NSLog(@"Title   : %@", newMap.mTitle);
  367.             NSLog(@"Artist  : %@", newMap.mArtist);
  368.             NSLog(@"Country : %@", newMap.mCountry);
  369.             NSLog(@"Company : %@", newMap.mCompany);
  370.             NSLog(@"Price   : %@", newMap.mPrice);
  371.             NSLog(@"Year       : %@", newMap.mYear);
  372.         }
  373.         [newMap release];
  374.     }
  375.     @catch (NSException *exception) {
  376.         NSLog(@"Exception - %@", exception);
  377.     }
  378. }
  379.  
  380.  
  381. #pragma mark -
  382. #pragma mark Private methods
  383.  
  384.  
  385. -(void)showCurrentDepth {
  386.     NSLog(@"Current depth: %d", depth);
  387. }
  388.  
  389.  
  390. -(NSMutableArray *)returnControllerArray {
  391.     return array;
  392. }
  393.  
  394. @end
Add Comment
Please, Sign In to add comment