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.

ZXExpandedProductResultParser.m 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 "ZXExpandedProductResultParser.h"
  17. #import "ZXExpandedProductParsedResult.h"
  18. #import "ZXResult.h"
  19. @implementation ZXExpandedProductResultParser
  20. - (ZXParsedResult *)parse:(ZXResult *)result {
  21. ZXBarcodeFormat format = [result barcodeFormat];
  22. if (kBarcodeFormatRSSExpanded != format) {
  23. // ExtendedProductParsedResult NOT created. Not a RSS Expanded barcode
  24. return nil;
  25. }
  26. NSString *rawText = [ZXResultParser massagedText:result];
  27. NSString *productID = nil;
  28. NSString *sscc = nil;
  29. NSString *lotNumber = nil;
  30. NSString *productionDate = nil;
  31. NSString *packagingDate = nil;
  32. NSString *bestBeforeDate = nil;
  33. NSString *expirationDate = nil;
  34. NSString *weight = nil;
  35. NSString *weightType = nil;
  36. NSString *weightIncrement = nil;
  37. NSString *price = nil;
  38. NSString *priceIncrement = nil;
  39. NSString *priceCurrency = nil;
  40. NSMutableDictionary *uncommonAIs = [NSMutableDictionary dictionary];
  41. int i = 0;
  42. while (i < [rawText length]) {
  43. NSString *ai = [self findAIvalue:i rawText:rawText];
  44. if (ai == nil) {
  45. // Error. Code doesn't match with RSS expanded pattern
  46. // ExtendedProductParsedResult NOT created. Not match with RSS Expanded pattern
  47. return nil;
  48. }
  49. i += [ai length] + 2;
  50. NSString *value = [self findValue:i rawText:rawText];
  51. i += [value length];
  52. if ([@"00" isEqualToString:ai]) {
  53. sscc = value;
  54. } else if ([@"01" isEqualToString:ai]) {
  55. productID = value;
  56. } else if ([@"10" isEqualToString:ai]) {
  57. lotNumber = value;
  58. } else if ([@"11" isEqualToString:ai]) {
  59. productionDate = value;
  60. } else if ([@"13" isEqualToString:ai]) {
  61. packagingDate = value;
  62. } else if ([@"15" isEqualToString:ai]) {
  63. bestBeforeDate = value;
  64. } else if ([@"17" isEqualToString:ai]) {
  65. expirationDate = value;
  66. } else if ([@"3100" isEqualToString:ai] || [@"3101" isEqualToString:ai] || [@"3102" isEqualToString:ai] || [@"3103" isEqualToString:ai] || [@"3104" isEqualToString:ai] || [@"3105" isEqualToString:ai] || [@"3106" isEqualToString:ai] || [@"3107" isEqualToString:ai] || [@"3108" isEqualToString:ai] || [@"3109" isEqualToString:ai]) {
  67. weight = value;
  68. weightType = ZX_KILOGRAM;
  69. weightIncrement = [ai substringFromIndex:3];
  70. } else if ([@"3200" isEqualToString:ai] || [@"3201" isEqualToString:ai] || [@"3202" isEqualToString:ai] || [@"3203" isEqualToString:ai] || [@"3204" isEqualToString:ai] || [@"3205" isEqualToString:ai] || [@"3206" isEqualToString:ai] || [@"3207" isEqualToString:ai] || [@"3208" isEqualToString:ai] || [@"3209" isEqualToString:ai]) {
  71. weight = value;
  72. weightType = ZX_POUND;
  73. weightIncrement = [ai substringFromIndex:3];
  74. } else if ([@"3920" isEqualToString:ai] || [@"3921" isEqualToString:ai] || [@"3922" isEqualToString:ai] || [@"3923" isEqualToString:ai]) {
  75. price = value;
  76. priceIncrement = [ai substringFromIndex:3];
  77. } else if ([@"3930" isEqualToString:ai] || [@"3931" isEqualToString:ai] || [@"3932" isEqualToString:ai] || [@"3933" isEqualToString:ai]) {
  78. if ([value length] < 4) {
  79. // The value must have more of 3 symbols (3 for currency and
  80. // 1 at least for the price)
  81. // ExtendedProductParsedResult NOT created. Not match with RSS Expanded pattern
  82. return nil;
  83. }
  84. price = [value substringFromIndex:3];
  85. priceCurrency = [value substringToIndex:3];
  86. priceIncrement = [ai substringFromIndex:3];
  87. } else {
  88. // No match with common AIs
  89. uncommonAIs[ai] = value;
  90. }
  91. }
  92. return [ZXExpandedProductParsedResult expandedProductParsedResultWithRawText:rawText
  93. productID:productID
  94. sscc:sscc
  95. lotNumber:lotNumber
  96. productionDate:productionDate
  97. packagingDate:packagingDate
  98. bestBeforeDate:bestBeforeDate
  99. expirationDate:expirationDate
  100. weight:weight
  101. weightType:weightType
  102. weightIncrement:weightIncrement
  103. price:price
  104. priceIncrement:priceIncrement
  105. priceCurrency:priceCurrency
  106. uncommonAIs:uncommonAIs];
  107. }
  108. - (NSString *)findAIvalue:(int)i rawText:(NSString *)rawText {
  109. unichar c = [rawText characterAtIndex:i];
  110. // First character must be a open parenthesis.If not, ERROR
  111. if (c != '(') {
  112. return nil;
  113. }
  114. NSString *rawTextAux = [rawText substringFromIndex:i + 1];
  115. NSMutableString *buf = [NSMutableString string];
  116. for (int index = 0; index < [rawTextAux length]; index++) {
  117. unichar currentChar = [rawTextAux characterAtIndex:index];
  118. if (currentChar == ')') {
  119. return buf;
  120. } else if (currentChar >= '0' && currentChar <= '9') {
  121. [buf appendFormat:@"%C", currentChar];
  122. } else {
  123. return nil;
  124. }
  125. }
  126. return buf;
  127. }
  128. - (NSString *)findValue:(int)i rawText:(NSString *)rawText {
  129. NSMutableString *buf = [NSMutableString string];
  130. NSString *rawTextAux = [rawText substringFromIndex:i];
  131. for (int index = 0; index < [rawTextAux length]; index++) {
  132. unichar c = [rawTextAux characterAtIndex:index];
  133. if (c == '(') {
  134. // We look for a new AI. If it doesn't exist (ERROR), we coninue
  135. // with the iteration
  136. if ([self findAIvalue:index rawText:rawTextAux] == nil) {
  137. [buf appendString:@"("];
  138. } else {
  139. break;
  140. }
  141. } else {
  142. [buf appendFormat:@"%C", c];
  143. }
  144. }
  145. return buf;
  146. }
  147. @end