1 package net.minidev.json.parser;
2
3 /*
4  *    Copyright 2011 JSON-SMART authors
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 import static net.minidev.json.parser.ParseException.ERROR_UNEXPECTED_CHAR;
19 import static net.minidev.json.parser.ParseException.ERROR_UNEXPECTED_EOF;
20 import static net.minidev.json.parser.ParseException.ERROR_UNEXPECTED_TOKEN;
21
22 import java.io.IOException;
23
24 /**
25  * Parser for JSON text. Please note that JSONParser is NOT thread-safe.
26  * 
27  * @author Uriel Chemouni <uchemouni@gmail.com>
28  * @see JSONParserString
29  * @see JSONParserByteArray
30  */

31 abstract class JSONParserMemory extends JSONParserBase {
32     protected int len;
33
34     public JSONParserMemory(int permissiveMode) {
35         super(permissiveMode);
36     }
37
38     protected void readNQString(boolean[] stop) throws IOException {
39         int start = pos;
40         skipNQString(stop);
41         extractStringTrim(start, pos);
42     }
43
44     protected Object readNumber(boolean[] stop) throws ParseException, IOException {
45         int start = pos;
46         // accept first char digit or -
47         read();
48         skipDigits();
49
50         // Integer digit
51         if (c != '.' && c != 'E' && c != 'e') {
52             skipSpace();
53             if (c >= 0 && c < MAX_STOP && !stop[c] && c != EOI) {
54                 // convert string
55                 skipNQString(stop);
56                 extractStringTrim(start, pos);
57                 if (!acceptNonQuote)
58                     throw new ParseException(pos, ERROR_UNEXPECTED_TOKEN, xs);
59                 return xs;
60             }
61             extractStringTrim(start, pos);
62             return parseNumber(xs);
63         }
64         // floating point
65         if (c == '.') {
66             //
67             read();
68             skipDigits();
69         }
70         if (c != 'E' && c != 'e') {
71             skipSpace();
72             if (c >= 0 && c < MAX_STOP && !stop[c] && c != EOI) {
73                 // convert string
74                 skipNQString(stop);
75                 extractStringTrim(start, pos);
76                 if (!acceptNonQuote)
77                     throw new ParseException(pos, ERROR_UNEXPECTED_TOKEN, xs);
78                 return xs;
79             }
80             extractStringTrim(start, pos);
81             return extractFloat();
82         }
83         sb.append('E');
84         read();
85         if (c == '+' || c == '-' || c >= '0' && c <= '9') {
86             sb.append(c);
87             read(); // skip first char
88             skipDigits();
89             skipSpace();
90             if (c >= 0 && c < MAX_STOP && !stop[c] && c != EOI) {
91                 // convert string
92                 skipNQString(stop);
93                 extractStringTrim(start, pos);
94                 if (!acceptNonQuote)
95                     throw new ParseException(pos, ERROR_UNEXPECTED_TOKEN, xs);
96                 return xs;
97             }
98             extractStringTrim(start, pos);
99             return extractFloat();
100         } else {
101             skipNQString(stop);
102             extractStringTrim(start, pos);
103             if (!acceptNonQuote)
104                 throw new ParseException(pos, ERROR_UNEXPECTED_TOKEN, xs);
105             if (!acceptLeadinZero)
106                 checkLeadinZero();
107             return xs;
108         }
109         // throw new ParseException(pos - 1, ERROR_UNEXPECTED_CHAR, null);
110     }
111
112     protected void readString() throws ParseException, IOException {
113         if (!acceptSimpleQuote && c == '\'') {
114             if (acceptNonQuote) {
115                 readNQString(stopAll);
116                 return;
117             }
118             throw new ParseException(pos, ERROR_UNEXPECTED_CHAR, c);
119         }
120         int tmpP = indexOf(c, pos + 1);
121         if (tmpP == -1)
122             throw new ParseException(len, ERROR_UNEXPECTED_EOF, null);
123         extractString(pos + 1, tmpP);
124         if (xs.indexOf('\\') == -1) {
125             checkControleChar();
126             pos = tmpP;
127             read();
128             // handler.primitive(tmp);
129             return;
130         }
131         sb.clear();
132         readString2();
133     }
134
135     abstract protected void extractString(int start, int stop);
136
137     abstract protected int indexOf(char c, int pos);
138
139     abstract protected void extractStringTrim(int start, int stop);
140 }
141