PageRenderTime 27ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/MSBuild/Microsoft/Build/CommandLine/ProjectSchemaValidationHandler.cs

#
C# | 119 lines | 110 code | 8 blank | 1 comment | 13 complexity | a757717b242a75e8f5b588ae4ae64e4c MD5 | raw file
Possible License(s): Apache-2.0, LGPL-3.0
  1. namespace Microsoft.Build.CommandLine
  2. {
  3. using Microsoft.Build.Shared;
  4. using System;
  5. using System.IO;
  6. using System.Xml;
  7. using System.Xml.Schema;
  8. internal sealed class ProjectSchemaValidationHandler
  9. {
  10. private bool syntaxError;
  11. private static string BuildStringFromResource(string projectFile, int fileLine, int fileEndLine, int fileColumn, int fileEndColumn, string resourceName, params object[] args)
  12. {
  13. string str;
  14. string str2;
  15. string message = Microsoft.Build.Shared.ResourceUtilities.FormatResourceString(out str, out str2, resourceName, args);
  16. return Microsoft.Build.Shared.EventArgsFormatting.FormatEventMessage("error", Microsoft.Build.Shared.AssemblyResources.GetString("SubCategoryForSchemaValidationErrors"), message, str, projectFile, fileLine, fileEndLine, fileColumn, fileEndColumn, 0);
  17. }
  18. private void OnSchemaValidationError(object sender, ValidationEventArgs args)
  19. {
  20. this.syntaxError = true;
  21. string projectFile = string.Empty;
  22. if (args.Exception.SourceUri.Length != 0)
  23. {
  24. projectFile = new Uri(args.Exception.SourceUri).LocalPath;
  25. }
  26. Console.WriteLine(BuildStringFromResource(projectFile, args.Exception.LineNumber, 0, args.Exception.LinePosition, 0, "SchemaValidationError", new object[] { args.Exception.Message }));
  27. }
  28. private static void ThrowInitializationExceptionWithResource(string projectFile, int fileLine, int fileEndLine, int fileColumn, int fileEndColumn, string resourceName, params object[] args)
  29. {
  30. InitializationException.Throw(BuildStringFromResource(projectFile, fileLine, fileEndLine, fileColumn, fileEndColumn, resourceName, args), null);
  31. }
  32. private void VerifyProjectSchema(string projectFile, string schemaFile)
  33. {
  34. Microsoft.Build.Shared.ErrorUtilities.VerifyThrowArgumentNull(schemaFile, "schemaFile");
  35. Microsoft.Build.Shared.ErrorUtilities.VerifyThrowArgumentNull(projectFile, "projectFile");
  36. XmlReaderSettings settings = new XmlReaderSettings {
  37. ValidationType = ValidationType.Schema,
  38. XmlResolver = null
  39. };
  40. settings.ValidationEventHandler += new ValidationEventHandler(this.OnSchemaValidationError);
  41. XmlTextReader schemaDocument = new XmlTextReader(schemaFile);
  42. XmlTextReader reader4 = schemaDocument;
  43. try
  44. {
  45. settings.Schemas.Add("http://schemas.microsoft.com/developer/msbuild/2003", schemaDocument);
  46. projectFile = Path.GetFullPath(projectFile);
  47. using (StreamReader reader2 = new StreamReader(projectFile))
  48. {
  49. using (XmlReader reader3 = XmlReader.Create(reader2, settings, projectFile))
  50. {
  51. this.syntaxError = false;
  52. bool flag = true;
  53. while (flag)
  54. {
  55. try
  56. {
  57. flag = reader3.Read();
  58. continue;
  59. }
  60. catch (XmlException)
  61. {
  62. continue;
  63. }
  64. }
  65. VerifyThrowInitializationExceptionWithResource(!this.syntaxError, projectFile, 0, 0, 0, 0, "ProjectSchemaErrorHalt", new object[0]);
  66. }
  67. }
  68. }
  69. catch (XmlException exception)
  70. {
  71. ThrowInitializationExceptionWithResource((exception.SourceUri.Length == 0) ? string.Empty : new Uri(exception.SourceUri).LocalPath, exception.LineNumber, 0, exception.LinePosition, 0, "InvalidSchemaFile", new object[] { schemaFile, exception.Message });
  72. }
  73. catch (XmlSchemaException exception2)
  74. {
  75. ThrowInitializationExceptionWithResource((exception2.SourceUri.Length == 0) ? string.Empty : new Uri(exception2.SourceUri).LocalPath, exception2.LineNumber, 0, exception2.LinePosition, 0, "InvalidSchemaFile", new object[] { schemaFile, exception2.Message });
  76. }
  77. finally
  78. {
  79. if (reader4 != null)
  80. {
  81. //reader4.Dispose();
  82. }
  83. }
  84. }
  85. internal static void VerifyProjectSchema(string projectFile, string schemaFile, string binPath)
  86. {
  87. Microsoft.Build.Shared.ErrorUtilities.VerifyThrowArgumentNull(projectFile, "projectFile");
  88. Microsoft.Build.Shared.ErrorUtilities.VerifyThrowArgumentNull(binPath, "binPath");
  89. if ((schemaFile == null) || (schemaFile.Length == 0))
  90. {
  91. schemaFile = Path.Combine(binPath, "Microsoft.Build.xsd");
  92. }
  93. if (File.Exists(schemaFile))
  94. {
  95. Console.WriteLine(Microsoft.Build.Shared.AssemblyResources.GetString("SchemaFileLocation"), schemaFile);
  96. }
  97. else
  98. {
  99. InitializationException.Throw(Microsoft.Build.Shared.ResourceUtilities.FormatResourceString("SchemaNotFoundErrorWithFile", new object[] { schemaFile }), null);
  100. }
  101. new ProjectSchemaValidationHandler().VerifyProjectSchema(projectFile, schemaFile);
  102. }
  103. private static void VerifyThrowInitializationExceptionWithResource(bool condition, string projectFile, int fileLine, int fileEndLine, int fileColumn, int fileEndColumn, string resourceName, params object[] args)
  104. {
  105. if (!condition)
  106. {
  107. ThrowInitializationExceptionWithResource(projectFile, fileLine, fileEndLine, fileColumn, fileEndColumn, resourceName, args);
  108. }
  109. }
  110. }
  111. }