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