CompilationException.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. //
  2. // System.Web.Compilation.CompilationException
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // (C) 2002,2003 Ximian, Inc (http://www.ximian.com)
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Collections;
  31. using System.Collections.Specialized;
  32. using System.CodeDom.Compiler;
  33. using System.Runtime.Serialization;
  34. using System.Security.Permissions;
  35. using System.Text;
  36. using System.Web;
  37. namespace System.Web.Compilation
  38. {
  39. [Serializable]
  40. internal class CompilationException : HtmlizedException
  41. {
  42. string filename;
  43. CompilerErrorCollection errors;
  44. CompilerResults results;
  45. string fileText;
  46. string errmsg;
  47. int [] errorLines;
  48. CompilationException (SerializationInfo info, StreamingContext context)
  49. : base (info, context)
  50. {
  51. filename = info.GetString ("filename");
  52. errors = info.GetValue ("errors", typeof (CompilerErrorCollection)) as CompilerErrorCollection;
  53. results = info.GetValue ("results", typeof (CompilerResults)) as CompilerResults;
  54. fileText = info.GetString ("fileText");
  55. errmsg = info.GetString ("errmsg");
  56. errorLines = info.GetValue ("errorLines", typeof (int[])) as int[];
  57. }
  58. public CompilationException (string filename, CompilerErrorCollection errors, string fileText)
  59. {
  60. this.filename = filename;
  61. this.errors = errors;
  62. this.fileText = fileText;
  63. }
  64. public CompilationException (string filename, CompilerResults results, string fileText)
  65. : this (filename, results != null ? results.Errors : null, fileText)
  66. {
  67. this.results = results;
  68. }
  69. [SecurityPermission (SecurityAction.Demand, SerializationFormatter = true)]
  70. public override void GetObjectData (SerializationInfo info, StreamingContext ctx)
  71. {
  72. base.GetObjectData (info, ctx);
  73. info.AddValue ("filename", filename);
  74. info.AddValue ("errors", errors);
  75. info.AddValue ("results", results);
  76. info.AddValue ("fileText", fileText);
  77. info.AddValue ("errmsg", errmsg);
  78. info.AddValue ("errorLines", errorLines);
  79. }
  80. public override string Message {
  81. get { return ErrorMessage; }
  82. }
  83. public override string SourceFile {
  84. get {
  85. if (errors == null || errors.Count == 0)
  86. return filename;
  87. return errors [0].FileName;
  88. }
  89. }
  90. public override string FileName {
  91. get { return filename; }
  92. }
  93. public override string Title {
  94. get { return "Compilation Error"; }
  95. }
  96. public override string Description {
  97. get {
  98. return "Error compiling a resource required to service this request. " +
  99. "Review your source file and modify it to fix this error.";
  100. }
  101. }
  102. public override string ErrorMessage {
  103. get {
  104. if (errmsg == null && errors != null) {
  105. CompilerError firstError = null;
  106. foreach (CompilerError err in errors) {
  107. if (err.IsWarning)
  108. continue;
  109. firstError = err;
  110. break;
  111. };
  112. errmsg = firstError.ToString ();
  113. int idx = errmsg.IndexOf (" : error ");
  114. if (idx > -1)
  115. errmsg = errmsg.Substring (idx + 9);
  116. }
  117. return errmsg;
  118. }
  119. }
  120. public override string FileText {
  121. get { return fileText; }
  122. }
  123. public override int [] ErrorLines {
  124. get {
  125. if (errorLines == null && errors != null) {
  126. ArrayList list = new ArrayList ();
  127. foreach (CompilerError err in errors) {
  128. if (err.IsWarning)
  129. continue;
  130. if (err.Line != 0 && !list.Contains (err.Line))
  131. list.Add (err.Line);
  132. }
  133. errorLines = (int []) list.ToArray (typeof (int));
  134. Array.Sort (errorLines);
  135. }
  136. return errorLines;
  137. }
  138. }
  139. public override bool ErrorLinesPaired {
  140. get { return false; }
  141. }
  142. public StringCollection CompilerOutput {
  143. get {
  144. if (results == null)
  145. return null;
  146. return results.Output;
  147. }
  148. }
  149. public CompilerResults Results {
  150. get { return results; }
  151. }
  152. }
  153. }