All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.vectorprint.configuration.generated.parser.JSONParser Maven / Gradle / Ivy

Go to download

This project is about configuration of applications and about parameterization of Objects. This library offers annotations (and annotation processors), parsers, typing, observing changes, serialization, cloning and more when working with settings and/or object parameters. Settings and its features can be declared using an xml format. The library offers syntax support for settings and parameters in a loosely coupled manner. You are not restricted to built in syntax, you can provide your own. At runtime this library tracks keys for which a default is used because they are not found in settings. Also it tracks unused keys. You can stack features for settings such as caching, preparing keys and values, readonlyness, threadsafety, helpsupport, reading / parsing from input. You can easily develop your own features for settings.

There is a newer version: 12.2
Show newest version
/* Generated By:JavaCC: Do not edit this line. JSONParser.java */
package com.vectorprint.configuration.generated.parser;

import java.io.*;
import java.util.*;
import java.math.*;

/**
* Basic JSON parser generated by JavaCC. It consumes the input provided through the constructor when 
* {@code parseObject()}, {@code parseList()}, or {@code parse()} are called, and there is no way to directly
* reset the state.
* Set the {@code fallbackToString} property if you want to enable
* unparseable constructs (such as unqouted strings) to be handled as Strings: otherwise, they are a parse
* error.
*
* 

* * This class makes no pretenses towards being threadsafe. */ public class JSONParser implements JSONParserConstants { private boolean nativeNumbers = false; public JSONParser(String input) { this(new StringReader(input)); } /** * Parses a JSON object into a Java {@code Map}. */ public LinkedHashMap parseObject() throws ParseException { LinkedHashMap toReturn = object(); if(!ensureEOF()) throw new IllegalStateException("Expected EOF, but still had content to parse"); return toReturn; } /** * Parses a JSON array into a Java {@code List}. */ public ArrayList parseArray() throws ParseException { ArrayList toReturn = list(); if(!ensureEOF()) throw new IllegalStateException("Expected EOF, but still had content to parse"); return toReturn; } /** * Parses any JSON-parseable object, returning the value. */ public Object parse() throws ParseException { Object toReturn = anything(); if(!ensureEOF()) throw new IllegalStateException("Expected EOF, but still had content to parse"); return toReturn; } private static String substringBefore(String str, char delim) { int pos = str.indexOf(delim); if(pos == -1) return str; return str.substring(0, pos); } public void setNativeNumbers(boolean value) { this.nativeNumbers = value; } public boolean getNativeNumbers() { return this.nativeNumbers; } final public boolean ensureEOF() throws ParseException { jj_consume_token(0); {if (true) return true;} throw new Error("Missing return statement in function"); } final public Object anything() throws ParseException { Object x; switch (jj_nt.kind) { case BRACE_OPEN: x = object(); break; case BRACKET_OPEN: x = list(); break; case NUMBER_INTEGER: case NUMBER_DECIMAL: case TRUE: case FALSE: case NULL: case STRING_SINGLE_EMPTY: case STRING_DOUBLE_EMPTY: case STRING_SINGLE_NONEMPTY: case STRING_DOUBLE_NONEMPTY: case RUBY_SYMBOL: x = value(); break; default: jj_la1[0] = jj_gen; jj_consume_token(-1); throw new ParseException(); } {if (true) return x;} throw new Error("Missing return statement in function"); } final public String objectKey() throws ParseException { Object o; String key; switch (jj_nt.kind) { case STRING_SINGLE_EMPTY: case STRING_DOUBLE_EMPTY: case STRING_SINGLE_NONEMPTY: case STRING_DOUBLE_NONEMPTY: case RUBY_SYMBOL: key = string(); break; case SYMBOL: key = symbol(); break; case NULL: nullValue(); key = null; break; case NUMBER_INTEGER: case NUMBER_DECIMAL: case TRUE: case FALSE: switch (jj_nt.kind) { case TRUE: case FALSE: o = booleanValue(); break; case NUMBER_INTEGER: case NUMBER_DECIMAL: o = number(); break; default: jj_la1[1] = jj_gen; jj_consume_token(-1); throw new ParseException(); } key = o.toString(); break; default: jj_la1[2] = jj_gen; jj_consume_token(-1); throw new ParseException(); } {if (true) return key;} throw new Error("Missing return statement in function"); } final public LinkedHashMap object() throws ParseException { final LinkedHashMap map = new LinkedHashMap(); String key; Object value; jj_consume_token(BRACE_OPEN); switch (jj_nt.kind) { case NUMBER_INTEGER: case NUMBER_DECIMAL: case TRUE: case FALSE: case NULL: case STRING_SINGLE_EMPTY: case STRING_DOUBLE_EMPTY: case STRING_SINGLE_NONEMPTY: case STRING_DOUBLE_NONEMPTY: case RUBY_SYMBOL: case SYMBOL: key = objectKey(); jj_consume_token(COLON); value = anything(); map.put(key, value); key = null; value = null; label_1: while (true) { switch (jj_nt.kind) { case COMMA: ; break; default: jj_la1[3] = jj_gen; break label_1; } jj_consume_token(COMMA); key = objectKey(); jj_consume_token(COLON); value = anything(); map.put(key, value); key = null; value = null; } break; default: jj_la1[4] = jj_gen; ; } jj_consume_token(BRACE_CLOSE); {if (true) return map;} throw new Error("Missing return statement in function"); } final public ArrayList list() throws ParseException { final ArrayList list = new ArrayList(); Object value; jj_consume_token(BRACKET_OPEN); value = anything(); list.add(value); value = null; label_2: while (true) { switch (jj_nt.kind) { case COMMA: ; break; default: jj_la1[5] = jj_gen; break label_2; } jj_consume_token(COMMA); value = anything(); list.add(value); value = null; } jj_consume_token(BRACKET_CLOSE); list.trimToSize(); {if (true) return list;} throw new Error("Missing return statement in function"); } final public Object value() throws ParseException { Object x; switch (jj_nt.kind) { case STRING_SINGLE_EMPTY: case STRING_DOUBLE_EMPTY: case STRING_SINGLE_NONEMPTY: case STRING_DOUBLE_NONEMPTY: case RUBY_SYMBOL: x = string(); break; case NUMBER_INTEGER: case NUMBER_DECIMAL: x = number(); break; case TRUE: case FALSE: x = booleanValue(); break; case NULL: x = nullValue(); break; default: jj_la1[6] = jj_gen; jj_consume_token(-1); throw new ParseException(); } {if (true) return x;} throw new Error("Missing return statement in function"); } final public Object nullValue() throws ParseException { jj_consume_token(NULL); {if (true) return null;} throw new Error("Missing return statement in function"); } final public Boolean booleanValue() throws ParseException { Boolean b; switch (jj_nt.kind) { case TRUE: jj_consume_token(TRUE); b = Boolean.TRUE; break; case FALSE: jj_consume_token(FALSE); b = Boolean.FALSE; break; default: jj_la1[7] = jj_gen; jj_consume_token(-1); throw new ParseException(); } {if (true) return b;} throw new Error("Missing return statement in function"); } final public Number number() throws ParseException { Token t; switch (jj_nt.kind) { case NUMBER_DECIMAL: t = jj_consume_token(NUMBER_DECIMAL); if(nativeNumbers) { {if (true) return new Double(t.image);} } else { {if (true) return new BigDecimal(t.image);} } break; case NUMBER_INTEGER: t = jj_consume_token(NUMBER_INTEGER); if(nativeNumbers) { {if (true) return new Long(substringBefore(t.image, '.'));} } else { {if (true) return new BigInteger(substringBefore(t.image, '.'));} } break; default: jj_la1[8] = jj_gen; jj_consume_token(-1); throw new ParseException(); } throw new Error("Missing return statement in function"); } final public String string() throws ParseException { String s; switch (jj_nt.kind) { case STRING_DOUBLE_EMPTY: case STRING_DOUBLE_NONEMPTY: s = doubleQuoteString(); break; case STRING_SINGLE_EMPTY: case STRING_SINGLE_NONEMPTY: s = singleQuoteString(); break; case RUBY_SYMBOL: s = rubySymbol(); break; default: jj_la1[9] = jj_gen; jj_consume_token(-1); throw new ParseException(); } {if (true) return s;} throw new Error("Missing return statement in function"); } final public String rubySymbol() throws ParseException { jj_consume_token(RUBY_SYMBOL); {if (true) return token.image.substring(1);} throw new Error("Missing return statement in function"); } final public String doubleQuoteString() throws ParseException { switch (jj_nt.kind) { case STRING_DOUBLE_EMPTY: jj_consume_token(STRING_DOUBLE_EMPTY); {if (true) return "";} break; case STRING_DOUBLE_NONEMPTY: jj_consume_token(STRING_DOUBLE_NONEMPTY); String image = token.image; {if (true) return image.substring(1, image.length()-1);} break; default: jj_la1[10] = jj_gen; jj_consume_token(-1); throw new ParseException(); } throw new Error("Missing return statement in function"); } final public String singleQuoteString() throws ParseException { switch (jj_nt.kind) { case STRING_SINGLE_EMPTY: jj_consume_token(STRING_SINGLE_EMPTY); {if (true) return "";} break; case STRING_SINGLE_NONEMPTY: jj_consume_token(STRING_SINGLE_NONEMPTY); String image = token.image; {if (true) return image.substring(1, image.length()-1);} break; default: jj_la1[11] = jj_gen; jj_consume_token(-1); throw new ParseException(); } throw new Error("Missing return statement in function"); } final public String symbol() throws ParseException { jj_consume_token(SYMBOL); {if (true) return token.image;} throw new Error("Missing return statement in function"); } /** Generated Token Manager. */ public JSONParserTokenManager token_source; JavaCharStream jj_input_stream; /** Current token. */ public Token token; /** Next token. */ public Token jj_nt; private int jj_gen; final private int[] jj_la1 = new int[12]; static private int[] jj_la1_0; static { jj_la1_init_0(); } private static void jj_la1_init_0() { jj_la1_0 = new int[] {0x1ccf8480,0x78000,0x3ccf8000,0x40,0x3ccf8000,0x40,0x1ccf8000,0x60000,0x18000,0x1cc00000,0x8800000,0x4400000,}; } /** Constructor with InputStream. */ public JSONParser(java.io.InputStream stream) { this(stream, null); } /** Constructor with InputStream and supplied encoding */ public JSONParser(java.io.InputStream stream, String encoding) { try { jj_input_stream = new JavaCharStream(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); } token_source = new JSONParserTokenManager(jj_input_stream); token = new Token(); token.next = jj_nt = token_source.getNextToken(); jj_gen = 0; for (int i = 0; i < 12; i++) jj_la1[i] = -1; } /** Reinitialise. */ public void ReInit(java.io.InputStream stream) { ReInit(stream, null); } /** Reinitialise. */ public void ReInit(java.io.InputStream stream, String encoding) { try { jj_input_stream.ReInit(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); } token_source.ReInit(jj_input_stream); token = new Token(); token.next = jj_nt = token_source.getNextToken(); jj_gen = 0; for (int i = 0; i < 12; i++) jj_la1[i] = -1; } /** Constructor. */ public JSONParser(java.io.Reader stream) { jj_input_stream = new JavaCharStream(stream, 1, 1); token_source = new JSONParserTokenManager(jj_input_stream); token = new Token(); token.next = jj_nt = token_source.getNextToken(); jj_gen = 0; for (int i = 0; i < 12; i++) jj_la1[i] = -1; } /** Reinitialise. */ public void ReInit(java.io.Reader stream) { jj_input_stream.ReInit(stream, 1, 1); token_source.ReInit(jj_input_stream); token = new Token(); token.next = jj_nt = token_source.getNextToken(); jj_gen = 0; for (int i = 0; i < 12; i++) jj_la1[i] = -1; } /** Constructor with generated Token Manager. */ public JSONParser(JSONParserTokenManager tm) { token_source = tm; token = new Token(); token.next = jj_nt = token_source.getNextToken(); jj_gen = 0; for (int i = 0; i < 12; i++) jj_la1[i] = -1; } /** Reinitialise. */ public void ReInit(JSONParserTokenManager tm) { token_source = tm; token = new Token(); token.next = jj_nt = token_source.getNextToken(); jj_gen = 0; for (int i = 0; i < 12; i++) jj_la1[i] = -1; } private Token jj_consume_token(int kind) throws ParseException { Token oldToken = token; if ((token = jj_nt).next != null) jj_nt = jj_nt.next; else jj_nt = jj_nt.next = token_source.getNextToken(); if (token.kind == kind) { jj_gen++; return token; } jj_nt = token; token = oldToken; jj_kind = kind; throw generateParseException(); } /** Get the next Token. */ final public Token getNextToken() { if ((token = jj_nt).next != null) jj_nt = jj_nt.next; else jj_nt = jj_nt.next = token_source.getNextToken(); jj_gen++; return token; } /** Get the specific Token. */ final public Token getToken(int index) { Token t = token; for (int i = 0; i < index; i++) { if (t.next != null) t = t.next; else t = t.next = token_source.getNextToken(); } return t; } private java.util.List jj_expentries = new java.util.ArrayList(); private int[] jj_expentry; private int jj_kind = -1; /** Generate ParseException. */ public ParseException generateParseException() { jj_expentries.clear(); boolean[] la1tokens = new boolean[30]; if (jj_kind >= 0) { la1tokens[jj_kind] = true; jj_kind = -1; } for (int i = 0; i < 12; i++) { if (jj_la1[i] == jj_gen) { for (int j = 0; j < 32; j++) { if ((jj_la1_0[i] & (1<