Browse Source

Validating .xsd files now uses the XmlSchema class which returns better error information.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@1202 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Matt Ward 20 years ago
parent
commit
f97a1dae96
  1. 158
      src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlView.cs
  2. 22
      src/AddIns/DisplayBindings/XmlEditor/Project/XmlEditor.csproj

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

@ -46,7 +46,7 @@ namespace ICSharpCode.XmlEditor
bool wasChangedExternally; bool wasChangedExternally;
static MessageViewCategory category; static MessageViewCategory category;
string stylesheetFileName; string stylesheetFileName;
public XmlView() public XmlView()
{ {
xmlEditor.Dock = DockStyle.Fill; xmlEditor.Dock = DockStyle.Fill;
@ -120,42 +120,18 @@ namespace ICSharpCode.XmlEditor
OutputWindowWriteLine(StringParser.Parse("${res:MainWindow.XmlValidationMessages.ValidationStarted}")); OutputWindowWriteLine(StringParser.Parse("${res:MainWindow.XmlValidationMessages.ValidationStarted}"));
try { if (IsSchema) {
StringReader stringReader = new StringReader(xmlEditor.Document.TextContent); if (!ValidateSchema()) {
XmlTextReader xmlReader = new XmlTextReader(stringReader); return;
xmlReader.XmlResolver = null; }
XmlReaderSettings settings = new XmlReaderSettings(); } else {
settings.ValidationType = ValidationType.Schema; if (!ValidateAgainstSchema()) {
settings.ValidationFlags = XmlSchemaValidationFlags.None;
settings.XmlResolver = null;
XmlSchemaCompletionData schemaData = null;
try {
for (int i = 0; i < XmlSchemaManager.SchemaCompletionDataItems.Count; ++i) {
schemaData = XmlSchemaManager.SchemaCompletionDataItems[i];
settings.Schemas.Add(schemaData.Schema);
}
} catch (XmlSchemaException ex) {
DisplayValidationError(schemaData.FileName, ex.Message, ex.LinePosition - 1, ex.LineNumber - 1);
ShowErrorList();
return; return;
} }
XmlReader reader = XmlReader.Create(xmlReader, settings);
XmlDocument doc = new XmlDocument();
doc.Load(reader);
OutputWindowWriteLine(String.Empty);
OutputWindowWriteLine(StringParser.Parse("${res:MainWindow.XmlValidationMessages.ValidationSuccess}"));
} catch (XmlSchemaException ex) {
DisplayValidationError(xmlEditor.FileName, ex.Message, ex.LinePosition - 1, ex.LineNumber - 1);
} catch (XmlException ex) {
DisplayValidationError(xmlEditor.FileName, ex.Message, ex.LinePosition - 1, ex.LineNumber - 1);
} }
ShowErrorList(); OutputWindowWriteLine(String.Empty);
OutputWindowWriteLine(StringParser.Parse("${res:MainWindow.XmlValidationMessages.ValidationSuccess}"));
} }
/// <summary> /// <summary>
@ -675,9 +651,13 @@ namespace ICSharpCode.XmlEditor
void DisplayValidationError(string fileName, string message, int column, int line) void DisplayValidationError(string fileName, string message, int column, int line)
{ {
OutputWindowWriteLine(message); OutputWindowWriteLine(message);
AddTask(fileName, message, column, line, TaskType.Error);
}
void ShowValidationFailedMessage()
{
OutputWindowWriteLine(String.Empty); OutputWindowWriteLine(String.Empty);
OutputWindowWriteLine(StringParser.Parse("${res:MainWindow.XmlValidationMessages.ValidationFailed}")); OutputWindowWriteLine(StringParser.Parse("${res:MainWindow.XmlValidationMessages.ValidationFailed}"));
AddTask(fileName, message, column, line, TaskType.Error);
} }
/// <summary> /// <summary>
@ -853,12 +833,9 @@ namespace ICSharpCode.XmlEditor
} catch(XsltCompileException ex) { } catch(XsltCompileException ex) {
string message = String.Empty; string message = String.Empty;
if(ex.InnerException != null) if(ex.InnerException != null) {
{
message = ex.InnerException.Message; message = ex.InnerException.Message;
} } else {
else
{
message = ex.ToString(); message = ex.ToString();
} }
@ -871,5 +848,106 @@ namespace ICSharpCode.XmlEditor
return false; return false;
} }
/// <summary>
/// Validates the XML in the editor against all the schemas in the
/// schema manager.
/// </summary>
bool ValidateAgainstSchema()
{
try {
StringReader stringReader = new StringReader(xmlEditor.Document.TextContent);
XmlTextReader xmlReader = new XmlTextReader(stringReader);
xmlReader.XmlResolver = null;
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.ValidationFlags = XmlSchemaValidationFlags.None;
settings.XmlResolver = null;
XmlSchemaCompletionData schemaData = null;
try {
for (int i = 0; i < XmlSchemaManager.SchemaCompletionDataItems.Count; ++i) {
schemaData = XmlSchemaManager.SchemaCompletionDataItems[i];
settings.Schemas.Add(schemaData.Schema);
}
} catch (XmlSchemaException ex) {
DisplayValidationError(schemaData.FileName, ex.Message, ex.LinePosition - 1, ex.LineNumber - 1);
ShowValidationFailedMessage();
ShowErrorList();
return false;
}
XmlReader reader = XmlReader.Create(xmlReader, settings);
XmlDocument doc = new XmlDocument();
doc.Load(reader);
return true;
} catch (XmlSchemaException ex) {
DisplayValidationError(xmlEditor.FileName, ex.Message, ex.LinePosition - 1, ex.LineNumber - 1);
} catch (XmlException ex) {
DisplayValidationError(xmlEditor.FileName, ex.Message, ex.LinePosition - 1, ex.LineNumber - 1);
}
ShowValidationFailedMessage();
ShowErrorList();
return false;
}
/// <summary>
/// Assumes the content in the editor is a schema and validates it using
/// the XmlSchema class. This is used instead of validating against the
/// XMLSchema.xsd file since it gives us better error information.
/// </summary>
bool ValidateSchema()
{
StringReader stringReader = new StringReader(xmlEditor.Document.TextContent);
XmlTextReader xmlReader = new XmlTextReader(stringReader);
xmlReader.XmlResolver = null;
try {
XmlSchema schema = XmlSchema.Read(xmlReader, new ValidationEventHandler(SchemaValidation));
schema.Compile(new ValidationEventHandler(SchemaValidation));
} catch (XmlSchemaException ex) {
DisplayValidationError(xmlEditor.FileName, ex.Message, ex.LinePosition - 1, ex.LineNumber - 1);
} catch (XmlException ex) {
DisplayValidationError(xmlEditor.FileName, ex.Message, ex.LinePosition - 1, ex.LineNumber - 1);
} finally {
xmlReader.Close();
}
if (TaskService.SomethingWentWrong) {
ShowValidationFailedMessage();
ShowErrorList();
return false;
}
return true;
}
void SchemaValidation(object source, ValidationEventArgs e)
{
if (e.Severity == XmlSeverityType.Error) {
DisplayValidationError(xmlEditor.FileName, e.Message, e.Exception.LinePosition - 1, e.Exception.LineNumber - 1);
} else {
DisplayValidationWarning(xmlEditor.FileName, e.Message, e.Exception.LinePosition - 1, e.Exception.LineNumber - 1);
}
}
/// <summary>
/// Displays the validation warning.
/// </summary>
void DisplayValidationWarning(string fileName, string message, int column, int line)
{
OutputWindowWriteLine(message);
AddTask(fileName, message, column, line, TaskType.Warning);
}
bool IsSchema {
get {
string extension = Path.GetExtension(xmlEditor.FileName);
if (extension != null) {
return String.Compare(".xsd", extension, true) == 0;
}
return false;
}
}
} }
} }

22
src/AddIns/DisplayBindings/XmlEditor/Project/XmlEditor.csproj

@ -14,22 +14,36 @@
<ProductVersion>8.0.50215</ProductVersion> <ProductVersion>8.0.50215</ProductVersion>
<StartAction>Program</StartAction> <StartAction>Program</StartAction>
<StartProgram>..\..\..\..\..\bin\SharpDevelop.exe</StartProgram> <StartProgram>..\..\..\..\..\bin\SharpDevelop.exe</StartProgram>
<RegisterForComInterop>False</RegisterForComInterop>
<GenerateSerializationAssemblies>Auto</GenerateSerializationAssemblies>
<BaseAddress>4194304</BaseAddress>
<PlatformTarget>AnyCPU</PlatformTarget>
<FileAlignment>4096</FileAlignment>
<NoWarn>0618</NoWarn>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>True</DebugSymbols>
<Optimize>False</Optimize> <Optimize>False</Optimize>
<AllowUnsafeBlocks>False</AllowUnsafeBlocks> <AllowUnsafeBlocks>False</AllowUnsafeBlocks>
<CheckForOverflowUnderflow>True</CheckForOverflowUnderflow> <CheckForOverflowUnderflow>True</CheckForOverflowUnderflow>
<OutputPath>..\..\..\..\..\AddIns\AddIns\DisplayBindings\XmlEditor\</OutputPath> <OutputPath>..\..\..\..\..\AddIns\AddIns\DisplayBindings\XmlEditor\</OutputPath>
<TreatWarningsAsErrors>False</TreatWarningsAsErrors> <TreatWarningsAsErrors>false</TreatWarningsAsErrors>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugSymbols>False</DebugSymbols>
<Optimize>True</Optimize> <Optimize>True</Optimize>
<AllowUnsafeBlocks>False</AllowUnsafeBlocks> <AllowUnsafeBlocks>False</AllowUnsafeBlocks>
<CheckForOverflowUnderflow>False</CheckForOverflowUnderflow> <CheckForOverflowUnderflow>False</CheckForOverflowUnderflow>
<OutputPath>..\..\..\..\..\AddIns\AddIns\DisplayBindings\XmlEditor\</OutputPath> <OutputPath>..\..\..\..\..\AddIns\AddIns\DisplayBindings\XmlEditor\</OutputPath>
<TreatWarningsAsErrors>False</TreatWarningsAsErrors> <TreatWarningsAsErrors>false</TreatWarningsAsErrors>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<DebugType>Full</DebugType>
<DebugSymbols>true</DebugSymbols>
<DefineConstants>DEBUG;TRACE</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<DebugType>None</DebugType>
<DebugSymbols>false</DebugSymbols>
<DefineConstants>TRACE</DefineConstants>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="System" /> <Reference Include="System" />

Loading…
Cancel
Save