Thursday, September 18, 2008

Abusing the String.Format() function, or an efficent way to check if a record can be deleted?

In my main project at work, I'm building an application to configure a database. An issue I had to overcome was how to determine if a record could be deleted, or if its deletion would result in orphan records.


I have come up with a model of creating an AssertCanDelete() function that checks all of the tables that could be referencing a given record, and throws an exception saying the name of the table that is referencing the record. The default format for this method is as follows:


Create a Select statement that has a Select Count(*) sub query for each table that could reference the object.

Go through the result set and if any value is greater than 0, then I know that it is being referenced, and can't be deleted.

I used the the String.Format in my Command Text and ended up something like this:



            cm.CommandText = String.Format("SELECT * FROM " & _
"(SELECT COUNT(*) FROM {0}{3} WHERE {3}.{1} = :{1} AND {3}.{2} = :{2} ), " & _
"(SELECT COUNT(*) FROM {0}{4} WHERE {4}.{1} = :{1} AND {4}.{2} = :{2} ), " & _
"(SELECT COUNT(*) FROM {0}{5} WHERE {5}.{1} = :{1} AND {5}.{2} = :{2} ) ", _
DatabaseOwner, "PRTNR_ID", "SECT_NAME", "PRTNR_SECT_DE", "PRTNR_SECT_SRVC_FIL", "PRTNR_SECT_WF_STEP_ROLE")
cm.Parameters.Add("PRTNR_ID", OracleType.Number).Value = criteria.PrtnrId
cm.Parameters.Add("SECT_NAME", OracleType.VarChar).Value = criteria.SectName





Which I kind of like. The first "{0}" is the database table schema or database owner. The second "{}" for each line is the Table Name, which then gets repeated with a . and the first part of the primary key "{1}" and AND'ed to the second part of the primary key "{2}". It saved me alot of time having to write every SQL statement and every table name multiple times. I could also just copy a line, and insert the table name once, and I was good to go. Another useful way to not repeat yourself.


When I was following this patern for another table, things quickly got ugly when I realized that there where multiple columns that would be referencing my record. Below is the code, what do you think? Is it an abuse of the String.Format() function and the entire process needs to be trashed, or is it an efficient way to check if a record can be deleted?



    Private Shared Sub AssertCanDelete(ByVal tr As OracleTransaction, ByVal criteria As Criteria)
Using cm As OracleCommand = tr.Connection.CreateCommand()
cm.Transaction = tr
cm.CommandType = CommandType.Text
cm.CommandText = String.Format("SELECT * FROM " & _
"(SELECT COUNT(*) FROM {0}{2} WHERE :{1} in ({2}.{1}, {2}.INIT_{1}, {2}.COMPARE_{1}) ), " & _
"(SELECT COUNT(*) FROM {0}{3} WHERE {3}.{1} = :{1} ), " & _
"(SELECT COUNT(*) FROM {0}{4} WHERE {4}.{1} = :{1} ), " & _
"(SELECT COUNT(*) FROM {0}{5} WHERE {5}.{1} = :{1} ), " & _
"(SELECT COUNT(*) FROM {0}{6} WHERE {6}.{1} = :{1} ), " & _
"(SELECT COUNT(*) FROM {0}{7} WHERE {7}.{1} = :{1} ), " & _
"(SELECT COUNT(*) FROM {0}{8} WHERE :{1} in ({8}.{1}, {8}.INIT_{1}, {8}.COMPARE_{1}) ), " & _
"(SELECT COUNT(*) FROM {0}{9} WHERE :{1} in ({9}.{1}, {9}.INIT_{1}) ), " & _
"(SELECT COUNT(*) FROM {0}{10} WHERE :{1} in ({10}.{1}, {10}.INIT_{1}, {10}.COMPARE_{1}) ), " & _
"(SELECT COUNT(*) FROM {0}{11} WHERE :{1} in ({11}.{1}, {11}.INIT_{1}, {11}.COMPARE_{1}), {11}.NEXT_{1}) ), " & _
"(SELECT COUNT(*) FROM {0}{12} WHERE :{1} in ({12}.{1}, {12}.INIT_{1}) ), " & _
"(SELECT COUNT(*) FROM {0}{13} WHERE :{1} in ({13}.{1}, {13}.INIT_{1}, {13}.COMPARE_{1}) ), " & _
"(SELECT COUNT(*) FROM {0}{14} WHERE :{1} in ({14}.{1}, {14}.INIT_{1}, {14}.COMPARE_{1}), {14}.NEXT_{1}) ) ", _
DatabaseOwner, "SECT_NAME", "sect_valid_ref", "sect_de_ref", "pkg_sect_de_ref", "prtnr_sect_de", "prtnr_sect", "pkg_sect_ref", "pkg_sect_valid_ref", "pkg_sect_de_fltr_ref", "pkg_sect_de_valid", "pkg_sect_de_nav_ref", "sect_de_fltr_ref", "sect_de_valid_ref", "sect_sect_de_nav")
cm.Parameters.Add("SECT_NAME", OracleType.VarChar).Value = criteria.SectName


Using dr As SafeDataReader = New SafeDataReader(cm.ExecuteReader())
dr.Read()
If dr.GetInt32(0) > 0 Then
Throw New ConstraintException("Section (" & criteria.SectName & ") can not be deleted because it is currently referenced in the Section Validation Reference table")
End If
If dr.GetInt32(1) > 0 Then
Throw New ConstraintException("Section (" & criteria.SectName & ") can not be deleted because it is currently referenced in the Section Data Element Reference table")
End If
If dr.GetInt32(2) > 0 Then
Throw New ConstraintException("Section (" & criteria.SectName & ") can not be deleted because it is currently referenced in the Package Section Data Element Reference table")
End If
If dr.GetInt32(3) > 0 Then
Throw New ConstraintException("Section (" & criteria.SectName & ") can not be deleted because it is currently referenced in the Partner Section Data Element table")
End If
If dr.GetInt32(4) > 0 Then
Throw New ConstraintException("Section (" & criteria.SectName & ") can not be deleted because it is currently referenced in the Partner Section table")
End If
If dr.GetInt32(5) > 0 Then
Throw New ConstraintException("Section (" & criteria.SectName & ") can not be deleted because it is currently referenced in the Package Section Reference table")
End If
If dr.GetInt32(6) > 0 Then
Throw New ConstraintException("Section (" & criteria.SectName & ") can not be deleted because it is currently referenced in the Package Section Validation Reference table")
End If
If dr.GetInt32(7) > 0 Then
Throw New ConstraintException("Section (" & criteria.SectName & ") can not be deleted because it is currently referenced in the Package Section Data Element Filter Reference table")
End If
If dr.GetInt32(8) > 0 Then
Throw New ConstraintException("Section (" & criteria.SectName & ") can not be deleted because it is currently referenced in the Package Section Data Element Validation Reference table")
End If
If dr.GetInt32(9) > 0 Then
Throw New ConstraintException("Section (" & criteria.SectName & ") can not be deleted because it is currently referenced in the Package Section Data Element Navigation Reference table")
End If
If dr.GetInt32(10) > 0 Then
Throw New ConstraintException("Section (" & criteria.SectName & ") can not be deleted because it is currently referenced in the Section Data Element Filter Reference table")
End If
If dr.GetInt32(11) > 0 Then
Throw New ConstraintException("Section (" & criteria.SectName & ") can not be deleted because it is currently referenced in the Section Data Element Validation Reference table")
End If
If dr.GetInt32(12) > 0 Then
Throw New ConstraintException("Section (" & criteria.SectName & ") can not be deleted because it is currently referenced in the Section Data Element Navigation Reference table")
End If
End Using
End Using
End Sub


Monday, September 15, 2008

Creating a VS.Net 2008 Addin

In trying to take the RTF Converter to a new level, I decided to create it as an Addin. As this is my first time creating an add in, I decided to share what I have learned.

VS 2008 has a Project wizard that is used to create the project that works quite well when trying to debug your add in, but when trying to actually run it, doesn't work at all. Some of the Enumerations that happen when running the addin in debug mode, never happen when trying to run it for real.

Some other issues that I ran into was a complete lack of examples for creating a link on the right click menu of the code. I did find a website and was able to add mix and match it with the auto generated code from the wizard to come up with a workable solution.

The first step in creating an Add in, is to Implement the Extensibility.IDTExtensibility2 interface and IDTCommand Target. There are three main functions that you need to be concerned about

1. OnConnection()
- This function gets called either when VS starts, or when the app is loaded. This is where code that is needed to either add your add int to the tool bar, or (as in my case) add it as a CommandBarPopup( Basically a menu that has children commands that pop out to the side). You have to find the "Code Window" Command bar, then add a CommandBarPopup, then add any commands that you want to have displayed.
I refactored a new AddContextMenuChildItem method to handle adding the new commands that I wanted to implement to my context menu. On of the most confusing parts was trying to determine what I call the IconId to use. I ended up having to create a tempory project that created every single icon that was possible, to go through and select the ones that made the most sense. I know you can add your own personal icons, but I didn't have the time or the patience.
I also ran into an issu with the command already existing if I opened up another version of VS, but not being added to the PopupBar. So I had to add extra logic if it already exists, to just add it. I guess I should probably change this even further to check to see if it exists first, not the other way around.

2. QueryStatus()
- This function is used to hide or show items. I wanted to show the Convert Selected option, only if something was selected, so I had to add logic for this. If you always wanted them to be displayed, I think you could always just return true.

3. Exec()
- This is the function that gets fired when the command gets clicked. It takes some simple logic to determine what got clicked, and then I'm on my way.

The source code is listed below. Hopefully this will be helpful for others.


using System;
using Extensibility;
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio.CommandBars;
using System.Resources;
using System.Reflection;
using System.Globalization;
using System.Windows.Forms;
namespace CodeToHtml

/// <summary>The object for implementing an Add-in.</summary>
/// <seealso class='IDTExtensibility2' />
public class Connect : IDTExtensibility2, IDTCommandTarget

private const string ConvertSelection = "ConvertSelectionToHtml";
private const string OpenWindowForConvert = "OpenConversionWindow";
private bool hasBeenLoaded = false;

/// <summary>Implements the constructor for the Add-in object. Place your initialization code within this method.</summary>
public Connect()



/// <summary>Implements the OnConnection method of the IDTExtensibility2 interface. Receives notification that the Add-in is being loaded.</summary>
/// <param term='application'>Root object of the host application.</param>
/// <param term='connectMode'>Describes how the Add-in is being loaded.</param>
/// <param term='addInInst'>Object representing this Add-in.</param>
/// <seealso class='IDTExtensibility2' />
public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)

try {
_applicationObject = (DTE2)application;
_addInInstance = (AddIn)addInInst;
//if (connectMode == ext_ConnectMode.ext_cm_UISetup) { //For some Reason, this never gets called, so I've changed it
if (connectMode != ext_ConnectMode.ext_cm_CommandLine) {
object[] contextGUIDS = new object[] { };
Commands2 commands = (Commands2)_applicationObject.Commands;

string toolsMenuName;

try {
//If you would like to move the command to a different menu, change the word "Tools" to the
// English version of the menu. This code will take the culture, append on the name of the menu
// then add the command to that menu. You can find a list of all the top-level menus in the file
// CommandBar.resx.
string resourceName;
ResourceManager resourceManager = new ResourceManager("CodeToHtml.CommandBar", Assembly.GetExecutingAssembly());
CultureInfo cultureInfo = new CultureInfo(_applicationObject.LocaleID);

if (cultureInfo.TwoLetterISOLanguageName == "zh") {
System.Globalization.CultureInfo parentCultureInfo = cultureInfo.Parent;
resourceName = String.Concat(parentCultureInfo.Name, "Tools");
}
else {
resourceName = String.Concat(cultureInfo.TwoLetterISOLanguageName, "Tools");
}
toolsMenuName = resourceManager.GetString(resourceName);
}
catch {
//We tried to find a localized version of the word Tools, but one was not found.
// Default to the en-US word, which may work for the current culture.
toolsMenuName = "Tools";
}

//Place the command on the tools menu.
//Find the MenuBar command bar, which is the top-level command bar holding all the main menu items:
Microsoft.VisualStudio.CommandBars.CommandBar codeWindowContextCommandBar = ((Microsoft.VisualStudio.CommandBars.CommandBars)_applicationObject.CommandBars)["Code Window"];

CommandBarPopup toolsPopup = (CommandBarPopup)codeWindowContextCommandBar.Controls.Add(MsoControlType.msoControlPopup, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);
toolsPopup.Caption = "Code To Html";
//This try/catch block can be duplicated if you wish to add multiple commands to be handled by your Add-in,
// just make sure you also update the QueryStatus/Exec method to include the new command names.
AddContextMenuChildItem(ref contextGUIDS, commands, toolsPopup, ConvertSelection, 223, "Convert Selection To HTML", "Converts the selected code to html");
AddContextMenuChildItem(ref contextGUIDS, commands, toolsPopup, OpenWindowForConvert, 178, "Open HTML Convert Window", "Opens a window to paste text in to convert");
hasBeenLoaded = true;
}

}
catch(Exception ex) {
MessageBox.Show(ex.ToString(), "Code To HTML");
}


private void AddContextMenuChildItem(ref object[] contextGUIDS, Commands2 commands, CommandBarPopup toolsPopup, string name, int iconId, string buttonText, string helpText) {
try {
//Add a command to the Commands collection:
Command command = commands.AddNamedCommand2(_addInInstance, name, buttonText, helpText, true, iconId, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);

//Add a control for the command to the tools menu:
if ((command != null) && (toolsPopup != null)) {
command.AddControl(toolsPopup.CommandBar, toolsPopup.CommandBar.accChildCount + 1);
}
else if(command == null){
MessageBox.Show("Command Was Null", "Code To HTML");
}
else
MessageBox.Show("Tools Popup Was Null", "Code To HTML");
}
}
catch (System.ArgumentException) {
if(!hasBeenLoaded){
// For some reason this was having issues adding the command, so if it already exists, I need to re-add it to the Command Bar.
foreach(Command command in commands){
if (command.Name == "CodeToHtml.Connect." + name) {
command.AddControl(toolsPopup.CommandBar, toolsPopup.CommandBar.accChildCount + 1);
return;
}
}

MessageBox.Show("Shows exists, when it hasn't been loaded", "Code To HTML");
}
}
}

/// <summary>Implements the OnDisconnection method of the IDTExtensibility2 interface. Receives notification that the Add-in is being unloaded.</summary>
/// <param term='disconnectMode'>Describes how the Add-in is being unloaded.</param>
/// <param term='custom'>Array of parameters that are host application specific.</param>
/// <seealso class='IDTExtensibility2' />
public void OnDisconnection(ext_DisconnectMode disconnectMode, ref Array custom)



/// <summary>Implements the OnAddInsUpdate method of the IDTExtensibility2 interface. Receives notification when the collection of Add-ins has changed.</summary>
/// <param term='custom'>Array of parameters that are host application specific.</param>
/// <seealso class='IDTExtensibility2' />
public void OnAddInsUpdate(ref Array custom)



/// <summary>Implements the OnStartupComplete method of the IDTExtensibility2 interface. Receives notification that the host application has completed loading.</summary>
/// <param term='custom'>Array of parameters that are host application specific.</param>
/// <seealso class='IDTExtensibility2' />
public void OnStartupComplete(ref Array custom)



/// <summary>Implements the OnBeginShutdown method of the IDTExtensibility2 interface. Receives notification that the host application is being unloaded.</summary>
/// <param term='custom'>Array of parameters that are host application specific.</param>
/// <seealso class='IDTExtensibility2' />
public void OnBeginShutdown(ref Array custom)



/// <summary>Implements the QueryStatus method of the IDTCommandTarget interface. This is called when the command's availability is updated</summary>
/// <param term='commandName'>The name of the command to determine state for.</param>
/// <param term='neededText'>Text that is needed for the command.</param>
/// <param term='status'>The state of the command in the user interface.</param>
/// <param term='commandText'>Text requested by the neededText parameter.</param>
/// <seealso class='Exec' />
public void QueryStatus(string commandName, vsCommandStatusTextWanted neededText, ref vsCommandStatus status, ref object commandText)

try {
//if (neededText == vsCommandStatusTextWanted.vsCommandStatusTextWantedNone) {
if (commandName == "CodeToHtml.Connect." + OpenWindowForConvert || commandName == "CodeToHtml.Connect." + ConvertSelection && UserHasTextSelected(false)) {
status = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported | vsCommandStatus.vsCommandStatusEnabled;
return;
}
//}
//MessageBox.Show(commandName + " not supported\n" + neededText.ToString());
} catch (Exception ex) {
MessageBox.Show(ex.ToString(), "Code To HTML");
}


/// <summary>Implements the Exec method of the IDTCommandTarget interface. This is called when the command is invoked.</summary>
/// <param term='commandName'>The name of the command to execute.</param>
/// <param term='executeOption'>Describes how the command should be run.</param>
/// <param term='varIn'>Parameters passed from the caller to the command handler.</param>
/// <param term='varOut'>Parameters passed from the command handler to the caller.</param>
/// <param term='handled'>Informs the caller if the command was handled or not.</param>
/// <seealso class='Exec' />
public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)

handled = false;
if(executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)

switch (commandName.Substring(commandName.LastIndexOf('.') + 1))
{
case OpenWindowForConvert:
RTF2HTMLConverter.RTF2HTMLForm form = new RTF2HTMLConverter.RTF2HTMLForm();
form.Show();
handled = true;
break;
case ConvertSelection:
if(UserHasTextSelected(true)){
((
EnvDTE.TextSelection)_applicationObject.ActiveDocument.Selection).Copy();
string text = Clipboard.GetText(System.Windows.Forms.TextDataFormat.Rtf);
Clipboard.SetText(RTF2HTMLConverter.ConvertLogic.GetFormattedHtml(text));
}
break;
}



private bool UserHasTextSelected(bool assertError) {
string error = string.Empty;
if(_applicationObject.ActiveDocument == null){
error = "No Document Selected";
}
else if (_applicationObject.ActiveDocument.Selection == null) {
error = "Nothing Selected";
}
else {
try {
EnvDTE.TextSelection text = (EnvDTE.TextSelection)_applicationObject.ActiveDocument.Selection;
if (text.Text == string.Empty) {
error = "No Text Selected";
}
}
catch {
error = "No Text Selected";
}
}

if(error != string.Empty){
if(assertError){
MessageBox.Show(error, "CodeToHtml");
}
return false;
}
else
return true;
}
}
private DTE2 _applicationObject;
private AddIn _addInInstance;




Friday, September 5, 2008

Using LINQ To Ensure No Objects Have Duplicate Keys

I had an interseting business requirement the other day. I'm working with a CSLA Collection of objects, and they wanted the end user to be able to be able to change a property that is one of the three columns that makes up the primary key. I sat down and started using some LINQ to objects and was amazed at how much power it has in just a 6 lines of code. It was also my first time using the Group By Function

Normally with the group by, you perform some agregate functions on the group. So the "Into Group" would be "Into Sum(naviagation.SectDeNavOrdrNbr)". But by Grouping it into group, the result is a collection that contains the objects in the Group By Clause (SectDeNavOrdNbr, SectName, DeName) and then a collection of all the objects that are in that group.

So after I have my group by clause, I perform a contains to see if any collections of the group have more than one item in them. The must amazing part, is techinally, it only took 1 line of code! In the words of a dear friend of mine, "God Bless .Net".




Dim hasDuplicatePrimaryKeys As Boolean = ( _
From navigation As SectionDataElementNavigationReference In Me _
Group By navigation.SectDeNavOrdNbr, navigation.SectName, navigation.DeName _
Into Group).Contains(Function(c) c.Group.Count() > 1)

If (hasDuplicatePrimaryKeys) Then
Throw New InvalidConstraintException("More than one Section Data Element Navigation Reference was found with the same Section Name, Data Element Name, and Section Data Element Navigation Order Number")
End If






Overcoming Displaying Code as HTML

On my previous post, I spent about an hour just getting the formatting to look right. I ended up pasting it into word, and fooling around with the formatting till I got it just right, and saving it, then e-mailing it to myself in Gmail so I could view it directly from there, and it looked sort of ok, except that it had some extra lines in it, so I had to work directly with the HTML to get it to look the way I wanted.

Because computer programmers like myself are lazy, and if we have to do something twice, that's once to much, I wrote a program to do this for me. Right now it consists of Two RichTextBoxes and a WebBrowser thrown onto a windows from. Paste RTF code (like the code from VS) into rtfTextBox, and press the convert button and presto, you're done. The Html is outputed in another RichTextBox, and copied to the Clipboard, while the Webrowser gives you a preview. No CSS are needed.

In the near future I'd love to turn this into a VS add in so I can just select some code, right click, and select Convert.

You'll fine the source code posted below, which was generated using itself of course. :)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RTF2HTMLConverter {
public partial class Form1 : Form {
const string BackwardsSlash = "&bws;";
const string OpenCurlyBracket = "&ocb;";
const string CloseCurlyBracker = "&ccb;";
const string DoubleQuote = "&quot;";
const string GreaterThan = "&gt;";
const string LessThan = "&lt;";
const string Ampersand = "&amp";
bool _inColorSpan;

public Form1() {
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e) {
string[] rtf = rtfTextBox.Rtf.Replace("&", Ampersand).Replace("\\\\", BackwardsSlash).Replace("\\}", CloseCurlyBracker).Replace("\\{", OpenCurlyBracket).Replace("\"", DoubleQuote).Replace(">", GreaterThan).Replace("<", LessThan).Split('\n');
string rtfLine;
_inColorSpan = false;
Dictionary<string, string> colors = new Dictionary<string, string>();
StringBuilder output = new StringBuilder();
output.Append("<pre class=\"code\"><span style=\"font-size:85%;\">");

for (int i = 1; i < rtf.Length; i++) {
rtfLine = rtf[i];
if(i == 1){
GetColors(rtfLine, colors);
}
else
output.Append(GetOutputLine(rtfLine, colors));
}
}
output.Append("</span></pre>\n");
htmlTextBox.Text = output.ToString().Replace(BackwardsSlash, "\\").Replace(CloseCurlyBracker, "}").Replace(OpenCurlyBracket,"{");
webBrowser1.DocumentText = htmlTextBox.Text;
Clipboard.SetText(htmlTextBox.Text);
}

private string GetOutputLine(string rtfLine, Dictionary<string, string> colors) {
string outputLine = string.Empty;
string[] rtfLineSegments = rtfLine.Split('\\');
string firstWord;
string currentColor = string.Empty;
foreach (string segment in rtfLineSegments) {
if(segment == string.Empty){
continue;
}
firstWord = GetFirstWord(segment);

if (firstWord.IndexOf(' ') == 0) {//Trailing Spaces, just add it
outputLine += segment;
}
else if (colors.Keys.Contains(firstWord.Trim())) {//Change the Color
if (segment == firstWord) { // No Other Text, just change the color
currentColor = firstWord;
outputLine += GetColorSpan(colors[segment]);
}
else if(segment.Substring(firstWord.Length).Trim() != string.Empty) { // Change color and Add the rest of the segment if there is something to update
if (currentColor != string.Empty && currentColor == firstWord.Trim()) { // Color hasn't changed, skip adding new color span
outputLine += segment.Substring(firstWord.Length);
}
else {
currentColor = firstWord.Trim();
outputLine += GetColorSpan(colors[currentColor]) + segment.Substring(firstWord.Length);
}
}
else//all Blank Spaces, don't worry about the color
outputLine += segment.Substring(firstWord.Length);
}
}
else if (!segment.Contains(' ') && rtfLineSegments.Length > 2) { // Control word, skip it
continue;
}
else if (!segment.Contains(' ') && rtfLineSegments[0] == segment && rtfLineSegments.Length == 2) {// There is no Color for this segment, so use everything
outputLine += segment;
}
else
//First word is control word, skip it and add the rest
outputLine += segment.Substring(firstWord.Length);
}
}
return outputLine += "\n";
}

private static string GetFirstWord(string segment) {
string firstWord;
if (!segment.Contains(' ')) {
firstWord = segment;
}
else {
firstWord = segment.Substring(0, segment.IndexOf(' ') + 1);
}
return firstWord;
}

private static void GetColors(string rtfLine, Dictionary<string, string> colors) {
string[] colorValues = rtfLine.Substring(1, rtfLine.Length - 2).Split(';');
string rtfColor;
string htmlColor;
for (int k = 1; k < colorValues.Length; k++) {
rtfColor = colorValues[k];
if (rtfColor == "}") {
continue;
}
if (rtfColor.Contains("\\red")) {
string[] hexColors = rtfColor.Split('\\');
htmlColor = "#" + GetHexValue(hexColors[1], "red") + GetHexValue(hexColors[2], "green") + GetHexValue(hexColors[3], "blue");
htmlColor = ConvertColor(htmlColor);

htmlColor = "color: " + htmlColor;
}
else {
throw new Exception("Unrecognized Color " + rtfColor);
}

colors.Add("cf" + k.ToString(), htmlColor);
}
}

private static string ConvertColor(string htmlColor) {// Convert the Colors from a Dark Scheme, to a White Scheme
switch (htmlColor) {
case "#ff8000":
htmlColor = "blue"; // Reserved Words
break;
case "#fefffe":
htmlColor = "#010001"; // Regular Text
break;
case "#808080":
htmlColor = "green"; // Comments
break;
case "#ffffff":
htmlColor = "black"; //;
break;
case "#ffff00":
htmlColor = "#2b91af"; //class Names
break;
case "#2892b0":
htmlColor = "red"; // Integers
break;
case "#00ff00":
htmlColor = "#a31515"; // Strings
break;

}
return htmlColor;
}

private static string GetHexValue(string hexColor, string colorName){
return System.Convert.ToString(int.Parse(hexColor.Substring(colorName.Length, hexColor.Length - colorName.Length)), 16).PadLeft(2, '0');
}

private string GetColorSpan(string color){
if (_inColorSpan) {
return string.Format("</span><span style=\"{0}\">", color);
}
else {
_inColorSpan = true;
return string.Format("<span style=\"{0}\">", color);
}
}
}
}