You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ZXBizcardResultParser.m 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 "ZXAddressBookParsedResult.h"
  17. #import "ZXBizcardResultParser.h"
  18. #import "ZXResult.h"
  19. @implementation ZXBizcardResultParser
  20. - (ZXParsedResult *)parse:(ZXResult *)result {
  21. NSString *rawText = [ZXResultParser massagedText:result];
  22. if (![rawText hasPrefix:@"BIZCARD:"]) {
  23. return nil;
  24. }
  25. NSString *firstName = [[self class] matchSingleDoCoMoPrefixedField:@"N:" rawText:rawText trim:YES];
  26. NSString *lastName = [[self class] matchSingleDoCoMoPrefixedField:@"X:" rawText:rawText trim:YES];
  27. NSString *fullName = [self buildName:firstName lastName:lastName];
  28. NSString *title = [[self class] matchSingleDoCoMoPrefixedField:@"T:" rawText:rawText trim:YES];
  29. NSString *org = [[self class] matchSingleDoCoMoPrefixedField:@"C:" rawText:rawText trim:YES];
  30. NSArray *addresses = [[self class] matchDoCoMoPrefixedField:@"A:" rawText:rawText trim:YES];
  31. NSString *phoneNumber1 = [[self class] matchSingleDoCoMoPrefixedField:@"B:" rawText:rawText trim:YES];
  32. NSString *phoneNumber2 = [[self class] matchSingleDoCoMoPrefixedField:@"M:" rawText:rawText trim:YES];
  33. NSString *phoneNumber3 = [[self class] matchSingleDoCoMoPrefixedField:@"F:" rawText:rawText trim:YES];
  34. NSString *email = [[self class] matchSingleDoCoMoPrefixedField:@"E:" rawText:rawText trim:YES];
  35. return [ZXAddressBookParsedResult addressBookParsedResultWithNames:[self maybeWrap:fullName]
  36. nicknames:nil
  37. pronunciation:nil
  38. phoneNumbers:[self buildPhoneNumbers:phoneNumber1 number2:phoneNumber2 number3:phoneNumber3]
  39. phoneTypes:nil
  40. emails:[self maybeWrap:email]
  41. emailTypes:nil
  42. instantMessenger:nil
  43. note:nil
  44. addresses:addresses
  45. addressTypes:nil
  46. org:org
  47. birthday:nil
  48. title:title
  49. urls:nil
  50. geo:nil];
  51. }
  52. - (NSArray *)buildPhoneNumbers:(NSString *)number1 number2:(NSString *)number2 number3:(NSString *)number3 {
  53. NSMutableArray *numbers = [NSMutableArray arrayWithCapacity:3];
  54. if (number1 != nil) {
  55. [numbers addObject:number1];
  56. }
  57. if (number2 != nil) {
  58. [numbers addObject:number2];
  59. }
  60. if (number3 != nil) {
  61. [numbers addObject:number3];
  62. }
  63. NSUInteger size = [numbers count];
  64. if (size == 0) {
  65. return nil;
  66. }
  67. NSMutableArray *result = [NSMutableArray arrayWithCapacity:size];
  68. for (int i = 0; i < size; i++) {
  69. [result addObject:numbers[i]];
  70. }
  71. return result;
  72. }
  73. - (NSString *)buildName:(NSString *)firstName lastName:(NSString *)lastName {
  74. if (firstName == nil) {
  75. return lastName;
  76. } else {
  77. return lastName == nil ? firstName : [[firstName stringByAppendingString:@" "] stringByAppendingString:lastName];
  78. }
  79. }
  80. @end