| 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 | import org.fuwjax.jon.accessor.Symbol; |
| 22 | import org.fuwjax.jon.accessor.Token; |
| 23 | import org.fuwjax.jon.type.ReferenceType; |
| 24 | |
| 25 | /** |
| 26 | * The strategy for determining whether or not a particular {@link IndirectType} |
| 27 | * will serialize with a reference. |
| 28 | * @author michaeldoberenz |
| 29 | */ |
| 30 | public enum ReferenceStrategy{ |
| 31 | /** |
| 32 | * Store <code>object</code> in <code>cache</code> and write the |
| 33 | * generated unique id to <code>writer</code>. |
| 34 | */ |
| 35 | AssignedReference{ |
| 36 | @Override |
| 37 | public void write(final CachedAppendable writer, final Object object) throws ReferenceExistsException, |
| 38 | ObjectAccessException, SerialFormatException{ |
| 39 | try{ |
| 40 | final String reference = writer.store(object); |
| 41 | Symbol.Literal.write(writer, reference, ReferenceType.DEFAULT); |
| 42 | Token.ReferenceAssignment.write(writer); |
| 43 | }catch(ReferenceExistsException e){ |
| 44 | Symbol.Literal.write(writer, e.getReference(), ReferenceType.DEFAULT); |
| 45 | throw e; |
| 46 | } |
| 47 | } |
| 48 | }, |
| 49 | /** |
| 50 | * Do not store or write a reference. |
| 51 | */ |
| 52 | NeverReference{ |
| 53 | @Override |
| 54 | public void write(final CachedAppendable writer, final Object object) throws ReferenceExistsException, |
| 55 | ObjectAccessException, SerialFormatException{ |
| 56 | // write nothing |
| 57 | } |
| 58 | }; |
| 59 | private static final Reference IGNORED_REFERENCE = new Reference(null); |
| 60 | |
| 61 | /** |
| 62 | * Writes a reference to the appender for the given object from the cache. |
| 63 | * @param writer the destination for the reference string |
| 64 | * @param object the object whose reference should be written |
| 65 | * @throws ReferenceExistsException if <code>object</code> has already been |
| 66 | * stored on <code>writer</code> |
| 67 | * @throws ObjectAccessException if an object relationship cannot be followed |
| 68 | * @throws SerialFormatException if the JON format cannot be preserved |
| 69 | */ |
| 70 | public abstract void write(final CachedAppendable writer, final Object object) throws ReferenceExistsException, |
| 71 | ObjectAccessException, SerialFormatException; |
| 72 | |
| 73 | /** |
| 74 | * Reads a reference id from <code>lexer</code> for the next object and |
| 75 | * returns the reference for the id. |
| 76 | * @param lexer the source for the reference id |
| 77 | * @return the reference corresponding to the reference id |
| 78 | * @throws ReferenceExistsException if the reference id has been previously |
| 79 | * reserved |
| 80 | * @throws ObjectAccessException if an object relationship cannot be restored |
| 81 | * @throws SerialFormatException if the JON format has not been observed |
| 82 | */ |
| 83 | public static Reference read(final CachedLexable lexer) throws ReferenceExistsException, ObjectAccessException, |
| 84 | SerialFormatException{ |
| 85 | String referenceId; |
| 86 | final int pos = lexer.getPosition(); |
| 87 | try{ |
| 88 | referenceId = Symbol.Literal.read(lexer, null, ReferenceType.DEFAULT).toString(); |
| 89 | }catch(SerialFormatException e){ |
| 90 | e.assertPosition(pos); |
| 91 | return IGNORED_REFERENCE; |
| 92 | } |
| 93 | final Reference ref = lexer.reserve(referenceId); |
| 94 | try{ |
| 95 | Token.ReferenceAssignment.read(lexer); |
| 96 | }catch(SerialFormatException e){ |
| 97 | throw new ReferenceExistsException(ref); |
| 98 | } |
| 99 | return ref; |
| 100 | } |
| 101 | } |