001/** 002 * Unit-API - Units of Measurement API for Java 003 * Copyright (c) 2014 Jean-Marie Dautelle, Werner Keil, V2COM 004 * All rights reserved. 005 * 006 * See LICENSE.txt for details. 007 */ 008package javax.measure.format; 009 010import javax.measure.MeasurementException; 011 012/** 013 * Signals that an error has been reached unexpectedly while parsing. 014 * 015 * @author Werner Keil 016 * @version 0.4, $Date: 2014-08-04 $ 017 */ 018public class ParserException extends MeasurementException { 019 020 /** 021 * 022 */ 023 private static final long serialVersionUID = -3179553925611520368L; 024 025 /** 026 * The zero-based character position in the string being parsed at which the 027 * error was found while parsing. 028 * 029 * @serial 030 */ 031 private int position; 032 033 /** The original input data. */ 034 private CharSequence data; 035 036 /** 037 * Constructs a ParserException with the specified detail message, 038 * parsed text and index. A detail message is a String that describes this 039 * particular exception. 040 * 041 * @param message 042 * the detail message 043 * @param parsedData 044 * the parsed text, should not be null 045 * @param position 046 * the position where the error was found while parsing. 047 */ 048 public ParserException(String message, CharSequence parsedData, 049 int position) { 050 super(message); 051 this.data = parsedData; 052 this.position = position; 053 } 054 055 /** 056 * Constructs a ParserException with the parsed text and offset. A 057 * detail message is a String that describes this particular exception. 058 * 059 * @param parsedData 060 * the parsed text, should not be null 061 * @param errorIndex 062 * the position where the error is found while parsing. 063 */ 064 public ParserException(CharSequence parsedData, 065 int errorIndex) { 066 super("Parse Error"); 067 this.data = parsedData; 068 this.position = errorIndex; 069 } 070 071 /** 072 * Constructs a ParserException with the specified cause. 073 * 074 * @param cause 075 * the root cause 076 */ 077 public ParserException(Throwable cause) { 078 super(cause); 079 } 080 081 /** 082 * Returns the position where the error was found. 083 * 084 * @return the position of the error 085 */ 086 public int getPosition() { 087 return position; 088 } 089 090 /** 091 * Returns the string that was being parsed. 092 * 093 * @return the parsed string, or {@code null}, if {@code null} was passed as 094 * input. 095 */ 096 public String getParsedString() { 097 if (data == null) 098 return null; 099 return data.toString(); 100 } 101 102}