// // KMFileAttribute.m // PDF to Word // // Created by wangshuai on 13-8-14. // Copyright (c) 2013年 kdanmobile. All rights reserved. // #import "KMFileAttribute.h" #import @implementation KMFileAttribute @synthesize filePath = _filePath; @synthesize bAllPage = _bAllPage; @synthesize selectPages = _selectPages; @synthesize pagesString = _pagesString; @synthesize isLocked = _isLocked; @synthesize password = _password; - (id)init { self = [super init]; if (self) { _bAllPage = YES; _isLocked = NO; _password = nil; _pagesString = @""; } return self; } - (void)dealloc {} - (NSMutableArray*)selectPages { [self isInvalidString:_pagesString]; if (!_bAllPage) { [self QuickSort:_selectPages StartIndex:0 EndIndex:[_selectPages count]-1]; } return _selectPages; } - (BOOL)isInvalidString:(NSString*)text { PDFDocument * document; if (_myPDFDocument) { document = self.myPDFDocument; } else { document = [[PDFDocument alloc] initWithURL:[NSURL fileURLWithPath:self.filePath]]; } NSInteger pageNumber = [document pageCount]; if (_bAllPage) { _selectPages = [[NSMutableArray alloc] init]; for (int i=1; i<=pageNumber; i++) { [_selectPages addObject:[NSNumber numberWithInteger:i]]; } return NO; } NSMutableArray *pageNumbers = [[NSMutableArray alloc] init]; BOOL isInvalid = NO; unichar c; for (int i=0; i<[text length]; i++) { c = [text characterAtIndex:i]; if (c!='0'&&c!='1'&&c!='2'&&c!='3'&&c!='4'&&c!='5'&&c!='6'&&c!='7'&&c!='8'&&c!='9'&&c!=','&&c!='-') { isInvalid = YES; break; }else{ isInvalid = NO; } } if (!isInvalid) { NSArray *array = [text componentsSeparatedByString:@","]; for(NSString *s in array){ if ([s isEqualToString:@""]) { isInvalid = YES; break; }else{ NSArray *pages = [s componentsSeparatedByString:@"-"]; if ([pages count]>2) { isInvalid = YES; break; }else if([pages count]==1){ NSString *p = [pages objectAtIndex:0]; if ([p isEqualToString:@""]||[p intValue]>pageNumber||[p integerValue]==0) { isInvalid = YES; break; }else{ BOOL isEqual = NO; for(NSNumber *pageNumber in pageNumbers){ if ([pageNumber integerValue]==[p integerValue]) { isEqual = YES; isInvalid = YES; break; } } if (!isEqual) { [pageNumbers addObject:[NSNumber numberWithInteger:[p integerValue]]]; } } }else if([pages count]==2){ NSString *p1 = [pages objectAtIndex:0]; NSString *p2 = [pages objectAtIndex:1]; if ([p1 isEqualToString:@""]||[p2 isEqualToString:@""]|| [p1 intValue]>=[p2 intValue]||[p2 intValue]>pageNumber||[p1 integerValue]==0) { isInvalid = YES; break; }else{ BOOL isEqual = NO; for (int i=[p1 intValue]; i<=[p2 intValue]; i++) { for(NSNumber *pageNumber in pageNumbers){ if ([pageNumber integerValue]==i) { isEqual = YES; isInvalid = YES; break; } } } if (!isEqual) { for (int i=[p1 intValue]; i<=[p2 intValue]; i++) { [pageNumbers addObject:[NSNumber numberWithInteger:i]]; } } } } } } } if ([text length]==0) { isInvalid = YES; } if (isInvalid) { _selectPages = nil; }else{ _selectPages = pageNumbers; } return isInvalid; } -(void)QuickSort:(NSMutableArray *)list StartIndex:(NSInteger)startIndex EndIndex:(NSInteger)endIndex{ if(startIndex >= endIndex)return; NSNumber * temp = [list objectAtIndex:startIndex]; NSInteger tempIndex = startIndex; for(NSInteger i = startIndex + 1 ; i <= endIndex ; i++){ NSNumber *t = [list objectAtIndex:i]; if([temp intValue] > [t intValue]){ tempIndex = tempIndex + 1; [list exchangeObjectAtIndex:tempIndex withObjectAtIndex:i]; } } [list exchangeObjectAtIndex:tempIndex withObjectAtIndex:startIndex]; [self QuickSort:list StartIndex:startIndex EndIndex:tempIndex-1]; [self QuickSort:list StartIndex:tempIndex+1 EndIndex:endIndex]; } /* give our representation to the image browser */ - (id)imageRepresentation { return self.filePath; } /* use the absolute filepath as identifier */ - (NSString *)imageUID { return self.filePath; } - (NSString*)imageTitle { return [[self.filePath lastPathComponent] stringByDeletingPathExtension]; } @end