Save This Page
Home » openjdk-7 » java » util » regex » [javadoc | source]
    1   /*
    2    * Copyright 1999-2006 Sun Microsystems, Inc.  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.  Sun designates this
    8    * particular file as subject to the "Classpath" exception as provided
    9    * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
   22    * CA 95054 USA or visit www.sun.com if you need additional information or
   23    * have any questions.
   24    */
   25   
   26   package java.util.regex;
   27   
   28   import sun.security.action.GetPropertyAction;
   29   
   30   
   31   /**
   32    * Unchecked exception thrown to indicate a syntax error in a
   33    * regular-expression pattern.
   34    *
   35    * @author  unascribed
   36    * @since 1.4
   37    * @spec JSR-51
   38    */
   39   
   40   public class PatternSyntaxException
   41       extends IllegalArgumentException
   42   {
   43   
   44       private final String desc;
   45       private final String pattern;
   46       private final int index;
   47   
   48       /**
   49        * Constructs a new instance of this class.
   50        *
   51        * @param  desc
   52        *         A description of the error
   53        *
   54        * @param  regex
   55        *         The erroneous pattern
   56        *
   57        * @param  index
   58        *         The approximate index in the pattern of the error,
   59        *         or <tt>-1</tt> if the index is not known
   60        */
   61       public PatternSyntaxException(String desc, String regex, int index) {
   62           this.desc = desc;
   63           this.pattern = regex;
   64           this.index = index;
   65       }
   66   
   67       /**
   68        * Retrieves the error index.
   69        *
   70        * @return  The approximate index in the pattern of the error,
   71        *         or <tt>-1</tt> if the index is not known
   72        */
   73       public int getIndex() {
   74           return index;
   75       }
   76   
   77       /**
   78        * Retrieves the description of the error.
   79        *
   80        * @return  The description of the error
   81        */
   82       public String getDescription() {
   83           return desc;
   84       }
   85   
   86       /**
   87        * Retrieves the erroneous regular-expression pattern.
   88        *
   89        * @return  The erroneous pattern
   90        */
   91       public String getPattern() {
   92           return pattern;
   93       }
   94   
   95       private static final String nl =
   96           java.security.AccessController
   97               .doPrivileged(new GetPropertyAction("line.separator"));
   98   
   99       /**
  100        * Returns a multi-line string containing the description of the syntax
  101        * error and its index, the erroneous regular-expression pattern, and a
  102        * visual indication of the error index within the pattern.
  103        *
  104        * @return  The full detail message
  105        */
  106       public String getMessage() {
  107           StringBuffer sb = new StringBuffer();
  108           sb.append(desc);
  109           if (index >= 0) {
  110               sb.append(" near index ");
  111               sb.append(index);
  112           }
  113           sb.append(nl);
  114           sb.append(pattern);
  115           if (index >= 0) {
  116               sb.append(nl);
  117               for (int i = 0; i < index; i++) sb.append(' ');
  118               sb.append('^');
  119           }
  120           return sb.toString();
  121       }
  122   
  123   }

Save This Page
Home » openjdk-7 » java » util » regex » [javadoc | source]