|
////////////////////////////////////////////////////////////////////////////////
// MixedContent Sample - Free to use
// � Copyright 2007, Rudi Breedenraedt
////////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace CodeProject.MixedContentSample.Parser
{
/// <summary>
/// Represents a file with mixed content.
/// </summary>
public class MixedContentFile
{
private string content;
private List<ContentPart> parts = new List<ContentPart>();
/// <summary>
/// Constructs a MixedContentFile for the given file having given conetent.
/// </summary>
public MixedContentFile(string content, int initialContentType)
{
// Set properties:
this.content = content;
// Seed the parts list:
this.parts.Add(new ContentPart(this, initialContentType, 0, content.Length, null));
}
/// <summary>
/// Content of the file.
/// </summary>
public string Content
{
get { return this.content; }
}
/// <summary>
/// List of parts the file is made of.
/// </summary>
public List<ContentPart> Parts
{
get { return this.parts; }
}
/// <summary>
/// Applies a parser regular expression to identifiy types of content.
/// </summary>
/// <param name="typeToSearch">The content type to search in.</param>
/// <param name="typeToSearchFor">The content type of the parts matching the given regular expression.</param>
/// <param name="expressionToSearchFor">The regular expression to use to match content parts.</param>
public void ApplyParserRegex(int typeToSearch, int typeToSearchFor, Regex expressionToSearchFor)
{
List<ContentPart> result = new List<ContentPart>();
foreach (ContentPart section in this.parts)
{
if (section.Type == typeToSearch)
{
int pos = 0;
foreach (Match match in expressionToSearchFor.Matches(section.Content))
{
if (pos < match.Index)
{
result.Add(new ContentPart(section.File, section.Type, section.Offset + pos, match.Index - pos, section.Match));
}
result.Add(new ContentPart(section.File, typeToSearchFor, section.Offset + match.Index, match.Length, match));
pos = match.Index + match.Length;
}
if (pos < section.Length)
{
result.Add(new ContentPart(section.File, section.Type, section.Offset + pos, section.Length - pos, section.Match));
}
}
else
{
result.Add(section);
}
}
// Perform iteration substitution:
this.parts = result;
}
}
}
|
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.