Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ZXVEventResultParser.m 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright 2012 ZXing authors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "ZXCalendarParsedResult.h"
  17. #import "ZXResult.h"
  18. #import "ZXVCardResultParser.h"
  19. #import "ZXVEventResultParser.h"
  20. @implementation ZXVEventResultParser
  21. - (ZXParsedResult *)parse:(ZXResult *)result {
  22. NSString *rawText = [ZXResultParser massagedText:result];
  23. if (rawText == nil) {
  24. return nil;
  25. }
  26. NSUInteger vEventStart = [rawText rangeOfString:@"BEGIN:VEVENT"].location;
  27. if (vEventStart == NSNotFound) {
  28. return nil;
  29. }
  30. NSString *summary = [self matchSingleVCardPrefixedField:@"SUMMARY" rawText:rawText trim:YES];
  31. NSString *start = [self matchSingleVCardPrefixedField:@"DTSTART" rawText:rawText trim:YES];
  32. if (start == nil) {
  33. return nil;
  34. }
  35. NSString *end = [self matchSingleVCardPrefixedField:@"DTEND" rawText:rawText trim:YES];
  36. NSString *duration = [self matchSingleVCardPrefixedField:@"DURATION" rawText:rawText trim:YES];
  37. NSString *location = [self matchSingleVCardPrefixedField:@"LOCATION" rawText:rawText trim:YES];
  38. NSString *organizer = [self stripMailto:[self matchSingleVCardPrefixedField:@"ORGANIZER" rawText:rawText trim:YES]];
  39. NSMutableArray *attendees = [self matchVCardPrefixedField:@"ATTENDEE" rawText:rawText trim:YES];
  40. if (attendees != nil) {
  41. for (int i = 0; i < attendees.count; i++) {
  42. attendees[i] = [self stripMailto:attendees[i]];
  43. }
  44. }
  45. NSString *description = [self matchSingleVCardPrefixedField:@"DESCRIPTION" rawText:rawText trim:YES];
  46. NSString *geoString = [self matchSingleVCardPrefixedField:@"GEO" rawText:rawText trim:YES];
  47. double latitude;
  48. double longitude;
  49. if (geoString == nil) {
  50. latitude = NAN;
  51. longitude = NAN;
  52. } else {
  53. NSUInteger semicolon = [geoString rangeOfString:@";"].location;
  54. if (semicolon == NSNotFound) {
  55. return nil;
  56. }
  57. latitude = [[geoString substringToIndex:semicolon] doubleValue];
  58. longitude = [[geoString substringFromIndex:semicolon + 1] doubleValue];
  59. }
  60. @try {
  61. return [ZXCalendarParsedResult calendarParsedResultWithSummary:summary
  62. startString:start
  63. endString:end
  64. durationString:duration
  65. location:location
  66. organizer:organizer
  67. attendees:attendees
  68. description:description
  69. latitude:latitude
  70. longitude:longitude];
  71. } @catch (NSException *iae) {
  72. return nil;
  73. }
  74. }
  75. - (NSString *)matchSingleVCardPrefixedField:(NSString *)prefix rawText:(NSString *)rawText trim:(BOOL)trim {
  76. NSArray *values = [ZXVCardResultParser matchSingleVCardPrefixedField:prefix rawText:rawText trim:trim parseFieldDivider:NO];
  77. return values == nil || values.count == 0 ? nil : values[0];
  78. }
  79. - (NSMutableArray *)matchVCardPrefixedField:(NSString *)prefix rawText:(NSString *)rawText trim:(BOOL)trim {
  80. NSMutableArray *values = [ZXVCardResultParser matchVCardPrefixedField:prefix rawText:rawText trim:trim parseFieldDivider:NO];
  81. if (values == nil || values.count == 0) {
  82. return nil;
  83. }
  84. NSUInteger size = values.count;
  85. NSMutableArray *result = [NSMutableArray arrayWithCapacity:size];
  86. for (int i = 0; i < size; i++) {
  87. [result addObject:values[i][0]];
  88. }
  89. return result;
  90. }
  91. - (NSString *)stripMailto:(NSString *)s {
  92. if (s != nil && ([s hasPrefix:@"mailto:"] || [s hasPrefix:@"MAILTO:"])) {
  93. s = [s substringFromIndex:7];
  94. }
  95. return s;
  96. }
  97. @end