Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to parse from NSData #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Classes/MWFeedParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
@class MWFeedParser;

// Types
typedef enum { ConnectionTypeAsynchronously, ConnectionTypeSynchronously } ConnectionType;
typedef enum { ConnectionTypeAsynchronously, ConnectionTypeSynchronously, ConnectionTypeLocalData } ConnectionType;
typedef enum { ParseTypeFull, ParseTypeItemsOnly, ParseTypeInfoOnly } ParseType;
typedef enum { FeedTypeUnknown, FeedTypeRSS, FeedTypeRSS1, FeedTypeAtom } FeedType;

Expand All @@ -74,8 +74,8 @@ typedef enum { FeedTypeUnknown, FeedTypeRSS, FeedTypeRSS1, FeedTypeAtom } FeedTy

// Connection
NSURLConnection *urlConnection;
NSMutableData *asyncData;
NSString *asyncTextEncodingName;
NSMutableData *xmlData;
NSString *xmlTextEncodingName;
ConnectionType connectionType;

// Parsing
Expand Down Expand Up @@ -130,6 +130,7 @@ typedef enum { FeedTypeUnknown, FeedTypeRSS, FeedTypeRSS1, FeedTypeAtom } FeedTy

// Init MWFeedParser with a URL string
- (id)initWithFeedURL:(NSURL *)feedURL;
- (id)initWithFeedData:(NSData *)data textEncodingName:(NSString *)textEncodingName;

// Begin parsing
- (BOOL)parse;
Expand Down
51 changes: 33 additions & 18 deletions Classes/MWFeedParser.m
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ @implementation MWFeedParser

// Properties
@synthesize url, delegate;
@synthesize urlConnection, asyncData, asyncTextEncodingName, connectionType;
@synthesize urlConnection, xmlData, xmlTextEncodingName, connectionType;
@synthesize feedParseType, feedParser, currentPath, currentText, currentElementAttributes, item, info;
@synthesize pathOfElementWithXHTMLType;
@synthesize stopped, failed, parsing;
Expand Down Expand Up @@ -97,6 +97,15 @@ - (id)initWithFeedURL:(NSURL *)feedURL {
return self;
}

- (id)initWithFeedData:(NSData *)data textEncodingName:(NSString *)textEncodingName {
if ((self = [self init])) {
connectionType = ConnectionTypeLocalData;
self.xmlData = [NSMutableData dataWithData:data];
self.xmlTextEncodingName = textEncodingName;
}
return self;
}

- (void)dealloc {
[urlConnection release];
[url release];
Expand All @@ -117,9 +126,12 @@ - (void)dealloc {

// Reset data variables before processing
// Exclude parse state variables as they are needed after parse
// Exclude data and encoding if using local data
- (void)reset {
self.asyncData = nil;
self.asyncTextEncodingName = nil;
if(connectionType != ConnectionTypeLocalData) {
self.xmlData = nil;
self.xmlTextEncodingName = nil;
}
self.urlConnection = nil;
feedType = FeedTypeUnknown;
self.currentPath = @"/";
Expand All @@ -139,7 +151,7 @@ - (BOOL)parse {
[self reset];

// Perform checks before parsing
if (!url || !delegate) { [self parsingFailedWithErrorCode:MWErrorCodeNotInitiated
if (connectionType!=ConnectionTypeLocalData && (!url || !delegate)) { [self parsingFailedWithErrorCode:MWErrorCodeNotInitiated
andDescription:@"Delegate or URL not specified"]; return NO; }
if (parsing) { [self parsingFailedWithErrorCode:MWErrorCodeGeneral
andDescription:@"Cannot start parsing as parsing is already in progress"]; return NO; }
Expand Down Expand Up @@ -169,14 +181,14 @@ - (BOOL)parse {
// Async
urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (urlConnection) {
asyncData = [[NSMutableData alloc] init];// Create data
xmlData = [[NSMutableData alloc] init];// Create data
} else {
[self parsingFailedWithErrorCode:MWErrorCodeConnectionFailed
andDescription:[NSString stringWithFormat:@"Asynchronous connection failed to URL: %@", url]];
success = NO;
}

} else {
} else if (connectionType == ConnectionTypeSynchronously) {

// Sync
NSURLResponse *response = nil;
Expand All @@ -190,7 +202,10 @@ - (BOOL)parse {
success = NO;
}

}
} else {
// Local data (no request)
[self startParsingData:self.xmlData textEncodingName:self.xmlTextEncodingName];
}

// Cleanup & return
[request release];
Expand Down Expand Up @@ -317,8 +332,8 @@ - (void)stopParsing {
// Stop downloading
[urlConnection cancel];
self.urlConnection = nil;
self.asyncData = nil;
self.asyncTextEncodingName = nil;
self.xmlData = nil;
self.xmlTextEncodingName = nil;

// Abort
aborted = YES;
Expand Down Expand Up @@ -389,20 +404,20 @@ - (void)parsingFailedWithErrorCode:(int)code andDescription:(NSString *)descript
#pragma mark NSURLConnection Delegate (Async)

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[asyncData setLength:0];
self.asyncTextEncodingName = [response textEncodingName];
[xmlData setLength:0];
self.xmlTextEncodingName = [response textEncodingName];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[asyncData appendData:data];
[xmlData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {

// Failed
self.urlConnection = nil;
self.asyncData = nil;
self.asyncTextEncodingName = nil;
self.xmlData = nil;
self.xmlTextEncodingName = nil;

// Error
[self parsingFailedWithErrorCode:MWErrorCodeConnectionFailed andDescription:[error localizedDescription]];
Expand All @@ -412,15 +427,15 @@ - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)err
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

// Succeed
MWLog(@"MWFeedParser: Connection successful... received %d bytes of data", [asyncData length]);
MWLog(@"MWFeedParser: Connection successful... received %d bytes of data", [xmlData length]);

// Parse
if (!stopped) [self startParsingData:asyncData textEncodingName:self.asyncTextEncodingName];
if (!stopped) [self startParsingData:xmlData textEncodingName:self.xmlTextEncodingName];

// Cleanup
self.urlConnection = nil;
self.asyncData = nil;
self.asyncTextEncodingName = nil;
self.xmlData = nil;
self.xmlTextEncodingName = nil;

}

Expand Down
4 changes: 2 additions & 2 deletions Classes/MWFeedParser_Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
// Feed Downloading Properties
@property (nonatomic, copy) NSURL *url;
@property (nonatomic, retain) NSURLConnection *urlConnection;
@property (nonatomic, retain) NSMutableData *asyncData;
@property (nonatomic, retain) NSString *asyncTextEncodingName;
@property (nonatomic, retain) NSMutableData *xmlData;
@property (nonatomic, retain) NSString *xmlTextEncodingName;

// Parsing Properties
@property (nonatomic, retain) NSXMLParser *feedParser;
Expand Down