| 1 | /* |
| 2 | * This file is part of JON. |
| 3 | * |
| 4 | * JON is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU Lesser General Public License as published by |
| 6 | * the Free Software Foundation; either version 3 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * JON is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU Lesser General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Lesser General Public License |
| 15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | * |
| 17 | * Copyright 2007 Michael Doberenz |
| 18 | */ |
| 19 | package org.fuwjax.jon; |
| 20 | |
| 21 | /** |
| 22 | * Thrown when the reader or writer cannot maintain JON format integrity. |
| 23 | * @author michaeldoberenz |
| 24 | */ |
| 25 | public class SerialFormatException extends Exception{ |
| 26 | private static final long serialVersionUID = 1L; |
| 27 | |
| 28 | /** |
| 29 | * The set of possible {@link SerialFormatException} subtypes. |
| 30 | * @author michaeldoberenz |
| 31 | */ |
| 32 | public enum Messages{ |
| 33 | /** |
| 34 | * Indicates the formatted output could not physically be written to the |
| 35 | * output medium. |
| 36 | */ |
| 37 | OutputError, |
| 38 | /** |
| 39 | * Indicates that an identifier was expected but not found on the input |
| 40 | * medium. |
| 41 | */ |
| 42 | UnexpectedIdentifier, |
| 43 | /** |
| 44 | * Indicates that a token was expected but not found on the input medium. |
| 45 | */ |
| 46 | UnexpectedToken, |
| 47 | /** |
| 48 | * Indicates that more input is needed, but no more was found. |
| 49 | */ |
| 50 | UnexpectedEndOfInput; |
| 51 | /** |
| 52 | * Returns an exception of this subtype. |
| 53 | * @return an {@link SerialFormatException} |
| 54 | */ |
| 55 | public SerialFormatException exception(){ |
| 56 | return new SerialFormatException(name()); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Returns an exception of this subtype. |
| 61 | * @param cause the cause of this exception |
| 62 | * @return an {@link SerialFormatException} |
| 63 | */ |
| 64 | public SerialFormatException exception(final Throwable cause){ |
| 65 | return new SerialFormatException(name(), cause); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Returns an exception of this subtype. |
| 70 | * @param pos the current position of the reader |
| 71 | * @return an {@link SerialFormatException} |
| 72 | */ |
| 73 | public SerialFormatException exception(final int pos){ |
| 74 | return new SerialFormatException(name(), pos); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | private final int position; |
| 79 | |
| 80 | /** |
| 81 | * Creates a new instance with a message <code>message</code>. |
| 82 | * @param message a description of the cause of this exception |
| 83 | * @param cause the cause of this exception |
| 84 | */ |
| 85 | public SerialFormatException(final String message, final Throwable cause){ |
| 86 | super(message, cause); |
| 87 | this.position = -1; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Creates a new instance with a message <code>message</code>. |
| 92 | * @param message a description of the cause of this exception |
| 93 | * @param pos the current position of the reader |
| 94 | */ |
| 95 | public SerialFormatException(final String message, final int pos){ |
| 96 | super(message); |
| 97 | this.position = pos; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Creates a new instance with a message <code>message</code>. |
| 102 | * @param message a description of the cause of this exception |
| 103 | */ |
| 104 | public SerialFormatException(final String message){ |
| 105 | super(message); |
| 106 | this.position = -1; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Asserts that the curernt position of the reader is equal to |
| 111 | * <code>pos</code>. |
| 112 | * @param pos the expected position of the reader |
| 113 | * @throws SerialFormatException if the JON format has not been observed |
| 114 | */ |
| 115 | public void assertPosition(final int pos) throws SerialFormatException{ |
| 116 | if(pos != position){ |
| 117 | throw this; |
| 118 | } |
| 119 | } |
| 120 | } |