KMFileAttribute.m 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. //
  2. // KMFileAttribute.m
  3. // PDF to Word
  4. //
  5. // Created by wangshuai on 13-8-14.
  6. // Copyright (c) 2013年 kdanmobile. All rights reserved.
  7. //
  8. #import "KMFileAttribute.h"
  9. #import <Quartz/Quartz.h>
  10. @implementation KMFileAttribute
  11. @synthesize filePath = _filePath;
  12. @synthesize bAllPage = _bAllPage;
  13. @synthesize selectPages = _selectPages;
  14. @synthesize pagesString = _pagesString;
  15. @synthesize isLocked = _isLocked;
  16. @synthesize password = _password;
  17. - (id)init
  18. {
  19. self = [super init];
  20. if (self) {
  21. _bAllPage = YES;
  22. _isLocked = NO;
  23. _password = nil;
  24. _pagesString = @"";
  25. }
  26. return self;
  27. }
  28. - (void)dealloc {}
  29. - (NSMutableArray*)selectPages
  30. {
  31. [self isInvalidString:_pagesString];
  32. if (!_bAllPage) {
  33. [self QuickSort:_selectPages StartIndex:0 EndIndex:[_selectPages count]-1];
  34. }
  35. return _selectPages;
  36. }
  37. - (BOOL)isInvalidString:(NSString*)text
  38. {
  39. PDFDocument * document;
  40. if (_myPDFDocument) {
  41. document = self.myPDFDocument;
  42. } else {
  43. document = [[PDFDocument alloc] initWithURL:[NSURL fileURLWithPath:self.filePath]];
  44. }
  45. NSInteger pageNumber = [document pageCount];
  46. if (_bAllPage) {
  47. _selectPages = [[NSMutableArray alloc] init];
  48. for (int i=1; i<=pageNumber; i++) {
  49. [_selectPages addObject:[NSNumber numberWithInteger:i]];
  50. }
  51. return NO;
  52. }
  53. NSMutableArray *pageNumbers = [[NSMutableArray alloc] init];
  54. BOOL isInvalid = NO;
  55. unichar c;
  56. for (int i=0; i<[text length]; i++) {
  57. c = [text characterAtIndex:i];
  58. if (c!='0'&&c!='1'&&c!='2'&&c!='3'&&c!='4'&&c!='5'&&c!='6'&&c!='7'&&c!='8'&&c!='9'&&c!=','&&c!='-') {
  59. isInvalid = YES;
  60. break;
  61. }else{
  62. isInvalid = NO;
  63. }
  64. }
  65. if (!isInvalid) {
  66. NSArray *array = [text componentsSeparatedByString:@","];
  67. for(NSString *s in array){
  68. if ([s isEqualToString:@""]) {
  69. isInvalid = YES;
  70. break;
  71. }else{
  72. NSArray *pages = [s componentsSeparatedByString:@"-"];
  73. if ([pages count]>2) {
  74. isInvalid = YES;
  75. break;
  76. }else if([pages count]==1){
  77. NSString *p = [pages objectAtIndex:0];
  78. if ([p isEqualToString:@""]||[p intValue]>pageNumber||[p integerValue]==0) {
  79. isInvalid = YES;
  80. break;
  81. }else{
  82. BOOL isEqual = NO;
  83. for(NSNumber *pageNumber in pageNumbers){
  84. if ([pageNumber integerValue]==[p integerValue]) {
  85. isEqual = YES;
  86. isInvalid = YES;
  87. break;
  88. }
  89. }
  90. if (!isEqual) {
  91. [pageNumbers addObject:[NSNumber numberWithInteger:[p integerValue]]];
  92. }
  93. }
  94. }else if([pages count]==2){
  95. NSString *p1 = [pages objectAtIndex:0];
  96. NSString *p2 = [pages objectAtIndex:1];
  97. if ([p1 isEqualToString:@""]||[p2 isEqualToString:@""]||
  98. [p1 intValue]>=[p2 intValue]||[p2 intValue]>pageNumber||[p1 integerValue]==0) {
  99. isInvalid = YES;
  100. break;
  101. }else{
  102. BOOL isEqual = NO;
  103. for (int i=[p1 intValue]; i<=[p2 intValue]; i++) {
  104. for(NSNumber *pageNumber in pageNumbers){
  105. if ([pageNumber integerValue]==i) {
  106. isEqual = YES;
  107. isInvalid = YES;
  108. break;
  109. }
  110. }
  111. }
  112. if (!isEqual) {
  113. for (int i=[p1 intValue]; i<=[p2 intValue]; i++) {
  114. [pageNumbers addObject:[NSNumber numberWithInteger:i]];
  115. }
  116. }
  117. }
  118. }
  119. }
  120. }
  121. }
  122. if ([text length]==0) {
  123. isInvalid = YES;
  124. }
  125. if (isInvalid) {
  126. _selectPages = nil;
  127. }else{
  128. _selectPages = pageNumbers;
  129. }
  130. return isInvalid;
  131. }
  132. -(void)QuickSort:(NSMutableArray *)list StartIndex:(NSInteger)startIndex EndIndex:(NSInteger)endIndex{
  133. if(startIndex >= endIndex)return;
  134. NSNumber * temp = [list objectAtIndex:startIndex];
  135. NSInteger tempIndex = startIndex;
  136. for(NSInteger i = startIndex + 1 ; i <= endIndex ; i++){
  137. NSNumber *t = [list objectAtIndex:i];
  138. if([temp intValue] > [t intValue]){
  139. tempIndex = tempIndex + 1;
  140. [list exchangeObjectAtIndex:tempIndex withObjectAtIndex:i];
  141. }
  142. }
  143. [list exchangeObjectAtIndex:tempIndex withObjectAtIndex:startIndex];
  144. [self QuickSort:list StartIndex:startIndex EndIndex:tempIndex-1];
  145. [self QuickSort:list StartIndex:tempIndex+1 EndIndex:endIndex];
  146. }
  147. /* give our representation to the image browser */
  148. - (id)imageRepresentation
  149. {
  150. return self.filePath;
  151. }
  152. /* use the absolute filepath as identifier */
  153. - (NSString *)imageUID
  154. {
  155. return self.filePath;
  156. }
  157. - (NSString*)imageTitle
  158. {
  159. return [[self.filePath lastPathComponent] stringByDeletingPathExtension];
  160. }
  161. @end