| 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.type; |
| 20 | |
| 21 | import org.fuwjax.jon.ClassCastStrategy; |
| 22 | import org.fuwjax.jon.ObjectAccessException; |
| 23 | import org.fuwjax.jon.ReferenceStrategy; |
| 24 | import org.fuwjax.jon.accessor.Literal; |
| 25 | import org.fuwjax.jon.accessor.LiteralAccessor; |
| 26 | import org.fuwjax.jon.accessor.Symbol; |
| 27 | |
| 28 | /** |
| 29 | * Offers an abstract layer over String. |
| 30 | * @author michaeldoberenz |
| 31 | */ |
| 32 | public final class StringType extends AbstractIndirectType implements LiteralAccessor{ |
| 33 | /** |
| 34 | * The singleton instance. |
| 35 | */ |
| 36 | public static final StringType DEFAULT = new StringType(); |
| 37 | private static final int HEX_RADIX = 16; |
| 38 | private static final int UNICODE_ESCAPE_LENGTH = 6; |
| 39 | private static final char[] CONTROL_CHARS = new char[]{'a', '\b', 'c', 'd', 'e', '\f', 'g', 'h', 'i', 'j', 'k', 'l', |
| 40 | 'm', '\n', 'o', 'p', 'q', '\r', 's', '\t', 'u', 'v', 'w', 'x', 'y', 'z'}; |
| 41 | |
| 42 | private StringType(){ |
| 43 | super(String.class); |
| 44 | } |
| 45 | |
| 46 | public ClassCastStrategy getClassCastStrategy(){ |
| 47 | return ClassCastStrategy.NeverCast; |
| 48 | } |
| 49 | |
| 50 | public ReferenceStrategy getReferenceStrategy(){ |
| 51 | return ReferenceStrategy.NeverReference; |
| 52 | } |
| 53 | |
| 54 | public Symbol getSymbolSpec(){ |
| 55 | return Symbol.Literal; |
| 56 | } |
| 57 | |
| 58 | public Literal getLiteral(){ |
| 59 | return Literal.String; |
| 60 | } |
| 61 | |
| 62 | public CharSequence toString(final Object object) throws ObjectAccessException{ |
| 63 | String out = object.toString(); |
| 64 | out = out.replace("\\", "\\\\"); //$NON-NLS-1$ //$NON-NLS-2$ |
| 65 | out = out.replace("\"", "\\\""); //$NON-NLS-1$ //$NON-NLS-2$ |
| 66 | out = out.replace("\n", "\\n"); //$NON-NLS-1$ //$NON-NLS-2$ |
| 67 | out = out.replace("\t", "\\t"); //$NON-NLS-1$ //$NON-NLS-2$ |
| 68 | return out; |
| 69 | } |
| 70 | |
| 71 | public Object createInstance(final CharSequence parse, final Object source) throws ObjectAccessException{ |
| 72 | final StringBuilder builder = new StringBuilder(); |
| 73 | final String str = parse.toString(); |
| 74 | int hold = 0; |
| 75 | int slash = str.indexOf('\\'); |
| 76 | while(slash >= 0){ |
| 77 | builder.append(str.substring(hold, slash)); |
| 78 | hold = slash + appendEscape(builder, str.substring(slash, slash + UNICODE_ESCAPE_LENGTH)); |
| 79 | slash = str.indexOf('\\', hold); |
| 80 | } |
| 81 | builder.append(str.substring(hold)); |
| 82 | return builder.toString(); |
| 83 | } |
| 84 | |
| 85 | private int appendEscape(final StringBuilder builder, final String str){ |
| 86 | char c = str.charAt(1); |
| 87 | if(c == 'u'){ |
| 88 | final String code = str.substring(2, UNICODE_ESCAPE_LENGTH); |
| 89 | builder.appendCodePoint(Integer.parseInt(code, HEX_RADIX)); |
| 90 | return UNICODE_ESCAPE_LENGTH; |
| 91 | } |
| 92 | if(isLowerAlpha(c)){ |
| 93 | c = CONTROL_CHARS[c - 'a']; |
| 94 | } |
| 95 | builder.append(c); |
| 96 | return 2; |
| 97 | } |
| 98 | |
| 99 | private boolean isLowerAlpha(final char ch){ |
| 100 | return ch >= 'a' && ch <= 'z'; |
| 101 | } |
| 102 | } |