Click here to Skip to main content
15,888,231 members
Articles / Programming Languages / C#

Harvesting Web Content into MHTML Archive

Rate me:
Please Sign up or sign in to vote.
4.69/5 (14 votes)
18 Feb 20068 min read 61.9K   729   49  
Capture and archive web resources by saving them in RFC-2557 (MHTML) compliant format. This library includes a framework for augmenting the capture process and allowing programmatic control, from downloading through saving. A replacement for CDOSYS/CDONTS.
using System;
using System.Collections;
using System.Configuration;
using System.Xml;
using Rilling.MhtmlLib.Media.Handlers;

namespace Rilling.Web.Mhtml.Configuration
{
	/// <summary>
	///		Reads the mimeHandlers configuration settings from the app.config.
	///		Initializes all the configured <see cref="MimeHandleBase"/> object
	///		as defined in the app.config.
	/// </summary>
	public class MimeHandlersConfigurationHandler : IConfigurationSectionHandler
	{
		/// <summary>
		///		Initializes the <see cref="MimeHandlerBase"/> objects as defined
		///		in the app.config.
		/// </summary>
		/// <param name="parent">
		///		Not used.
		/// </param>
		/// <param name="input">
		///		Not used.
		/// </param>
		/// <param name="node">
		///		The Xml as laid out in the app.config.
		/// </param>
		/// <returns>
		///		A collection of <see cref="MimeHandlerBase"/> objects.
		/// </returns>
		public object Create(object parent, object input, XmlNode node)
		{
			if(node == null) throw(new ArgumentNullException("node"));
			ArrayList al = new ArrayList();

			foreach(XmlElement mimeHandlerNode in node.SelectNodes("add"))
			{
				if(mimeHandlerNode.Attributes["mimePattern"] == null)
					throw(new ConfigurationException("Required 'mimePattern' attribute missing from configuration section.", node));
				if(mimeHandlerNode.Attributes["type"] == null)
					throw(new ConfigurationException("Required 'type' attribute missing from configuration section.", node));

				string mimePattern =
					mimeHandlerNode.Attributes["mimePattern"].Value.Trim();
				Type mimeHandlerType = 
					Type.GetType(mimeHandlerNode.Attributes["type"].Value.Trim());

				if(mimeHandlerType != null)
				{
					foreach(string specificPattern in ExtractMimeAssociations(mimePattern))
					{
						//object[] args = new object[] { specificPattern };
						IContentFactory mh = (IContentFactory)Activator.CreateInstance(mimeHandlerType, null, null);

						//XmlNode settingParam = mimeHandlerNode.FirstChild;
						//if( settingParam != null )
						//    mh.Configure( (XmlElement)settingParam );

						al.Add(new MimeHandlerInfo(mh, specificPattern));
					}
				}

				// TODO:  Throw exception when type could not be loaded.
			}

			return al;
		}

		private string[] ExtractMimeAssociations(string mimePattern)
		{
			if(mimePattern == null || mimePattern.Trim() == String.Empty)
				throw(new ArgumentNullException("mimePattern"));

			return mimePattern.Split(',', ';');
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions