using System;
using System.Diagnostics;
using System.Collections;
using HtmlHelp.ChmDecoding;
namespace HtmlHelp
{
///
/// The class TableOfContents holds the TOC of the htmlhelp system class.
///
public class TableOfContents
{
private ArrayList _toc = new ArrayList();
///
/// Standard constructor
///
public TableOfContents()
{
}
///
/// Constructor of the class
///
///
public TableOfContents(ArrayList toc)
{
_toc = toc;
}
///
/// Gets the internal stored table of contents
///
public ArrayList TOC
{
get { return _toc; }
}
///
/// Clears the current toc
///
public void Clear()
{
if(_toc!=null)
_toc.Clear();
}
///
/// Gets the number of topics in the toc
///
/// Returns the number of topics in the toc
public int Count()
{
if(_toc!=null)
return _toc.Count;
else
return 0;
}
///
/// Merges the arrToC list to the one in this instance
///
/// the toc list which should be merged with the current one
internal void MergeToC( ArrayList arrToC )
{
if(_toc==null)
_toc = new ArrayList();
MergeToC(_toc, arrToC, null);
}
///
/// Merges the arrToC list to the one in this instance (called if merged files
/// were found in a CHM)
///
/// the toc list which should be merged with the current one
/// An arraylist of CHMFile instances.
internal void MergeToC( ArrayList arrToC, ArrayList openFiles )
{
if(_toc==null)
_toc = new ArrayList();
MergeToC(_toc, arrToC, openFiles);
}
///
/// Internal method for recursive toc merging
///
/// level of global toc
/// level of local toc
/// An arraylist of CHMFile instances.
private void MergeToC( ArrayList globalLevel, ArrayList localLevel, ArrayList openFiles )
{
foreach( TOCItem curItem in localLevel)
{
// if it is a part of the merged-links, we have to do nothing,
// because the method HtmlHelpSystem.RecalculateMergeLinks() has already
// placed this item at its correct position.
if(!IsMergedItem(curItem.Name, curItem.Local, openFiles))
{
TOCItem globalItem = ContainsToC(globalLevel, curItem.Name);
if(globalItem == null)
{
// the global toc doesn't have a topic with this name
// so we need to add the complete toc node to the global toc
globalLevel.Add( curItem );
}
else
{
// the global toc contains the current topic
// advance to the next level
if( (globalItem.Local.Length <= 0) && (curItem.Local.Length > 0) )
{
// set the associated url
globalItem.Local = curItem.Local;
globalItem.ChmFile = curItem.ChmFile;
}
MergeToC(globalItem.Children, curItem.Children);
}
}
}
}
///
/// Checks if the item is part of the merged-links
///
/// name of the topic
/// local of the topic
/// An arraylist of CHMFile instances.
/// Returns true if this item is part of the merged-links
private bool IsMergedItem(string name, string local, ArrayList openFiles)
{
if(openFiles==null)
return false;
foreach(CHMFile curFile in openFiles)
{
foreach(TOCItem curItem in curFile.MergLinks)
if( (curItem.Name == name) && (curItem.Local == local) )
return true;
}
return false;
}
///
/// Checks if a topicname exists in a SINGLE toc level
///
/// toc list
/// topic to search
/// Returns the topic item if found, otherwise null
private TOCItem ContainsToC(ArrayList arrToC, string Topic)
{
foreach(TOCItem curItem in arrToC)
{
if(curItem.Name == Topic)
return curItem;
}
return null;
}
///
/// Searches the table of contents for a special topic
///
/// topic to search
/// Returns an instance of TOCItem if found, otherwise null
public TOCItem SearchTopic(string topic)
{
return SearchTopic(topic, _toc);
}
///
/// Internal recursive tree search
///
/// topic to search
/// tree level list to look in
/// Returns an instance of TOCItem if found, otherwise null
private TOCItem SearchTopic(string topic, ArrayList searchIn)
{
foreach(TOCItem curItem in searchIn)
{
if(curItem.Name.ToLower() == topic.ToLower() )
return curItem;
if(curItem.Children.Count>0)
{
TOCItem nf = SearchTopic(topic, curItem.Children);
if(nf != null)
return nf;
}
}
return null;
}
}
}