Playlist Generator  1.0
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Properties Defines
SBJsonStreamParserAdapter.m
Go to the documentation of this file.
00001 /*
00002  Copyright (c) 2010, Stig Brautaset.
00003  All rights reserved.
00004  
00005  Redistribution and use in source and binary forms, with or without
00006  modification, are permitted provided that the following conditions are
00007  met:
00008  
00009    Redistributions of source code must retain the above copyright
00010    notice, this list of conditions and the following disclaimer.
00011   
00012    Redistributions in binary form must reproduce the above copyright
00013    notice, this list of conditions and the following disclaimer in the
00014    documentation and/or other materials provided with the distribution.
00015  
00016    Neither the name of the the author nor the names of its contributors
00017    may be used to endorse or promote products derived from this software
00018    without specific prior written permission.
00019  
00020  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
00021  IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
00022  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00023  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00024  HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00025  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00026  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00027  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00028  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00029  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00030  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031  */
00032 
00033 #import "SBJsonStreamParserAdapter.h"
00034 
00035 @interface SBJsonStreamParserAdapter ()
00036 
00037 - (void)pop;
00038 - (void)parser:(SBJsonStreamParser*)parser found:(id)obj;
00039 
00040 @end
00041 
00042 
00043 
00044 @implementation SBJsonStreamParserAdapter
00045 
00046 @synthesize delegate;
00047 @synthesize levelsToSkip;
00048 
00049 #pragma mark Housekeeping
00050 
00051 - (id)init {
00052         self = [super init];
00053         if (self) {
00054                 keyStack = [[NSMutableArray alloc] initWithCapacity:32];
00055                 stack = [[NSMutableArray alloc] initWithCapacity:32];
00056                 
00057                 currentType = SBJsonStreamParserAdapterNone;
00058         }
00059         return self;
00060 }       
00061 
00062 
00063 #pragma mark Private methods
00064 
00065 - (void)pop {
00066         [stack removeLastObject];
00067         array = nil;
00068         dict = nil;
00069         currentType = SBJsonStreamParserAdapterNone;
00070         
00071         id value = [stack lastObject];
00072         
00073         if ([value isKindOfClass:[NSArray class]]) {
00074                 array = value;
00075                 currentType = SBJsonStreamParserAdapterArray;
00076         } else if ([value isKindOfClass:[NSDictionary class]]) {
00077                 dict = value;
00078                 currentType = SBJsonStreamParserAdapterObject;
00079         }
00080 }
00081 
00082 - (void)parser:(SBJsonStreamParser*)parser found:(id)obj {
00083         NSParameterAssert(obj);
00084         
00085         switch (currentType) {
00086                 case SBJsonStreamParserAdapterArray:
00087                         [array addObject:obj];
00088                         break;
00089 
00090                 case SBJsonStreamParserAdapterObject:
00091                         NSParameterAssert(keyStack.count);
00092                         [dict setObject:obj forKey:[keyStack lastObject]];
00093                         [keyStack removeLastObject];
00094                         break;
00095                         
00096                 case SBJsonStreamParserAdapterNone:
00097                         if ([obj isKindOfClass:[NSArray class]]) {
00098                                 [delegate parser:parser foundArray:obj];
00099                         } else {
00100                                 [delegate parser:parser foundObject:obj];
00101                         }                               
00102                         break;
00103 
00104                 default:
00105                         break;
00106         }
00107 }
00108 
00109 
00110 #pragma mark Delegate methods
00111 
00112 - (void)parserFoundObjectStart:(SBJsonStreamParser*)parser {
00113         if (++depth > self.levelsToSkip) {
00114                 dict = [NSMutableDictionary new];
00115                 [stack addObject:dict];
00116                 currentType = SBJsonStreamParserAdapterObject;
00117         }
00118 }
00119 
00120 - (void)parser:(SBJsonStreamParser*)parser foundObjectKey:(NSString*)key_ {
00121         [keyStack addObject:key_];
00122 }
00123 
00124 - (void)parserFoundObjectEnd:(SBJsonStreamParser*)parser {
00125         if (depth-- > self.levelsToSkip) {
00126                 id value = dict;
00127                 [self pop];
00128                 [self parser:parser found:value];
00129         }
00130 }
00131 
00132 - (void)parserFoundArrayStart:(SBJsonStreamParser*)parser {
00133         if (++depth > self.levelsToSkip) {
00134                 array = [NSMutableArray new];
00135                 [stack addObject:array];
00136                 currentType = SBJsonStreamParserAdapterArray;
00137         }
00138 }
00139 
00140 - (void)parserFoundArrayEnd:(SBJsonStreamParser*)parser {
00141         if (depth-- > self.levelsToSkip) {
00142                 id value = array;
00143                 [self pop];
00144                 [self parser:parser found:value];
00145         }
00146 }
00147 
00148 - (void)parser:(SBJsonStreamParser*)parser foundBoolean:(BOOL)x {
00149         [self parser:parser found:[NSNumber numberWithBool:x]];
00150 }
00151 
00152 - (void)parserFoundNull:(SBJsonStreamParser*)parser {
00153         [self parser:parser found:[NSNull null]];
00154 }
00155 
00156 - (void)parser:(SBJsonStreamParser*)parser foundNumber:(NSNumber*)num {
00157         [self parser:parser found:num];
00158 }
00159 
00160 - (void)parser:(SBJsonStreamParser*)parser foundString:(NSString*)string {
00161         [self parser:parser found:string];
00162 }
00163 
00164 @end