rt/emul/compact/src/main/java/java/util/regex/PatternSyntaxException.java
author Jaroslav Tulach <jtulach@netbeans.org>
Mon, 07 Oct 2013 16:17:21 +0200
changeset 1350 f14e9730d4e9
parent 1348 bca65655b36b
permissions -rw-r--r--
ReExp builds on top of mini emul Character
     1 /*
     2  * Copyright (c) 1999, 2008, 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.util.regex;
    27 
    28 /**
    29  * Unchecked exception thrown to indicate a syntax error in a
    30  * regular-expression pattern.
    31  *
    32  * @author  unascribed
    33  * @since 1.4
    34  * @spec JSR-51
    35  */
    36 
    37 public class PatternSyntaxException
    38     extends IllegalArgumentException
    39 {
    40     private static final long serialVersionUID = -3864639126226059218L;
    41 
    42     private final String desc;
    43     private final String pattern;
    44     private final int index;
    45 
    46     /**
    47      * Constructs a new instance of this class.
    48      *
    49      * @param  desc
    50      *         A description of the error
    51      *
    52      * @param  regex
    53      *         The erroneous pattern
    54      *
    55      * @param  index
    56      *         The approximate index in the pattern of the error,
    57      *         or <tt>-1</tt> if the index is not known
    58      */
    59     public PatternSyntaxException(String desc, String regex, int index) {
    60         this.desc = desc;
    61         this.pattern = regex;
    62         this.index = index;
    63     }
    64 
    65     /**
    66      * Retrieves the error index.
    67      *
    68      * @return  The approximate index in the pattern of the error,
    69      *         or <tt>-1</tt> if the index is not known
    70      */
    71     public int getIndex() {
    72         return index;
    73     }
    74 
    75     /**
    76      * Retrieves the description of the error.
    77      *
    78      * @return  The description of the error
    79      */
    80     public String getDescription() {
    81         return desc;
    82     }
    83 
    84     /**
    85      * Retrieves the erroneous regular-expression pattern.
    86      *
    87      * @return  The erroneous pattern
    88      */
    89     public String getPattern() {
    90         return pattern;
    91     }
    92 
    93     private static final String nl = System.lineSeparator();
    94 
    95     /**
    96      * Returns a multi-line string containing the description of the syntax
    97      * error and its index, the erroneous regular-expression pattern, and a
    98      * visual indication of the error index within the pattern.
    99      *
   100      * @return  The full detail message
   101      */
   102     public String getMessage() {
   103         StringBuffer sb = new StringBuffer();
   104         sb.append(desc);
   105         if (index >= 0) {
   106             sb.append(" near index ");
   107             sb.append(index);
   108         }
   109         sb.append(nl);
   110         sb.append(pattern);
   111         if (index >= 0) {
   112             sb.append(nl);
   113             for (int i = 0; i < index; i++) sb.append(' ');
   114             sb.append('^');
   115         }
   116         return sb.toString();
   117     }
   118 
   119 }