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 _OpenDict = new Dictionary(); private Dictionary _SaveDict = new Dictionary(); private Dictionary _TextBoxDict = new Dictionary(); 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(""); foreach (XmlNode nd in nl) sb.Append(string.Format("\r\n {1}",nd.GetType().Name,nd.OuterXml)); sb.Append("\r\n"); 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) == " sl = new SortedList(); 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); } } }