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

A library for settings and parameterization of objects. Key features are support for data types, help for settings and parameters, annotations for ease of use. Settings and parameters both are Clonable and Serializable. More features for settings such as parsing a settingsfile, being observable, readonliness, caching etc. are available. The library contains javacc generated parsers for syntax support for properties, multi valued properties, parameterized objects and multi valued parameters.

The newest version!
/* JSONParser.java */
/* Generated by: ParserGeneratorCC: 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 ("" != null) return true;} throw new IllegalStateException ("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 ("" != null) return x;} throw new IllegalStateException ("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 ("" != null) return key;} throw new IllegalStateException ("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 ("" != null) return map;} throw new IllegalStateException ("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 ("" != null) return list;} throw new IllegalStateException ("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 ("" != null) return x;} throw new IllegalStateException ("Missing return statement in function"); } final public Object nullValue() throws ParseException { jj_consume_token(NULL); {if ("" != null) return null;} throw new IllegalStateException ("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 ("" != null) return b;} throw new IllegalStateException ("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 ("" != null) return Double.valueOf(t.image);} } else { {if ("" != null) return new BigDecimal(t.image);} } break; } case NUMBER_INTEGER:{ t = jj_consume_token(NUMBER_INTEGER); if(nativeNumbers) { {if ("" != null) return Long.valueOf(substringBefore(t.image, '.'));} } else { {if ("" != null) return new BigInteger(substringBefore(t.image, '.'));} } break; } default: jj_la1[8] = jj_gen; jj_consume_token(-1); throw new ParseException(); } throw new IllegalStateException ("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 ("" != null) return s;} throw new IllegalStateException ("Missing return statement in function"); } final public String rubySymbol() throws ParseException { jj_consume_token(RUBY_SYMBOL); {if ("" != null) return token.image.substring(1);} throw new IllegalStateException ("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 ("" != null) return "";} break; } case STRING_DOUBLE_NONEMPTY:{ jj_consume_token(STRING_DOUBLE_NONEMPTY); String image = token.image; {if ("" != null) return image.substring(1, image.length()-1);} break; } default: jj_la1[10] = jj_gen; jj_consume_token(-1); throw new ParseException(); } throw new IllegalStateException ("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 ("" != null) return "";} break; } case STRING_SINGLE_NONEMPTY:{ jj_consume_token(STRING_SINGLE_NONEMPTY); String image = token.image; {if ("" != null) return image.substring(1, image.length()-1);} break; } default: jj_la1[11] = jj_gen; jj_consume_token(-1); throw new ParseException(); } throw new IllegalStateException ("Missing return statement in function"); } final public String symbol() throws ParseException { jj_consume_token(SYMBOL); {if ("" != null) return token.image;} throw new IllegalStateException ("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 and supplied encoding * @param stream input stream * @param encoding charset to be used. May not be null. */ public JSONParser(final java.io.InputStream stream, final String encoding) { try { jj_input_stream = new JavaCharStream(stream, encoding, 1, 1); } catch(final java.io.UnsupportedEncodingException e) { throw new IllegalStateException(e); } token_source = new JSONParserTokenManager(jj_input_stream); token = new Token(); jj_nt = token_source.getNextToken(); token.next = jj_nt; jj_gen = 0; for (int i = 0; i < 12; i++) jj_la1[i] = -1; } /** * Reinitialise * @param stream input stream * @param encoding charset to be used. May not be null. */ public void ReInit(final java.io.InputStream stream, final String encoding) { try { jj_input_stream.reInit(stream, encoding, 1, 1); } catch(final java.io.UnsupportedEncodingException e) { throw new IllegalStateException(e); } token_source.ReInit(jj_input_stream); token = new Token(); jj_nt = token_source.getNextToken(); token.next = jj_nt; jj_gen = 0; for (int i = 0; i < 12; i++) jj_la1[i] = -1; } /** * Constructor with InputStream. * @param stream char stream */ public JSONParser(final 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 * @param stream char stream */ public void ReInit(final java.io.Reader stream) { if (jj_input_stream == null) { jj_input_stream = new JavaCharStream(stream, 1, 1); } else { jj_input_stream.reInit(stream, 1, 1); } if (token_source == null) { token_source = new JSONParserTokenManager(jj_input_stream); } 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. * @param tm Token manager to use */ public JSONParser(final 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 * @param tm Token manager to use */ public void ReInit(final 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(final int kind) throws ParseException { final Token oldToken = token; token = jj_nt; if (token.next != null) jj_nt = jj_nt.next; else { jj_nt.next = token_source.getNextToken(); jj_nt = jj_nt.next; } if (token.kind == kind) { jj_gen++; return token; } jj_nt = token; token = oldToken; jj_kind = kind; throw generateParseException(); } /** * @return the next Token. */ public final Token getNextToken() { token = jj_nt; if (token.next != null) jj_nt = jj_nt.next; else jj_nt = jj_nt.next = token_source.getNextToken(); jj_gen++; return token; } /** * @param index index to be retrieved * @return the specific Token. */ public final Token getToken(final int index) { Token t = token; for (int i = 0; i < index; i++) { if (t.next == null) t.next = token_source.getNextToken(); t = t.next; } return t; } private java.util.List jj_expentries = new java.util.ArrayList(); private int[] jj_expentry; private int jj_kind = -1; /** * Generate ParseException. * @return new Exception object. Never null */ 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<false. */ public final boolean trace_enabled() { return false; } /** Enable tracing. */ public final void enable_tracing() {} /** Disable tracing. */ public final void disable_tracing() {} }