Click here to Skip to main content
15,891,864 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 62K   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.Collections.Specialized;
using Rilling.MhtmlLib.Media.Handlers;
using Rilling.Web.Mhtml.Exceptions;

namespace Rilling.MhtmlLib.Collections
{
	/// <summary>
	///		A collection containing all registered <see cref="ContentHandlerBase"/>
	///		objects.
	/// </summary>
	public class MimeHandlerCollection : NameObjectCollectionBase
	{
		/// <summary>
        ///		Adds a new <see cref="ContentHandlerBase"/> object.
		/// </summary>
		/// <param name="handler">
        ///		The <see cref="ContentHandlerBase"/> to add to the collection.
		/// </param>
		/// <param name="mimePattern">
		///		The pattern that the specified handler will accept.
		/// </param>
		/// <exception cref="DuplicateHandlerRegistrationException">
		///		Exception thrown when a handler with the same mime pattern
		///		is already registered.  A mime pattern can only be handled
        ///		by a single <see cref="ContentHandlerBase"/> object.
		/// </exception>
		public void Add(IContentFactory handler, string mimePattern)
		{
            if (handler == null)
                throw (new ArgumentNullException("handler"));
            if (mimePattern == null)
                throw (new ArgumentNullException("mimePattern"));

			MimeHandlerInfo handlerInfo = new MimeHandlerInfo(handler, mimePattern);

			Add(handlerInfo);
		}

		/// <summary>
        ///		Adds a new <see cref="ContentHandlerBase"/> object.
		/// </summary>
		/// <param name="handlerInfo">
        ///		Structure containing the <see cref="ContentHandlerBase"/> to add to 
		///		the collection.
		/// </param>
		/// <exception cref="DuplicateHandlerRegistrationException">
		///		Exception thrown when a handler with the same mime pattern
		///		is already registered.  A mime pattern can only be handled
        ///		by a single <see cref="ContentHandlerBase"/> object.
		/// </exception>
		public void Add(MimeHandlerInfo handlerInfo)
		{
			string mimePattern = handlerInfo.Pattern;
			int patternIdx = Array.IndexOf(base.BaseGetAllKeys(), handlerInfo.Pattern);

			if(patternIdx != -1)
				throw(new DuplicateHandlerRegistrationException(handlerInfo.Pattern));

			// Add the handler to the collection.
			BaseAdd(handlerInfo.Pattern, handlerInfo);
		}

		/// <summary>
		///		Gets or sets an object in the collection based on the mime pattern.
		/// </summary>
		public MimeHandlerInfo this[string mimePattern]
		{
			get{
				object mh = BaseGet(mimePattern);

				if(mh == null)
					throw(new ArgumentOutOfRangeException("mimePattern", mimePattern));

				return (MimeHandlerInfo)mh;
			}
			set{BaseSet(mimePattern, value);}
		}

		/// <summary>
		///		Gets or sets an object in the collection based on its position.
		/// </summary>
		public MimeHandlerInfo this[int index]
		{
			get{return (MimeHandlerInfo)BaseGet(index);}
			set{BaseSet(index, value);}
		}

		/// <summary>
		///		Removes the specified <see cref="MimeHandlerInfo"/> object.
		/// </summary>
		/// <param name="mimePattern">
		///		The pattern to remove.
		/// </param>
		public void Remove(string mimePattern)
		{
            if (mimePattern == null)
                throw (new ArgumentNullException("mimePattern"));

			BaseRemove(mimePattern);
		}

		/// <summary>
		///		Removes the specified <see cref="MimeHandlerInfo"/> object.
		/// </summary>
		/// <param name="index">
		///		The index in the collection.
		/// </param>
		public void RemoveAt(int index)
		{
			BaseRemoveAt(index);
		}

		/// <summary>
		///		Removes all objects from the collection.
		/// </summary>
		public void Clear()
		{
			 BaseClear();
		}
	}
}

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