001: /*
002: * @(#)URISyntaxException.java 1.3 01/12/03
003: *
004: * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
005: * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
006: */
007:
008: package com.sun.portal.rewriter.util.uri;
009:
010: /**
011: * Checked exception thrown to indicate that a string could not be parsed as a
012: * URI reference.
013: *
014: * @author Mark Reinhold
015: * @version 1.3, 01/12/03
016: * @see URI
017: */
018:
019: public class URISyntaxException extends Exception {
020: private String input;
021: private int index;
022:
023: /**
024: * Constructs an instance from the given input string, reason, and error
025: * index.
026: *
027: * @param input The input string
028: * @param reason A string explaining why the input could not be parsed
029: * @param index The index at which the parse error occurred,
030: * or <tt>-1</tt> if the index is not known
031: *
032: * @throws NullPointerException
033: * If either the input or reason strings are <tt>null</tt>
034: *
035: * @throws IllegalArgumentException
036: * If the error index is less than <tt>-1</tt>
037: */
038: public URISyntaxException(final String input, final String reason,
039: final int index) {
040: super (reason);
041: if ((input == null) || (reason == null))
042: throw new NullPointerException();
043: if (index < -1)
044: throw new IllegalArgumentException();
045: this .input = input;
046: this .index = index;
047: }
048:
049: /**
050: * Constructs an instance from the given input string and reason. The
051: * resulting object will have an error index of <tt>-1</tt>.
052: *
053: * @param input The input string
054: * @param reason A string explaining why the input could not be parsed
055: *
056: * @throws NullPointerException
057: * If either the input or reason strings are <tt>null</tt>
058: */
059: public URISyntaxException(final String input, final String reason) {
060: this (input, reason, -1);
061: }
062:
063: /**
064: * Returns the input string.
065: *
066: * @return The input string
067: */
068: public String getInput() {
069: return input;
070: }
071:
072: /**
073: * Returns a string explaining why the input string could not be parsed.
074: *
075: * @return The reason string
076: */
077: public String getReason() {
078: return super .getMessage();
079: }
080:
081: /**
082: * Returns an index into the input string of the position at which the
083: * parse error occurred, or <tt>-1</tt> if this position is not known.
084: *
085: * @return The error index
086: */
087: public int getIndex() {
088: return index;
089: }
090:
091: /**
092: * Returns a string describing the parse error. The resulting string
093: * consists of the reason string followed by a colon character
094: * (<tt>':'</tt>), a space, and the input string. If the error index is
095: * defined then the string <tt>" at index "</tt> followed by the index, in
096: * decimal, is inserted after the reason string and before the colon
097: * character.
098: *
099: * @return A string describing the parse error
100: */
101: public String getMessage() {
102: final StringBuffer sb = new StringBuffer();
103: sb.append(getReason());
104: if (index > -1) {
105: sb.append(" at index ");
106: sb.append(index);
107: }
108: sb.append(": ");
109: sb.append(input);
110: return sb.toString();
111: }
112:
113: }
|