| 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.CachedAppendable; |
| 22 | import org.fuwjax.jon.ObjectAccessException; |
| 23 | import org.fuwjax.jon.SerialFormatException; |
| 24 | |
| 25 | /** |
| 26 | * A closure for writing key value pairs. |
| 27 | * @author michaeldoberenz |
| 28 | */ |
| 29 | public class KeyValueWriter extends ListWriter{ |
| 30 | private Token keyValueSeparator; |
| 31 | private boolean shouldBeValue; |
| 32 | |
| 33 | /** |
| 34 | * Creates a new instance. |
| 35 | * @param writer the destination of the write operation |
| 36 | * @param separator the token between key value pairs |
| 37 | * @param keyValueSeparator the token between keys and values |
| 38 | */ |
| 39 | public KeyValueWriter(final CachedAppendable writer, final Token separator, final Token keyValueSeparator){ |
| 40 | super(writer, separator); |
| 41 | this.keyValueSeparator = keyValueSeparator; |
| 42 | } |
| 43 | |
| 44 | @Override |
| 45 | public void write(final Object object, final Accessor accessor) throws ObjectAccessException, SerialFormatException{ |
| 46 | if(shouldBeValue){ |
| 47 | throw new IllegalStateException(); |
| 48 | } |
| 49 | super.write(object, accessor); |
| 50 | shouldBeValue = true; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Writes <code>key</code> to the destination using <code>accessor</code>. |
| 55 | * @param key the key to serialize |
| 56 | * @param accessor the serialization specification for the key |
| 57 | * @throws ObjectAccessException if an object relationship cannot be followed |
| 58 | * @throws SerialFormatException if the JON format cannot be preserved |
| 59 | */ |
| 60 | public void writeKey(final Object key, final Accessor accessor) throws ObjectAccessException, SerialFormatException{ |
| 61 | write(key, accessor); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Writes <code>value</code> to the destination using <code>accessor</code>. |
| 66 | * @param value the value to serialize |
| 67 | * @param accessor the serialization specification for the value |
| 68 | * @throws ObjectAccessException if an object relationship cannot be followed |
| 69 | * @throws SerialFormatException if the JON format cannot be preserved |
| 70 | */ |
| 71 | public void writeValue(final Object value, final Accessor accessor) throws ObjectAccessException, |
| 72 | SerialFormatException{ |
| 73 | if(!shouldBeValue){ |
| 74 | throw new IllegalStateException(); |
| 75 | } |
| 76 | write(keyValueSeparator); |
| 77 | writeImpl(value, accessor); |
| 78 | shouldBeValue = false; |
| 79 | } |
| 80 | } |