SourceError.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. package gnu.text;
  2. import gnu.kawa.io.InPort;
  3. import java.io.File;
  4. /** Represents an error message from processing a "source" file.
  5. */
  6. public class SourceError extends SourceLocator.Simple
  7. // FIXME: If JAVA6, should implement: javax.tools.Diagnostic<Path>
  8. {
  9. /** Used to chain to the "next" message. */
  10. public SourceError next;
  11. /** The seriousness of the error - one of 'i' (for informational),
  12. * 'w' (for warning), 'e' (for error), or 'f' (for fatal error). */
  13. public char severity;
  14. /** If non-null, an error code, as might be specified by a standard. */
  15. public String code;
  16. /** The actual error message.
  17. * This is post-localization and -formatting.
  18. * It can contain multiple lines, separated by '\n'.*/
  19. public String message;
  20. /** Provides optional stack trace.
  21. * Filled when --debug-error-prints-stack-trace or
  22. * --debug-warning-prints-stack-trace option is used.*/
  23. public Throwable fakeException;
  24. public SourceError(char severity, String filename, int line, int column,
  25. String message) {
  26. this.severity = severity;
  27. this.filename = filename;
  28. this.position = SourceMapper.simpleEncode(line, column);
  29. this.message = message;
  30. }
  31. public SourceError(char severity, SourceLocator location, String message) {
  32. this.severity = severity;
  33. this.filename = location.getFileName();
  34. this.position = SourceMapper.simpleEncode(location);
  35. this.message = message;
  36. }
  37. /** Create a new SourceError using the current line/column from
  38. * a <code>InPort</code>. */
  39. public SourceError(InPort port, char severity, String message) {
  40. this(severity, port.getName(),
  41. adjustFromPort(port.getLineNumber()),
  42. adjustFromPort(port.getColumnNumber()),
  43. message);
  44. }
  45. private static int adjustFromPort(int portPosition) {
  46. return portPosition >= 0 ? portPosition + 1 : portPosition;
  47. }
  48. /** Convert the error to a String.
  49. * The String starts with filename, line and option column,
  50. * followed by the message. Warning messages are indicated as such. */
  51. public String toString() {
  52. return toString(false);
  53. }
  54. /** Convert the error to a String.
  55. * The String starts with filename, line and option column,
  56. * followed by the message. Warning messages are indicated as such. */
  57. public String toString(boolean stripDirectories) {
  58. StringBuilder buffer = new StringBuilder();
  59. appendTo(buffer, stripDirectories, null);
  60. return buffer.toString ();
  61. }
  62. public static void appendEscaped(Appendable out, CharSequence str)
  63. throws java.io.IOException {
  64. int len = str.length();
  65. for (int i = 0; i < len; i++) {
  66. char ch = str.charAt(i);
  67. if (ch == '<' || ch == '>' || ch == '&') {
  68. out.append(ch == '<' ? "&lt;"
  69. : ch == '>' ? "&gt;"
  70. : "&amp;");
  71. } else
  72. out.append(ch);
  73. }
  74. }
  75. public void appendTo(Appendable out, boolean stripDirectories,
  76. String newLine) {
  77. boolean isDomTerm = gnu.kawa.io.CheckConsole.forDomTerm(out);
  78. try {
  79. if (isDomTerm)
  80. out.append("\033]44;"+(isRepl() ? "repl: true" : "")+"\007");
  81. if (isDomTerm)
  82. out.append("\033[44;0u");
  83. String fname;
  84. if (filename == null)
  85. fname = "<unknown>";
  86. else {
  87. fname = filename;
  88. if (stripDirectories)
  89. fname = new File(fname).getName();
  90. }
  91. StringBuilder position = new StringBuilder();
  92. String endpos = "";
  93. int line = getStartLine();
  94. int column = getStartColumn();
  95. if (line > 0 || column > 0) {
  96. position.append(Integer.toString(line));
  97. if (column > 0) {
  98. position.append(':');
  99. position.append(Integer.toString(column));
  100. }
  101. int eline = getEndLine();
  102. int ecolumn = getEndColumn();
  103. if (eline > 0 && ecolumn > 0 && column > 0) {
  104. if (line == eline && ecolumn > column) {
  105. endpos = "-" + ecolumn;
  106. } else if (eline > line) {
  107. endpos = "-" + eline + ":" + ecolumn;
  108. }
  109. }
  110. }
  111. if (isDomTerm) {
  112. out.append("\033]72;");
  113. out.append("<a class='subtle' href='");
  114. out.append(new File(fname).toURI().toString());
  115. if (position.length() > 0) {
  116. out.append("#position=");
  117. out.append(position);
  118. out.append(endpos);
  119. }
  120. out.append("'>");
  121. appendEscaped(out, fname);
  122. }
  123. else
  124. out.append(fname);
  125. if (position.length() > 0) {
  126. out.append(':');
  127. out.append(position);
  128. }
  129. if (isDomTerm) {
  130. out.append("</a>\007");
  131. }
  132. out.append(": ");
  133. if (severity == 'w')
  134. out.append("warning - ");
  135. else if (severity == 'i')
  136. out.append("note - ");
  137. out.append(message);
  138. if (code != null) {
  139. out.append(" [");
  140. out.append(code);
  141. out.append("]");
  142. }
  143. if (fakeException != null) {
  144. StackTraceElement[] stackTrace = fakeException.getStackTrace();
  145. for (int i = 0; i < stackTrace.length; i++) {
  146. out.append(newLine != null ? newLine : "\n");
  147. out.append(" ");
  148. out.append(stackTrace[i].toString());
  149. }
  150. }
  151. if (isDomTerm)
  152. out.append("\033[44;0u");
  153. if (newLine != null)
  154. out.append(newLine);
  155. } catch (java.io.IOException ex) {
  156. throw new RuntimeException(ex);
  157. }
  158. }
  159. public void print(Appendable out) {
  160. appendTo(out, false, null);
  161. }
  162. public void println(Appendable out, boolean stripDirectories) {
  163. appendTo(out, stripDirectories,
  164. System.getProperty("line.separator", "\n"));
  165. }
  166. }