| 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.accessor; |
| 20 | |
| 21 | import org.fuwjax.jon.CachedLexable; |
| 22 | import org.fuwjax.jon.ObjectAccessException; |
| 23 | import org.fuwjax.jon.SerialFormatException; |
| 24 | |
| 25 | /** |
| 26 | * A closure for reading a list of elements from a lexer. |
| 27 | * @author michaeldoberenz |
| 28 | */ |
| 29 | public class ListReader{ |
| 30 | private CachedLexable lexer; |
| 31 | private Token separator; |
| 32 | private Symbol symbol; |
| 33 | private boolean first = true; |
| 34 | |
| 35 | /** |
| 36 | * Constructs an instance for reading elements from <code>lexer</code> |
| 37 | * separated by <code>separator</code>. |
| 38 | * @param lexer the input for the read operation |
| 39 | * @param separator the separator between members of the list |
| 40 | */ |
| 41 | public ListReader(final CachedLexable lexer, final Token separator){ |
| 42 | this(lexer, separator, Symbol.Element); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Constructs an instance for reading a list of type <code>symbol</code> |
| 47 | * from <code>lexer</code> separated by <code>separator</code>. |
| 48 | * @param lexer the input for the read operation |
| 49 | * @param separator the separator between members of the list |
| 50 | * @param symbol the type of items in the list |
| 51 | */ |
| 52 | public ListReader(final CachedLexable lexer, final Token separator, final Symbol symbol){ |
| 53 | this.symbol = symbol; |
| 54 | this.lexer = lexer; |
| 55 | this.separator = separator; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Reads the next item of the list of type <code>accessor</code> using |
| 60 | * <code>object</code> for storage. |
| 61 | * @param object the container to add items to |
| 62 | * @param accessor the type of items expected in the list |
| 63 | * @return the new container |
| 64 | * @throws ObjectAccessException if an object relationship cannot be restored |
| 65 | * @throws SerialFormatException if the JON format has not been observed |
| 66 | */ |
| 67 | protected Object readImpl(final Object object, final Accessor accessor) throws ObjectAccessException, |
| 68 | SerialFormatException{ |
| 69 | return symbol.read(lexer, object, accessor); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Reads the token from the input and asserts that it matches |
| 74 | * <code>token</code>. |
| 75 | * @param token the expected token |
| 76 | * @throws SerialFormatException if the JON format has not been observed |
| 77 | */ |
| 78 | protected void read(final Token token) throws SerialFormatException{ |
| 79 | token.read(lexer); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Reads the next object using <code>accessor</code> and uses |
| 84 | * <code>container</code> for storage. |
| 85 | * @param container the storage for the parsed input |
| 86 | * @param accessor the type expected from the input |
| 87 | * @return the deserialized object |
| 88 | * @throws ObjectAccessException if an object relationship cannot be restored |
| 89 | * @throws SerialFormatException if the JON format has not been observed |
| 90 | */ |
| 91 | public Object read(final Object container, final Accessor accessor) throws ObjectAccessException, |
| 92 | SerialFormatException{ |
| 93 | if(!first){ |
| 94 | read(separator); |
| 95 | }else{ |
| 96 | first = false; |
| 97 | } |
| 98 | return readImpl(container, accessor); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Returns the current position of the lexer. |
| 103 | * @return the current position |
| 104 | */ |
| 105 | public int getPosition(){ |
| 106 | return lexer.getPosition(); |
| 107 | } |
| 108 | } |