123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- //
- // KMFileAttribute.m
- // PDF to Word
- //
- // Created by wangshuai on 13-8-14.
- // Copyright (c) 2013年 kdanmobile. All rights reserved.
- //
- #import "KMFileAttribute.h"
- #import <Quartz/Quartz.h>
- @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
|