jtulach@1348: /*
jtulach@1348: * Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved.
jtulach@1348: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jtulach@1348: *
jtulach@1348: * This code is free software; you can redistribute it and/or modify it
jtulach@1348: * under the terms of the GNU General Public License version 2 only, as
jtulach@1348: * published by the Free Software Foundation. Oracle designates this
jtulach@1348: * particular file as subject to the "Classpath" exception as provided
jtulach@1348: * by Oracle in the LICENSE file that accompanied this code.
jtulach@1348: *
jtulach@1348: * This code is distributed in the hope that it will be useful, but WITHOUT
jtulach@1348: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jtulach@1348: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jtulach@1348: * version 2 for more details (a copy is included in the LICENSE file that
jtulach@1348: * accompanied this code).
jtulach@1348: *
jtulach@1348: * You should have received a copy of the GNU General Public License version
jtulach@1348: * 2 along with this work; if not, write to the Free Software Foundation,
jtulach@1348: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jtulach@1348: *
jtulach@1348: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jtulach@1348: * or visit www.oracle.com if you need additional information or have any
jtulach@1348: * questions.
jtulach@1348: */
jtulach@1348:
jtulach@1348: package java.util.regex;
jtulach@1348:
jtulach@1348: import sun.security.action.GetPropertyAction;
jtulach@1348:
jtulach@1348:
jtulach@1348: /**
jtulach@1348: * Unchecked exception thrown to indicate a syntax error in a
jtulach@1348: * regular-expression pattern.
jtulach@1348: *
jtulach@1348: * @author unascribed
jtulach@1348: * @since 1.4
jtulach@1348: * @spec JSR-51
jtulach@1348: */
jtulach@1348:
jtulach@1348: public class PatternSyntaxException
jtulach@1348: extends IllegalArgumentException
jtulach@1348: {
jtulach@1348: private static final long serialVersionUID = -3864639126226059218L;
jtulach@1348:
jtulach@1348: private final String desc;
jtulach@1348: private final String pattern;
jtulach@1348: private final int index;
jtulach@1348:
jtulach@1348: /**
jtulach@1348: * Constructs a new instance of this class.
jtulach@1348: *
jtulach@1348: * @param desc
jtulach@1348: * A description of the error
jtulach@1348: *
jtulach@1348: * @param regex
jtulach@1348: * The erroneous pattern
jtulach@1348: *
jtulach@1348: * @param index
jtulach@1348: * The approximate index in the pattern of the error,
jtulach@1348: * or -1 if the index is not known
jtulach@1348: */
jtulach@1348: public PatternSyntaxException(String desc, String regex, int index) {
jtulach@1348: this.desc = desc;
jtulach@1348: this.pattern = regex;
jtulach@1348: this.index = index;
jtulach@1348: }
jtulach@1348:
jtulach@1348: /**
jtulach@1348: * Retrieves the error index.
jtulach@1348: *
jtulach@1348: * @return The approximate index in the pattern of the error,
jtulach@1348: * or -1 if the index is not known
jtulach@1348: */
jtulach@1348: public int getIndex() {
jtulach@1348: return index;
jtulach@1348: }
jtulach@1348:
jtulach@1348: /**
jtulach@1348: * Retrieves the description of the error.
jtulach@1348: *
jtulach@1348: * @return The description of the error
jtulach@1348: */
jtulach@1348: public String getDescription() {
jtulach@1348: return desc;
jtulach@1348: }
jtulach@1348:
jtulach@1348: /**
jtulach@1348: * Retrieves the erroneous regular-expression pattern.
jtulach@1348: *
jtulach@1348: * @return The erroneous pattern
jtulach@1348: */
jtulach@1348: public String getPattern() {
jtulach@1348: return pattern;
jtulach@1348: }
jtulach@1348:
jtulach@1348: private static final String nl =
jtulach@1348: java.security.AccessController
jtulach@1348: .doPrivileged(new GetPropertyAction("line.separator"));
jtulach@1348:
jtulach@1348: /**
jtulach@1348: * Returns a multi-line string containing the description of the syntax
jtulach@1348: * error and its index, the erroneous regular-expression pattern, and a
jtulach@1348: * visual indication of the error index within the pattern.
jtulach@1348: *
jtulach@1348: * @return The full detail message
jtulach@1348: */
jtulach@1348: public String getMessage() {
jtulach@1348: StringBuffer sb = new StringBuffer();
jtulach@1348: sb.append(desc);
jtulach@1348: if (index >= 0) {
jtulach@1348: sb.append(" near index ");
jtulach@1348: sb.append(index);
jtulach@1348: }
jtulach@1348: sb.append(nl);
jtulach@1348: sb.append(pattern);
jtulach@1348: if (index >= 0) {
jtulach@1348: sb.append(nl);
jtulach@1348: for (int i = 0; i < index; i++) sb.append(' ');
jtulach@1348: sb.append('^');
jtulach@1348: }
jtulach@1348: return sb.toString();
jtulach@1348: }
jtulach@1348:
jtulach@1348: }