|
using System;
using System.IO;
using System.Text;
namespace CodeTemplate
{
/// <summary>
/// Summary description for TemplateFileParser.
/// </summary>
internal class TemplateFileParser
{
public TemplateFileParser(String filename)
{
m_filename = filename;
}
public void Parse(TemplateFile parent, TemplatePopupMenu menu)
{
m_parent = parent;
m_curLine = 0;
using(m_reader = new StreamReader(m_filename, Encoding.Default))
{
m_parent.AddFileChangeInfo(m_filename);
String lineBuf;
while((lineBuf = ReadLine()) != null)
{
if(lineBuf.Length == 0 || IsComment(lineBuf))
continue;
if(IsInclude(lineBuf))
{
TemplateFileParser parser = new TemplateFileParser(ExtractIncludeFilename(lineBuf));
parser.Parse(parent, menu);
continue;
}
if(IsMenuSeparator(lineBuf))
{
menu.AddSeparator();
continue;
}
if(!IsMenuOpen(lineBuf))
throw new TemplateParserException("File must start with a menu definition", m_filename, m_curLine);
ParseMenu(ExtractMenuName(lineBuf), menu);
}
}
}
private String ReadLine()
{
String s = m_reader.ReadLine();
++m_curLine;
return s;
}
private String ExtractMenuName(String lineBuf)
{
return lineBuf.Trim().Substring(Delim.MenuOpen.Length);
}
private Boolean IsMenuOpen(String lineBuf)
{
return lineBuf.IndexOf(Delim.MenuOpen) != -1;
}
private Boolean IsMenuClose(String lineBuf)
{
return lineBuf.IndexOf(Delim.MenuClose) != -1;
}
private Boolean IsMenuSeparator(String lineBuf)
{
return lineBuf.IndexOf(Delim.Separator) != -1;
}
private Boolean IsComment(String lineBuf)
{
return lineBuf.StartsWith(Delim.Comment);
}
private Boolean IsInclude(String lineBuf)
{
return lineBuf.StartsWith(Delim.Include);
}
private String ExtractIncludeFilename(String lineBuf)
{
String includeFile = lineBuf.Substring(Delim.Include.Length).Trim();
String dir = Path.GetDirectoryName(includeFile);
if(dir != String.Empty && Path.IsPathRooted(dir))
return includeFile;
return Path.Combine(Path.GetDirectoryName(m_filename), includeFile);
}
private void ParseMenu(String menuName, TemplatePopupMenu menu)
{
if(menuName.Length == 0)
throw new TemplateParserException("Menu name expected", m_filename, m_curLine);
String menuID = null;
{
String[] nameParts = menuName.Split('|');
menuName = nameParts[0];
if(nameParts.Length > 1)
menuID = nameParts[1].ToUpper();
}
StringBuilder sb = new StringBuilder();
String lineBuf;
String menuItemText = menuName;
if(menuID != null)
menuItemText = String.Format("{0}\t[{1}]", menuItemText, menuID);
TemplateMenuItem menuItem = menu.EnterScope(menuItemText);
Int32 len = 0;
while((lineBuf = ReadLine()) != null)
{
// Only parse include statements found between menudefs
if(len == 0 && IsInclude(lineBuf))
{
TemplateFileParser parser = new TemplateFileParser(ExtractIncludeFilename(lineBuf));
parser.Parse(m_parent, menu);
continue;
}
else if(IsMenuOpen(lineBuf))
{
ParseMenu(ExtractMenuName(lineBuf), menu);
continue;
}
else if(IsMenuSeparator(lineBuf))
{
menu.AddSeparator();
}
else if(IsMenuClose(lineBuf))
{
if(menu.CurrentScope.Count == 0) // If it's not a popup menu
{
sb.Length = len; // Remove extra NewLine at the end
menu.Update(sb.ToString());
m_parent.CacheMenuID(menuID, menuItem);
}
menu.LeaveScope();
break;
}
else
{
sb.Append(lineBuf);
len = sb.Length;
sb.Append(Environment.NewLine);
}
}
}
private String m_filename;
private StreamReader m_reader;
private TemplateFile m_parent;
private Int32 m_curLine;
}
}
|
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.
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
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.