1. /*
  2. * @(#)URISyntaxException.java 1.4 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.net;
  8. /**
  9. * Checked exception thrown to indicate that a string could not be parsed as a
  10. * URI reference.
  11. *
  12. * @author Mark Reinhold
  13. * @version 1.4, 03/01/23
  14. * @see URI
  15. */
  16. public class URISyntaxException
  17. extends Exception
  18. {
  19. private String input;
  20. private int index;
  21. /**
  22. * Constructs an instance from the given input string, reason, and error
  23. * index.
  24. *
  25. * @param input The input string
  26. * @param reason A string explaining why the input could not be parsed
  27. * @param index The index at which the parse error occurred,
  28. * or <tt>-1</tt> if the index is not known
  29. *
  30. * @throws NullPointerException
  31. * If either the input or reason strings are <tt>null</tt>
  32. *
  33. * @throws IllegalArgumentException
  34. * If the error index is less than <tt>-1</tt>
  35. */
  36. public URISyntaxException(String input, String reason, int index) {
  37. super(reason);
  38. if ((input == null) || (reason == null))
  39. throw new NullPointerException();
  40. if (index < -1)
  41. throw new IllegalArgumentException();
  42. this.input = input;
  43. this.index = index;
  44. }
  45. /**
  46. * Constructs an instance from the given input string and reason. The
  47. * resulting object will have an error index of <tt>-1</tt>.
  48. *
  49. * @param input The input string
  50. * @param reason A string explaining why the input could not be parsed
  51. *
  52. * @throws NullPointerException
  53. * If either the input or reason strings are <tt>null</tt>
  54. */
  55. public URISyntaxException(String input, String reason) {
  56. this(input, reason, -1);
  57. }
  58. /**
  59. * Returns the input string.
  60. *
  61. * @return The input string
  62. */
  63. public String getInput() {
  64. return input;
  65. }
  66. /**
  67. * Returns a string explaining why the input string could not be parsed.
  68. *
  69. * @return The reason string
  70. */
  71. public String getReason() {
  72. return super.getMessage();
  73. }
  74. /**
  75. * Returns an index into the input string of the position at which the
  76. * parse error occurred, or <tt>-1</tt> if this position is not known.
  77. *
  78. * @return The error index
  79. */
  80. public int getIndex() {
  81. return index;
  82. }
  83. /**
  84. * Returns a string describing the parse error. The resulting string
  85. * consists of the reason string followed by a colon character
  86. * (<tt>':'</tt>), a space, and the input string. If the error index is
  87. * defined then the string <tt>" at index "</tt> followed by the index, in
  88. * decimal, is inserted after the reason string and before the colon
  89. * character.
  90. *
  91. * @return A string describing the parse error
  92. */
  93. public String getMessage() {
  94. StringBuffer sb = new StringBuffer();
  95. sb.append(getReason());
  96. if (index > -1) {
  97. sb.append(" at index ");
  98. sb.append(index);
  99. }
  100. sb.append(": ");
  101. sb.append(input);
  102. return sb.toString();
  103. }
  104. }