PageRenderTime 54ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/XmlVisualizer/Visualizer/VisualizerUserControl.cs

#
C# | 1901 lines | 1630 code | 265 blank | 6 comment | 162 complexity | b6a4fb3f8b987a0891633a347c95c33f MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. // Xml Visualizer v.2
  2. // by Lars Hove Christiansen (larshove@gmail.com)
  3. // http://www.codeplex.com/XmlVisualizer
  4. using System;
  5. using System.ComponentModel;
  6. using System.IO;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Xml;
  10. using System.Xml.XPath;
  11. namespace XmlVisualizer
  12. {
  13. public partial class VisualizerUserControl : UserControl
  14. {
  15. public delegate void CloseEventHandler();
  16. public event CloseEventHandler CloseEvent;
  17. public delegate void StatusTextEventHandler(string statusText);
  18. public event StatusTextEventHandler StatusTextEvent;
  19. public bool inject;
  20. public string originalXmlFile;
  21. private const int numberOfXsdFilesToSave = 10;
  22. private const int numberOfXPathQueriesToSave = 10;
  23. private const int numberOfXsltFilesToSave = 10;
  24. private const int numberOfXmlFilesToSave = 10;
  25. private States ActiveState;
  26. private States StateBeforeNewFile;
  27. private States StateBeforeXsltError;
  28. private string previousXPathQuery;
  29. private string previousXsltFile;
  30. private string previousXmlFile;
  31. private string previousXsdFile;
  32. private string previousFileFromXPath;
  33. private string previousFileFromXslt;
  34. private string appliedXsdFile;
  35. private string appliedXsltFile;
  36. private string debugString;
  37. private bool mainFormLoaded;
  38. private bool errorInXslt;
  39. private bool errorInXml;
  40. private bool editorEnabled;
  41. private bool treeviewEnabled;
  42. private bool newFile;
  43. private bool doNotDeleteFile;
  44. private bool debugMode;
  45. private bool standAlone;
  46. public VisualizerUserControl()
  47. {
  48. InitializeComponent();
  49. SetXPathDefaultType();
  50. FillXPathComboBox();
  51. FillXsdFileComboBox();
  52. FillXsltFileComboBox();
  53. FillInputFileComboBox();
  54. SetActive(States.InputFile);
  55. ReadInjectState();
  56. InitializeTreeViewEvents();
  57. InitializeEditorEvents();
  58. InitializeVisualizerEvents();
  59. originalXmlFile = "";
  60. debugString = "";
  61. SetNoInputFileOptions();
  62. }
  63. public void SetWebBrowserFocus()
  64. {
  65. webBrowser.Focus();
  66. }
  67. private void InitializeVisualizerEvents()
  68. {
  69. OnDisposeEvent += VisualizerUserControl_OnDisposeEvent;
  70. }
  71. private void VisualizerUserControl_OnDisposeEvent()
  72. {
  73. webBrowser.Dispose();
  74. Cleanup();
  75. }
  76. /// <summary>
  77. /// Gets or Sets the visibility of the status bar
  78. /// </summary>
  79. [Description("Gets or Sets the visibility of the status bar"), Category("Visualizer Control"), DefaultValue(true), Browsable(true)]
  80. public bool StatusBarVisible
  81. {
  82. get
  83. {
  84. return statusStrip.Visible;
  85. }
  86. set
  87. {
  88. statusStrip.Visible = value;
  89. }
  90. }
  91. public void SetCloseButtonText(string text)
  92. {
  93. closeButton.Text = text;
  94. }
  95. public void SetDebugMode()
  96. {
  97. debugMode = true;
  98. }
  99. public void SetStandAlone()
  100. {
  101. standAlone = true;
  102. }
  103. public bool AnyChangesToInputXml()
  104. {
  105. return editorControlsUserControl.AnyChangesToInject();
  106. }
  107. public void LoadXmlFromString(string objectString)
  108. {
  109. ClearSession();
  110. doNotDeleteFile = false;
  111. debugString = objectString;
  112. if (!String.IsNullOrEmpty(objectString))
  113. {
  114. SetInputFileOptions();
  115. InitiateWebBrowser();
  116. }
  117. else
  118. {
  119. SetNoInputFileOptions();
  120. }
  121. }
  122. public void LoadXmlFromString(string objectString, bool replaceable)
  123. {
  124. LoadXmlFromString(objectString);
  125. if (debugMode)
  126. {
  127. HandleInjectAction(replaceable);
  128. }
  129. }
  130. public string GetModifiedXml()
  131. {
  132. string modifiedXml = "";
  133. try
  134. {
  135. if (!String.IsNullOrEmpty(originalXmlFile))
  136. {
  137. modifiedXml = File.ReadAllText(originalXmlFile);
  138. }
  139. }
  140. catch (Exception e)
  141. {
  142. Util.ShowMessage(string.Format("Can't get modified Xml. Please make sure that the input file is correct.\r\nError: {0}", e.Message));
  143. }
  144. return modifiedXml;
  145. }
  146. private void InitiateWebBrowser()
  147. {
  148. if ((!String.IsNullOrEmpty(debugString)) && !doNotDeleteFile)
  149. {
  150. originalXmlFile = string.Format(@"{0}{1}.xml", Path.GetTempPath(), Guid.NewGuid());
  151. previousXmlFile = originalXmlFile;
  152. inputFileComboBox.Text = originalXmlFile;
  153. WriteFile(originalXmlFile, debugString);
  154. webBrowser.Url = new Uri(originalXmlFile);
  155. }
  156. mainFormLoaded = true;
  157. }
  158. private void HandleInjectAction(bool replaceable)
  159. {
  160. if (replaceable)
  161. {
  162. injectCheckBox.Visible = true;
  163. ReadOnlyLabel.Visible = false;
  164. }
  165. else
  166. {
  167. injectCheckBox.Visible = false;
  168. ReadOnlyLabel.Visible = true;
  169. }
  170. }
  171. private void InitializeTreeViewEvents()
  172. {
  173. treeViewUserControl.StatusTextEvent += treeViewUserControl_StatusTextEvent;
  174. }
  175. private void InitializeEditorEvents()
  176. {
  177. editorControlsUserControl.StatusTextEvent += editorControlsUserControl_StatusTextEvent;
  178. editorControlsUserControl.CloseEvent += editorControlsUserControl_CloseEvent;
  179. editorControlsUserControl.SaveEvent += editorControlsUserControl_SaveEvent;
  180. editorControlsUserControl.SaveAsEvent += editorControlsUserControl_SaveAsEvent;
  181. editorControlsUserControl.UnformatXmlEvent += editorControlsUserControl_UnformatXmlEvent;
  182. editorControlsUserControl.FormatXmlEvent += editorControlsUserControl_FormatXmlEvent;
  183. editorControlsUserControl.CaretChangeEvent += editorControlsUserControl_CaretChangeEvent;
  184. }
  185. private void SetToolStripText(string text)
  186. {
  187. toolStripStatusLabel.Text = text;
  188. if (StatusTextEvent != null)
  189. {
  190. StatusTextEvent(text);
  191. }
  192. }
  193. private void editorControlsUserControl_CaretChangeEvent(int line, int column)
  194. {
  195. SetToolStripText(string.Format("Ln {0}, Col {1}", line, column));
  196. }
  197. private void treeViewUserControl_StatusTextEvent(string statusText)
  198. {
  199. SetToolStripText(statusText);
  200. }
  201. private void editorControlsUserControl_StatusTextEvent(string statusText)
  202. {
  203. SetToolStripText(statusText);
  204. }
  205. private void editorControlsUserControl_CloseEvent()
  206. {
  207. DisableEditorControl(false);
  208. }
  209. private void SaveBeforeUnformating(string messageBoxText, string unformattedText)
  210. {
  211. bool saved = false;
  212. DialogResult dr = MessageBox.Show(messageBoxText, Util.GetTitle(), MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
  213. if (dr.ToString() == "Yes")
  214. {
  215. if (newFile)
  216. {
  217. saved = editorControlsUserControl.HandleSaveAs(false);
  218. }
  219. else
  220. {
  221. saved = editorControlsUserControl.HandleSave(false);
  222. }
  223. }
  224. else if (dr.ToString() == "No")
  225. {
  226. editorControlsUserControl.ChangesInEditor = false;
  227. editorControlsUserControl.SetText(unformattedText);
  228. }
  229. else
  230. {
  231. editorControlsUserControl.SetDoNotHandleFormat(true);
  232. editorControlsUserControl.SetFormatXmlCheckBox(true);
  233. editorControlsUserControl.SetDoNotHandleFormat(false);
  234. }
  235. if (saved)
  236. {
  237. editorControlsUserControl.ChangesInEditor = false;
  238. editorControlsUserControl.useSaveAsOnSave = false;
  239. }
  240. }
  241. private void editorControlsUserControl_UnformatXmlEvent()
  242. {
  243. string unformattedText;
  244. switch (ActiveState)
  245. {
  246. case States.XsdFile:
  247. if (newFile)
  248. {
  249. unformattedText = GetNewXsd();
  250. }
  251. else
  252. {
  253. unformattedText = File.ReadAllText(appliedXsdFile);
  254. }
  255. break;
  256. case States.XsltFile:
  257. if (newFile)
  258. {
  259. unformattedText = GetNewXslt();
  260. }
  261. else
  262. {
  263. unformattedText = File.ReadAllText(appliedXsltFile);
  264. }
  265. break;
  266. default:
  267. if (newFile)
  268. {
  269. unformattedText = GetNewXml();
  270. }
  271. else
  272. {
  273. unformattedText = File.ReadAllText(originalXmlFile);
  274. }
  275. break;
  276. }
  277. if (editorControlsUserControl.ChangesInEditor)
  278. {
  279. SaveBeforeUnformating("Changes need to be saved before unformating.\r\nSave changes?", unformattedText);
  280. }
  281. else
  282. {
  283. editorControlsUserControl.SetText(unformattedText);
  284. }
  285. }
  286. private void editorControlsUserControl_FormatXmlEvent(string textToSave)
  287. {
  288. CommitFormatting();
  289. }
  290. private void CommitFormatting()
  291. {
  292. XmlDocument xml = new XmlDocument();
  293. xml.LoadXml(editorControlsUserControl.GetText());
  294. XmlDeclaration dec = null;
  295. if (xml.FirstChild.NodeType == XmlNodeType.XmlDeclaration)
  296. {
  297. dec = (XmlDeclaration)xml.FirstChild;
  298. }
  299. XmlWriterSettings writerSettings = new XmlWriterSettings();
  300. writerSettings.OmitXmlDeclaration = true;
  301. writerSettings.IndentChars = "\t";
  302. writerSettings.Indent = true;
  303. StringWriter stringWriter = new StringWriter();
  304. XmlWriter xmlWriter = XmlWriter.Create(stringWriter, writerSettings);
  305. if (dec != null)
  306. {
  307. stringWriter.WriteLine(xml.CreateXmlDeclaration(dec.Version, dec.Encoding, dec.Standalone).OuterXml);
  308. }
  309. xmlWriter.Flush();
  310. stringWriter.Flush();
  311. xml.Save(xmlWriter);
  312. editorControlsUserControl.SetText(stringWriter.ToString());
  313. xmlWriter.Close();
  314. stringWriter.Dispose();
  315. }
  316. private void editorControlsUserControl_SaveEvent(bool applyAfterSave)
  317. {
  318. switch (ActiveState)
  319. {
  320. case States.XsltFile:
  321. SaveEditorContent(xsltFileComboBox.Text, applyAfterSave);
  322. break;
  323. case States.XsdFile:
  324. SaveEditorContent(xsdFileComboBox.Text, applyAfterSave);
  325. break;
  326. case States.InputFile:
  327. SaveEditorContent(originalXmlFile, applyAfterSave);
  328. break;
  329. }
  330. }
  331. private void editorControlsUserControl_SaveAsEvent(string fileName, bool applyAfterSave)
  332. {
  333. switch (ActiveState)
  334. {
  335. case States.XsltFile:
  336. xsltFileComboBox.Text = fileName;
  337. SaveEditorContent(fileName, applyAfterSave);
  338. appliedXsltFile = xsltFileComboBox.Text;
  339. break;
  340. case States.XsdFile:
  341. xsdFileComboBox.Text = fileName;
  342. SaveEditorContent(fileName, applyAfterSave);
  343. appliedXsdFile = xsdFileComboBox.Text;
  344. break;
  345. case States.InputFile:
  346. inputFileComboBox.Text = fileName;
  347. if (originalXmlFile != inputFileComboBox.Text)
  348. {
  349. if (!String.IsNullOrEmpty(originalXmlFile))
  350. {
  351. DeleteFile(originalXmlFile);
  352. }
  353. originalXmlFile = inputFileComboBox.Text;
  354. doNotDeleteFile = true;
  355. if (newFile)
  356. {
  357. if (debugString == "")
  358. {
  359. debugString = editorControlsUserControl.GetText();
  360. }
  361. }
  362. }
  363. SaveEditorContent(fileName, applyAfterSave);
  364. break;
  365. }
  366. }
  367. private void SaveEditorContent(string fileName, bool applyAfterSave)
  368. {
  369. switch (ActiveState)
  370. {
  371. case States.XsltFile:
  372. WriteFile(fileName, editorControlsUserControl.GetText());
  373. if (applyAfterSave)
  374. {
  375. ApplyXsltFile();
  376. if (!errorInXslt && !errorInXml)
  377. {
  378. DisableEditorControl(true);
  379. }
  380. errorInXml = false;
  381. }
  382. break;
  383. case States.XsdFile:
  384. WriteFile(fileName, editorControlsUserControl.GetText());
  385. if (applyAfterSave)
  386. {
  387. ApplyXsdFile();
  388. DisableEditorControl(true);
  389. }
  390. break;
  391. case States.InputFile:
  392. WriteFile(fileName, editorControlsUserControl.GetText());
  393. if (applyAfterSave)
  394. {
  395. Util.SaveComboBoxItemToRegistry(inputFileComboBox.Text, "XmlFile", numberOfXmlFilesToSave);
  396. FillInputFileComboBox();
  397. SetInputFileOptions();
  398. DisableEditorControl(true);
  399. Reload(inputFileComboBox.Text);
  400. }
  401. break;
  402. }
  403. }
  404. private static void WriteFile(string fileName, string content)
  405. {
  406. try
  407. {
  408. File.WriteAllText(fileName, content);
  409. }
  410. catch (Exception e)
  411. {
  412. Util.ShowMessage(Util.GetDetailedErrorMessage(e));
  413. }
  414. }
  415. private enum States
  416. {
  417. InputFile,
  418. XsltFile,
  419. XsdFile,
  420. XPath
  421. };
  422. private void SetXPathDefaultType()
  423. {
  424. string xPathQueryType = Util.ReadFromRegistry("XPathQueryType");
  425. if (xPathQueryType == "")
  426. {
  427. xPathTypeComboBox.SelectedIndex = xPathTypeComboBox.FindString("InnerXml");
  428. }
  429. else
  430. {
  431. xPathTypeComboBox.SelectedIndex = xPathTypeComboBox.FindString(xPathQueryType);
  432. }
  433. if (xPathTypeComboBox.SelectedItem.ToString() == "Value")
  434. {
  435. treeViewCheckBox.Enabled = false;
  436. }
  437. }
  438. private void CloseButton_Click(object sender, EventArgs e)
  439. {
  440. if (editorControlsUserControl.ChangesInEditor)
  441. {
  442. DialogResult dr = MessageBox.Show("Discard changes?", Util.GetTitle(), MessageBoxButtons.YesNo, MessageBoxIcon.Information);
  443. if (dr.ToString() == "No")
  444. {
  445. return;
  446. }
  447. }
  448. if (injectCheckBox.Checked)
  449. {
  450. inject = true;
  451. }
  452. if (CloseEvent != null)
  453. {
  454. CloseEvent();
  455. }
  456. if (!debugMode && !standAlone)
  457. {
  458. ClearSession();
  459. }
  460. }
  461. private void ClearSession()
  462. {
  463. Cleanup();
  464. inputFileComboBox.Text = "";
  465. SetActive(States.InputFile);
  466. SetNoInputFileOptions();
  467. originalXmlFile = "";
  468. previousXmlFile = "";
  469. webBrowser.Url = new Uri("about:blank");
  470. }
  471. private void AboutButton_Click(object sender, EventArgs e)
  472. {
  473. using (AboutForm af = new AboutForm())
  474. {
  475. string version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
  476. af.ProgramVersion = version.Substring(0, version.Length - 4);
  477. af.Title = Util.GetTitle();
  478. af.ShowDialog();
  479. }
  480. }
  481. private void ReloadWebBrowser(string url)
  482. {
  483. if (!string.IsNullOrEmpty(url))
  484. {
  485. Uri uri = new Uri(url);
  486. webBrowser.Url = uri;
  487. }
  488. }
  489. private void ApplyXsltFile()
  490. {
  491. string previousFile = inputFileComboBox.Text;
  492. DeleteAutoGeneratedFiles();
  493. try
  494. {
  495. string outputFile = string.Format(@"{0}{1}.html", Path.GetTempPath(), Guid.NewGuid());
  496. CommitXslt(outputFile);
  497. inputFileComboBox.Text = outputFile;
  498. previousFileFromXslt = outputFile;
  499. errorInXslt = false;
  500. appliedXsltFile = xsltFileComboBox.Text;
  501. Reload(inputFileComboBox.Text);
  502. }
  503. catch (System.Xml.Xsl.XsltException e)
  504. {
  505. Util.ShowMessage(Util.GetDetailedErrorMessage(e));
  506. inputFileComboBox.Text = previousFile;
  507. HandleXsltError();
  508. editorControlsUserControl.SetEditorStatusTextBox(Util.GetDetailedErrorMessage(e));
  509. editorControlsUserControl.GotoLine(e.LineNumber, e.LinePosition);
  510. }
  511. catch (XmlException e)
  512. {
  513. Util.ShowMessage(Util.GetDetailedErrorMessage(e));
  514. inputFileComboBox.Text = originalXmlFile;
  515. errorInXml = true;
  516. errorInXslt = false;
  517. SetActive(States.InputFile);
  518. HandleEditor();
  519. editorControlsUserControl.SetEditorStatusTextBox(Util.GetDetailedErrorMessage(e));
  520. editorControlsUserControl.GotoLine(e.LineNumber, e.LinePosition);
  521. }
  522. }
  523. private void HandleXsltError()
  524. {
  525. if (!errorInXslt)
  526. {
  527. StateBeforeXsltError = ActiveState;
  528. }
  529. if (newFile)
  530. {
  531. StateBeforeXsltError = StateBeforeNewFile;
  532. }
  533. if (ActiveState != States.XsltFile)
  534. {
  535. SetActive(States.XsltFile);
  536. }
  537. errorInXslt = true;
  538. if (!editorEnabled)
  539. {
  540. HandleEditor();
  541. }
  542. }
  543. private void DeleteAutoGeneratedFiles()
  544. {
  545. if (!string.IsNullOrEmpty(previousFileFromXPath))
  546. {
  547. DeleteFile(previousFileFromXPath);
  548. previousFileFromXPath = "";
  549. }
  550. if (!string.IsNullOrEmpty(previousFileFromXslt))
  551. {
  552. DeleteFile(previousFileFromXslt);
  553. previousFileFromXslt = "";
  554. }
  555. }
  556. private void ApplyXPathQuery()
  557. {
  558. string previousFile = inputFileComboBox.Text;
  559. DeleteAutoGeneratedFiles();
  560. try
  561. {
  562. string fileExtension = "xml";
  563. if (xPathTypeComboBox.SelectedItem.ToString() == "Value")
  564. {
  565. fileExtension = "html";
  566. treeViewCheckBox.Checked = false;
  567. treeViewCheckBox.Enabled = false;
  568. }
  569. else
  570. {
  571. treeViewCheckBox.Enabled = true;
  572. }
  573. inputFileComboBox.Text = string.Format(@"{0}{1}.{2}", Path.GetTempPath(), Guid.NewGuid(), fileExtension);
  574. previousFileFromXPath = inputFileComboBox.Text;
  575. CommitXPath();
  576. Reload(inputFileComboBox.Text);
  577. }
  578. catch (Exception e)
  579. {
  580. inputFileComboBox.Text = previousFile;
  581. Util.ShowMessage(Util.GetDetailedErrorMessage(e));
  582. }
  583. }
  584. private void Reload(string url)
  585. {
  586. if (treeviewEnabled)
  587. {
  588. if (!treeViewUserControl.ReloadTreeView(url))
  589. {
  590. DisableTreeView(url);
  591. }
  592. }
  593. else
  594. {
  595. ReloadWebBrowser(url);
  596. }
  597. }
  598. private void CommitXslt(string outputFile)
  599. {
  600. XPathDocument xPathDoc = new XPathDocument(originalXmlFile);
  601. System.Xml.Xsl.XslCompiledTransform xslTrans = new System.Xml.Xsl.XslCompiledTransform();
  602. xslTrans.Load(xsltFileComboBox.Text);
  603. XmlTextWriter writer = new XmlTextWriter(outputFile, System.Text.Encoding.Default);
  604. xslTrans.Transform(xPathDoc, null, writer);
  605. writer.Close();
  606. if (previousXsltFile != xsltFileComboBox.Text)
  607. {
  608. previousXsltFile = xsltFileComboBox.Text;
  609. Util.SaveComboBoxItemToRegistry(xsltFileComboBox.Text, "XsltFile", numberOfXsltFilesToSave);
  610. FillXsltFileComboBox();
  611. }
  612. if (ActiveState != States.XsltFile)
  613. {
  614. SetActive(States.XsltFile);
  615. }
  616. }
  617. private void CommitXPath()
  618. {
  619. XPathDocument doc = new XPathDocument(originalXmlFile);
  620. XPathNavigator nav = doc.CreateNavigator();
  621. string result = "";
  622. if (nav.Evaluate(xPathComboBox.Text).GetType().ToString() == "MS.Internal.Xml.XPath.XPathSelectionIterator")
  623. {
  624. XPathNodeIterator iterator = (XPathNodeIterator)nav.Evaluate(xPathComboBox.Text);
  625. while (iterator.MoveNext())
  626. {
  627. string resultType = "";
  628. switch (xPathTypeComboBox.SelectedItem.ToString())
  629. {
  630. case "InnerXml":
  631. resultType = iterator.Current.InnerXml;
  632. break;
  633. case "OuterXml":
  634. resultType = iterator.Current.OuterXml;
  635. break;
  636. case "Value":
  637. resultType = iterator.Current.Value;
  638. break;
  639. }
  640. result += string.Format("{0}\r\n", resultType);
  641. }
  642. }
  643. else
  644. {
  645. result = string.Format("{0}\r\n", nav.Evaluate(xPathComboBox.Text));
  646. }
  647. if (xPathTypeComboBox.SelectedItem.ToString() == "Value")
  648. {
  649. result = string.Format("<pre>\r\n{0}</pre>", result);
  650. }
  651. else
  652. {
  653. result = string.Format("<xml>\r\n{0}</xml>", result);
  654. }
  655. WriteFile(inputFileComboBox.Text, result);
  656. if (previousXPathQuery != xPathComboBox.Text)
  657. {
  658. previousXPathQuery = xPathComboBox.Text;
  659. Util.SaveComboBoxItemToRegistry(xPathComboBox.Text, "XPath", numberOfXPathQueriesToSave);
  660. FillXPathComboBox();
  661. }
  662. if (ActiveState != States.XPath)
  663. {
  664. SetActive(States.XPath);
  665. }
  666. }
  667. private void SetActive(States state)
  668. {
  669. switch (state)
  670. {
  671. case States.InputFile:
  672. ActiveState = States.InputFile;
  673. inputFileComboBox.Enabled = true;
  674. applyXmlButton.Text = "Apply";
  675. inputFilePictureBox.Visible = true;
  676. xsdFilePictureBox.Visible = false;
  677. xsltFilePictureBox.Visible = false;
  678. xPathPictureBox.Visible = false;
  679. editButton.Enabled = true;
  680. treeViewCheckBox.Enabled = true;
  681. break;
  682. case States.XsltFile:
  683. ActiveState = States.XsltFile;
  684. inputFileComboBox.Enabled = false;
  685. applyXmlButton.Text = "Revert Xml";
  686. inputFilePictureBox.Visible = false;
  687. xsdFilePictureBox.Visible = false;
  688. xsltFilePictureBox.Visible = true;
  689. xPathPictureBox.Visible = false;
  690. editButton.Enabled = true;
  691. treeViewCheckBox.Enabled = true;
  692. break;
  693. case States.XsdFile:
  694. ActiveState = States.XsdFile;
  695. inputFileComboBox.Enabled = true;
  696. applyXmlButton.Text = "Apply";
  697. xsdFilePictureBox.Visible = true;
  698. xsltFilePictureBox.Visible = false;
  699. inputFilePictureBox.Visible = false;
  700. xPathPictureBox.Visible = false;
  701. editButton.Enabled = true;
  702. treeViewCheckBox.Enabled = true;
  703. break;
  704. case States.XPath:
  705. ActiveState = States.XPath;
  706. inputFileComboBox.Enabled = false;
  707. applyXmlButton.Text = "Revert Xml";
  708. inputFilePictureBox.Visible = false;
  709. xsdFilePictureBox.Visible = false;
  710. xsltFilePictureBox.Visible = false;
  711. xPathPictureBox.Visible = true;
  712. editButton.Enabled = false;
  713. if (xPathTypeComboBox.SelectedItem.ToString() == "Value")
  714. {
  715. treeViewCheckBox.Enabled = false;
  716. }
  717. else
  718. {
  719. treeViewCheckBox.Enabled = true;
  720. }
  721. break;
  722. }
  723. }
  724. private void FillXsltFileComboBox()
  725. {
  726. xsltFileComboBox.Items.Clear();
  727. for (int i = 1; i <= numberOfXsltFilesToSave; i++)
  728. {
  729. string item = Util.ReadFromRegistry("XsltFile" + i);
  730. if (item != "")
  731. {
  732. xsltFileComboBox.Items.Add(item);
  733. }
  734. }
  735. xsltFileComboBox.Text = Util.ReadFromRegistry("XsltFile1");
  736. previousXsltFile = xsltFileComboBox.Text;
  737. appliedXsltFile = xsltFileComboBox.Text;
  738. }
  739. private void FillXsdFileComboBox()
  740. {
  741. xsdFileComboBox.Items.Clear();
  742. for (int i = 1; i <= numberOfXsdFilesToSave; i++)
  743. {
  744. string item = Util.ReadFromRegistry("XsdFile" + i);
  745. if (item != "")
  746. {
  747. xsdFileComboBox.Items.Add(item);
  748. }
  749. }
  750. xsdFileComboBox.Text = Util.ReadFromRegistry("XsdFile1");
  751. appliedXsdFile = xsdFileComboBox.Text;
  752. }
  753. private void FillInputFileComboBox()
  754. {
  755. inputFileComboBox.Items.Clear();
  756. for (int i = 1; i <= numberOfXmlFilesToSave; i++)
  757. {
  758. string item = Util.ReadFromRegistry("XmlFile" + i);
  759. if (item != "")
  760. {
  761. inputFileComboBox.Items.Add(item);
  762. }
  763. }
  764. }
  765. private void FillXPathComboBox()
  766. {
  767. xPathComboBox.Items.Clear();
  768. for (int i = 1; i <= numberOfXPathQueriesToSave; i++)
  769. {
  770. string item = Util.ReadFromRegistry("XPath" + i);
  771. if (item != "")
  772. {
  773. xPathComboBox.Items.Add(item);
  774. }
  775. }
  776. xPathComboBox.Text = Util.ReadFromRegistry("XPath1");
  777. previousXPathQuery = xPathComboBox.Text;
  778. }
  779. private void ToClipboardButton_Click(object sender, EventArgs e)
  780. {
  781. string sr;
  782. if (editorEnabled)
  783. {
  784. sr = editorControlsUserControl.GetText();
  785. }
  786. else
  787. {
  788. sr = File.ReadAllText(GetActiveDocumentUrl());
  789. if (xPathTypeComboBox.SelectedItem.ToString() == "Value" && ActiveState == States.XPath)
  790. {
  791. sr = sr.Substring(7, sr.Length - 15);
  792. }
  793. }
  794. StringBuilder sb = new StringBuilder(sr);
  795. for (int i = 0; i < sb.Length; i++)
  796. {
  797. if (sb[i].ToString() == "\n")
  798. {
  799. if (sb[i - 1].ToString() != "\r")
  800. {
  801. sb[i - 1] = Convert.ToChar("\r");
  802. }
  803. }
  804. }
  805. Clipboard.SetDataObject(sb.ToString(), true);
  806. }
  807. private void SetNoInputFileOptions()
  808. {
  809. openInBrowserButton.Enabled = false;
  810. toClipboardButton.Enabled = false;
  811. editButton.Enabled = false;
  812. applyXsdButton.Enabled = false;
  813. applyXpathButton.Enabled = false;
  814. applyXsltButton.Enabled = false;
  815. treeViewCheckBox.Enabled = false;
  816. xPathTypeComboBox.Enabled = false;
  817. xsltFileComboBox.Enabled = false;
  818. xsdFileComboBox.Enabled = false;
  819. xPathComboBox.Enabled = false;
  820. selectXsdFileButton.Enabled = false;
  821. selectXsltFileButton.Enabled = false;
  822. newXsltFileButton.Enabled = false;
  823. newXsdFileButton.Enabled = false;
  824. }
  825. private void SetInputFileOptions()
  826. {
  827. openInBrowserButton.Enabled = true;
  828. toClipboardButton.Enabled = true;
  829. editButton.Enabled = true;
  830. applyXsdButton.Enabled = true;
  831. applyXpathButton.Enabled = true;
  832. applyXsltButton.Enabled = true;
  833. treeViewCheckBox.Enabled = true;
  834. xPathTypeComboBox.Enabled = true;
  835. xsltFileComboBox.Enabled = true;
  836. xsdFileComboBox.Enabled = true;
  837. xPathComboBox.Enabled = true;
  838. selectXsdFileButton.Enabled = true;
  839. selectXsltFileButton.Enabled = true;
  840. newXsltFileButton.Enabled = true;
  841. newXsdFileButton.Enabled = true;
  842. }
  843. private void OpenInBrowserButton_Click(object sender, EventArgs e)
  844. {
  845. System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(Util.GetDefaultWebBrowser());
  846. psi.Arguments = GetActiveDocumentUrl();
  847. System.Diagnostics.Process.Start(psi);
  848. }
  849. private void EditButton_Click(object sender, EventArgs e)
  850. {
  851. HandleEditor();
  852. }
  853. private void HandleEditor()
  854. {
  855. string fileToEdit = "";
  856. switch (ActiveState)
  857. {
  858. case States.XsltFile:
  859. if (errorInXslt)
  860. {
  861. fileToEdit = xsltFileComboBox.Text;
  862. }
  863. else
  864. {
  865. fileToEdit = appliedXsltFile;
  866. }
  867. break;
  868. case States.XsdFile:
  869. fileToEdit = appliedXsdFile;
  870. break;
  871. case States.InputFile:
  872. fileToEdit = originalXmlFile;
  873. break;
  874. }
  875. editorControlsUserControl.SetText(File.ReadAllText(fileToEdit));
  876. EnableEditorControl();
  877. if (errorInXslt)
  878. {
  879. editorControlsUserControl.ValidateDocument("XSL");
  880. }
  881. }
  882. private void EnableEditorControl()
  883. {
  884. if (newFile)
  885. {
  886. editorControlsUserControl.useSaveAsOnSave = true;
  887. }
  888. else
  889. {
  890. editorControlsUserControl.useSaveAsOnSave = false;
  891. }
  892. toClipboardButton.Enabled = true;
  893. newXmlFileButton.Enabled = false;
  894. newXsdFileButton.Enabled = false;
  895. inputFileComboBox.Enabled = false;
  896. newXsltFileButton.Enabled = false;
  897. xsltFileComboBox.Enabled = false;
  898. xsdFileComboBox.Enabled = false;
  899. xPathComboBox.Enabled = false;
  900. xPathTypeComboBox.Enabled = false;
  901. applyXmlButton.Enabled = false;
  902. applyXpathButton.Enabled = false;
  903. applyXsltButton.Enabled = false;
  904. applyXsdButton.Enabled = false;
  905. openInBrowserButton.Enabled = false;
  906. editButton.Enabled = false;
  907. selectXmlFileButton.Enabled = false;
  908. selectXsdFileButton.Enabled = false;
  909. selectXsltFileButton.Enabled = false;
  910. treeViewCheckBox.Enabled = false;
  911. closeButton.Enabled = false;
  912. if (treeviewEnabled)
  913. {
  914. treeViewUserControl.Visible = false;
  915. }
  916. else
  917. {
  918. webBrowser.Visible = false;
  919. }
  920. editorControlsUserControl.originalXmlFile = originalXmlFile;
  921. editorControlsUserControl.appliedXsdFile = appliedXsdFile;
  922. editorControlsUserControl.activeState = ActiveState.ToString();
  923. editorControlsUserControl.ReadFormatXmlState();
  924. editorControlsUserControl.Visible = true;
  925. editorEnabled = true;
  926. editorControlsUserControl.EnableEditor();
  927. }
  928. private void DisableEditorControl(bool applyAfterSave)
  929. {
  930. States StateAfterClose = ActiveState;
  931. if (newFile)
  932. {
  933. if (!applyAfterSave)
  934. {
  935. StateAfterClose = StateBeforeNewFile;
  936. }
  937. newFile = false;
  938. }
  939. if (errorInXslt)
  940. {
  941. if (StateBeforeXsltError == States.XsltFile)
  942. {
  943. inputFileComboBox.Text = originalXmlFile;
  944. Reload(inputFileComboBox.Text);
  945. StateAfterClose = States.InputFile;
  946. }
  947. else
  948. {
  949. StateAfterClose = StateBeforeXsltError;
  950. }
  951. errorInXslt = false;
  952. }
  953. if (debugString != "")
  954. {
  955. SetActive(StateAfterClose);
  956. }
  957. else
  958. {
  959. inputFileComboBox.Enabled = true;
  960. }
  961. xsltFileComboBox.Text = appliedXsltFile;
  962. xsdFileComboBox.Text = appliedXsdFile;
  963. switch (StateAfterClose)
  964. {
  965. case States.XsltFile:
  966. inputFileComboBox.Text = previousFileFromXslt;
  967. break;
  968. case States.XPath:
  969. inputFileComboBox.Text = previousFileFromXPath;
  970. break;
  971. case States.InputFile:
  972. case States.XsdFile:
  973. inputFileComboBox.Text = originalXmlFile;
  974. break;
  975. }
  976. newXmlFileButton.Enabled = true;
  977. selectXmlFileButton.Enabled = true;
  978. if (debugString != "")
  979. {
  980. newXsltFileButton.Enabled = true;
  981. selectXsltFileButton.Enabled = true;
  982. xsltFileComboBox.Enabled = true;
  983. selectXsdFileButton.Enabled = true;
  984. xsdFileComboBox.Enabled = true;
  985. xPathComboBox.Enabled = true;
  986. xPathTypeComboBox.Enabled = true;
  987. applyXpathButton.Enabled = true;
  988. applyXsltButton.Enabled = true;
  989. openInBrowserButton.Enabled = true;
  990. xsdFileComboBox.Enabled = true;
  991. applyXsdButton.Enabled = true;
  992. newXsdFileButton.Enabled = true;
  993. selectXsdFileButton.Enabled = true;
  994. }
  995. else
  996. {
  997. toClipboardButton.Enabled = false;
  998. }
  999. applyXmlButton.Enabled = true;
  1000. closeButton.Enabled = true;
  1001. if (treeviewEnabled)
  1002. {
  1003. treeViewUserControl.Visible = true;
  1004. }
  1005. else
  1006. {
  1007. webBrowser.Visible = true;
  1008. }
  1009. editorControlsUserControl.CloseStatusBox();
  1010. editorControlsUserControl.Visible = false;
  1011. editorEnabled = false;
  1012. }
  1013. private void XPathComboBox_KeyDown(object sender, KeyEventArgs e)
  1014. {
  1015. if (e.KeyCode == Keys.Enter)
  1016. {
  1017. ApplyXPathQuery();
  1018. }
  1019. }
  1020. private void XPathComboBox_SelectedIndexChanged(object sender, EventArgs e)
  1021. {
  1022. if (mainFormLoaded)
  1023. {
  1024. ApplyXPathQuery();
  1025. }
  1026. }
  1027. private void XPathTypeComboBox_SelectedIndexChanged(object sender, EventArgs e)
  1028. {
  1029. if (mainFormLoaded)
  1030. {
  1031. Util.SaveToRegistry("XPathQueryType", xPathTypeComboBox.SelectedItem.ToString());
  1032. ApplyXPathQuery();
  1033. }
  1034. }
  1035. private void XsltFileComboBox_KeyDown(object sender, KeyEventArgs e)
  1036. {
  1037. if (e.KeyCode == Keys.Enter)
  1038. {
  1039. CheckForValidXsltInput();
  1040. }
  1041. }
  1042. private void NewXsltFileButton_Click(object sender, EventArgs e)
  1043. {
  1044. editorControlsUserControl.SetText(GetNewXslt());
  1045. xsltFileComboBox.Text = "";
  1046. StateBeforeNewFile = ActiveState;
  1047. SetActive(States.XsltFile);
  1048. newFile = true;
  1049. EnableEditorControl();
  1050. }
  1051. private static string GetNewXsd()
  1052. {
  1053. return "<?xml version=\"1.0\"?>\r\n<xs:schema elementFormDefault=\"qualified\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">\r\n\t\r\n</xs:schema>\r\n";
  1054. }
  1055. private static string GetNewXslt()
  1056. {
  1057. return "<?xml version=\"1.0\"?>\r\n<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">\r\n\t\r\n</xsl:stylesheet>\r\n";
  1058. }
  1059. private static string GetNewXml()
  1060. {
  1061. return "<?xml version=\"1.0\"?>\r\n<root>\r\n\t\r\n</root>\r\n";
  1062. }
  1063. private void EnableTreeView(string url)
  1064. {
  1065. if (treeViewUserControl.ReloadTreeView(url))
  1066. {
  1067. webBrowser.Visible = false;
  1068. treeviewEnabled = true;
  1069. treeViewUserControl.Visible = true;
  1070. }
  1071. else
  1072. {
  1073. DisableTreeView(inputFileComboBox.Text);
  1074. }
  1075. }
  1076. private void DisableTreeView(string url)
  1077. {
  1078. ReloadWebBrowser(url);
  1079. webBrowser.Visible = true;
  1080. treeViewCheckBox.Checked = false;
  1081. treeviewEnabled = false;
  1082. treeViewUserControl.Visible = false;
  1083. }
  1084. private string GetActiveDocumentUrl()
  1085. {
  1086. string url;
  1087. switch (ActiveState)
  1088. {
  1089. case States.XsdFile:
  1090. url = appliedXsdFile;
  1091. break;
  1092. default:
  1093. url = originalXmlFile;
  1094. break;
  1095. }
  1096. return url;
  1097. }
  1098. private void TreeViewCheckBox_CheckedChanged(object sender, EventArgs e)
  1099. {
  1100. if (treeViewCheckBox.Checked)
  1101. {
  1102. EnableTreeView(GetActiveDocumentUrl());
  1103. }
  1104. else
  1105. {
  1106. DisableTreeView(GetActiveDocumentUrl());
  1107. }
  1108. }
  1109. private void XsltFileComboBox_SelectionChangeCommitted(object sender, EventArgs e)
  1110. {
  1111. xsltFileComboBox.Text = xsltFileComboBox.SelectedItem.ToString();
  1112. CheckForValidXsltInput();
  1113. }
  1114. private void openInBrowserButton_MouseEnter(object sender, EventArgs e)
  1115. {
  1116. SetToolStripText("Open in Internet Explorer");
  1117. }
  1118. private void openInBrowserButton_MouseLeave(object sender, EventArgs e)
  1119. {
  1120. SetToolStripText("");
  1121. }
  1122. private void aboutButton_MouseEnter(object sender, EventArgs e)
  1123. {
  1124. SetToolStripText(string.Format("About {0}", Util.GetTitle()));
  1125. }
  1126. private void aboutButton_MouseLeave(object sender, EventArgs e)
  1127. {
  1128. SetToolStripText("");
  1129. }
  1130. private void toClipboardButton_MouseEnter(object sender, EventArgs e)
  1131. {
  1132. SetToolStripText("Copy content to Clipboard");
  1133. }
  1134. private void toClipboardButton_MouseLeave(object sender, EventArgs e)
  1135. {
  1136. SetToolStripText("");
  1137. }
  1138. private void editButton_MouseEnter(object sender, EventArgs e)
  1139. {
  1140. switch (ActiveState)
  1141. {
  1142. case States.InputFile:
  1143. SetToolStripText("Edit Xml document");
  1144. break;
  1145. case States.XsltFile:
  1146. SetToolStripText("Edit XSLT document");
  1147. break;
  1148. }
  1149. }
  1150. private void editButton_MouseLeave(object sender, EventArgs e)
  1151. {
  1152. SetToolStripText("");
  1153. }
  1154. private void treeViewCheckBox_MouseEnter(object sender, EventArgs e)
  1155. {
  1156. switch (ActiveState)
  1157. {
  1158. case States.InputFile:
  1159. SetToolStripText("Show Xml in Tree view");
  1160. break;
  1161. case States.XPath:
  1162. SetToolStripText("Show XPath result in Tree view");
  1163. break;
  1164. }
  1165. }
  1166. private void treeViewCheckBox_MouseLeave(object sender, EventArgs e)
  1167. {
  1168. SetToolStripText("");
  1169. }
  1170. private void injectCheckBox_MouseEnter(object sender, EventArgs e)
  1171. {
  1172. SetToolStripText("Inject Xml back into running application on Close");
  1173. }
  1174. private void injectCheckBox_MouseLeave(object sender, EventArgs e)
  1175. {
  1176. SetToolStripText("");
  1177. }
  1178. private void closeButton_MouseEnter(object sender, EventArgs e)
  1179. {
  1180. SetToolStripText(string.Format("Close {0}", Util.GetTitle()));
  1181. }
  1182. private void closeButton_MouseLeave(object sender, EventArgs e)
  1183. {
  1184. SetToolStripText("");
  1185. }
  1186. private void xPathTypeComboBox_MouseEnter(object sender, EventArgs e)
  1187. {
  1188. SetToolStripText("Change representation of XPath result");
  1189. }
  1190. private void xPathTypeComboBox_MouseLeave(object sender, EventArgs e)
  1191. {
  1192. SetToolStripText("");
  1193. }
  1194. private void xPathComboBox_MouseEnter(object sender, EventArgs e)
  1195. {
  1196. SetToolStripText("Enter XPath query or function");
  1197. }
  1198. private void xPathComboBox_MouseLeave(object sender, EventArgs e)
  1199. {
  1200. SetToolStripText("");
  1201. }
  1202. private void selectXsltFileButton_MouseEnter(object sender, EventArgs e)
  1203. {
  1204. SetToolStripText("Open XSLT document");
  1205. }
  1206. private void selectXsltFileButton_MouseLeave(object sender, EventArgs e)
  1207. {
  1208. SetToolStripText("");
  1209. }
  1210. private void newXsltFileButton_MouseEnter(object sender, EventArgs e)
  1211. {
  1212. SetToolStripText("Create new XSLT document");
  1213. }
  1214. private void newXsltFileButton_MouseLeave(object sender, EventArgs e)
  1215. {
  1216. SetToolStripText("");
  1217. }
  1218. private void xsltFileComboBox_MouseEnter(object sender, EventArgs e)
  1219. {
  1220. SetToolStripText("Active XSLT document");
  1221. }
  1222. private void xsltFileComboBox_MouseLeave(object sender, EventArgs e)
  1223. {
  1224. SetToolStripText("");
  1225. }
  1226. private void inputFileComboBox_MouseEnter(object sender, EventArgs e)
  1227. {
  1228. SetToolStripText("Active Xml document");
  1229. }
  1230. private void inputFileComboBox_MouseLeave(object sender, EventArgs e)
  1231. {
  1232. SetToolStripText("");
  1233. }
  1234. private void applyXsltButton_MouseEnter(object sender, EventArgs e)
  1235. {
  1236. SetToolStripText("Apply XSL Transformation");
  1237. }
  1238. private void applyXsltButton_MouseLeave(object sender, EventArgs e)
  1239. {
  1240. SetToolStripText("");
  1241. }
  1242. private void applyXpathButton_MouseEnter(object sender, EventArgs e)
  1243. {
  1244. SetToolStripText("Execute XPath query or function");
  1245. }
  1246. private void applyXpathButton_MouseLeave(object sender, EventArgs e)
  1247. {
  1248. SetToolStripText("");
  1249. }
  1250. private void newXmlFileButton_Click(object sender, EventArgs e)
  1251. {
  1252. editorControlsUserControl.SetText(GetNewXml());
  1253. inputFileComboBox.Text = "";
  1254. StateBeforeNewFile = ActiveState;
  1255. SetActive(States.InputFile);
  1256. newFile = true;
  1257. EnableEditorControl();
  1258. }
  1259. private void inputFileComboBox_KeyDown(object sender, KeyEventArgs e)
  1260. {
  1261. if (e.KeyCode == Keys.Enter)
  1262. {
  1263. CheckForValidXmlInput();
  1264. }
  1265. }
  1266. private void ApplyXmlFile()
  1267. {
  1268. SetInputFileOptions();
  1269. originalXmlFile = inputFileComboBox.Text;
  1270. SetActive(States.InputFile);
  1271. if (previousXmlFile != inputFileComboBox.Text)
  1272. {
  1273. previousXmlFile = inputFileComboBox.Text;
  1274. Util.SaveComboBoxItemToRegistry(inputFileComboBox.Text, "XmlFile", numberOfXmlFilesToSave);
  1275. FillInputFileComboBox();
  1276. inputFileComboBox.Text = originalXmlFile;
  1277. }
  1278. Reload(inputFileComboBox.Text);
  1279. }
  1280. private static void DeleteFile(string file)
  1281. {
  1282. Util.DeleteFile(file);
  1283. }
  1284. private void inputFileComboBox_SelectionChangeCommitted(object sender, EventArgs e)
  1285. {
  1286. inputFileComboBox.Text = inputFileComboBox.SelectedItem.ToString();
  1287. CheckForValidXmlInput();
  1288. }
  1289. private void applyXmlButton_Click(object sender, EventArgs e)
  1290. {
  1291. CheckForValidXmlInput();
  1292. }
  1293. private void CheckForValidXmlInput()
  1294. {
  1295. string fileToDelete = inputFileComboBox.Text;
  1296. if (ActiveState != States.InputFile)
  1297. {
  1298. inputFileComboBox.Text = originalXmlFile;
  1299. }
  1300. inputFileComboBox.Text = inputFileComboBox.Text.Trim();
  1301. if (inputFileComboBox.Text == "")
  1302. {
  1303. Util.ShowMessage("No Xml file specified.");
  1304. inputFileComboBox.Text = originalXmlFile;
  1305. }

Large files files are truncated, but you can click here to view the full file