---
title: "这只是中转站"
author: "kmplayer"
date: "2011-05-10T09:11:26Z"
tags: []
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2011-10-31T20:11:02Z"
id: 1038534
url: "https://kmplayer.iteye.com/blog/1038534"
---

// 
// Copyright (c) 2011 Shun Takebayashi
// 
// 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:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// 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 "STWebArchiver.h"
#import <libxml/HTMLparser.h>
#import <libxml/xpath.h>

@interface STWebArchiver ()

- (NSArray *)valueForAttributeName:(NSString *)attributeName
			   withEvaluatingXPath:(NSString *)xpathExpression
						inDocument:(xmlDocPtr)document;
- (NSArray *)absoluteURLsForPaths:(NSArray *)paths baseURL:(NSURL *)base;

@end

@implementation STWebArchiver

- (void)archiveHTMLData:(NSData *)aData
		   textEncoding:(NSString *)anEncoding
				baseURL:(NSURL *)anURL
        completionBlock:(void (^)(NSData *))completion {
	htmlDocPtr doc = htmlParseDoc((xmlChar *), );
	NSArray *pathsForImagesAndScripts = |//img" inDocument:doc];
	NSArray *pathsForStylesheets = " inDocument:doc];
	NSArray *resourcesPaths = ;
	NSArray *resourceUrls = ;
	dispatch_async(dispatch_queue_create("Downloads", 0), ^{
		NSMutableDictionary *resources = ;
		dispatch_apply(, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^(size_t i) {
			NSURL *url = ;
			NSString *urlString = ;
			BOOL unfetched = NO;
			@synchronized (resources) {
				unfetched = !;
				if (unfetched) {
					 forKey:urlString];
				}
			}
			if (unfetched) {
				NSURLResponse *response;
				NSError *error;
				NSURLRequest *request = ;
				NSData *data = ;
				NSMutableDictionary *resourceArchive = , @"WebResourceMIMEType",
														data, @"WebResourceData", nil];
				if () {
					 forKey:@"WebResourceTextEncodingName"];
				}
				@synchronized (resources) {
					;
				}
			}
		});
		NSMutableDictionary *archiveSource =  forKey:@"WebSubresources"];
		NSMutableDictionary *mainResource = ;
		;
		;
		;
		;
		 forKey:@"WebResourceURL"];
		;
		NSData *webArchive = ;
        completion(webArchive);
	});
	xmlFreeDoc(doc);
}

- (NSArray *)valueForAttributeName:(NSString *)attributeName
			   withEvaluatingXPath:(NSString *)xpathExpression
						inDocument:(xmlDocPtr)document {
	xmlXPathContextPtr context = xmlXPathNewContext(document);
	xmlXPathObjectPtr xpathObject = xmlXPathEvalExpression((xmlChar *), context);
	xmlNodeSetPtr nodes = xpathObject->nodesetval;
	NSMutableArray *results = nil;
	if (!xmlXPathNodeSetIsEmpty(nodes)) {
		results = ;
		for (int i = 0; i < nodes->nodeNr; i++) {
			xmlNodePtr node = nodes->nodeTab*;
			char *attributeChars = (char *)xmlGetProp(node, (xmlChar *));
			NSString *attributeString = ;
			xmlFree(attributeChars);
			;
		}
	}
	xmlXPathFreeObject(xpathObject);
	xmlXPathFreeContext(context);
	return results;
}

- (NSArray *)absoluteURLsForPaths:(NSArray *)paths baseURL:(NSURL *)base {
	NSMutableArray *results = ];
	for (NSString *path in paths) {
		];
	}
	return results;
}

@end