File: RSSFactory.m

package info (click to toggle)
rsskit 0.3-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 656 kB
  • ctags: 572
  • sloc: objc: 2,041; makefile: 48; sh: 4
file content (175 lines) | stat: -rw-r--r-- 5,615 bytes parent folder | download | duplicates (2)
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
/*  -*-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 "RSSFactory.h"
#import "RSSArticle+Storage.h"
#import "GNUstep.h"

static id<RSSFactory> sharedFactory = nil;
static NSString* RSSArticleStorageDirectory = nil;



/**
 * Converts a string to a string that's usable as a file system name. This is done
 * by removing several forbidden characters, only leaving the allowed ones. (A helper method)
 */
NSString* stringToFSString( NSString* aString )
{
    NSScanner* scanner = [NSScanner scannerWithString: aString];
    NSMutableString* string = AUTORELEASE([[NSMutableString alloc] init]);
    NSCharacterSet* allowedSet = [NSCharacterSet alphanumericCharacterSet];
    
    do {
        NSString* nextPart;
        BOOL success;

        // discard any unknown characters
        if ([scanner scanUpToCharactersFromSet: allowedSet intoString: NULL] == YES) {
            [string appendString: @"_"];
        }
        
        // scan known characters...
        success = [scanner scanCharactersFromSet: allowedSet intoString: &nextPart];
        
        // ...and add them to the string
        if (success == YES) {
            [string appendString: nextPart];
        }
    } while ([scanner isAtEnd] == NO);
    
    return [NSString stringWithString: string];
}


@implementation RSSFactory

/**
 * Returns the shared factory instance.
 */
+ (id<RSSFactory>) sharedFactory
{
    if (sharedFactory == nil) {
        ASSIGN(sharedFactory, AUTORELEASE([[RSSFactory alloc] init]));
    }
    
    return sharedFactory;
}

/**
 * Sets another shared factory instance than the currently selected one.
 */
+ (void) setFactory: (id<RSSFactory>) aFactory
{
    ASSIGN(sharedFactory, aFactory);
}


/**
 * The default implementation of this method returns a new feed of
 * the RSSFeed class.
 */
- (id<RSSFeed>) feedWithURL: (NSURL*) aURL
{
    return [RSSFeed feedWithURL: aURL];
}


/**
 * The default implementation of this method returns a new article
 * of the RSSArticle class.
 */
- (id<RSSArticle>) articleWithHeadline: (NSString*) aHeadline
                                   URL: (NSString*) aURL
                               content: (NSString*) aContent
                                  date: (NSDate*) aDate
{
    id <RSSArticle> article = [[RSSArticle alloc] initWithHeadline: aHeadline
                                                               url: aURL
                                                       description: aContent
                                                              date: aDate];
    return AUTORELEASE(article);
}

/**
 * The default implementation of this method returns a new article
 * of the RSSArticle class.
 */
- (id<RSSArticle>) articleFromStorageWithURL: (NSString*) aURL
{
    return [self articleFromDictionary:
            [NSDictionary dictionaryWithContentsOfFile:
                [self storagePathForURL: aURL]]];
}



/**
 * The default implementation of this method returns a article
 * of the RSSArticle class.
 */
- (id<RSSArticle>) articleFromDictionary: (NSDictionary*) aDictionary
{
    return AUTORELEASE([[RSSArticle alloc] initWithDictionary: aDictionary]);
}

/**
 * Returns the file path where an article with the anURL URL would be stored to.
 */
-(NSString*) storagePathForURL: (NSString*) anURL
{
    if (RSSArticleStorageDirectory == nil) {
        NSFileManager* manager;
        BOOL isDir, exists;
        NSString *storagePath;
        
        storagePath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
        storagePath = [storagePath stringByAppendingPathComponent:[[NSProcessInfo processInfo] processName]];
        storagePath = [storagePath stringByAppendingPathComponent:@"RSSArticles"];


        ASSIGN(RSSArticleStorageDirectory, storagePath);
        
        manager = [NSFileManager defaultManager];
        
        exists = [manager fileExistsAtPath: RSSArticleStorageDirectory isDirectory: &isDir];
        
        if (exists) {
            if (isDir == NO) {
                [[NSException exceptionWithName: @"RSSArticleStorageDirectoryIsNotADirectory"
                                         reason: @"The storage directory for RSS articles is not a directory."
                                       userInfo: nil] raise];
            }
        } else {
            if ([manager createDirectoryAtPath: RSSArticleStorageDirectory
                                    attributes: nil] == NO) {
                [[NSException exceptionWithName: @"RSSArticleStorageDirectoryCreationFailed"
                                         reason: @"Creation of the storage directory for RSS Articles failed."
                                       userInfo: nil] raise];
            }
        }
    }
    
    return [NSString stringWithFormat: @"%@/%@.rssarticle", RSSArticleStorageDirectory, stringToFSString(anURL)];
}

@end