Click here to Skip to main content
15,885,914 members
Articles / Programming Languages / C#

Check Help Links Tool

Rate me:
Please Sign up or sign in to vote.
4.97/5 (13 votes)
6 Oct 2005CPOL6 min read 85.8K   17.3K   34  
A tool to check links across merged help (CHM) files.
using System;
using System.IO;
using System.Collections;
using System.Collections.Specialized;
using System.Windows.Forms;
using System.Diagnostics;

using HtmlHelp;
using HtmlHelp.ChmDecoding;

using Common;

namespace CheckHelpLinks
{

//-----------------------------------------------------------------------------

	public class ChmFileItem : TreeItem
	{
		private CHMFile _ChmFile = null;
		private HtmlFileItemCollection _HtmlFiles = new HtmlFileItemCollection();
		private int _HtmlCopies = 0;

		private NonHtmlFileHeader _NonHtmlFileHeader = null;

		public CHMFile ChmFile { get { return _ChmFile; } }
		public HtmlFileItemCollection HtmlFiles { get { return _HtmlFiles; } }

//		public NonHtmlFileItemCollection NonHtmlFiles { get { return _NonHtmlFiles; } }

		public int HtmlCopies { get { return _HtmlCopies; } }
		public int HtmlTopics { get { return _HtmlFiles.Count; } }
		public int NonHtmlTopics { get { return _NonHtmlFileHeader.NonHtmlFiles.Count; } }

		public ChmFileItem( TreeItem parent, CHMFile chmFile ) : base ( parent )
		{
			_ChmFile = chmFile;
		}

		public string Filename { get { return Path.GetFileName( _ChmFile.ChmFilePath ); } }

		public void Count( ref int total, ref int good, ref int unknown, ref int http, ref int script, ref int broken )
		{
			total = good = unknown = http = script = broken = 0;

			foreach ( HtmlFileItem file in _HtmlFiles.Values )
				foreach ( LinkItem link in file.Links )
				{
					total++;

					switch ( link.State )
					{
					case State.Good: good++; break;
					case State.Unknown: unknown++; break;
					case State.Http: http++; break;
					case State.Script: script++; break;
					case State.Broken: broken++; break;
					default:Debug.Assert( false ); break;
					}
				}
		}

		public override string ToString()
		{
			int total = 0, good = 0, unknown = 0, http = 0, script = 0, broken = 0;

			Count( ref total, ref good, ref unknown, ref http, ref script, ref broken );

			string s = Filename + " ( " +
				HtmlTopics + " html, " + 
				HtmlCopies + " copies, " +
				NonHtmlTopics + " non-html ";
			
			if ( PrintLinks ) 
				s += "; " +
				total + " links : " +
				good + " good, " + 
				unknown + " unknown, " + 
				http + " http, " + 
				script + " script, " +
				broken + " broken ";
			
			s += ")";

			return s;
		}

		protected virtual bool PrintLinks { get { return true; } }

		public HtmlFileItem HtmlFileItem( string linkFile )
		{
			string file = linkFile;

			int i = linkFile.IndexOf( '#' );
			if ( i >= 0 ) file = linkFile.Substring( 0, i );

			return _HtmlFiles[ file ] as HtmlFileItem;
		}

		public bool Contains( string linkFile )
		{
			string file = linkFile;
			string anchor = null;

			int i = linkFile.IndexOf( '#' );
			if ( i >= 0 )
			{
				file = linkFile.Substring( 0, i );
				anchor = linkFile.Substring( i + 1 );
			}

			HtmlFileItem item = _HtmlFiles[ file ] as HtmlFileItem;
			if ( item != null )
			{
				if ( anchor == null ) return true;

				return item.AnchorsHeader.AnchorItems.Contains( anchor );
			}

//			if ( ! _HtmlFiles.Contains( linkFile ) ) return true;
//			if ( _NonHtmlFileHeader.NonHtmlFiles.Contains( linkFile ) ) return true;

			return false;
		}

		public virtual void Load()
		{
			_NonHtmlFileHeader = new NonHtmlFileHeader( this );

			CHMTopics topics = ChmFile.TopicsFile;

			foreach ( TopicEntry entry in topics.TopicTable )
			{
				if ( entry.ContentFile == null ) continue;
	
				if ( ! Global.IsHtml( entry.URL ) )
				{
					_NonHtmlFileHeader.Load( entry );
				}
				else
				{
					HtmlFileItem item = CreateHtmlFileItem( entry );
					string s = item.ToString();

					HtmlFileItemCollection c = HtmlFiles;

					if ( c.Contains( s ) )
					{
						_HtmlCopies++;
					}
					else
						c.Add( s, item );
				}
			}
		}

		protected virtual HtmlFileItem CreateHtmlFileItem( TopicEntry entry )
		{
			return new HtmlFileItem( this, entry );
		}

		public override void FillTreeView( TreeNodeCollection nodes )
		{
			AddTreeNode( nodes );

			_NonHtmlFileHeader.FillTreeView( _TreeNode.Nodes );

			foreach ( HtmlFileItem htmlFile in _HtmlFiles.Values )
				htmlFile.FillTreeView( _TreeNode.Nodes );
		}

		public override void Work()
		{
			if ( All.ParseTopics )
			{
				DlgBrowser dlg = new DlgBrowser();
				dlg.HtmlFiles = _HtmlFiles;
				dlg.ShowDialog();

				foreach ( HtmlFileItem htmlFile in _HtmlFiles.Values )
					htmlFile.SetLinkNodes();
			}

			using ( new CWaitCursor() )
			{
				foreach ( HtmlFileItem htmlFile in _HtmlFiles.Values )
				{
					foreach ( LinkItem link in htmlFile.Links )
					{
						link.CheckLink();
						link.UpdateState( false );
					}

					htmlFile.UpdateState( false );
				}

				UpdateState( true );
			}
		}

		public override void UpdateState( bool updateParentState )
		{
			_State = State.Good;

			foreach ( HtmlFileItem htmlFile in _HtmlFiles.Values )
				UpdateStateFromChild( htmlFile );

			SetImage();

			_TreeNode.Text = ToString();

			if ( updateParentState ) UpdateParentState();
		}

	}

//-----------------------------------------------------------------------------

	public class ReferencedChmFileItem : ChmFileItem
	{
		public ReferencedChmFileItem( TreeItem parent, CHMFile chmFile ) : base ( parent, chmFile ) {}

		protected override bool PrintLinks { get { return false; } }

		public override void Load()
		{
			base.Load();

			State = State.Good;
		}

		protected override HtmlFileItem CreateHtmlFileItem( TopicEntry entry )
		{
			return new ReferencedHtmlFileItem( this, entry );
		}

		public override void Work() {}

		public void WorkCore()
		{
			foreach ( ReferencedHtmlFileItem item in HtmlFiles.Values )
				item.WorkCore();
		}
	}

//-----------------------------------------------------------------------------

	public class HtmlFileItem : LinkItem
	{
		private TopicEntry _HtmlFile = null;
		private AnchorsHeader _AnchorsHeader = null;
		private LinkItemCollection _Links = new LinkItemCollection();

		public new ChmFileItem Parent { get { return ( ChmFileItem ) _Parent; } }
		public TopicEntry TopicEntry { get { return _HtmlFile; } }
		public AnchorsHeader AnchorsHeader { get { return _AnchorsHeader; } }
		public LinkItemCollection Links { get { return _Links; } }

		public HtmlFileItem( ChmFileItem parent, TopicEntry topicEntry ) : base ( parent, topicEntry.URL )
		{
			_HtmlFile = topicEntry;

			_AnchorsHeader = new AnchorsHeader( this );
		}

		public override string ToString()
		{
			if ( _LinkType == LinkType.Help ) return _LinkFile;

			return _Url;
		}

		public string Html
		{
			get
			{
				return _HtmlFile.FileContents;
			}
		}

		public override void FillTreeView( TreeNodeCollection nodes )
		{
			AddTreeNode( nodes );
		}

		public override string Link { get { return _HtmlFile.URL; } }

		public override void OnAfterSelect() 
		{
			if ( Links.Count > 0 ) return;
			if ( _State != State.Unknown ) return;

			DlgBrowser dlg = new DlgBrowser();
			dlg.HtmlFile = this;
			dlg.ShowDialog();

			SetLinkNodes();

			TreeNode.Expand();
		}

		public void SetLinkNodes()
		{
			TreeNode.Nodes.Clear();

			_AnchorsHeader.FillTreeView( TreeNode.Nodes );

			foreach ( LinkItem link in Links )
			{
				TreeNode n = new TreeNode();

				n.Text = link.ToString();
				n.ImageIndex = (int) link.State;
				n.Tag = link;

				TreeNode.Nodes.Add( n );

				link.TreeNode = n;
			}
		}

		public override void Work()
		{
			DlgBrowser dlg = new DlgBrowser();
			dlg.HtmlFile = this;
			dlg.GetLinks = GetLinks;
			dlg.ShowDialog();

			SetLinkNodes();

			foreach ( LinkItem link in _Links )
			{
				link.CheckLink();
				link.UpdateState( false );
			}

			UpdateState( true );
		}

		protected virtual bool GetLinks { get { return true; } }

		public override void UpdateState( bool updateParentState )
		{
			_State = State.Good;

			UpdateStateFromChild( _AnchorsHeader );

			foreach ( LinkItem link in _Links )
			{
				if ( link.LinkType != LinkType.Help ) continue;

				UpdateStateFromChild( link );
			}

			SetImage();

			if ( updateParentState ) UpdateParentState();
		}

	}

//-----------------------------------------------------------------------------

	public class ReferencedHtmlFileItem : HtmlFileItem
	{
		string _Link = String.Empty;

		public ReferencedHtmlFileItem( ChmFileItem parent, TopicEntry topicEntry )
			: base ( parent, topicEntry )
		{
			State = State.Good;

			_Link = base.Link;
		}

		public override string Link { get { return _Link; } }

		public override void OnAfterSelect() {}

		public override void Work() {}

		protected override bool GetLinks { get { return false; } }

		public void WorkCore()
		{
			base.Work();
		}
	}

//-----------------------------------------------------------------------------

	public class HtmlFileItemComparer : IComparer  
	{
		int IComparer.Compare( Object x, Object y )  
		{
			return Comparer.Default.Compare( x.ToString(), y.ToString() );
		}
	}

//-----------------------------------------------------------------------------

	public class HtmlLinkItem : LinkItem
	{
		public HtmlLinkItem( HtmlFileItem parent, string url ) : base ( parent, url )
		{
		}

		public override void FillTreeView( TreeNodeCollection nodes )
		{
		}

		public override void OnDoubleClick()
		{
			Work();

			UpdateParentState();
		}

		public override void Work()
		{
			CheckLink();

			UpdateState( false );
		}

		public override void UpdateState( bool updateParentState )
		{
			SetImage();

			if ( updateParentState ) UpdateParentState();
		}

	}

//-----------------------------------------------------------------------------

	public class NonHtmlFileHeader : TreeItem
	{
		private NonHtmlFileItemCollection _NonHtmlFiles = new NonHtmlFileItemCollection();

		public NonHtmlFileItemCollection NonHtmlFiles { get { return _NonHtmlFiles; } }

		public NonHtmlFileHeader( ChmFileItem parent ) : base ( parent )
		{
			_State = State.Good;
		}

		public override string ToString() { return "Non-HTML Topics"; }

		public void Load( TopicEntry entry )
		{
			NonHtmlFileItem item = new NonHtmlFileItem( this, entry );

			string s = item.LinkFile;

			if ( s == null ) return;
//			if ( s.IndexOf( '.' ) < 0 ) return;

			NonHtmlFileItemCollection c = _NonHtmlFiles;

			if ( c.Contains( s ) ) return;

			c.Add( s, item );
		}

		public override void FillTreeView( TreeNodeCollection nodes )
		{
			AddTreeNode( nodes );

			foreach ( NonHtmlFileItem item in _NonHtmlFiles.Values )
				item.FillTreeView( _TreeNode.Nodes );
		}

		public override void Work() {}

		public override void UpdateState( bool updateParentState )
		{
			if ( updateParentState ) UpdateParentState();
		}
	}

//-----------------------------------------------------------------------------

	public class NonHtmlFileItem : LinkItem
	{
		private TopicEntry _TopicEntry = null;

//		public new ChmFileItem Parent { get { return ( ChmFileItem ) _Parent; } }
		public TopicEntry TopicEntry { get { return _TopicEntry; } }

		public NonHtmlFileItem( NonHtmlFileHeader parent, TopicEntry topicEntry ) : base ( parent, topicEntry.URL )
		{
			_TopicEntry = topicEntry;

			_State = State.Good;
		}

//		public override string ToString() { return _TopicEntry.URL; }

		public override void FillTreeView( TreeNodeCollection nodes )
		{
			AddTreeNode( nodes );
		}

		public override string Link { get { return _TopicEntry.URL; } }

		public override void Work() {}

		public override void UpdateState( bool updateParentState ) {}
	}

//-----------------------------------------------------------------------------

	public class AnchorsHeader : TreeItem
	{
		private AnchorItemCollection _AnchorItems = new AnchorItemCollection();

		public AnchorItemCollection AnchorItems { get { return _AnchorItems; } }

		public AnchorsHeader( HtmlFileItem parent ) : base ( parent )
		{
			_State = State.Good;
		}

		public override string ToString() { return "Anchors"; }

		public void Load( TopicEntry entry )
		{
//			NonHtmlFileItem item = new NonHtmlFileItem( this, entry );
//
//			string s = item.LinkFile;
//
//			if ( s == null ) return;
//			//			if ( s.IndexOf( '.' ) < 0 ) return;
//
//			NonHtmlFileItemCollection c = _NonHtmlFiles;
//
//			if ( c.Contains( s ) ) return;
//
//			c.Add( s, item );
		}

		public override void FillTreeView( TreeNodeCollection nodes )
		{
			AddTreeNode( nodes );

			foreach ( AnchorItem item in _AnchorItems.Values )
				item.FillTreeView( _TreeNode.Nodes );
		}

		public override void Work()
		{
			UpdateState( true );
		}

		public override void UpdateState( bool updateParentState )
		{
			_State = State.Good;

			foreach ( AnchorItem link in _AnchorItems.Values )
			{
				UpdateStateFromChild( link );
			}

			if ( updateParentState ) UpdateParentState();
		}
	}

//-----------------------------------------------------------------------------

	public class AnchorItem : LinkItem
	{
		private string _Anchor = String.Empty;
		private bool _Copy = false;

		public string Anchor { get { return _Anchor; } }
		public bool Copy { get { return _Copy; } }

		public AnchorItem( AnchorsHeader parent, string url ) : base ( parent, url )
		{
			int i = url.IndexOf( '#' );

			if ( i < 0 ) Debug.Assert( false );
			else
				_Anchor = url.Substring( i + 1 );

			_State = State.Good;
		}

		public override string ToString() { return _Anchor; }

		public override void FillTreeView( TreeNodeCollection nodes )
		{
			AddTreeNode( nodes );
		}

//		public override string Link { get { return _TopicEntry.URL; } }

		public override void Work() {}

		public override void UpdateState( bool updateParentState )
		{
			if ( updateParentState ) UpdateParentState();
		}
	}

//-----------------------------------------------------------------------------

}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United Kingdom United Kingdom
I discovered C# and .NET 1.0 Beta 1 in late 2000 and loved them immediately.
I have been writing software professionally in C# ever since

In real life, I have spent 3 years travelling abroad,
I have held a UK Private Pilots Licence for 20 years,
and I am a PADI Divemaster.

I now live near idyllic Bournemouth in England.

I can work 'virtually' anywhere!

Comments and Discussions