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
jtulach@1348
     1
/*
jtulach@1348
     2
 * Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved.
jtulach@1348
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jtulach@1348
     4
 *
jtulach@1348
     5
 * This code is free software; you can redistribute it and/or modify it
jtulach@1348
     6
 * under the terms of the GNU General Public License version 2 only, as
jtulach@1348
     7
 * published by the Free Software Foundation.  Oracle designates this
jtulach@1348
     8
 * particular file as subject to the "Classpath" exception as provided
jtulach@1348
     9
 * by Oracle in the LICENSE file that accompanied this code.
jtulach@1348
    10
 *
jtulach@1348
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
jtulach@1348
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jtulach@1348
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jtulach@1348
    14
 * version 2 for more details (a copy is included in the LICENSE file that
jtulach@1348
    15
 * accompanied this code).
jtulach@1348
    16
 *
jtulach@1348
    17
 * You should have received a copy of the GNU General Public License version
jtulach@1348
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
jtulach@1348
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jtulach@1348
    20
 *
jtulach@1348
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jtulach@1348
    22
 * or visit www.oracle.com if you need additional information or have any
jtulach@1348
    23
 * questions.
jtulach@1348
    24
 */
jtulach@1348
    25
jtulach@1348
    26
package java.util.regex;
jtulach@1348
    27
jtulach@1348
    28
/**
jtulach@1348
    29
 * Unchecked exception thrown to indicate a syntax error in a
jtulach@1348
    30
 * regular-expression pattern.
jtulach@1348
    31
 *
jtulach@1348
    32
 * @author  unascribed
jtulach@1348
    33
 * @since 1.4
jtulach@1348
    34
 * @spec JSR-51
jtulach@1348
    35
 */
jtulach@1348
    36
jtulach@1348
    37
public class PatternSyntaxException
jtulach@1348
    38
    extends IllegalArgumentException
jtulach@1348
    39
{
jtulach@1348
    40
    private static final long serialVersionUID = -3864639126226059218L;
jtulach@1348
    41
jtulach@1348
    42
    private final String desc;
jtulach@1348
    43
    private final String pattern;
jtulach@1348
    44
    private final int index;
jtulach@1348
    45
jtulach@1348
    46
    /**
jtulach@1348
    47
     * Constructs a new instance of this class.
jtulach@1348
    48
     *
jtulach@1348
    49
     * @param  desc
jtulach@1348
    50
     *         A description of the error
jtulach@1348
    51
     *
jtulach@1348
    52
     * @param  regex
jtulach@1348
    53
     *         The erroneous pattern
jtulach@1348
    54
     *
jtulach@1348
    55
     * @param  index
jtulach@1348
    56
     *         The approximate index in the pattern of the error,
jtulach@1348
    57
     *         or <tt>-1</tt> if the index is not known
jtulach@1348
    58
     */
jtulach@1348
    59
    public PatternSyntaxException(String desc, String regex, int index) {
jtulach@1348
    60
        this.desc = desc;
jtulach@1348
    61
        this.pattern = regex;
jtulach@1348
    62
        this.index = index;
jtulach@1348
    63
    }
jtulach@1348
    64
jtulach@1348
    65
    /**
jtulach@1348
    66
     * Retrieves the error index.
jtulach@1348
    67
     *
jtulach@1348
    68
     * @return  The approximate index in the pattern of the error,
jtulach@1348
    69
     *         or <tt>-1</tt> if the index is not known
jtulach@1348
    70
     */
jtulach@1348
    71
    public int getIndex() {
jtulach@1348
    72
        return index;
jtulach@1348
    73
    }
jtulach@1348
    74
jtulach@1348
    75
    /**
jtulach@1348
    76
     * Retrieves the description of the error.
jtulach@1348
    77
     *
jtulach@1348
    78
     * @return  The description of the error
jtulach@1348
    79
     */
jtulach@1348
    80
    public String getDescription() {
jtulach@1348
    81
        return desc;
jtulach@1348
    82
    }
jtulach@1348
    83
jtulach@1348
    84
    /**
jtulach@1348
    85
     * Retrieves the erroneous regular-expression pattern.
jtulach@1348
    86
     *
jtulach@1348
    87
     * @return  The erroneous pattern
jtulach@1348
    88
     */
jtulach@1348
    89
    public String getPattern() {
jtulach@1348
    90
        return pattern;
jtulach@1348
    91
    }
jtulach@1348
    92
jtulach@1350
    93
    private static final String nl = System.lineSeparator();
jtulach@1348
    94
jtulach@1348
    95
    /**
jtulach@1348
    96
     * Returns a multi-line string containing the description of the syntax
jtulach@1348
    97
     * error and its index, the erroneous regular-expression pattern, and a
jtulach@1348
    98
     * visual indication of the error index within the pattern.
jtulach@1348
    99
     *
jtulach@1348
   100
     * @return  The full detail message
jtulach@1348
   101
     */
jtulach@1348
   102
    public String getMessage() {
jtulach@1348
   103
        StringBuffer sb = new StringBuffer();
jtulach@1348
   104
        sb.append(desc);
jtulach@1348
   105
        if (index >= 0) {
jtulach@1348
   106
            sb.append(" near index ");
jtulach@1348
   107
            sb.append(index);
jtulach@1348
   108
        }
jtulach@1348
   109
        sb.append(nl);
jtulach@1348
   110
        sb.append(pattern);
jtulach@1348
   111
        if (index >= 0) {
jtulach@1348
   112
            sb.append(nl);
jtulach@1348
   113
            for (int i = 0; i < index; i++) sb.append(' ');
jtulach@1348
   114
            sb.append('^');
jtulach@1348
   115
        }
jtulach@1348
   116
        return sb.toString();
jtulach@1348
   117
    }
jtulach@1348
   118
jtulach@1348
   119
}