| 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 java.util.regex.Pattern; |
| 22 | |
| 23 | import org.fuwjax.jon.CachedAppendable; |
| 24 | import org.fuwjax.jon.CachedLexable; |
| 25 | import org.fuwjax.jon.ObjectAccessException; |
| 26 | import org.fuwjax.jon.SerialFormatException; |
| 27 | import org.fuwjax.jon.type.StringType; |
| 28 | |
| 29 | /** |
| 30 | * Contains the format for turning a particular kind of datatype into a string |
| 31 | * literal. |
| 32 | * @author michaeldoberenz |
| 33 | */ |
| 34 | public enum Literal{ |
| 35 | /** |
| 36 | * Writes the sequence as a reference identifier. |
| 37 | */ |
| 38 | Reference("&(\\w+)"){ //$NON-NLS-1$ |
| 39 | @Override |
| 40 | public void write(final CachedAppendable writer, final Object object, final LiteralAccessor type) |
| 41 | throws ObjectAccessException, SerialFormatException{ |
| 42 | Token.ReferenceIdentifierStart.write(writer); |
| 43 | writer.write(type.toString(object)); |
| 44 | } |
| 45 | }, |
| 46 | /** |
| 47 | * Writes the sequence as an identifier. |
| 48 | */ |
| 49 | Identifier("[-\\w.$_]+"){ //$NON-NLS-1$ |
| 50 | @Override |
| 51 | public void write(final CachedAppendable writer, final Object object, final LiteralAccessor type) |
| 52 | throws ObjectAccessException, SerialFormatException{ |
| 53 | writer.write(type.toString(object)); |
| 54 | } |
| 55 | }, |
| 56 | /** |
| 57 | * Writes the sequence as a class. |
| 58 | */ |
| 59 | Class("\\w+(?:\\.\\w+)*(?:\\$\\w+)*(?:\\[\\])*"){ //$NON-NLS-1$ |
| 60 | @Override |
| 61 | public void write(final CachedAppendable writer, final Object object, final LiteralAccessor type) |
| 62 | throws ObjectAccessException, SerialFormatException{ |
| 63 | writer.write(type.toString(object)); |
| 64 | } |
| 65 | }, |
| 66 | /** |
| 67 | * Writes the sequence as a string. |
| 68 | */ |
| 69 | String("\"((?:[^\\\\\"]|\\\\.)*)\""){ //$NON-NLS-1$ |
| 70 | @Override |
| 71 | public void write(final CachedAppendable writer, final Object object, final LiteralAccessor type) |
| 72 | throws ObjectAccessException, SerialFormatException{ |
| 73 | Token.StringDelimiter.write(writer); |
| 74 | writer.write(type.toString(object)); |
| 75 | Token.StringDelimiter.write(writer); |
| 76 | } |
| 77 | }, |
| 78 | /** |
| 79 | * reads an unknown literal. |
| 80 | */ |
| 81 | Unknown(){ |
| 82 | @Override |
| 83 | public void write(final CachedAppendable writer, final Object object, final LiteralAccessor type) |
| 84 | throws ObjectAccessException, SerialFormatException{ |
| 85 | if(Object.class.equals(object.getClass())){ |
| 86 | Token.ObjectStart.write(writer); |
| 87 | Token.ObjectStop.write(writer); |
| 88 | }else{ |
| 89 | throw new UnsupportedOperationException(); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public Object read(final CachedLexable lexer, final Object object, final LiteralAccessor type) |
| 95 | throws ObjectAccessException, SerialFormatException{ |
| 96 | final int pos = lexer.getPosition(); |
| 97 | try{ |
| 98 | return String.read(lexer, object, StringType.DEFAULT); |
| 99 | }catch(SerialFormatException e){ |
| 100 | e.assertPosition(pos); |
| 101 | try{ |
| 102 | Token.ObjectStart.read(lexer); |
| 103 | Token.ObjectStop.read(lexer); |
| 104 | return new Object(); |
| 105 | }catch(SerialFormatException ute){ |
| 106 | ute.assertPosition(pos); |
| 107 | return Identifier.read(lexer, object, type); |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | }; |
| 112 | private final Pattern pattern; |
| 113 | |
| 114 | private Literal(final String pattern){ |
| 115 | this.pattern = Pattern.compile(pattern); |
| 116 | } |
| 117 | |
| 118 | private Literal(){ |
| 119 | pattern = null; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Returns the pattern matching this literal. |
| 124 | * @return the pattern for this literal |
| 125 | */ |
| 126 | public Pattern getPattern(){ |
| 127 | return pattern; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Sends the specified character sequence to the writer according to this |
| 132 | * literal format. |
| 133 | * @param writer the destination for the literal. |
| 134 | * @param object the source for the literal. |
| 135 | * @param type the type of the object. |
| 136 | * @throws ObjectAccessException if an object relationship cannot be followed |
| 137 | * @throws SerialFormatException if the JON format cannot be preserved |
| 138 | */ |
| 139 | public abstract void write(CachedAppendable writer, Object object, LiteralAccessor type) |
| 140 | throws ObjectAccessException, SerialFormatException; |
| 141 | |
| 142 | /** |
| 143 | * Reads the next literal character sequence from the lexer according to this |
| 144 | * literal format. |
| 145 | * @param lexer the source for the input. |
| 146 | * @param object the storage for the parse |
| 147 | * @param type the type of the literal |
| 148 | * @return the literal. |
| 149 | * @throws ObjectAccessException if an object relationship cannot be restored |
| 150 | * @throws SerialFormatException if the JON format has not been observed |
| 151 | */ |
| 152 | public Object read(final CachedLexable lexer, final Object object, final LiteralAccessor type) |
| 153 | throws ObjectAccessException, SerialFormatException{ |
| 154 | final CharSequence seq = lexer.read(getPattern()); |
| 155 | return type.createInstance(seq, object); |
| 156 | } |
| 157 | } |