Browse Source

Fixed SD2-1051 - Unhandled xml exception when trying to create an xml schema from an xml document that is not well formed.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@1776 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Matt Ward 20 years ago
parent
commit
30258f361f
  1. 43
      src/AddIns/DisplayBindings/XmlEditor/Project/Src/CreateSchemaCommand.cs
  2. 57
      src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlView.cs

43
src/AddIns/DisplayBindings/XmlEditor/Project/Src/CreateSchemaCommand.cs

@ -10,7 +10,6 @@ using ICSharpCode.SharpDevelop.DefaultEditor.Gui.Editor;
using ICSharpCode.SharpDevelop.Gui; using ICSharpCode.SharpDevelop.Gui;
using ICSharpCode.SharpDevelop; using ICSharpCode.SharpDevelop;
using System; using System;
using System.Data;
using System.IO; using System.IO;
using System.Text; using System.Text;
using System.Windows.Forms; using System.Windows.Forms;
@ -32,46 +31,14 @@ namespace ICSharpCode.XmlEditor
// Find active XmlView. // Find active XmlView.
XmlView xmlView = XmlView.ActiveXmlView; XmlView xmlView = XmlView.ActiveXmlView;
if (xmlView != null) { if (xmlView != null) {
// Create a schema based on the xml. // Create a schema based on the xml.
SharpDevelopTextEditorProperties properties = xmlView.TextEditorControl.TextEditorProperties as SharpDevelopTextEditorProperties; string schema = xmlView.CreateSchema();
string schema = CreateSchema(xmlView.TextEditorControl.Document.TextContent, xmlView.TextEditorControl.Encoding, properties.ConvertTabsToSpaces, properties.TabIndent); if (schema != null) {
// Create a new file and display the generated schema.
// Create a new file and display the generated schema. string fileName = GenerateSchemaFileName(xmlView.TextEditorControl.FileName);
string fileName = GenerateSchemaFileName(xmlView.TitleName); OpenNewXmlFile(fileName, schema);
OpenNewXmlFile(fileName, schema);
}
}
/// <summary>
/// Creates a schema based on the xml content.
/// </summary>
/// <returns>A generated schema.</returns>
string CreateSchema(string xml, Encoding encoding, bool convertTabsToSpaces, int tabIndent)
{
string schema = String.Empty;
using (DataSet dataSet = new DataSet()) {
dataSet.ReadXml(new StringReader(xml), XmlReadMode.InferSchema);
EncodedStringWriter writer = new EncodedStringWriter(encoding);
XmlTextWriter xmlWriter = new XmlTextWriter(writer);
xmlWriter.Formatting = Formatting.Indented;
if (convertTabsToSpaces) {
xmlWriter.Indentation = tabIndent;
xmlWriter.IndentChar = ' ';
} else {
xmlWriter.Indentation = 1;
xmlWriter.IndentChar = '\t';
} }
dataSet.WriteXmlSchema(xmlWriter);
schema = writer.ToString();
writer.Close();
xmlWriter.Close();
} }
return schema;
} }
/// <summary> /// <summary>

57
src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlView.cs

@ -8,6 +8,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Data;
using System.Drawing; using System.Drawing;
using System.Drawing.Printing; using System.Drawing.Printing;
using System.IO; using System.IO;
@ -371,6 +372,32 @@ namespace ICSharpCode.XmlEditor
} }
} }
/// <summary>
/// Creates a schema based on the xml content.
/// </summary>
/// <returns>A generated schema or null if the xml content is not
/// well formed.</returns>
public string CreateSchema()
{
TaskService.ClearExceptCommentTasks();
if (IsWellFormed) {
string schema;
using (DataSet dataSet = new DataSet()) {
dataSet.ReadXml(new StringReader(Text), XmlReadMode.InferSchema);
EncodedStringWriter writer = new EncodedStringWriter(xmlEditor.TextEditorProperties.Encoding);
XmlTextWriter xmlWriter = CreateXmlTextWriter(writer);
dataSet.WriteXmlSchema(xmlWriter);
schema = writer.ToString();
writer.Close();
xmlWriter.Close();
}
return schema;
} else {
ShowErrorList();
}
return null;
}
/// <summary> /// <summary>
/// Finds the definition of the xml element or attribute under the cursor /// Finds the definition of the xml element or attribute under the cursor
/// in the corresponding schema and then displays that schema and the definition /// in the corresponding schema and then displays that schema and the definition
@ -946,15 +973,7 @@ namespace ICSharpCode.XmlEditor
reader.WhitespaceHandling = WhitespaceHandling.None; reader.WhitespaceHandling = WhitespaceHandling.None;
StringWriter indentedXmlWriter = new StringWriter(); StringWriter indentedXmlWriter = new StringWriter();
XmlTextWriter writer = new XmlTextWriter(indentedXmlWriter); XmlTextWriter writer = CreateXmlTextWriter(indentedXmlWriter);
if (xmlEditor.TextEditorProperties.ConvertTabsToSpaces) {
writer.Indentation = xmlEditor.TextEditorProperties.TabIndent;
writer.IndentChar = ' ';
} else {
writer.Indentation = 1;
writer.IndentChar = '\t';
}
writer.Formatting = Formatting.Indented;
writer.WriteNode(reader, false); writer.WriteNode(reader, false);
writer.Flush(); writer.Flush();
@ -966,6 +985,20 @@ namespace ICSharpCode.XmlEditor
return indentedText; return indentedText;
} }
XmlTextWriter CreateXmlTextWriter(TextWriter textWriter)
{
XmlTextWriter writer = new XmlTextWriter(textWriter);
if (xmlEditor.TextEditorProperties.ConvertTabsToSpaces) {
writer.Indentation = xmlEditor.TextEditorProperties.TabIndent;
writer.IndentChar = ' ';
} else {
writer.Indentation = 1;
writer.IndentChar = '\t';
}
writer.Formatting = Formatting.Indented;
return writer;
}
/// <summary> /// <summary>
/// Checks that the xml in this view is well-formed. /// Checks that the xml in this view is well-formed.
/// </summary> /// </summary>
@ -976,11 +1009,7 @@ namespace ICSharpCode.XmlEditor
Document.LoadXml(Text); Document.LoadXml(Text);
return true; return true;
} catch(XmlException ex) { } catch(XmlException ex) {
string fileName = FileName; AddTask(xmlEditor.FileName, ex.Message, ex.LinePosition - 1, ex.LineNumber - 1, TaskType.Error);
if (fileName == null || fileName.Length == 0) {
fileName = TitleName;
}
AddTask(fileName, ex.Message, ex.LinePosition - 1, ex.LineNumber - 1, TaskType.Error);
} }
return false; return false;
} }

Loading…
Cancel
Save