Add CBarcodeFormat (which mirrors BarcodeFormat, but is in Obj-C). Pass it to all ResultParsers.

git-svn-id: https://zxing.googlecode.com/svn/trunk@1856 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
gln@google.com 2011-07-13 18:10:33 +00:00
parent f6e9f3d2cd
commit 8b0ca11c98
17 changed files with 144 additions and 27 deletions

View file

@ -0,0 +1,37 @@
#ifndef __CBARCODE_FORMAT_H__
#define __CBARCODE_FORMAT_H__
/*
* CBarcodeFormat.h
* zxing
*
* Copyright 2011 ZXing authors All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// This must remain parallel to zxing::BarcodeFormat.
typedef enum BarcodeFormat {
BarcodeFormat_None = 0,
BarcodeFormat_QR_CODE,
BarcodeFormat_DATA_MATRIX,
BarcodeFormat_UPC_E,
BarcodeFormat_UPC_A,
BarcodeFormat_EAN_8,
BarcodeFormat_EAN_13,
BarcodeFormat_CODE_128,
BarcodeFormat_CODE_39,
BarcodeFormat_ITF
} BarcodeFormat;
#endif // __CBARCODE_FORMAT_H__

View file

@ -0,0 +1,49 @@
/**
* Copyright 2011 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#import "CBarcodeFormat.h"
#import "BarcodeFormat.h"
BarcodeFormat CBarcodeFormatConvert(zxing::BarcodeFormat value);
// The purpose of this function is to issue a warning when a value is added to
// zxing::BarcodeFormat.
BarcodeFormat CBarcodeFormatConvert(zxing::BarcodeFormat value) {
switch (value) {
case zxing::BarcodeFormat_None:
return BarcodeFormat_None;
case zxing::BarcodeFormat_QR_CODE:
return BarcodeFormat_QR_CODE;
case zxing::BarcodeFormat_DATA_MATRIX:
return BarcodeFormat_DATA_MATRIX;
case zxing::BarcodeFormat_UPC_E:
return BarcodeFormat_UPC_E;
case zxing::BarcodeFormat_UPC_A:
return BarcodeFormat_UPC_A;
case zxing::BarcodeFormat_EAN_8:
return BarcodeFormat_EAN_8;
case zxing::BarcodeFormat_EAN_13:
return BarcodeFormat_EAN_13;
case zxing::BarcodeFormat_CODE_128:
return BarcodeFormat_CODE_128;
case zxing::BarcodeFormat_CODE_39:
return BarcodeFormat_CODE_39;
case zxing::BarcodeFormat_ITF:
return BarcodeFormat_ITF;
}
return BarcodeFormat_None;
}

View file

@ -21,6 +21,7 @@
#import "BookmarkDoCoMoResultParser.h" #import "BookmarkDoCoMoResultParser.h"
#import "URIParsedResult.h" #import "URIParsedResult.h"
#import "CBarcodeFormat.h"
@implementation BookmarkDoCoMoResultParser @implementation BookmarkDoCoMoResultParser
@ -28,7 +29,8 @@
[ResultParser registerResultParserClass:self]; [ResultParser registerResultParserClass:self];
} }
+ (ParsedResult *)parsedResultForString:(NSString *)s { + (ParsedResult *)parsedResultForString:(NSString *)s
format:(BarcodeFormat)format {
NSRange foundRange = [s rangeOfString:@"MEBKM:"]; NSRange foundRange = [s rangeOfString:@"MEBKM:"];
if (foundRange.location == NSNotFound) { if (foundRange.location == NSNotFound) {
return nil; return nil;

View file

@ -21,6 +21,7 @@
#import "EmailDoCoMoResultParser.h" #import "EmailDoCoMoResultParser.h"
#import "EmailParsedResult.h" #import "EmailParsedResult.h"
#import "CBarcodeFormat.h"
@implementation EmailDoCoMoResultParser @implementation EmailDoCoMoResultParser
@ -28,7 +29,8 @@
[ResultParser registerResultParserClass:self]; [ResultParser registerResultParserClass:self];
} }
+ (ParsedResult *)parsedResultForString:(NSString *)s { + (ParsedResult *)parsedResultForString:(NSString *)s
format:(BarcodeFormat)format {
NSRange foundRange = [s rangeOfString:@"MATMSG:"]; NSRange foundRange = [s rangeOfString:@"MATMSG:"];
if (foundRange.location == NSNotFound) { if (foundRange.location == NSNotFound) {
return nil; return nil;

View file

@ -21,6 +21,7 @@
#import "GeoResultParser.h" #import "GeoResultParser.h"
#import "GeoParsedResult.h" #import "GeoParsedResult.h"
#import "CBarcodeFormat.h"
#define PREFIX @"geo:" #define PREFIX @"geo:"
@ -30,7 +31,8 @@
[ResultParser registerResultParserClass:self]; [ResultParser registerResultParserClass:self];
} }
+ (ParsedResult *)parsedResultForString:(NSString *)s { + (ParsedResult *)parsedResultForString:(NSString *)s
format:(BarcodeFormat)format {
NSRange prefixRange = [s rangeOfString:PREFIX options:NSCaseInsensitiveSearch]; NSRange prefixRange = [s rangeOfString:PREFIX options:NSCaseInsensitiveSearch];
if (prefixRange.location == 0) { if (prefixRange.location == 0) {
int restStart = /*prefixRange.location + */ prefixRange.length; int restStart = /*prefixRange.location + */ prefixRange.length;

View file

@ -21,6 +21,7 @@
#import "MeCardParser.h" #import "MeCardParser.h"
#import "BusinessCardParsedResult.h" #import "BusinessCardParsedResult.h"
#import "CBarcodeFormat.h"
@implementation MeCardParser @implementation MeCardParser
@ -28,7 +29,8 @@
[ResultParser registerResultParserClass:self]; [ResultParser registerResultParserClass:self];
} }
+ (ParsedResult *)parsedResultForString:(NSString *)s { + (ParsedResult *)parsedResultForString:(NSString *)s
format:(BarcodeFormat)format {
NSRange foundRange = [s rangeOfString:@"MECARD:"]; NSRange foundRange = [s rangeOfString:@"MECARD:"];
if (foundRange.location == NSNotFound) { if (foundRange.location == NSNotFound) {
return nil; return nil;

View file

@ -21,6 +21,7 @@
#import "PlainEmailResultParser.h" #import "PlainEmailResultParser.h"
#import "EmailParsedResult.h" #import "EmailParsedResult.h"
#import "CBarcodeFormat.h"
@implementation PlainEmailResultParser @implementation PlainEmailResultParser
@ -28,7 +29,8 @@
[ResultParser registerResultParserClass:self]; [ResultParser registerResultParserClass:self];
} }
+ (ParsedResult *)parsedResultForString:(NSString *)s { + (ParsedResult *)parsedResultForString:(NSString *)s
format:(BarcodeFormat)format {
if ([EmailParsedResult looksLikeAnEmailAddress:s]) { if ([EmailParsedResult looksLikeAnEmailAddress:s]) {
EmailParsedResult *result = [[[EmailParsedResult alloc] init] autorelease]; EmailParsedResult *result = [[[EmailParsedResult alloc] init] autorelease];
[result setTo:s]; [result setTo:s];

View file

@ -21,11 +21,14 @@
#import <UIKit/UIKit.h> #import <UIKit/UIKit.h>
#import "../parsedResults/ParsedResult.h" #import "../parsedResults/ParsedResult.h"
#import "CBarcodeFormat.h"
@interface ResultParser : NSObject { @interface ResultParser : NSObject {
} }
+ (void)registerResultParserClass:(Class)resultParser; + (void)registerResultParserClass:(Class)resultParser;
+ (ParsedResult *)parsedResultForString:(NSString *)s; + (ParsedResult *)parsedResultForString:(NSString *)s;
+ (ParsedResult *)parsedResultForString:(NSString *)s
format:(BarcodeFormat)barcodeFormat;
@end @end

View file

@ -20,6 +20,7 @@
*/ */
#import "ResultParser.h" #import "ResultParser.h"
#import "TextResultParser.h"
@implementation ResultParser @implementation ResultParser
@ -44,7 +45,8 @@ static NSMutableSet *sResultParsers = nil;
return resultParsers; return resultParsers;
} }
+ (ParsedResult *)parsedResultForString:(NSString *)s { + (ParsedResult *)parsedResultForString:(NSString *)s
format:(BarcodeFormat)barcodeFormat {
#ifdef DEBUG #ifdef DEBUG
NSLog(@"parsing result:\n<<<\n%@\n>>>\n", s); NSLog(@"parsing result:\n<<<\n%@\n>>>\n", s);
#endif #endif
@ -52,7 +54,7 @@ static NSMutableSet *sResultParsers = nil;
#ifdef DEBUG #ifdef DEBUG
NSLog(@"trying %@", NSStringFromClass(c)); NSLog(@"trying %@", NSStringFromClass(c));
#endif #endif
ParsedResult *result = [c parsedResultForString:s]; ParsedResult *result = [c parsedResultForString:s format:barcodeFormat];
if (result != nil) { if (result != nil) {
#ifdef DEBUG #ifdef DEBUG
NSLog(@"parsed as %@ %@", NSStringFromClass([result class]), result); NSLog(@"parsed as %@ %@", NSStringFromClass([result class]), result);
@ -60,7 +62,15 @@ static NSMutableSet *sResultParsers = nil;
return result; return result;
} }
} }
return nil;
#ifdef DEBUG
NSLog(@"No result parsers matched. Falling back to text.");
#endif
return [TextResultParser parsedResultForString:s format:barcodeFormat];
}
+ (ParsedResult *)parsedResultForString:(NSString *)s {
return [ResultParser parsedResultForString:s format:BarcodeFormat_None];
} }
@end @end

View file

@ -21,6 +21,7 @@
#import "SMSResultParser.h" #import "SMSResultParser.h"
#import "SMSParsedResult.h" #import "SMSParsedResult.h"
#import "CBarcodeFormat.h"
#define PREFIX @"sms:" #define PREFIX @"sms:"
@ -30,7 +31,8 @@
[ResultParser registerResultParserClass:self]; [ResultParser registerResultParserClass:self];
} }
+ (ParsedResult *)parsedResultForString:(NSString *)s { + (ParsedResult *)parsedResultForString:(NSString *)s
format:(BarcodeFormat)format {
NSRange prefixRange = [s rangeOfString:PREFIX options:NSCaseInsensitiveSearch]; NSRange prefixRange = [s rangeOfString:PREFIX options:NSCaseInsensitiveSearch];
if (prefixRange.location == 0) { if (prefixRange.location == 0) {
int max = [s length]; int max = [s length];

View file

@ -21,6 +21,7 @@
#import "SMSTOResultParser.h" #import "SMSTOResultParser.h"
#import "SMSParsedResult.h" #import "SMSParsedResult.h"
#import "CBarcodeFormat.h"
#define PREFIX @"SMSTO:" #define PREFIX @"SMSTO:"
@ -30,7 +31,8 @@
[ResultParser registerResultParserClass:self]; [ResultParser registerResultParserClass:self];
} }
+ (ParsedResult *)parsedResultForString:(NSString *)s { + (ParsedResult *)parsedResultForString:(NSString *)s
format:(BarcodeFormat)format {
NSRange prefixRange = [s rangeOfString:PREFIX options:NSCaseInsensitiveSearch]; NSRange prefixRange = [s rangeOfString:PREFIX options:NSCaseInsensitiveSearch];
if (prefixRange.location == 0) { if (prefixRange.location == 0) {
int max = [s length]; int max = [s length];

View file

@ -21,6 +21,7 @@
#import "TelResultParser.h" #import "TelResultParser.h"
#import "TelParsedResult.h" #import "TelParsedResult.h"
#import "CBarcodeFormat.h"
#define PREFIX @"tel:" #define PREFIX @"tel:"
@ -30,7 +31,8 @@
[ResultParser registerResultParserClass:self]; [ResultParser registerResultParserClass:self];
} }
+ (ParsedResult *)parsedResultForString:(NSString *)s { + (ParsedResult *)parsedResultForString:(NSString *)s
format:(BarcodeFormat)format {
NSRange telRange = [s rangeOfString:PREFIX options:NSCaseInsensitiveSearch]; NSRange telRange = [s rangeOfString:PREFIX options:NSCaseInsensitiveSearch];
if (telRange.location == 0) { if (telRange.location == 0) {
int restStart = /*telRange.location + */ telRange.length; int restStart = /*telRange.location + */ telRange.length;

View file

@ -21,14 +21,12 @@
#import "TextResultParser.h" #import "TextResultParser.h"
#import "TextParsedResult.h" #import "TextParsedResult.h"
#import "CBarcodeFormat.h"
@implementation TextResultParser @implementation TextResultParser
+ (void)load { + (ParsedResult *)parsedResultForString:(NSString *)s
[ResultParser registerResultParserClass:self]; format:(BarcodeFormat)format {
}
+ (ParsedResult *)parsedResultForString:(NSString *)s {
return [[[TextParsedResult alloc] initWithString:s] autorelease]; return [[[TextParsedResult alloc] initWithString:s] autorelease];
} }

View file

@ -21,6 +21,7 @@
#import "URLResultParser.h" #import "URLResultParser.h"
#import "URIParsedResult.h" #import "URIParsedResult.h"
#import "CBarcodeFormat.h"
@implementation NSString (ZXingURLExtensions) @implementation NSString (ZXingURLExtensions)
@ -59,7 +60,8 @@
[ResultParser registerResultParserClass:self]; [ResultParser registerResultParserClass:self];
} }
+ (ParsedResult *)parsedResultForString:(NSString *)s { + (ParsedResult *)parsedResultForString:(NSString *)s
format:(BarcodeFormat)format {
NSAutoreleasePool *myPool = [[NSAutoreleasePool alloc] init]; NSAutoreleasePool *myPool = [[NSAutoreleasePool alloc] init];
ParsedResult *result = nil; ParsedResult *result = nil;

View file

@ -21,6 +21,7 @@
#import "URLTOResultParser.h" #import "URLTOResultParser.h"
#import "URIParsedResult.h" #import "URIParsedResult.h"
#import "CBarcodeFormat.h"
#define PREFIX @"URLTO:" #define PREFIX @"URLTO:"
@ -30,7 +31,8 @@
[ResultParser registerResultParserClass:self]; [ResultParser registerResultParserClass:self];
} }
+ (ParsedResult *)parsedResultForString:(NSString *)s { + (ParsedResult *)parsedResultForString:(NSString *)s
format:(BarcodeFormat)format {
NSRange prefixRange = [s rangeOfString:PREFIX options:NSCaseInsensitiveSearch]; NSRange prefixRange = [s rangeOfString:PREFIX options:NSCaseInsensitiveSearch];
if (prefixRange.location == 0) { if (prefixRange.location == 0) {
int max = [s length]; int max = [s length];

View file

@ -16,5 +16,6 @@
//@property(nonatomic,retain) NSMutableArray *parsers; //@property(nonatomic,retain) NSMutableArray *parsers;
+ (void)initWithDefaultParsers; + (void)initWithDefaultParsers;
+ (ParsedResult *)parsedResultForString:(NSString *)theString; + (ParsedResult *)parsedResultForString:(NSString *)s
format:(BarcodeFormat)format;
@end @end

View file

@ -18,12 +18,12 @@
#import "BookmarkDoCoMoResultParser.h" #import "BookmarkDoCoMoResultParser.h"
#import "GeoResultParser.h" #import "GeoResultParser.h"
#import "TextResultParser.h" #import "TextResultParser.h"
#import "CBarcodeFormat.h"
@implementation UniversalResultParser @implementation UniversalResultParser
static NSMutableArray *sTheResultParsers = nil; static NSMutableArray *sTheResultParsers = nil;
//@synthesize parsers; //@synthesize parsers;
//static NSMutableSet *sResultParsers = nil;
+(void) load { +(void) load {
[self initWithDefaultParsers]; [self initWithDefaultParsers];
} }
@ -33,11 +33,6 @@ static NSMutableArray *sTheResultParsers = nil;
} }
+ (void) initWithDefaultParsers { + (void) initWithDefaultParsers {
// NSMutableArray *set = [[NSMutableArray alloc] initWithCapacity:11];
// self.parsers = set;
// [set release];
//
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
@synchronized(self) { @synchronized(self) {
if (!sTheResultParsers) { if (!sTheResultParsers) {
@ -58,7 +53,8 @@ static NSMutableArray *sTheResultParsers = nil;
[self addParserClass:[TextResultParser class]]; [self addParserClass:[TextResultParser class]];
} }
+ (ParsedResult *)parsedResultForString:(NSString *)s { + (ParsedResult *)parsedResultForString:(NSString *)s
format:(BarcodeFormat)format {
#ifdef DEBUG #ifdef DEBUG
NSLog(@"parsing result:\n<<<\n%@\n>>>\n", s); NSLog(@"parsing result:\n<<<\n%@\n>>>\n", s);
#endif #endif
@ -66,7 +62,7 @@ static NSMutableArray *sTheResultParsers = nil;
#ifdef DEBUG #ifdef DEBUG
NSLog(@"trying %@", NSStringFromClass(c)); NSLog(@"trying %@", NSStringFromClass(c));
#endif #endif
ParsedResult *result = [c parsedResultForString:s]; ParsedResult *result = [c parsedResultForString:s format:format];
if (result != nil) { if (result != nil) {
#ifdef DEBUG #ifdef DEBUG
NSLog(@"parsed as %@ %@", NSStringFromClass([result class]), result); NSLog(@"parsed as %@ %@", NSStringFromClass([result class]), result);
@ -77,6 +73,9 @@ static NSMutableArray *sTheResultParsers = nil;
return nil; return nil;
} }
+ (ParsedResult *)parsedResultForString:(NSString *)theString {
return [self parsedResultForString:theString format:BarcodeFormat_None];
}
-(void)dealloc { -(void)dealloc {
[super dealloc]; [super dealloc];