1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.kuali.student.poc.jsonparser.consumer;
18
19 import org.kuali.student.poc.jsonparser.producer.BaseProducer;
20 import org.kuali.student.poc.jsonparser.tokenizer.token.BaseToken;
21 import org.kuali.student.poc.jsonparser.tokenizer.token.NumberToken;
22
23 import java.text.ParseException;
24
25
26
27
28
29
30 public class NumberConsumer implements BaseConsumer {
31 @Override
32 public BaseToken consume(BaseProducer producer) throws ParseException {
33 if (!producer.hasNext()) {
34 return null;
35 }
36 char ch = producer.peek();
37 StringBuilder buf = new StringBuilder();
38 int dotCount = 0;
39 int i = 0;
40 while (ch == '-' || Character.isDigit(ch) || ch == '.') {
41 if (ch == '-' && i > 0) {
42 String err = "Error at row = " + producer.getRow() + " col = " + producer.getColumn();
43 throw new ParseException(err, producer.getColumn());
44 }
45 if (ch == '.') {
46 dotCount++;
47 if (dotCount > 1) {
48 String err = "Error at row = " + producer.getRow() + " col = " + producer.getColumn();
49 throw new ParseException(err, producer.getColumn());
50 }
51 }
52 buf.append(ch);
53 producer.consume();
54 ch = producer.peek();
55 i++;
56 }
57 String numStr = buf.toString();
58 NumberToken numberToken = new NumberToken(numStr);
59 return numberToken;
60 }
61 }