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

COVERAGE SUMMARY FOR SOURCE FILE [DefaultCachedLexer.java]

nameclass, %method, %block, %line, %
DefaultCachedLexer.java100% (1/1)90%  (9/10)80%  (121/151)83%  (35/42)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class DefaultCachedLexer100% (1/1)90%  (9/10)80%  (121/151)83%  (35/42)
toString (): String 0%   (0/1)0%   (0/10)0%   (0/1)
skipWhitespace (): void 100% (1/1)36%  (5/14)40%  (2/5)
current (): char 100% (1/1)50%  (6/12)33%  (1/3)
read (Pattern): CharSequence 100% (1/1)88%  (35/40)90%  (9/10)
DefaultCachedLexer (CharSequence): void 100% (1/1)100% (11/11)100% (4/4)
getMatch (Matcher): String 100% (1/1)100% (16/16)100% (6/6)
getPosition (): int 100% (1/1)100% (6/6)100% (1/1)
next (): void 100% (1/1)100% (7/7)100% (2/2)
read (char): void 100% (1/1)100% (14/14)100% (5/5)
reserve (String): Reference 100% (1/1)100% (21/21)100% (5/5)

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;
20 
21import java.util.HashMap;
22import java.util.Map;
23import java.util.regex.Matcher;
24import java.util.regex.Pattern;
25 
26/**
27 * A default instance of {@link CachedLexable} that allows for JON input to be
28 * supplied in a string.
29 * @author michaeldoberenz
30 */
31public class DefaultCachedLexer implements CachedLexable{
32        private CharSequence seq;
33        private Map<String, Reference> references;
34        private int space;
35        private int position;
36 
37        /**
38         * Creates a new instance using <code>input</code> as the source
39         * JON-formatted text.
40         * @param input the JON-formatted text to use as input for a read operation
41         */
42        public DefaultCachedLexer(final CharSequence input){
43                seq = input;
44                references = new HashMap<String, Reference>();
45        }
46 
47        public void read(final char token) throws SerialFormatException{
48                skipWhitespace();
49                if(current() != token){
50                        throw SerialFormatException.Messages.UnexpectedToken.exception(getPosition());
51                }
52                next();
53        }
54 
55        public Reference reserve(final String referenceId) throws ReferenceExistsException{
56                Reference ref = references.get(referenceId);
57                if(ref == null){
58                        ref = new Reference(referenceId);
59                        references.put(referenceId, ref);
60                }
61                return ref;
62        }
63 
64        public CharSequence read(final Pattern pattern) throws SerialFormatException{
65                skipWhitespace();
66                final Matcher matcher = pattern.matcher(seq);
67                matcher.region(position, matcher.regionEnd());
68                if(!matcher.lookingAt()){
69                        throw SerialFormatException.Messages.UnexpectedIdentifier.exception(getPosition());
70                }
71                final String match = getMatch(matcher);
72                if(match.length() == 0){
73                        throw SerialFormatException.Messages.UnexpectedIdentifier.exception(getPosition());
74                }
75                position = matcher.end();
76                return match;
77        }
78 
79        private String getMatch(final Matcher matcher){
80                String match = null;
81                if(matcher.groupCount() > 0){
82                        match = matcher.group(1);
83                }
84                if(match == null){
85                        match = matcher.group();
86                }
87                return match;
88        }
89 
90        private void next(){
91                ++position;
92        }
93 
94        private char current() throws SerialFormatException{
95                try{
96                        return seq.charAt(position);
97                }catch(RuntimeException e){
98                        throw SerialFormatException.Messages.UnexpectedEndOfInput.exception(getPosition());
99                }
100        }
101 
102        private void skipWhitespace() throws SerialFormatException{
103                while(Character.isWhitespace(current())){
104                        ++space;
105                        next();
106                }
107        }
108 
109        public int getPosition(){
110                return position - space;
111        }
112 
113        @Override
114        public String toString(){
115                return seq.subSequence(position, seq.length()).toString();
116        }
117}

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