forked from mwaterfall/MWFeedParser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MWFeedParser.h
157 lines (126 loc) · 5.62 KB
/
MWFeedParser.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//
// MWFeedParser.h
// MWFeedParser
//
// Copyright (c) 2010 Michael Waterfall
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// 1. The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// 2. This Software cannot be used to archive or collect data such as (but not
// limited to) that of events, news, experiences and activities, for the
// purpose of any concept relating to diary/journal keeping.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#import <Foundation/Foundation.h>
#import "MWFeedInfo.h"
#import "MWFeedItem.h"
// Debug Logging
#if 0 // Set to 1 to enable debug logging
#define MWLog(x, ...) NSLog(x, ## __VA_ARGS__);
#else
#define MWLog(x, ...)
#endif
// Errors & codes
#define MWErrorDomain @"MWFeedParser"
#define MWErrorCodeNotInitiated 1 /* MWFeedParser not initialised correctly */
#define MWErrorCodeConnectionFailed 2 /* Connection to the URL failed */
#define MWErrorCodeFeedParsingError 3 /* NSXMLParser encountered a parsing error */
#define MWErrorCodeFeedValidationError 4 /* NSXMLParser encountered a validation error */
#define MWErrorCodeGeneral 5 /* MWFeedParser general error */
// Class
@class MWFeedParser;
// Types
typedef enum { ConnectionTypeAsynchronously, ConnectionTypeSynchronously } ConnectionType;
typedef enum { ParseTypeFull, ParseTypeItemsOnly, ParseTypeInfoOnly } ParseType;
typedef enum { FeedTypeUnknown, FeedTypeRSS, FeedTypeRSS1, FeedTypeAtom } FeedType;
// Delegate
@protocol MWFeedParserDelegate <NSObject>
@optional
- (void)feedParserDidStart:(MWFeedParser *)parser;
- (void)feedParser:(MWFeedParser *)parser didParseFeedInfo:(MWFeedInfo *)info;
- (void)feedParser:(MWFeedParser *)parser didParseFeedItem:(MWFeedItem *)item;
- (void)feedParserDidFinish:(MWFeedParser *)parser;
- (void)feedParser:(MWFeedParser *)parser didFailWithError:(NSError *)error;
/** This allows for selection of alternative credentials when NSURLConnection finds present credentials in the shared storage.
* Return YES(default) if the parser should consult any present credentials without further asking.
* When NO is returned feedParser:didReceiveAuthenticationChallenge is called whenever the parser encounters authentication
*/
- (BOOL)feedParserShouldUseCredentialStorage:(MWFeedParser *)parser;
/** This method passes on to the NSURLConnection which the parser is using. It should be implemented when
* feedParser:shouldUseCredentialStorage returns YES. If in that case it is not, it throws an exception
*
*/
- (void)feedParser:(MWFeedParser *)parser didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;
@end
// MWFeedParser
@interface MWFeedParser : NSObject <NSXMLParserDelegate, NSURLConnectionDelegate, NSCopying> {
@private
// Required
__unsafe_unretained id <MWFeedParserDelegate> delegate;
// Connection
NSURLConnection *urlConnection;
NSMutableData *asyncData;
NSString *asyncTextEncodingName;
ConnectionType connectionType;
// Parsing
ParseType feedParseType;
NSXMLParser *feedParser;
FeedType feedType;
NSDateFormatter *dateFormatterRFC822, *dateFormatterRFC3339;
// Parsing State
NSURL *url;
BOOL aborted; // Whether parse stopped due to abort
BOOL parsing; // Whether the MWFeedParser has started parsing
BOOL stopped; // Whether the parse was stopped
BOOL failed; // Whether the parse failed
BOOL parsingComplete; // Whether NSXMLParser parsing has completed
BOOL hasEncounteredItems; // Whether the parser has started parsing items
// Parsing of XML structure as content
NSString *pathOfElementWithXHTMLType; // Hold the path of the element who's type="xhtml" so we can stop parsing when it's ended
BOOL parseStructureAsContent; // For atom feeds when element type="xhtml"
// Parsing Data
NSString *currentPath;
NSMutableString *currentText;
NSDictionary *currentElementAttributes;
MWFeedItem *item;
MWFeedInfo *info;
}
#pragma mark Public Properties
// Delegate to recieve data as it is parsed
@property (nonatomic, assign) id <MWFeedParserDelegate> delegate;
// Whether to parse feed info & all items, just feed info, or just feed items
@property (nonatomic) ParseType feedParseType;
// Set whether to download asynchronously or synchronously
@property (nonatomic) ConnectionType connectionType;
// Whether parsing was stopped
@property (nonatomic, readonly, getter=isStopped) BOOL stopped;
// Whether parsing failed
@property (nonatomic, readonly, getter=didFail) BOOL failed;
// Whether parsing is in progress
@property (nonatomic, readonly, getter=isParsing) BOOL parsing;
#pragma mark Public Methods
- (id)copyWithZone:(NSZone*)zone;
// Init MWFeedParser with a URL string
- (id)initWithFeedURL:(NSURL *)feedURL;
// Begin parsing
- (BOOL)parse;
// Stop parsing
- (void)stopParsing;
// Returns the URL
- (NSURL *)url;
@end