| 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 which supports reading key value pairs from the input. |
| 27 | * @author michaeldoberenz |
| 28 | */ |
| 29 | public class KeyValueReader extends ListReader{ |
| 30 | private Token keyValueSeparator; |
| 31 | private boolean shouldBeValue; |
| 32 | |
| 33 | /** |
| 34 | * Creates a new reader. |
| 35 | * @param lexer the source for the read operation |
| 36 | * @param separator the token between key value pairs |
| 37 | * @param keyValueSeparator the token between keys and values |
| 38 | */ |
| 39 | public KeyValueReader(final CachedLexable lexer, final Token separator, final Token keyValueSeparator){ |
| 40 | super(lexer, separator); |
| 41 | this.keyValueSeparator = keyValueSeparator; |
| 42 | } |
| 43 | |
| 44 | @Override |
| 45 | public Object read(final Object object, final Accessor accessor) throws ObjectAccessException, SerialFormatException{ |
| 46 | if(shouldBeValue){ |
| 47 | throw new IllegalStateException(); |
| 48 | } |
| 49 | final Object value = super.read(object, accessor); |
| 50 | shouldBeValue = true; |
| 51 | return value; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Reads a key from the input. |
| 56 | * @param object the storage for the key |
| 57 | * @param accessor the expected type of the key |
| 58 | * @return the key |
| 59 | * @throws ObjectAccessException if an object relationship cannot be restored |
| 60 | * @throws SerialFormatException if the JON format has not been observed |
| 61 | */ |
| 62 | public Object readKey(final Object object, final Accessor accessor) throws ObjectAccessException, |
| 63 | SerialFormatException{ |
| 64 | return read(object, accessor); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Reads a value from the input. |
| 69 | * @param object the storage for the value |
| 70 | * @param accessor the expected type of the value |
| 71 | * @return the value |
| 72 | * @throws ObjectAccessException if an object relationship cannot be restored |
| 73 | * @throws SerialFormatException if the JON format has not been observed |
| 74 | */ |
| 75 | public Object readValue(final Object object, final Accessor accessor) throws ObjectAccessException, |
| 76 | SerialFormatException{ |
| 77 | if(!shouldBeValue){ |
| 78 | throw new IllegalStateException(); |
| 79 | } |
| 80 | read(keyValueSeparator); |
| 81 | final Object value = readImpl(object, accessor); |
| 82 | shouldBeValue = false; |
| 83 | return value; |
| 84 | } |
| 85 | } |