Skip to content

Commit ced0099

Browse files
committed
• SSYRSSParserClass is "done" now.
1 parent 8766967 commit ced0099

File tree

2 files changed

+63
-28
lines changed

2 files changed

+63
-28
lines changed

SSYRSSParser.h

+44-12
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ extern NSString* const SSYRSSParserErrorDomain ;
66
/*!
77
@brief A class for parsing RSS feeds
88
9-
@detail I made this as a pretty much drop-in replacement for Brent Simmons'
10-
'RSS' class which he published in year 2002, and which was based on the
11-
now-deprecated CFXMLParser. This class is based on NSXMLParser instead.
9+
@detail I made this to replace Brent Simmons' 'RSS' class which he published
10+
in year 2002, and which was based on the now-deprecated CFXMLParser. This
11+
class is based on NSXMLParser instead.
1212
*/
1313
@interface SSYRSSParser : NSObject <NSXMLParserDelegate> {
1414
NSData* m_data ;
@@ -23,13 +23,37 @@ extern NSString* const SSYRSSParserErrorDomain ;
2323

2424
/*!
2525
@brief Designated initializer for SSYRSS class
26-
27-
@detail Synchronously parses .
2826
*/
29-
- (SSYRSSParser*)initWithData:(NSData*)data
30-
error_p:(NSError**)error ;
27+
- (SSYRSSParser*)initWithData:(NSData*)data ;
28+
29+
/*!
30+
@brief Synchronously parses the data
31+
@details You must invoke this method prior to -headerItems, -newsItems or
32+
-version in order for the latter methods to give expected results.
33+
@param error_p If result is NO, upon return, will point to an NSError
34+
explaining the problem.
35+
@result YES if data was successfully parsed; otherwise NO
36+
*/
37+
- (BOOL)parseError_p:(NSError**)error ;
38+
39+
/*!
40+
@brief Returns the element keys and values parsed from the header of the
41+
receiver's data
42+
@details Will return nil if -parseError_p: has not run successfully.
43+
*/
3144
- (NSMutableDictionary*)headerItems ;
45+
46+
/*!
47+
@brief Returns the news items parsed from the receiver's data. Each item is
48+
a dictionary containing the item's element keys and values.
49+
@details Will return nil if -parseError_p: has not run successfully.
50+
*/
3251
- (NSMutableArray*)newsItems ;
52+
53+
/*!
54+
@brief Returns the version parsed from the RSS (not the version of the XML)
55+
@details Will return nil if -parseError_p: has not run successfully.
56+
*/
3357
- (NSString*)version ;
3458

3559
@end
@@ -77,17 +101,25 @@ extern NSString* const SSYRSSParserErrorDomain ;
77101

78102
+ (void)test {
79103
NSError* error ;
80-
SSYRSSParser* parser = [[SSYRSSParser alloc] initWithData:[self testData]
81-
error_p:&error] ;
82-
printf("*** Parsed version: %s\n", [[[parser version] description] UTF8String]) ;
83-
printf("*** Parsed header items:\n%s\n", [[[parser headerItems] description] UTF8String]) ;
84-
printf("*** Parsed news items:\n%s\n", [[[parser newsItems] description] UTF8String]) ;
104+
SSYRSSParser* parser = [[SSYRSSParser alloc] initWithData:[self testData]] ;
105+
BOOL ok = [parser parseError_p:&error] ;
106+
NSString* judgment = ok ? @"succeeded" : @"failed" ;
107+
printf("*** Parser %s with error: %s\n",
108+
[judgment UTF8String],
109+
[[error description] UTF8String]) ;
110+
printf("*** Parsed version: %s\n",
111+
[[[parser version] description] UTF8String]) ;
112+
printf("*** Parsed header items:\n%s\n",
113+
[[[parser headerItems] description] UTF8String]) ;
114+
printf("*** Parsed news items:\n%s\n",
115+
[[[parser newsItems] description] UTF8String]) ;
85116
[parser release] ;
86117
}
87118

88119
#if 0
89120
***** Expected result from +test : *****
90121
********************************************************************************
122+
*** Parser succeeded with error: (null)
91123
*** Parsed version: 2.02
92124
*** Parsed header items:
93125
{

SSYRSSParser.m

+19-16
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ @implementation SSYRSSParser
2929
@synthesize currentElementValue = m_currentElementValue ;
3030
@synthesize currentItem = m_currentItem ;
3131

32-
- (SSYRSSParser*)initWithData:(NSData*)data
33-
error_p:(NSError**)error_p {
32+
- (SSYRSSParser*)initWithData:(NSData*)data {
3433
if (data) {
3534
self = [super init] ;
3635

@@ -58,26 +57,30 @@ - (SSYRSSParser*)initWithData:(NSData*)data
5857
object = [[NSMutableDictionary alloc] init] ;
5958
[self setCurrentItem:object] ;
6059
[object release] ;
61-
62-
NSXMLParser* parser = [[NSXMLParser alloc] initWithData:data] ;
63-
[parser setDelegate:self] ;
64-
[parser setShouldResolveExternalEntities:YES] ;
65-
[parser parse] ;
66-
[parser release] ;
67-
68-
if ([self error]) {
69-
[self release] ;
70-
self = nil ;
71-
if (error_p) {
72-
*error_p = [self error] ;
73-
}
74-
}
7560
}
7661
}
7762

7863
return self ;
7964
}
8065

66+
- (BOOL)parseError_p:(NSError**)error_p {
67+
BOOL ok = YES ;
68+
NSXMLParser* parser = [[NSXMLParser alloc] initWithData:[self data]] ;
69+
[parser setDelegate:self] ;
70+
[parser setShouldResolveExternalEntities:YES] ;
71+
[parser parse] ;
72+
[parser release] ;
73+
74+
if ([self error]) {
75+
ok = NO ;
76+
if (error_p) {
77+
*error_p = [self error] ;
78+
}
79+
}
80+
81+
return ok ;
82+
}
83+
8184
- (void) dealloc {
8285
[m_data release] ;
8386
[m_error release] ;

0 commit comments

Comments
 (0)