Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 "ZXURIResultParser.h"
  17. #import "ZXResult.h"
  18. #import "ZXURIParsedResult.h"
  19. static NSRegularExpression *ZX_URL_WITH_PROTOCOL_PATTERN = nil;
  20. static NSRegularExpression *ZX_URL_WITHOUT_PROTOCOL_PATTERN = nil;
  21. @implementation ZXURIResultParser
  22. + (void)initialize {
  23. if ([self class] != [ZXURIResultParser class]) return;
  24. // See http://www.ietf.org/rfc/rfc2396.txt
  25. ZX_URL_WITH_PROTOCOL_PATTERN = [[NSRegularExpression alloc] initWithPattern:@"^[a-zA-Z][a-zA-Z0-9+-.]+:"
  26. options:0
  27. error:nil];
  28. ZX_URL_WITHOUT_PROTOCOL_PATTERN = [[NSRegularExpression alloc] initWithPattern:
  29. [[@"([a-zA-Z0-9\\-]+\\.)+[a-zA-Z]{2,}" // host name elements
  30. stringByAppendingString:@"(:\\d{1,5})?"] // maybe port
  31. stringByAppendingString:@"(/|\\?|$)"] // query, path or nothing
  32. options:0
  33. error:nil];
  34. }
  35. - (ZXParsedResult *)parse:(ZXResult *)result {
  36. NSString *rawText = [ZXResultParser massagedText:result];
  37. // We specifically handle the odd "URL" scheme here for simplicity and add "URI" for fun
  38. // Assume anything starting this way really means to be a URI
  39. if ([rawText hasPrefix:@"URL:"] || [rawText hasPrefix:@"URI:"]) {
  40. return [[ZXURIParsedResult alloc] initWithUri:[[rawText substringFromIndex:4] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]
  41. title:nil];
  42. }
  43. rawText = [rawText stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
  44. return [[self class] isBasicallyValidURI:rawText] ? [ZXURIParsedResult uriParsedResultWithUri:rawText title:nil] : nil;
  45. }
  46. + (BOOL)isBasicallyValidURI:(NSString *)uri {
  47. if ([uri rangeOfString:@" "].location != NSNotFound) {
  48. // Quick hack check for a common case
  49. return NO;
  50. }
  51. if ([ZX_URL_WITH_PROTOCOL_PATTERN numberOfMatchesInString:uri options:NSMatchingWithoutAnchoringBounds range:NSMakeRange(0, uri.length)] > 0) { // match at start only
  52. return YES;
  53. }
  54. return [ZX_URL_WITHOUT_PROTOCOL_PATTERN numberOfMatchesInString:uri options:NSMatchingWithoutAnchoringBounds range:NSMakeRange(0, uri.length)] > 0;
  55. }
  56. @end