rt/emul/compact/src/main/java/java/util/regex/PatternSyntaxException.java
branchjdk7-b147
changeset 1348 bca65655b36b
child 1350 f14e9730d4e9
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/emul/compact/src/main/java/java/util/regex/PatternSyntaxException.java	Mon Oct 07 16:13:27 2013 +0200
     1.3 @@ -0,0 +1,124 @@
     1.4 +/*
     1.5 + * Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package java.util.regex;
    1.30 +
    1.31 +import sun.security.action.GetPropertyAction;
    1.32 +
    1.33 +
    1.34 +/**
    1.35 + * Unchecked exception thrown to indicate a syntax error in a
    1.36 + * regular-expression pattern.
    1.37 + *
    1.38 + * @author  unascribed
    1.39 + * @since 1.4
    1.40 + * @spec JSR-51
    1.41 + */
    1.42 +
    1.43 +public class PatternSyntaxException
    1.44 +    extends IllegalArgumentException
    1.45 +{
    1.46 +    private static final long serialVersionUID = -3864639126226059218L;
    1.47 +
    1.48 +    private final String desc;
    1.49 +    private final String pattern;
    1.50 +    private final int index;
    1.51 +
    1.52 +    /**
    1.53 +     * Constructs a new instance of this class.
    1.54 +     *
    1.55 +     * @param  desc
    1.56 +     *         A description of the error
    1.57 +     *
    1.58 +     * @param  regex
    1.59 +     *         The erroneous pattern
    1.60 +     *
    1.61 +     * @param  index
    1.62 +     *         The approximate index in the pattern of the error,
    1.63 +     *         or <tt>-1</tt> if the index is not known
    1.64 +     */
    1.65 +    public PatternSyntaxException(String desc, String regex, int index) {
    1.66 +        this.desc = desc;
    1.67 +        this.pattern = regex;
    1.68 +        this.index = index;
    1.69 +    }
    1.70 +
    1.71 +    /**
    1.72 +     * Retrieves the error index.
    1.73 +     *
    1.74 +     * @return  The approximate index in the pattern of the error,
    1.75 +     *         or <tt>-1</tt> if the index is not known
    1.76 +     */
    1.77 +    public int getIndex() {
    1.78 +        return index;
    1.79 +    }
    1.80 +
    1.81 +    /**
    1.82 +     * Retrieves the description of the error.
    1.83 +     *
    1.84 +     * @return  The description of the error
    1.85 +     */
    1.86 +    public String getDescription() {
    1.87 +        return desc;
    1.88 +    }
    1.89 +
    1.90 +    /**
    1.91 +     * Retrieves the erroneous regular-expression pattern.
    1.92 +     *
    1.93 +     * @return  The erroneous pattern
    1.94 +     */
    1.95 +    public String getPattern() {
    1.96 +        return pattern;
    1.97 +    }
    1.98 +
    1.99 +    private static final String nl =
   1.100 +        java.security.AccessController
   1.101 +            .doPrivileged(new GetPropertyAction("line.separator"));
   1.102 +
   1.103 +    /**
   1.104 +     * Returns a multi-line string containing the description of the syntax
   1.105 +     * error and its index, the erroneous regular-expression pattern, and a
   1.106 +     * visual indication of the error index within the pattern.
   1.107 +     *
   1.108 +     * @return  The full detail message
   1.109 +     */
   1.110 +    public String getMessage() {
   1.111 +        StringBuffer sb = new StringBuffer();
   1.112 +        sb.append(desc);
   1.113 +        if (index >= 0) {
   1.114 +            sb.append(" near index ");
   1.115 +            sb.append(index);
   1.116 +        }
   1.117 +        sb.append(nl);
   1.118 +        sb.append(pattern);
   1.119 +        if (index >= 0) {
   1.120 +            sb.append(nl);
   1.121 +            for (int i = 0; i < index; i++) sb.append(' ');
   1.122 +            sb.append('^');
   1.123 +        }
   1.124 +        return sb.toString();
   1.125 +    }
   1.126 +
   1.127 +}