-
-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathExtractor.m
354 lines (297 loc) · 8.89 KB
/
Extractor.m
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
//
// Exctractor.m
// ExtractorAction
//
// Created by Vitaly Davidenko on 12/10/06.
// Copyright 2006 Vitaly Davidenko.
//
// Apple Public Source License
// http://www.opensource.apple.com/apsl/
//
// Updated and refactored by Rob Rohan on 2007-09-18
#import "Extractor.h"
static NSString* composeEntryPointPath(NSString* packagePath, NSString* indexName)
{
return [packagePath stringByAppendingPathComponent:indexName];
}
@implementation Extractor
- (id) init
{
self = [super init];
if(self != nil) {
//default to XHTML if there is nothing else
contentKind = NSXMLDocumentXHTMLKind;
}
return self;
}
-(void) loadWebArchive:(NSString*) pathToWebArchive
{
if (m_resources)
{
[m_resources removeAllObjects];
[m_resourceLookupTable removeAllObjects];
}
else
{
m_resources = [NSMutableSet set];
m_resourceLookupTable = [NSMutableDictionary dictionary];
}
NSData * webArchiveContent = [NSData dataWithContentsOfFile:pathToWebArchive];
WebArchive * archive = [[WebArchive alloc] initWithData:webArchiveContent];
/* Added method parseWebArchive to more easily deal with subframeArchives in a looping fashion
Deal with main resource first...may or may not cover it all - Robert Covington [email protected]
12/12/11
*/
[self parseWebArchive:archive ];
/*
Check for SubFrameArchives - catches anything left over...some sites using frames will
invoke this and otherwise would generate only a single HTML index file
- Robert Covington [email protected] 12/12/11
*/
// TODO: Causes bad thread access. don't want to bother with this at the moment
#if 0
NSArray * subArchives = [archive subframeArchives];
if (subArchives)
{
NSUInteger i;
for (i=0; i<[subArchives count]; i++)
{
WebArchive * nuArchive = [WebArchive alloc];
nuArchive = [subArchives objectAtIndex:i];
if (nuArchive)
{
[self parseWebArchive:nuArchive];
}
}
} /* end subArchive processing */
#endif
} /* end method */
-(void) parseWebArchive:(WebArchive *) archiveToParse
{
/* Added method parseWebArchive to more easily deal with subframeArchives in a looping fashion
- Robert Covington [email protected]
12/12/11
*/
m_mainResource = [archiveToParse mainResource];
[self addResource:m_mainResource];
NSArray * subresources = [archiveToParse subresources];
if (subresources)
{
WebResource* resource;
NSUInteger i;
for (i=0; i<[subresources count]; i++)
{
resource = (WebResource*) [subresources objectAtIndex:i];
[self addResource:resource];
}
}
// [archiveToParse release];
}
-(void) addResource:(WebResource *)resource
{
[m_resources addObject:resource];
//url of resource
NSURL* url = [resource URL];
NSString* absoluteString = [url absoluteString];
NSString* path = [url path];
if(path != nil) {
[m_resourceLookupTable setObject:resource forKey:absoluteString];
[m_resourceLookupTable setObject:resource forKey:path];
}
}
- (NSString *) extractResources:(NSString *) path
{
NSFileManager * fm = [NSFileManager defaultManager];
BOOL isDirectory = YES;
if ([fm fileExistsAtPath:path isDirectory: &isDirectory])
{
//removeItemAtURL:error:
if ([fm removeItemAtPath:path error:nil] == NO)
{
NSLog(
NSLocalizedStringFromTable(
@"cannot delete",
@"InfoPlist",
@"cannot delete file - path first param"
),
path
);
return nil;
}
}
if([fm createDirectoryAtPath:path withIntermediateDirectories:true attributes:nil error:nil] == NO)
{
NSLog(
NSLocalizedStringFromTable(
@"cannot create",
@"InfoPlist",
@"cannot create file - path first param"
),
path
);
return nil;
}
NSEnumerator *enumerator = [m_resources objectEnumerator];
id value;
while ((value = [enumerator nextObject])) {
WebResource * resource = (WebResource*) value;
[self extractResource: resource packagePath:path];
}
return composeEntryPointPath(path, [self entryFileName]);
}
- (void) extractResource:(WebResource *) resource packagePath: (NSString*) path
{
NSFileManager * fm = [NSFileManager defaultManager];
NSString * urlPath = [[resource URL] path];
if ([urlPath isEqual:@"/"]) {
//spec case - main resource name is equals site name
urlPath=@"/__index.html";
}
NSMutableString * filePath = [NSMutableString stringWithCapacity:[path length]+[urlPath length]];
[filePath appendString:path];
NSArray * components = [urlPath componentsSeparatedByString:@"/"];
NSUInteger i;
for (i=0; i<[components count]; i++) {
NSString * fname = (NSString*) [components objectAtIndex:i];
if ([fname length] > 0) {
[filePath appendString:@"/"];
[filePath appendString:fname];
if (i+1 == [components count]) {
//last path component - write file
[self outputResource:resource filePath:filePath packagePath:path];
} else {
//create directory
BOOL isDirectory = YES;
if (![fm fileExistsAtPath:filePath isDirectory: &isDirectory] && [fm createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil] != YES) {
NSLog(
NSLocalizedStringFromTable(
@"cannot create",
@"InfoPlist",
@"cannot create file - path first param"
),
filePath
);
return;
}
}
}
}
}
- (void) outputResource: (WebResource *) resource
filePath: (NSString*) filePath
packagePath: (NSString*) packagePath
{
if (resource == m_mainResource) {
NSString *encodingString = [m_mainResource textEncodingName];
NSStringEncoding encoding = CFStringConvertEncodingToNSStringEncoding(CFStringConvertIANACharSetNameToEncoding((CFStringRef) encodingString));
NSString * source = [[NSString alloc] initWithData:[resource data]
encoding: encoding];
#if 0
NSLog(
NSLocalizedStringFromTable(@"resource encoding is", @"InfoPlist", @"Resource encoding"),
[resource textEncodingName]
);
#endif
NSError * err = nil;
NSXMLDocument * doc = [NSXMLDocument alloc];
doc = [doc initWithXMLString: source options: NSXMLDocumentTidyHTML error: &err];
/*
Returns the kind of document content for output.
- (NSXMLDocumentContentKind)documentContentKind
Discussion
Most of the differences among content kind have to do with the handling of content-less
tags such as <br>. The valid NSXMLDocumentContentKind constants are
NSXMLDocumentXMLKind, NSXMLDocumentXHTMLKind, NSXMLDocumentHTMLKind,
and NSXMLDocumentTextKind.
*/
[doc setDocumentContentKind: contentKind];
if (doc != nil) {
//process images
err = nil;
NSArray* images = [doc nodesForXPath:@"descendant::node()[@src] | descendant::node()[@href]"
error: &err];
if (err != nil) {
NSLog(@"%@",
NSLocalizedStringFromTable(
@"cannot execute xpath",
@"InfoPlist",
@"Xpath execute error"
)
);
} else {
NSUInteger i;
for (i=0; i<[images count]; i++) {
NSXMLElement * link = (NSXMLElement *) [images objectAtIndex: i];
NSXMLNode * href = [link attributeForName: @"href"];
if (href == nil) {
href = [link attributeForName: @"src"];
}
if (href != nil) {
NSString * hrefValue = [href objectValue];
WebResource * res = [m_resourceLookupTable objectForKey: hrefValue];
if (res != nil) {
[href setObjectValue: [NSString stringWithFormat:@"%@%@", [self URLPrepend], [[[res URL] path] substringFromIndex:1]]];
}
}
}
}
NSString * filePathXHtml = composeEntryPointPath(packagePath, [self entryFileName]);
[doc setCharacterEncoding: @"UTF-8"];
if (![[doc XMLDataWithOptions: NSXMLDocumentTidyHTML] writeToFile: filePathXHtml atomically: NO]) {
NSLog(
NSLocalizedStringFromTable(
@"cannot write xhtml",
@"InfoPlist",
@"xhtml file error"
),
filePath
);
}
} else {
NSLog(
NSLocalizedStringFromTable(
@"error code",
@"InfoPlist",
@"extractor error. error code first param"
),
[[err userInfo] valueForKey:NSLocalizedDescriptionKey]
);
}
} else {
if (![[resource data] writeToFile:filePath atomically:NO]) {
NSLog(
NSLocalizedStringFromTable(
@"cannot write xhtml",
@"InfoPlist",
@"xhtml file error"
),
filePath
);
}
}
}
- (void) setEntryFileName:(NSString *) filename;
{
entryFileName = [filename copy];
}
- (NSString *) entryFileName;
{
return entryFileName;
}
- (void) setURLPrepend:(NSString *) url
{
URLPrepend = [url copy];
}
- (NSString *) URLPrepend
{
return URLPrepend;
}
- (void) setContentKind:(NSXMLDocumentContentKind) kind
{
contentKind = kind;
}
- (NSXMLDocumentContentKind) contentKind
{
return contentKind;
}
@end