PageRenderTime 51ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/main/Source/Application/Designers/XmlEditorDesigner/XmlEditor.cs

#
C# | 206 lines | 170 code | 31 blank | 5 comment | 10 complexity | 6009ad62339e7d284fef64acb12d5556 MD5 | raw file
  1. ///////////////////////////////////////////////
  2. // Copyright (C) 2013 ISWIX, LLC
  3. // Web: http://www.iswix.com
  4. // All Rights Reserved
  5. ///////////////////////////////////////////////
  6. using System;
  7. using System.Collections.Generic;
  8. using System.ComponentModel;
  9. using System.Drawing;
  10. using System.Data;
  11. using System.IO;
  12. using System.Linq;
  13. using System.Reflection;
  14. using System.Xml.Linq;
  15. using System.Xml.Schema;
  16. using System.Text;
  17. using System.Windows.Forms;
  18. using FireworksFramework.Interfaces;
  19. using FireworksFramework.Types;
  20. using ICSharpCode.XmlEditor;
  21. using ICSharpCode.Core;
  22. using IsWiXAutomationInterface;
  23. namespace XmlEditorDesigner
  24. {
  25. public partial class XmlEditor : UserControl, IFireworksDesigner
  26. {
  27. IDocumentManager _mgr;
  28. XmlEditorControl editor;
  29. bool _validXML;
  30. public XmlEditor()
  31. {
  32. string applicationDirectory = new FileInfo(Application.ExecutablePath).DirectoryName;
  33. PropertyService.InitializeService(applicationDirectory, applicationDirectory, "Fireworks");
  34. InitializeComponent();
  35. editor = new XmlEditorControl();
  36. editor.Dock = DockStyle.Fill;
  37. this.splitContainer1.Panel1.Controls.Add(editor);
  38. }
  39. public void LoadData()
  40. {
  41. editor.Text = _mgr.Document.ToString();
  42. editor.TextEditorProperties.EnableFolding = true;
  43. editor.Document.FoldingManager.FoldingStrategy = new XmlFoldingStrategy();
  44. editor.Document.FoldingManager.UpdateFoldings("", null);
  45. this.editor.TextChanged += new System.EventHandler(this.editor_TextChanged);
  46. editor.Validating += new CancelEventHandler(editor_Validating);
  47. this.editor.SetHighlighting("XML");
  48. ValidateXML();
  49. XmlSchemaCompletionDataCollection schemas = new XmlSchemaCompletionDataCollection();
  50. string schemasDir;
  51. try
  52. {
  53. if (_mgr.Document.GetWiXNameSpace() == "http://schemas.microsoft.com/wix/2006/wi")
  54. {
  55. schemasDir = Path.Combine(PropertyService.DataDirectory, "Schemas");
  56. }
  57. else
  58. {
  59. schemasDir = Path.Combine(PropertyService.DataDirectory, @"Schemas\v4");
  60. }
  61. foreach (var file in new DirectoryInfo(schemasDir).GetFiles("*.xsd"))
  62. {
  63. schemas.Add(new XmlSchemaCompletionData(file.FullName));
  64. }
  65. }
  66. catch (Exception)
  67. {
  68. }
  69. editor.SchemaCompletionDataItems = schemas;
  70. }
  71. private void ValidateXML()
  72. {
  73. _validXML = false;
  74. string xmlValidationMessage = string.Empty;
  75. XDocument doc = new XDocument();
  76. try
  77. {
  78. doc = XDocument.Parse(editor.Text);
  79. if (_mgr.Schemas.Contains(_mgr.DefaultNamespace.ToString()))
  80. {
  81. xmlValidationMessage = "Valid XML ( Validated against available schemas )";
  82. doc.Validate(_mgr.Schemas, (o, ex) => { _validXML = false; xmlValidationMessage = ex.Exception.Message; });
  83. }
  84. _validXML = true;
  85. }
  86. catch (Exception ex)
  87. {
  88. xmlValidationMessage = ex.Message;
  89. }
  90. finally
  91. {
  92. }
  93. if (_validXML)
  94. {
  95. textBoxStatus.Text = xmlValidationMessage;
  96. _mgr.DocumentText = editor.Text;
  97. _mgr.CanSave = true;
  98. }
  99. else
  100. {
  101. textBoxStatus.Text = xmlValidationMessage;
  102. _mgr.CanSave = false;
  103. }
  104. }
  105. #region IFireworksDesigner Members
  106. private void editor_TextChanged(object sender, EventArgs e)
  107. {
  108. editor.Document.FoldingManager.UpdateFoldings("", null);
  109. ValidateXML();
  110. }
  111. public IDesignerManager DesignerManager
  112. {
  113. set { _mgr = value.DocumentManager; }
  114. }
  115. public string PluginName
  116. {
  117. get { return "Xml Editor"; }
  118. }
  119. public Image PluginImage
  120. {
  121. get
  122. { return Image.FromStream(Assembly.GetExecutingAssembly().GetManifestResourceStream("XmlEditorDesigner.XmlEditor.ico")); }
  123. }
  124. public PluginType PluginType
  125. {
  126. get { return PluginType.Designer; }
  127. }
  128. public string PluginOrder
  129. {
  130. get { return "XmlEditor"; }
  131. }
  132. public string PluginInformation
  133. {
  134. get { return new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream("XmlEditorDesigner.MS-PL.txt")).ReadToEnd(); }
  135. }
  136. public bool IsValidContext()
  137. {
  138. return true;
  139. }
  140. #endregion
  141. private void XmlEditor_Leave(object sender, EventArgs e)
  142. {
  143. editor.TextChanged -= new System.EventHandler(this.editor_TextChanged);
  144. editor.Validating -= new CancelEventHandler(editor_Validating);
  145. }
  146. void editor_Validating(object sender, CancelEventArgs e)
  147. {
  148. if (!_validXML)
  149. {
  150. if (MessageBox.Show("Invalid XML has been detected. Are you sure you want to discard it?", Application.ProductName, MessageBoxButtons.OKCancel) == DialogResult.Cancel)
  151. {
  152. e.Cancel = true;
  153. }
  154. else
  155. {
  156. editor.Text = _mgr.Document.ToString();
  157. }
  158. }
  159. }
  160. private void XmlEditor_Validating(object sender, CancelEventArgs e)
  161. {
  162. if (!_validXML)
  163. {
  164. if (MessageBox.Show("Invalid XML has been detected. Are you sure you want to discard it?", Application.ProductName, MessageBoxButtons.OKCancel) == DialogResult.Cancel)
  165. {
  166. e.Cancel = true;
  167. }
  168. else
  169. {
  170. editor.Text = _mgr.Document.ToString();
  171. }
  172. }
  173. }
  174. }
  175. }