J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2000-2006 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package java.net; |
| 27 | |
| 28 | |
| 29 | /** |
| 30 | * Checked exception thrown to indicate that a string could not be parsed as a |
| 31 | * URI reference. |
| 32 | * |
| 33 | * @author Mark Reinhold |
| 34 | * @see URI |
| 35 | * @since 1.4 |
| 36 | */ |
| 37 | |
| 38 | public class URISyntaxException |
| 39 | extends Exception |
| 40 | { |
| 41 | private String input; |
| 42 | private int index; |
| 43 | |
| 44 | /** |
| 45 | * Constructs an instance from the given input string, reason, and error |
| 46 | * index. |
| 47 | * |
| 48 | * @param input The input string |
| 49 | * @param reason A string explaining why the input could not be parsed |
| 50 | * @param index The index at which the parse error occurred, |
| 51 | * or <tt>-1</tt> if the index is not known |
| 52 | * |
| 53 | * @throws NullPointerException |
| 54 | * If either the input or reason strings are <tt>null</tt> |
| 55 | * |
| 56 | * @throws IllegalArgumentException |
| 57 | * If the error index is less than <tt>-1</tt> |
| 58 | */ |
| 59 | public URISyntaxException(String input, String reason, int index) { |
| 60 | super(reason); |
| 61 | if ((input == null) || (reason == null)) |
| 62 | throw new NullPointerException(); |
| 63 | if (index < -1) |
| 64 | throw new IllegalArgumentException(); |
| 65 | this.input = input; |
| 66 | this.index = index; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Constructs an instance from the given input string and reason. The |
| 71 | * resulting object will have an error index of <tt>-1</tt>. |
| 72 | * |
| 73 | * @param input The input string |
| 74 | * @param reason A string explaining why the input could not be parsed |
| 75 | * |
| 76 | * @throws NullPointerException |
| 77 | * If either the input or reason strings are <tt>null</tt> |
| 78 | */ |
| 79 | public URISyntaxException(String input, String reason) { |
| 80 | this(input, reason, -1); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Returns the input string. |
| 85 | * |
| 86 | * @return The input string |
| 87 | */ |
| 88 | public String getInput() { |
| 89 | return input; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Returns a string explaining why the input string could not be parsed. |
| 94 | * |
| 95 | * @return The reason string |
| 96 | */ |
| 97 | public String getReason() { |
| 98 | return super.getMessage(); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Returns an index into the input string of the position at which the |
| 103 | * parse error occurred, or <tt>-1</tt> if this position is not known. |
| 104 | * |
| 105 | * @return The error index |
| 106 | */ |
| 107 | public int getIndex() { |
| 108 | return index; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Returns a string describing the parse error. The resulting string |
| 113 | * consists of the reason string followed by a colon character |
| 114 | * (<tt>':'</tt>), a space, and the input string. If the error index is |
| 115 | * defined then the string <tt>" at index "</tt> followed by the index, in |
| 116 | * decimal, is inserted after the reason string and before the colon |
| 117 | * character. |
| 118 | * |
| 119 | * @return A string describing the parse error |
| 120 | */ |
| 121 | public String getMessage() { |
| 122 | StringBuffer sb = new StringBuffer(); |
| 123 | sb.append(getReason()); |
| 124 | if (index > -1) { |
| 125 | sb.append(" at index "); |
| 126 | sb.append(index); |
| 127 | } |
| 128 | sb.append(": "); |
| 129 | sb.append(input); |
| 130 | return sb.toString(); |
| 131 | } |
| 132 | |
| 133 | } |