Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // FileSystemManager.h
- //
- #import <Foundation/Foundation.h>
- #import "xml.h"
- #import "MindMap.h"
- #import "MindMapController.h"
- @interface FileSystemManager : NSObject <xml> {
- NSFileManager *fileManager;
- MindMapController *mapController;
- // check filestatus
- BOOL fileExisting;
- BOOL contentIsEqual;
- BOOL isWriteable;
- // fileinformation
- NSString *filePath;
- NSString *fileName;
- // filereading & writeing
- NSData *databuffer;
- NSString *fileContent;
- // parsing
- MindMap *mindMap;
- NSMutableArray *array;
- }
- // properties
- @property(nonatomic, assign) BOOL fileExisting;
- @property(nonatomic, assign) BOOL contentIsEqual;
- @property(nonatomic, assign) BOOL isWriteable;
- @property(nonatomic, retain) NSString *filePath;
- @property(nonatomic, retain) NSString *fileName;
- @property(nonatomic, retain) NSData *databuffer;
- @property(nonatomic, retain) NSString *fileContent;
- // init
- -(id)initWithPath:(NSString *)path andFileName:(NSString *)name;
- // check & compare
- -(void)checkFileExisting:(NSString *)path;
- -(void)compareContent:(NSString *)firstPath andSecondPath:(NSString *)secondPath;
- -(void)checkIsWriteable:(NSString *)path;
- // methods
- -(void)movingOrRenameFile:(NSString *)atPath toPath:(NSString *)toPath;
- -(void)copyFile:(NSString *)atPath toPath:(NSString *)toPath;
- -(void)removeFile:(NSString *)atPath;
- -(void)createSymbolicLink:(NSString *)atPath destPath:(NSString *)destPath;
- -(void)readFile:(NSString *)atPath;
- -(void)writeFile:(NSString *)atPath;
- -(void)convertBufferToString;
- -(void)printFileContent;
- -(void)showCurrentDepth;
- -(NSMutableArray *)returnControllerArray;
- @end
- //
- // FileSystemManager.m
- //
- #import "FileSystemManager.h"
- @interface FileSystemManager () <NSXMLParserDelegate>
- @end
- @implementation FileSystemManager
- // FileSystemManager.h
- @synthesize fileExisting;
- @synthesize contentIsEqual;
- @synthesize fileName;
- @synthesize filePath;
- @synthesize isWriteable;
- @synthesize databuffer;
- @synthesize fileContent;
- // xml.h
- @synthesize depth;
- @synthesize currentName;
- @synthesize currentElement;
- @synthesize adressParser;
- #pragma GCC diagnostic ignored "-Wformat-security"
- -(id)initWithPath:(NSString *)path andFileName:(NSString *)name {
- fileManager = [NSFileManager defaultManager];
- self.filePath = [path stringByAppendingFormat:name];
- self.fileName = name;
- self.fileExisting = NO;
- self.contentIsEqual = NO;
- return self;
- }
- -(void)dealloc {
- [filePath release];
- [fileName release];
- [fileManager release];
- [databuffer release];
- [fileContent release];
- [currentElement release];
- [currentName release];
- [super dealloc];
- }
- #pragma mark -
- #pragma mark FileSystem methods
- -(void)checkFileExisting:(NSString *)path {
- if ([path length] != 0) {
- if([fileManager fileExistsAtPath:path] == YES) {
- self.fileExisting = YES;
- }
- else {
- self.fileExisting = NO;
- }
- }
- else {
- self.fileExisting = NO;
- NSLog(@"There is an empty path to the file! - existing");
- }
- }
- -(void)compareContent:(NSString *)firstPath andSecondPath:(NSString *)secondPath {
- if([firstPath length] != 0 && [secondPath length] != 0) {
- if ([fileManager contentsEqualAtPath:firstPath andPath:secondPath] == YES) {
- self.contentIsEqual = YES;
- }
- else {
- self.contentIsEqual = NO;
- }
- }
- else {
- self.contentIsEqual = NO;
- NSLog(@"There is an empty path to the file! - compare");
- }
- }
- -(void)checkIsWriteable:(NSString *)path {
- if([path length] != 0) {
- if([fileManager isWritableFileAtPath:path] == YES) {
- self.isWriteable = YES;
- }
- else {
- self.isWriteable = NO;
- }
- }
- else {
- self.isWriteable = NO;
- NSLog(@"There is an empty path to the file! - writeable");
- }
- }
- -(void)movingOrRenameFile:(NSString *)atPath toPath:(NSString *)toPath {
- if([atPath length] != 0 && [toPath length] != 0) {
- [fileManager moveItemAtPath:atPath toPath:toPath error:nil];
- }
- else {
- NSLog(@"There is an empty path to the files! - move_rename");
- }
- }
- -(void)copyFile:(NSString *)atPath toPath:(NSString *)toPath {
- if([atPath length] != 0 && [toPath length] != 0) {
- [fileManager copyItemAtPath:atPath toPath:toPath error:nil];
- }
- else {
- NSLog(@"There is an empty path to the files! - copy");
- }
- }
- -(void)removeFile:(NSString *)atPath {
- if([atPath length] != 0) {
- [fileManager removeItemAtPath:atPath error:nil];
- }
- else {
- NSLog(@"There is an empty path to the files! - remove");
- }
- }
- -(void)createSymbolicLink:(NSString *)atPath destPath:(NSString *)destPath {
- if([atPath length] != 0 && [destPath length] != 0) {
- [fileManager createSymbolicLinkAtPath:atPath withDestinationPath:destPath error:nil];
- }
- else {
- NSLog(@"There is an empty path to the files! - symbolic");
- }
- }
- -(void)readFile:(NSString *)atPath {
- if([atPath length] != 0) {
- self.databuffer = [fileManager contentsAtPath:atPath];
- [self parseXMLFile:atPath];
- }
- else {
- NSLog(@"There is an empty Path! - read_file");
- }
- }
- -(void)writeFile:(NSString *)atPath {
- if([atPath length] != 0) {
- [fileManager createFileAtPath:atPath contents:self.databuffer attributes:nil];
- }
- else {
- NSLog(@"There is an empty Path! - write_file");
- }
- }
- -(void)convertBufferToString {
- if([databuffer length] != 0) {
- self.fileContent = [[[NSString alloc] initWithData:self.databuffer encoding:NSUTF8StringEncoding] autorelease];
- }
- else {
- NSLog(@"There is an empty buffer! - convert_buffer");
- }
- }
- -(void)printFileContent {
- NSLog(@"Dateiinhalt: - %@", fileContent);
- }
- -(void)parseXMLFile:(NSString *)pathToFile {
- @try {
- BOOL success;
- NSURL *xmlURL = [NSURL fileURLWithPath:pathToFile];
- if(adressParser) {
- [adressParser release];
- }
- adressParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
- [adressParser setDelegate:self];
- [adressParser setShouldResolveExternalEntities:YES];
- success = [adressParser parse];
- }
- @catch (NSException *exception) {
- NSLog(@"Exception - %@", exception);
- }
- }
- #pragma mark -
- #pragma mark NSXMLParserDelegate methods
- -(void)parserDidStartDocument:(NSXMLParser *)parser{
- NSLog(@"Document started - %@ - Path: %@", fileName, filePath);
- depth = 0;
- currentElement = nil;
- }
- -(void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
- NSLog(@"There was an error during parsing - %@ - Reason : %@", parseError, [parseError localizedFailureReason]);
- }
- - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
- [currentElement release];
- currentElement = [elementName copy];
- if ([currentElement isEqualToString:@"CATALOG"])
- {
- ++depth;
- [self showCurrentDepth];
- array = [[[NSMutableArray alloc] init] autorelease];
- }
- else if ([currentElement isEqualToString:@"CD"])
- {
- ++depth;
- [self showCurrentDepth];
- mindMap = [[MindMap alloc] init];
- }
- }
- - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
- @try {
- if ([elementName isEqualToString:@"CATALOG"])
- {
- --depth;
- [self showCurrentDepth];
- }
- else if ([elementName isEqualToString:@"CD"]) {
- }
- else if ([elementName isEqualToString:@"TITLE"] || [elementName isEqualToString:@"ARTIST"] || [elementName isEqualToString:@"COUNTRY"] || [elementName isEqualToString:@"COMPANY"] || [elementName isEqualToString:@"PRICE"] || [elementName isEqualToString:@"YEAR"])
- {
- if (depth == 1)
- {
- NSLog(@"Outer name tag: %@", currentName);
- }
- else
- {
- NSLog(@"Inner name tag: %@", currentName);
- if([elementName isEqualToString:@"TITLE"]) {
- [mindMap release];
- mindMap = [[MindMap alloc] init];
- [mindMap setTitle:currentName];
- }
- else if([elementName isEqualToString:@"ARTIST"]) {
- [mindMap setArtist:currentName];
- }
- else if([elementName isEqualToString:@"COUNTRY"]) {
- [mindMap setCountry:currentName];
- }
- else if([elementName isEqualToString:@"COMPANY"]) {
- [mindMap setCompany:currentName];
- }
- else if([elementName isEqualToString:@"PRICE"]) {
- [mindMap setPrice:currentName];
- }
- else if([elementName isEqualToString:@"YEAR"]) {
- [mindMap setYear:currentName];
- if([currentElement isEqualToString: @"YEAR"]) {
- [array addObject:mindMap];
- [mindMap release];
- }
- }
- else {
- NSLog(@"There was an error while parsing!");
- }
- }
- }
- }
- @catch (NSException *exception) {
- NSLog(@"Exception - %@", exception);
- }
- }
- -(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
- [currentName release];
- currentName = [[NSMutableString alloc] init];
- [currentName appendString:string];
- }
- -(void)parserDidEndDocument:(NSXMLParser *)parser {
- @try {
- NSLog(@"Document finished", nil);
- NSLog(@"There are %i entries", [array count]);
- MindMap *newMap = [[MindMap alloc] init];
- int i;
- for (i = 0; i < [array count]; i++) {
- newMap =[array objectAtIndex:i];
- NSLog(@"Title : %@", newMap.mTitle);
- NSLog(@"Artist : %@", newMap.mArtist);
- NSLog(@"Country : %@", newMap.mCountry);
- NSLog(@"Company : %@", newMap.mCompany);
- NSLog(@"Price : %@", newMap.mPrice);
- NSLog(@"Year : %@", newMap.mYear);
- }
- [newMap release];
- }
- @catch (NSException *exception) {
- NSLog(@"Exception - %@", exception);
- }
- }
- #pragma mark -
- #pragma mark Private methods
- -(void)showCurrentDepth {
- NSLog(@"Current depth: %d", depth);
- }
- -(NSMutableArray *)returnControllerArray {
- return array;
- }
- @end
Add Comment
Please, Sign In to add comment