YHLinkDefaultParser.m 5.49 KB
//
//  YHLinkDefaultParser.m
//  YohoExplorerDemo
//
//  Created by gaoqiang xu on 4/21/15.
//  Copyright (c) 2015 gaoqiang xu. All rights reserved.
//

#import "YHLinkDefaultParser.h"

NSString * const YHLinkParseResult_FreshUrlKey = @"YHLinkParseResult_FreshUrl";
NSString * const YHLinkParseResult_OpenByKey = @"YHLinkParseResult_OpenBy";
NSString * const YHLinkParseResult_OpenParametersKey = @"YHLinkParseResult_OpenParameters";
NSString * const YHLinkParseResult_OriginalUrlKey = @"YHLinkParseResult_OriginalUrl";

@implementation YHLinkDefaultParser
@synthesize completion = _completion;

- (NSString *)leadingString
{
    return @"openby:";
}

- (BOOL)parseString:(NSString *)string
{
    if (string.length == 0) {
        return NO;
    }
    
    BOOL b = NO;
    
    NSString *freshUrl = @"";
    NSString *contentString = string;
    
    NSString *begin = [self leadingString];
    
    NSRange range = [contentString rangeOfString:begin];
    if (range.location != NSNotFound) {
        contentString = [contentString substringFromIndex:range.location+range.length];
        freshUrl = [string substringToIndex:range.location];
        if (freshUrl.length > 0
            && [[freshUrl substringWithRange:NSMakeRange(freshUrl.length-1, 1)] isEqualToString:@"?"]) {
            freshUrl = [freshUrl substringToIndex:freshUrl.length-1];
        }
    } else {
        return b;
    }
    
    if (contentString.length == 0) {
        return b;
    }
    
    contentString = [contentString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    NSArray *comps = nil;
    NSRange rangeOfEqual = [contentString rangeOfString:@"="];
    if (rangeOfEqual.location != NSNotFound) {
        comps = @[ [contentString substringToIndex:rangeOfEqual.location],
                   [contentString substringFromIndex:rangeOfEqual.location+rangeOfEqual.length] ];
    } else {// 兼容全角的"="
        rangeOfEqual = [contentString rangeOfString:@"="];
        if (rangeOfEqual.location != NSNotFound) {
            comps = @[ [contentString substringToIndex:rangeOfEqual.location],
                       [contentString substringFromIndex:rangeOfEqual.location+rangeOfEqual.length] ];
        }
    }

    if ([comps[0] isEqualToString:YHLinkOpenByType_yohobuy]
        || [comps[0] isEqualToString:YHLinkOpenByType_show]
        || [comps[0] isEqualToString:YHLinkOpenByType_boy]
        || [comps[0] isEqualToString:YHLinkOpenByType_girl]
        || [comps[0] isEqualToString:YHLinkOpenByType_browser]
        || [comps[0] isEqualToString:YHLinkOpenByType_mars]) {

        NSString *comp1 = comps[0];
        NSError *error = nil;
        NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:[comps.lastObject dataUsingEncoding:NSUTF8StringEncoding]
                                                                options:NSJSONReadingMutableContainers
                                                                  error:&error];
        if (error || !jsonDic) {
            b = NO;
        } else {
            b = YES;
            !self.completion?:self.completion(@{ YHLinkParseResult_OpenByKey:comp1,
                                                 YHLinkParseResult_OpenParametersKey:jsonDic,
                                                 YHLinkParseResult_FreshUrlKey: freshUrl,
                                                 YHLinkParseResult_OriginalUrlKey:[string stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding] });
        }
    }
    
    return b;
}

@end

#pragma mark - YHLinkParserOld

@implementation YHLinkParserOld
@synthesize completion = _completion;

- (NSString *)leadingString
{
    return @"app:";
}

- (BOOL)parseString:(NSString *)string
{
    if (string.length == 0) {
        return NO;
    }
    
    BOOL b = NO;
    
    NSString *freshUrl = @"";
    NSString *contentString = string;
    
    NSString *begin = [self leadingString];
    
    NSRange range = [contentString rangeOfString:begin];
    if (range.location != NSNotFound) {
        contentString = [contentString substringFromIndex:range.location+range.length];
        freshUrl = [string substringToIndex:range.location];
        if ([[freshUrl substringWithRange:NSMakeRange(freshUrl.length-1, 1)] isEqualToString:@"?"]) {
            freshUrl = [freshUrl substringToIndex:freshUrl.length-1];
        }
    } else {
        return b;
    }
    
    if (contentString.length == 0) {
        return b;
    }
    
    contentString = [contentString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    NSArray *comps = [contentString componentsSeparatedByString:@","];
    
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithCapacity:1];
    for (NSString *kv in comps) {
        NSRange equalRange = [kv rangeOfString:@"="];
        if (equalRange.location == NSNotFound) {
            continue;
        }
        
        NSString *key = [kv substringToIndex:equalRange.location];
        NSString *value = [kv substringFromIndex:equalRange.location+1];
        
        dic[key] = value;
    }
    
    if ([dic count] > 0) {
        b = YES;
        !self.completion?:self.completion(@{ YHLinkParseResult_OpenByKey:YHLinkOpenByType_compatible,
                                             YHLinkParseResult_OpenParametersKey:dic,
                                             YHLinkParseResult_FreshUrlKey: freshUrl,
                                             YHLinkParseResult_OriginalUrlKey:[string stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding] });
    }
    
    return b;
}

@end