289 lines
7.8 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml.Xsl;
using System.Xml;
using System.IO;
using System.Text.RegularExpressions;
namespace XSL_Transform
{
public partial class frmMain : Form
{
private bool[] _Dirty = new bool[] { false, false, false };
private string[] _Name = new string[] { "XML", "XSL", "Results" };
private SaveFileDialog[] _Sfd;
private OpenFileDialog[] _Ofd;
private TextBox[] _Tb;
private WebBrowser[] _Wb;
private Dictionary<OpenFileDialog, Tabs> _OpenDict = new Dictionary<OpenFileDialog, Tabs>();
private Dictionary<SaveFileDialog, Tabs> _SaveDict = new Dictionary<SaveFileDialog, Tabs>();
private Dictionary<TextBox, Tabs> _TextBoxDict = new Dictionary<TextBox, Tabs>();
enum Tabs : int
{
XML=0,XSL=1,Results=2
}
public frmMain()
{
InitializeComponent();
_Sfd = new SaveFileDialog[] { sfdxml, sfdxsl, sfdResults };
_Ofd = new OpenFileDialog[] { ofdxml, ofdxsl, ofdResults };
_Tb = new TextBox[] { tbSX, tbSS, tbSR };
_Wb = new WebBrowser[] { wbRX, wbRS, wbRR };
foreach (Tabs tab in Enum.GetValues(typeof(Tabs)))
{
_SaveDict.Add(_Sfd[(int)tab], tab);
_OpenDict.Add(_Ofd[(int)tab], tab);
_TextBoxDict.Add(_Tb[(int)tab], tab);
}
}
void SaveFileOk(object sender, CancelEventArgs e)
{
SaveIt(_SaveDict[(SaveFileDialog)sender]);
}
private void SaveIt(Tabs tab)
{
_Ofd[(int)tab].FileName = _Sfd[(int)tab].FileName;
StreamWriter sw = File.CreateText(_Sfd[(int)tab].FileName);
sw.Write(_Tb[(int)tab].Text);
sw.Close();
_Dirty[(int)tab] = false;
}
void OpenFileOk(object sender, CancelEventArgs e)
{
OpenIt(_OpenDict[(OpenFileDialog) sender]);
}
private void ShowTab(Tabs tabs)
{
if (tcResults.SelectedIndex == (int)tabs)
ShowActiveTab();
else
tcResults.SelectedIndex = (int)tabs;// Activate the Approproate Browser
}
private void OpenIt(Tabs tab)
{
_Sfd[(int)tab].FileName = _Ofd[(int)tab].FileName;
LoadTB(_Tb[(int)tab],_Ofd[(int)tab].FileName);
_Dirty[(int)tab] = false;
tcSource.SelectedIndex = (int)tab;// Activate the Appropriate Tab
ShowTab(tab);
}
private bool SaveChanges(Tabs tab)
{
if (!_Dirty[(int)tab]) return true;
DialogResult dr = MessageBox.Show("Save changes to " + _Name[(int)tab] + " file?", "Save Changes",
MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
if (dr == DialogResult.Cancel) return false;
if (dr == DialogResult.Yes) SaveIt(tab);
return true;
}
private void LoadTB(TextBox tb, string fileName)
{
FileInfo f = new FileInfo(fileName);
StreamReader sr = f.OpenText();
tb.Text = sr.ReadToEnd();
sr.Close();
}
private void miQuit_Click(object sender, EventArgs e)
{
if (SaveChanges()) this.Close();
}
private bool SaveChanges()
{
foreach (Tabs tab in Enum.GetValues(typeof(Tabs)))
if (!SaveChanges(tab)) return false;
return true;
}
private Tabs TabValue(ToolStripDropDownItem mi)
{
return (Tabs)int.Parse(mi.Tag.ToString());
}
private void miOpen_Click(object sender, EventArgs e)
{
Tabs tab = TabValue((ToolStripDropDownItem)sender);
if (SaveChanges(tab))
_Ofd[(int)tab].ShowDialog();
}
private void miRefresh_Click(object sender, EventArgs e)
{
Tabs tab = TabValue((ToolStripDropDownItem)sender);
OpenIt(tab);
}
private void miSaveAs_Click(object sender, EventArgs e)
{
Tabs tab = TabValue((ToolStripDropDownItem)sender);
_Sfd[(int)tab].ShowDialog();
}
private void miSave_Click(object sender, EventArgs e)
{
Tabs tab = TabValue((ToolStripDropDownItem)sender);
if (_Sfd[(int)tab].FileName == string.Empty)
_Sfd[(int)tab].ShowDialog();
else
SaveIt(tab);
}
private string FormatException(Exception ex)
{
return string.Format("{0} - {1}", ex.GetType().Name, ex.Message);
}
private void ShowException(Exception ex)
{
ShowException(ex, null);
}
private void ShowException(Exception ex,string sResults)
{
if (sResults != null)
{
LoadTB(tbSR, sResults);
}
StringBuilder sb = new StringBuilder(FormatException(ex));
sb.Append("\r\n");
for (; ex.InnerException != null; ex = ex.InnerException)
{
sb.Append(FormatException(ex.InnerException));
sb.Append("\r\n");
}
tbSR.SelectionStart = 0;
tbSR.SelectedText=sb.ToString();
}
private void miQuery_Click(object sender, EventArgs e)
{
try
{
XmlDocument xd = new XmlDocument();
xd.LoadXml(tbSX.Text);
XmlNodeList nl = xd.SelectNodes(tbSS.Text);
if (nl.Count > 0)
{
StringBuilder sb= new StringBuilder("<Results>");
foreach (XmlNode nd in nl)
sb.Append(string.Format("\r\n <Result Type='{0}'>{1}</Result>",nd.GetType().Name,nd.OuterXml));
sb.Append("\r\n</Results>");
tbSR.Text = sb.ToString();
}
else
tbSR.Text = "No nodes found matching criteria";
}
catch (Exception ex)
{
ShowException(ex);
}
ShowTab(Tabs.Results);
}
private void miTransform_Click(object sender, EventArgs e)
{
string sXSL = Save(tbSS, "tmpS");
string sXML = Save(tbSX, "tmpX");
string sResults = Application.StartupPath + "\\" + "tmpR.XML";
try
{
XslCompiledTransform xsl = new XslCompiledTransform();
xsl.Load(sXSL);
xsl.Transform(sXML, sResults); // Perform Transform
LoadTB(tbSR, sResults);
}
catch (Exception ex)
{
ShowException(ex,sResults);
}
ShowTab(Tabs.Results);
}
private void ShowActiveTab()
{
Tabs tab = (Tabs)tcResults.SelectedIndex;
_Wb[(int)tab].Navigate(Save(_Tb[(int)tab], _Name[(int)tab]));
}
private void tcResults_SelectedIndexChanged(object sender, EventArgs e)
{
switch (tcResults.SelectedIndex)
{
case 3:
if (wbXSLHelp.Url == null)
wbXSLHelp.Navigate("http://www.w3schools.com/xsl/default.asp");
break;
case 4:
if (wbXPathHelp.Url == null)
wbXPathHelp.Navigate("http://www.w3schools.com/xpath/");
break;
default:
ShowActiveTab();
break;
}
}
private string Extension(string txt)
{
if (txt.Substring(0, 5) == "<html") return ".html";
try
{
XmlDocument xd = new XmlDocument();
xd.LoadXml(txt);
if (xd.OuterXml == string.Empty) return ".txt";
else return ".xml";
}
catch (Exception)
{
return ".txt";
}
}
private string Save(TextBox tb, string path)
{
if (tb.Text == string.Empty) return "about:blank";
string sPath = Application.StartupPath + "\\" + path + Extension(tb.Text) ;
StreamWriter sw = File.CreateText(sPath);
sw.Write(tb.Text);
sw.Close();
return sPath;
}
private void tbTextChanged(object sender, EventArgs e)
{
_Dirty[(int)_TextBoxDict[(TextBox)sender]] = true;
}
private void tbSX_MouseUp(object sender, MouseEventArgs e)
{
RefreshLabels(tbSX.Text, tbSX.SelectionStart);
}
private void RefreshLabels(string s, int location)
{
int row = 0;
int col = 0;
int ii = 0;
while (ii < location)
{
if (s[ii] == '\r')
{
row++;
col = 0;
}
if (s[ii] != '\n') col++;
ii++;
}
RowLbl.Text = string.Format("Row: {0}", row);
ColLbl.Text = string.Format("Col: {0}", col);
}
private void specialToolStripMenuItem_Click(object sender, EventArgs e)
{
SortedList<int, int> sl = new SortedList<int, int>();
foreach (Match match in Regex.Matches(tbSX.Text,"&#x([0-9]*);"))
{
int num = int.Parse(match.Value.Substring(3).TrimEnd(";".ToCharArray()));
if(num != 9 && num != 10 && num != 13){
if (sl.ContainsKey(num)) sl[num] = sl[num] + 1;
else sl[num] = 1;
}
}
// When all done show the results in a list.
tbSR.Text = "Special Characters";
foreach (int key in sl.Keys)
{
tbSR.Text += string.Format("\r\n{0} Ctrl-{1} - {2}", key, Convert.ToChar('A' - 1 + key), sl[key]);
}
ShowTab(Tabs.Results);
}
}
}