reposted from my other blog

inspired by this article, i decided to write a .net version. i thought this could be something useful for searching our code base at work. kind of the same as this guy has setup for his single machine, but i figured i could search a whole subversion tree and pipe it to our dev intranet site.

Since I was at it, I figured I would make it a little more flexible. You can set all the options through the querystring. if you don't like that, feel free to just set them manually. i didn't want to pass around the xml object, so i just made it static. looking at this, i feel like there is a better way to do this, but i can't put my finger on it at the moment and it works fine. i skipped doing full regex and only match lines that have the requested item. i also skipped returning all the matching lines in a single rss entry and i just return every line match in it's own item. if i didn't do that, i had to kind of pick what format to use internally for the matches and i decided i'd let someone do that with a style sheet.

enjoy!

using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.UI;
using System.Xml;

public partial class _Default : Page
{
static XmlTextWriter xml;
static string title;
static string path;
static Regex re;
static string filefilter;

static void EnumFolders(DirectoryInfo d)
{
EnumFiles(d);
foreach(DirectoryInfo sd in d.GetDirectories())
EnumFolders(sd);
}

static void EnumFiles(DirectoryInfo theDir)
{
foreach (FileInfo f in theDir.GetFiles(filefilter))
Find(f.FullName);
}

static void Find(string fn)
{
using (StreamReader r = new StreamReader(fn))
{
Match m = re.Match(r.ReadToEnd());
while (m.Success)
{
xml.WriteStartElement("item");
xml.WriteElementString("title", fn);
xml.WriteElementString("description", m.Value.Trim());
xml.WriteEndElement();
m = m.NextMatch();
}
}
}
    private void Page_Load(object sender, EventArgs e)
    {
        title = Request.QueryString["t"]; // name your feed
        path = Request.QueryString["p"]; // c:\code\
        re = new Regex("^.*" + Request.QueryString["r"] + ".*$",RegexOptions.Multiline); //TODO
        filefilter = Request.QueryString["f"]; //*.cs
    
        Response.Clear();
        Response.ContentType = "application/xml+rss";
        xml = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);
        xml.WriteStartDocument();
        xml.WriteStartElement("rss");
        xml.WriteAttributeString("version", "2.0");
        xml.WriteStartElement("channel");
        xml.WriteElementString("title", title);
        xml.WriteElementString("link", Request.Url.PathAndQuery);
        xml.WriteElementString("description", "rss for (" + re + ") in " + path + filefilter + " and it's sub directories");
        xml.WriteElementString("copyright", "(c) " + DateTime.Now.Year + ", " + Request.Url + " All rights reserved.");
        xml.WriteElementString("ttl", "5");
        try{
            EnumFolders(new DirectoryInfo(path));
        }
        catch (Exception ex)
        {
            xml.WriteStartElement("item");
            xml.WriteElementString("title", "error");
            xml.WriteElementString("description", ex.Message);
            xml.WriteEndElement();
        }
        xml.WriteEndElement();
        xml.WriteEndElement();
        xml.WriteEndDocument();
        xml.Flush();
        xml.Close();
        Response.End();
    }
}