/interpreter/tags/at2-build190607/src/edu/vub/util/PatternSyntaxException.java

http://ambienttalk.googlecode.com/ · Java · 132 lines · 46 code · 11 blank · 75 comment · 3 complexity · db587684200f990a9b53b6c345f7a552 MD5 · raw file

  1. /* PatternSyntaxException - Indicates illegal pattern for regular expression.
  2. Copyright (C) 2002 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package edu.vub.util;
  32. /**
  33. * Indicates illegal pattern for regular expression.
  34. * Includes state to inspect the pattern and what and where the expression
  35. * was not valid regular expression.
  36. */
  37. public class PatternSyntaxException extends IllegalArgumentException
  38. {
  39. private static final long serialVersionUID = -3864639126226059218L;
  40. /**
  41. * Human readable escription of the syntax error.
  42. */
  43. private final String desc;
  44. /**
  45. * The original pattern that contained the syntax error.
  46. */
  47. private final String pattern;
  48. /**
  49. * Index of the first character in the String that was probably invalid,
  50. * or -1 when unknown.
  51. */
  52. private final int index;
  53. /**
  54. * Creates a new PatternSyntaxException.
  55. *
  56. * @param description Human readable escription of the syntax error.
  57. * @param pattern The original pattern that contained the syntax error.
  58. * @param index Index of the first character in the String that was
  59. * probably invalid, or -1 when unknown.
  60. */
  61. public PatternSyntaxException(String description,
  62. String pattern,
  63. int index)
  64. {
  65. super(description);
  66. this.desc = description;
  67. this.pattern = pattern;
  68. this.index = index;
  69. }
  70. /**
  71. * Returns a human readable escription of the syntax error.
  72. */
  73. public String getDescription()
  74. {
  75. return desc;
  76. }
  77. /**
  78. * Returns the original pattern that contained the syntax error.
  79. */
  80. public String getPattern()
  81. {
  82. return pattern;
  83. }
  84. /**
  85. * Returns the index of the first character in the String that was probably
  86. * invalid, or -1 when unknown.
  87. */
  88. public int getIndex()
  89. {
  90. return index;
  91. }
  92. /**
  93. * Returns a string containing a line with the description, a line with
  94. * the original pattern and a line indicating with a ^ which character is
  95. * probably the first invalid character in the pattern if the index is not
  96. * negative.
  97. */
  98. public String getMessage()
  99. {
  100. String lineSep = System.getProperty("line.separator");
  101. StringBuffer sb = new StringBuffer(desc);
  102. sb.append(lineSep);
  103. sb.append('\t');
  104. sb.append(pattern);
  105. if (index != -1)
  106. {
  107. sb.append(lineSep);
  108. sb.append('\t');
  109. for (int i=0; i<index; i++)
  110. sb.append(' ');
  111. sb.append('^');
  112. }
  113. return sb.toString();
  114. }
  115. }