KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > net > URISyntaxException


1 /*
2  * @(#)URISyntaxException.java 1.7 06/04/07
3  *
4  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.net;
9
10
11 /**
12  * Checked exception thrown to indicate that a string could not be parsed as a
13  * URI reference.
14  *
15  * @author Mark Reinhold
16  * @version 1.7, 06/04/07
17  * @see URI
18  * @since 1.4
19  */

20
21 public class URISyntaxException
22     extends Exception JavaDoc
23 {
24     private String JavaDoc input;
25     private int index;
26
27     /**
28      * Constructs an instance from the given input string, reason, and error
29      * index.
30      *
31      * @param input The input string
32      * @param reason A string explaining why the input could not be parsed
33      * @param index The index at which the parse error occurred,
34      * or <tt>-1</tt> if the index is not known
35      *
36      * @throws NullPointerException
37      * If either the input or reason strings are <tt>null</tt>
38      *
39      * @throws IllegalArgumentException
40      * If the error index is less than <tt>-1</tt>
41      */

42     public URISyntaxException(String JavaDoc input, String JavaDoc reason, int index) {
43     super(reason);
44     if ((input == null) || (reason == null))
45         throw new NullPointerException JavaDoc();
46     if (index < -1)
47         throw new IllegalArgumentException JavaDoc();
48     this.input = input;
49     this.index = index;
50     }
51
52     /**
53      * Constructs an instance from the given input string and reason. The
54      * resulting object will have an error index of <tt>-1</tt>.
55      *
56      * @param input The input string
57      * @param reason A string explaining why the input could not be parsed
58      *
59      * @throws NullPointerException
60      * If either the input or reason strings are <tt>null</tt>
61      */

62     public URISyntaxException(String JavaDoc input, String JavaDoc reason) {
63     this(input, reason, -1);
64     }
65
66     /**
67      * Returns the input string.
68      *
69      * @return The input string
70      */

71     public String JavaDoc getInput() {
72     return input;
73     }
74
75     /**
76      * Returns a string explaining why the input string could not be parsed.
77      *
78      * @return The reason string
79      */

80     public String JavaDoc getReason() {
81     return super.getMessage();
82     }
83
84     /**
85      * Returns an index into the input string of the position at which the
86      * parse error occurred, or <tt>-1</tt> if this position is not known.
87      *
88      * @return The error index
89      */

90     public int getIndex() {
91     return index;
92     }
93
94     /**
95      * Returns a string describing the parse error. The resulting string
96      * consists of the reason string followed by a colon character
97      * (<tt>':'</tt>), a space, and the input string. If the error index is
98      * defined then the string <tt>" at index "</tt> followed by the index, in
99      * decimal, is inserted after the reason string and before the colon
100      * character.
101      *
102      * @return A string describing the parse error
103      */

104     public String JavaDoc getMessage() {
105     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
106     sb.append(getReason());
107     if (index > -1) {
108         sb.append(" at index ");
109         sb.append(index);
110     }
111     sb.append(": ");
112     sb.append(input);
113     return sb.toString();
114     }
115
116 }
117
Popular Tags