EMMA Coverage Report (generated Wed Aug 29 00:03:59 CDT 2007)
[all classes][org.fuwjax.jon.type]

COVERAGE SUMMARY FOR SOURCE FILE [StringType.java]

nameclass, %method, %block, %line, %
StringType.java100% (1/1)100% (10/10)94%  (230/244)91%  (31.9/35)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class StringType100% (1/1)100% (10/10)94%  (230/244)91%  (31.9/35)
appendEscape (StringBuilder, String): int 100% (1/1)64%  (23/36)67%  (6/9)
isLowerAlpha (char): boolean 100% (1/1)90%  (9/10)90%  (0.9/1)
<static initializer> 100% (1/1)100% (112/112)100% (2/2)
StringType (): void 100% (1/1)100% (4/4)100% (2/2)
createInstance (CharSequence, Object): Object 100% (1/1)100% (49/49)100% (11/11)
getClassCastStrategy (): ClassCastStrategy 100% (1/1)100% (2/2)100% (1/1)
getLiteral (): Literal 100% (1/1)100% (2/2)100% (1/1)
getReferenceStrategy (): ReferenceStrategy 100% (1/1)100% (2/2)100% (1/1)
getSymbolSpec (): Symbol 100% (1/1)100% (2/2)100% (1/1)
toString (Object): CharSequence 100% (1/1)100% (25/25)100% (6/6)

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 */
19package org.fuwjax.jon.type;
20 
21import org.fuwjax.jon.ClassCastStrategy;
22import org.fuwjax.jon.ObjectAccessException;
23import org.fuwjax.jon.ReferenceStrategy;
24import org.fuwjax.jon.accessor.Literal;
25import org.fuwjax.jon.accessor.LiteralAccessor;
26import org.fuwjax.jon.accessor.Symbol;
27 
28/**
29 * Offers an abstract layer over String.
30 * @author michaeldoberenz
31 */
32public 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}

[all classes][org.fuwjax.jon.type]
EMMA 2.0.5312 (C) Vladimir Roubtsov