1 /*
   2  * Copyright (c) 2000, 2013, Oracle and/or its affiliates. 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * 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 static final long serialVersionUID = 2137979680897488891L;
  42 
  43     private String input;
  44     private int index;
  45 
  46     /**
  47      * Constructs an instance from the given input string, reason, and error
  48      * index.
  49      *
  50      * @param  input   The input string
  51      * @param  reason  A string explaining why the input could not be parsed
  52      * @param  index   The index at which the parse error occurred,
  53      *                 or {@code -1} if the index is not known
  54      *
  55      * @throws  NullPointerException
  56      *          If either the input or reason strings are {@code null}
  57      *
  58      * @throws  IllegalArgumentException
  59      *          If the error index is less than {@code -1}
  60      */
  61     public URISyntaxException(String input, String reason, int index) {
  62         super(reason);
  63         if ((input == null) || (reason == null))
  64             throw new NullPointerException();
  65         if (index < -1)
  66             throw new IllegalArgumentException();
  67         this.input = input;
  68         this.index = index;
  69     }
  70 
  71     /**
  72      * Constructs an instance from the given input string and reason.  The
  73      * resulting object will have an error index of {@code -1}.
  74      *
  75      * @param  input   The input string
  76      * @param  reason  A string explaining why the input could not be parsed
  77      *
  78      * @throws  NullPointerException
  79      *          If either the input or reason strings are {@code null}
  80      */
  81     public URISyntaxException(String input, String reason) {
  82         this(input, reason, -1);
  83     }
  84 
  85     /**
  86      * Returns the input string.
  87      *
  88      * @return  The input string
  89      */
  90     public String getInput() {
  91         return input;
  92     }
  93 
  94     /**
  95      * Returns a string explaining why the input string could not be parsed.
  96      *
  97      * @return  The reason string
  98      */
  99     public String getReason() {
 100         return super.getMessage();
 101     }
 102 
 103     /**
 104      * Returns an index into the input string of the position at which the
 105      * parse error occurred, or {@code -1} if this position is not known.
 106      *
 107      * @return  The error index
 108      */
 109     public int getIndex() {
 110         return index;
 111     }
 112 
 113     /**
 114      * Returns a string describing the parse error.  The resulting string
 115      * consists of the reason string followed by a colon character
 116      * ({@code ':'}), a space, and the input string.  If the error index is
 117      * defined then the string {@code " at index "} followed by the index, in
 118      * decimal, is inserted after the reason string and before the colon
 119      * character.
 120      *
 121      * @return  A string describing the parse error
 122      */
 123     public String getMessage() {
 124         StringBuilder sb = new StringBuilder();
 125         sb.append(getReason());
 126         if (index > -1) {
 127             sb.append(" at index ");
 128             sb.append(index);
 129         }
 130         sb.append(": ");
 131         sb.append(input);
 132         return sb.toString();
 133     }
 134 
 135 }