001: /* Generated By:JavaCC: Do not edit this line. JSONParser.java */
002: package de.schlund.pfixcore.webservice.json.parser;
003:
004: import java.io.File;
005: import java.io.FileInputStream;
006: import java.io.IOException;
007: import java.io.InputStreamReader;
008: import java.io.StringReader;
009:
010: import de.schlund.pfixcore.webservice.json.JSONArray;
011: import de.schlund.pfixcore.webservice.json.JSONObject;
012: import de.schlund.pfixcore.webservice.json.ParserUtils;
013:
014: public class JSONParser implements JSONParserConstants {
015:
016: Object jsonValue;
017: String memberName;
018:
019: public static void main(String[] args) {
020: try {
021: String query = args[0];
022: query = load(new File(query), "UTF-8");
023: JSONParser parser = new JSONParser(new StringReader(query));
024: Object val = parser.getJSONValue();
025: System.out.println(ParserUtils.javaToJson(val));
026: } catch (Exception x) {
027: x.printStackTrace();
028: }
029: }
030:
031: public Object getJSONValue() throws Exception {
032: if (jsonValue == null) {
033: value();
034: }
035: return jsonValue;
036: }
037:
038: public static String load(File file, String encoding)
039: throws IOException {
040: FileInputStream fis = new FileInputStream(file);
041: InputStreamReader reader = new InputStreamReader(fis, encoding);
042: StringBuffer strBuf = new StringBuffer();
043: char[] buffer = new char[4096];
044: int i = 0;
045: try {
046: while ((i = reader.read(buffer)) != -1)
047: strBuf.append(buffer, 0, i);
048: } finally {
049: fis.close();
050: }
051: return strBuf.toString();
052: }
053:
054: final public void value() throws ParseException {
055: Token valueToken;
056: switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
057: case LCB:
058: object();
059: break;
060: case LSB:
061: array();
062: break;
063: case QSTR:
064: valueToken = jj_consume_token(QSTR);
065: jsonValue = ParserUtils.jsonToJava(valueToken.image);
066: break;
067: case NUM:
068: valueToken = jj_consume_token(NUM);
069: jsonValue = ParserUtils.parseNumber(valueToken.image);
070: break;
071: case TRUE:
072: jj_consume_token(TRUE);
073: jsonValue = Boolean.TRUE;
074: break;
075: case FALSE:
076: jj_consume_token(FALSE);
077: jsonValue = Boolean.FALSE;
078: break;
079: case NULL:
080: jj_consume_token(NULL);
081: jsonValue = null;
082: break;
083: case DATE:
084: valueToken = jj_consume_token(DATE);
085: jsonValue = ParserUtils.parseDate(valueToken.image);
086: break;
087: default:
088: jj_la1[0] = jj_gen;
089: jj_consume_token(-1);
090: throw new ParseException();
091: }
092: }
093:
094: final public void member() throws ParseException {
095: Token memberNameToken;
096: memberNameToken = jj_consume_token(QSTR);
097: jj_consume_token(COL);
098: value();
099: memberName = ParserUtils.jsonToJava(memberNameToken.image);
100: }
101:
102: final public void object() throws ParseException {
103: JSONObject jsonObject;
104: jsonObject = new JSONObject();
105: jj_consume_token(LCB);
106: member();
107: jsonObject.putMember(memberName, jsonValue);
108: label_1: while (true) {
109: switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
110: case COM:
111: ;
112: break;
113: default:
114: jj_la1[1] = jj_gen;
115: break label_1;
116: }
117: jj_consume_token(COM);
118: member();
119: jsonObject.putMember(memberName, jsonValue);
120: }
121: jj_consume_token(RCB);
122: jsonValue = jsonObject;
123: }
124:
125: final public void array() throws ParseException {
126: JSONArray jsonArray;
127: jsonArray = new JSONArray();
128: jj_consume_token(LSB);
129: switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
130: case LCB:
131: case LSB:
132: case TRUE:
133: case FALSE:
134: case NULL:
135: case QSTR:
136: case NUM:
137: case DATE:
138: value();
139: jsonArray.add(jsonValue);
140: label_2: while (true) {
141: switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
142: case COM:
143: ;
144: break;
145: default:
146: jj_la1[2] = jj_gen;
147: break label_2;
148: }
149: jj_consume_token(COM);
150: value();
151: jsonArray.add(jsonValue);
152: }
153: break;
154: default:
155: jj_la1[3] = jj_gen;
156: ;
157: }
158: jj_consume_token(RSB);
159: jsonValue = jsonArray;
160: }
161:
162: public JSONParserTokenManager token_source;
163: SimpleCharStream jj_input_stream;
164: public Token token, jj_nt;
165: private int jj_ntk;
166: private int jj_gen;
167: final private int[] jj_la1 = new int[4];
168: final static private int[] jj_la1_0 = new int[] { 0x6f140, 0x800,
169: 0x800, 0x6f140, };
170:
171: public JSONParser(java.io.InputStream stream) {
172: this (stream, null);
173: }
174:
175: public JSONParser(java.io.InputStream stream, String encoding) {
176: try {
177: jj_input_stream = new SimpleCharStream(stream, encoding, 1,
178: 1);
179: } catch (java.io.UnsupportedEncodingException e) {
180: throw new RuntimeException(e);
181: }
182: token_source = new JSONParserTokenManager(jj_input_stream);
183: token = new Token();
184: jj_ntk = -1;
185: jj_gen = 0;
186: for (int i = 0; i < 4; i++)
187: jj_la1[i] = -1;
188: }
189:
190: public void ReInit(java.io.InputStream stream) {
191: ReInit(stream, null);
192: }
193:
194: public void ReInit(java.io.InputStream stream, String encoding) {
195: try {
196: jj_input_stream.ReInit(stream, encoding, 1, 1);
197: } catch (java.io.UnsupportedEncodingException e) {
198: throw new RuntimeException(e);
199: }
200: token_source.ReInit(jj_input_stream);
201: token = new Token();
202: jj_ntk = -1;
203: jj_gen = 0;
204: for (int i = 0; i < 4; i++)
205: jj_la1[i] = -1;
206: }
207:
208: public JSONParser(java.io.Reader stream) {
209: jj_input_stream = new SimpleCharStream(stream, 1, 1);
210: token_source = new JSONParserTokenManager(jj_input_stream);
211: token = new Token();
212: jj_ntk = -1;
213: jj_gen = 0;
214: for (int i = 0; i < 4; i++)
215: jj_la1[i] = -1;
216: }
217:
218: public void ReInit(java.io.Reader stream) {
219: jj_input_stream.ReInit(stream, 1, 1);
220: token_source.ReInit(jj_input_stream);
221: token = new Token();
222: jj_ntk = -1;
223: jj_gen = 0;
224: for (int i = 0; i < 4; i++)
225: jj_la1[i] = -1;
226: }
227:
228: public JSONParser(JSONParserTokenManager tm) {
229: token_source = tm;
230: token = new Token();
231: jj_ntk = -1;
232: jj_gen = 0;
233: for (int i = 0; i < 4; i++)
234: jj_la1[i] = -1;
235: }
236:
237: public void ReInit(JSONParserTokenManager tm) {
238: token_source = tm;
239: token = new Token();
240: jj_ntk = -1;
241: jj_gen = 0;
242: for (int i = 0; i < 4; i++)
243: jj_la1[i] = -1;
244: }
245:
246: final private Token jj_consume_token(int kind)
247: throws ParseException {
248: Token oldToken;
249: if ((oldToken = token).next != null)
250: token = token.next;
251: else
252: token = token.next = token_source.getNextToken();
253: jj_ntk = -1;
254: if (token.kind == kind) {
255: jj_gen++;
256: return token;
257: }
258: token = oldToken;
259: jj_kind = kind;
260: throw generateParseException();
261: }
262:
263: final public Token getNextToken() {
264: if (token.next != null)
265: token = token.next;
266: else
267: token = token.next = token_source.getNextToken();
268: jj_ntk = -1;
269: jj_gen++;
270: return token;
271: }
272:
273: final public Token getToken(int index) {
274: Token t = token;
275: for (int i = 0; i < index; i++) {
276: if (t.next != null)
277: t = t.next;
278: else
279: t = t.next = token_source.getNextToken();
280: }
281: return t;
282: }
283:
284: final private int jj_ntk() {
285: if ((jj_nt = token.next) == null)
286: return (jj_ntk = (token.next = token_source.getNextToken()).kind);
287: else
288: return (jj_ntk = jj_nt.kind);
289: }
290:
291: private java.util.Vector<int[]> jj_expentries = new java.util.Vector<int[]>();
292: private int[] jj_expentry;
293: private int jj_kind = -1;
294:
295: public ParseException generateParseException() {
296: jj_expentries.removeAllElements();
297: boolean[] la1tokens = new boolean[19];
298: for (int i = 0; i < 19; i++) {
299: la1tokens[i] = false;
300: }
301: if (jj_kind >= 0) {
302: la1tokens[jj_kind] = true;
303: jj_kind = -1;
304: }
305: for (int i = 0; i < 4; i++) {
306: if (jj_la1[i] == jj_gen) {
307: for (int j = 0; j < 32; j++) {
308: if ((jj_la1_0[i] & (1 << j)) != 0) {
309: la1tokens[j] = true;
310: }
311: }
312: }
313: }
314: for (int i = 0; i < 19; i++) {
315: if (la1tokens[i]) {
316: jj_expentry = new int[1];
317: jj_expentry[0] = i;
318: jj_expentries.addElement(jj_expentry);
319: }
320: }
321: int[][] exptokseq = new int[jj_expentries.size()][];
322: for (int i = 0; i < jj_expentries.size(); i++) {
323: exptokseq[i] = (int[]) jj_expentries.elementAt(i);
324: }
325: return new ParseException(token, exptokseq, tokenImage);
326: }
327:
328: final public void enable_tracing() {
329: }
330:
331: final public void disable_tracing() {
332: }
333:
334: }
|