File: RSSArticleCreationListener.m

package info (click to toggle)
rsskit 0.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 752 kB
  • sloc: objc: 2,041; makefile: 22; sh: 4
file content (336 lines) | stat: -rw-r--r-- 7,835 bytes parent folder | download | duplicates (4)
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*  -*-objc-*-
 *
 *  GNUstep RSS Kit
 *  Copyright (C) 2006 Guenther Noack
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation, in version 2.1
 *  of the License
 * 
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#import <Foundation/Foundation.h>

#import "RSSLinks.h"
#import "NewRSSArticleListener.h"
#import "RSSArticleCreationListener.h"
#import "DublinCore.h"
#import "RSSFactory.h"

#import "GNUstep.h"

#define REL_KEY @"rel"
#define TYPE_KEY @"type"


@implementation RSSArticleComposer

-(id) init
{
  return [super init];
}

-(void) dealloc
{
  DESTROY(headline);
  DESTROY(url);
  DESTROY(summary);
  DESTROY(content);
  DESTROY(date);
  
  DESTROY(links);
  
  [super dealloc];
}


//delegate accessor methods

- (void) setDelegate: (id)aDelegate
{
  ASSIGN(delegate, aDelegate);
}

- (id) delegate
{
  return delegate;
}



/**
 * Adds the article to the feed
 */
- (void) commitArticle
{
  id<RSSArticle> article = nil;
  NSDate* articleDate = nil;
  NSString* desc = nil;
  id<RSSMutableArticle> mutArt;
  
  // date
  if( date == nil )
    {
      ASSIGN(articleDate, [NSDate date]);
    }
  else
    {
      ASSIGN(articleDate, date);
    }
  
  // description
  if (content != nil)
    {
      ASSIGN(desc, content);
    }
  else if (summary != nil)
    {
      ASSIGN(desc, summary);
    }
  else
    {
      ASSIGN(desc, @"No content.");
    }
 
  // url
  if (url == nil) {
      NSAssert1([links count] > 0, @"Article %@ has no links!", headline);
      
      // better use a bad (="random") link as article URL than none
      ASSIGN(url, [[links objectAtIndex: 0] description]);
  } 
  
  // create
  article = [[RSSFactory sharedFactory] articleWithHeadline: headline
	                                                  URL: url
	                                              content: desc
	                                                 date: articleDate];
  // FIXME: This article creation was retained before, but it seems okay like that?
  // Was it a memory leak or am I having memory management problems now?
  
  NSAssert1(
      [article conformsToProtocol: @protocol(RSSMutableArticle)],
      @"Article %@ must be mutable for proper creation!", article
  );
  
  mutArt = (id<RSSMutableArticle>) article;
  // add links
  if ([links count] > 0)
    {
      [mutArt setLinks: links];
    }
  
  if (delegate != nil)
    [delegate newArticleFound: article];
  
  DESTROY(articleDate);
  DESTROY(desc);
}


/**
 * Gets called whenever a feed has been parsed completely.
 */
-(void) finished
{
  #ifdef DEBUG
  NSLog(@"%@ finished, rc=%d", self, [self retainCount]);
  #endif
  
  // empty at the moment, may be useful for things like thread locking
  // in the future.
}


/**
 * Starts a new article
 */
-(void) startArticle
{
  #ifdef DEBUG
  NSLog(@"start article");
  
  NSLog(@"retain counts in start article: %d %d %d",
	[headline retainCount],
	[links retainCount],
	[date retainCount]);
  #endif
  
  // Free all old stuff
  DESTROY(headline);
  DESTROY(url);
  DESTROY(summary);
  DESTROY(content);
  DESTROY(date);
  
  DESTROY(links);
  
  // Set default values
  [self setHeadline: @"No headline"];
  
  ASSIGN(links, AUTORELEASE([[NSMutableArray alloc] initWithCapacity: 1]));
}

// don't use this, use commitArticle
// and startArticle directly instead!
-(void) nextArticle
{
  #ifdef DEBUG
  NSLog(@"Warning: nextArticle should not be called.");
  #endif
  
  [self commitArticle];
  [self startArticle];
}


-(void)setHeadline: (NSString*) aHeadline
{
  ASSIGN(headline, aHeadline);
}

-(void) addLinkWithURL: (NSString*) anURL
{
  // default is 'alternate', see ATOM specification!
  [self addLinkWithURL: anURL
	andRel: @"alternate"];
}

-(void) addLinkWithURL: (NSString*) anURL
		andRel: (NSString*) aRelation
{
  [self addLinkWithURL: anURL
	andRel: aRelation
	andType: nil];
}

/**
 * Adds a link with a specified URL, relation type and file type.
 * 
 * Relation type is one of:
 * <ul>
 *  <li>"alternate": This link leads to an alternative location where
 *                   this article's contents can be found.</li>
 *  <li>"enclosure": A related resource which is probably large in size.
 *                   This may link to a movie, a mp3 file, etc.</li>
 *  <li>"related":   A related document</li>
 *  <li>"self":      The feed itself</li>
 *  <li>"via":       The source of the information provided in the entry.</li>
 * </ul>
 *
 * The <tt>nil</tt> relation type defaults to "related".
 * 
 * These relation types are compatible with the ones of the ATOM
 * specification. For details, see
 * <a href="http://www.atomenabled.org/developers/syndication/#link">
 * the ATOM specification.</a>
 */
-(void) addLinkWithURL: (NSString*) anURL
		andRel: (NSString*) aRelation
	       andType: (NSString*) aType
{
  RSSLink* link;

#ifdef DEBUG
  NSLog(@"addLinkWithURL: %@ andRel: %@ andType: %@",
	anURL, aRelation, aType);
#endif
  link = [RSSLink linkWithString: anURL
			     andRel: aRelation
			     andType: aType];
  /* Keep the default URL */
  if (url == nil && [aRelation isEqualToString: @"alternate"]) {
      ASSIGN(url, anURL);
  }
  
  if (link) {
    [links addObject: link];
  } else {
    NSLog(@"Link is nil: href: %@; rel: %@; type: %@", anURL, aRelation, aType);
  }
  
  
#ifdef DEBUG
  NSLog(@"links is now %@", links);
#endif
}

-(void) setContent: (NSString*) aContent
{
  ASSIGN(content, aContent);
}

-(void) setSummary: (NSString*) aSummary
{
  ASSIGN(summary, aSummary);
}

-(void) setDate: (NSDate*) aDate
{
  ASSIGN(date, aDate);
}

-(void) setDateFromString: (NSString*) str
{
    int i;
    NSDate* d;
    static NSArray* timeformats = nil;
    
    if (timeformats == nil) {
        // Everything that can be found in the wild is considered 'verified'.
        timeformats = [[NSArray alloc] initWithObjects:
            @"%a, %d %b %Y %H:%M:%S %z", // blogspot

            @"%a, %d %b %Y %H:%M:%S %Z", // found in thedailywtf, RFC822?
            @"%a, %d %b %Y %H:%M %Z",
            @"%a, %d %B %Y %H:%M:%S %Z", // same with full month name
            @"%a, %d %b %Y %H:%M:%S %z", 
            @"%a, %d %B %Y %H:%M:%S %z",

            @"%B %d, %Y", 
            @"%b %d, %Y", // verified by example
            @"%d %b %Y %H:%M:%S %Z", // verified by example (Joel on Software)
            @"%d %b %Y %H:%M:%S",    // not verified, derived from the upper
            @"%d %b %Y",             // not verified, derived from the upper
            // Dublin Core
            @"%Y-%m-%dT%H:%M:%S%Z",
            @"%Y-%m-%dT%H:%M:%S",
            @"%Y-%m-%dT%H:%M", 
            @"%Y-%m-%d", 
            @"%Y-%m", 
            @"%Y", 
            nil
        ];
    }
    
    d = nil;
    for (i=0; i<[timeformats count] && d == nil; i++) {
        d = [NSCalendarDate dateWithString: str
                            calendarFormat: [timeformats objectAtIndex: i]
        ];
    }
    if (d!=nil)
        NSLog(@"Date=%@, calc'd from %@, which matched to %@ (%dth try)", d, str, [timeformats objectAtIndex: i], i);
    else
        NSLog(@"Could not parse Date: %@", str);
    
   
    if (d==nil) {
        d = parseDublinCoreDate(str); // resistance is futile, nasty timezone definition!
    }
    
    if (d != nil) {
        [self setDate: d];
    }
}

@end