
com.sindicetech.siren.analysis.ExtendedJsonScanner Maven / Gradle / Ivy
/* The following code was generated by JFlex 1.4.3 on 11/17/14 12:41 PM */
/**
* Copyright (c) 2014, Sindice Limited. All Rights Reserved.
*
* This file is part of the SIREn project.
*
* SIREn is a free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* SIREn is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this program. If not, see .
*/
package com.sindicetech.siren.analysis;
import static com.sindicetech.siren.analysis.ExtendedJsonTokenizer.*;
import java.util.Stack;
import java.util.Arrays;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.apache.lucene.util.IntsRef;
import com.sindicetech.siren.util.ArrayUtils;
import com.sindicetech.siren.util.XSDDatatype;
import com.sindicetech.siren.util.JSONDatatype;
/**
* A scanner for JSON document that maps a JSON tree to a node-based tree model using a depth-first traversal approach.
*
* This scanner creates one token per attribute, value and object. Nested arrays are not flattened and will be mapped
* to an empty node.
*/
public final class ExtendedJsonScanner {
/** This character denotes the end of file */
public static final int YYEOF = -1;
/** initial size of the lookahead buffer */
private static final int ZZ_BUFFERSIZE = 16384;
/** lexical states */
public static final int sDATATYPE_STRING_VALUE = 14;
public static final int sARRAY_COMMA = 12;
public static final int sARRAY_OBJECT = 18;
public static final int sOBJECT = 4;
public static final int sARRAY = 10;
public static final int sFIELD_STRING = 8;
public static final int YYINITIAL = 0;
public static final int sSTRING = 2;
public static final int sFIELD = 6;
public static final int sDATATYPE_LABEL = 16;
/**
* ZZ_LEXSTATE[l] is the state in the DFA for the lexical state l
* ZZ_LEXSTATE[l+1] is the state in the DFA for the lexical state l
* at the beginning of a line
* l is of the form l = 2*k, k a non negative integer
*/
private static final int ZZ_LEXSTATE[] = {
0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7,
8, 8, 9, 9
};
/**
* Translates characters to character classes
*/
private static final String ZZ_CMAP_PACKED =
"\11\0\1\22\1\21\1\0\1\22\1\20\22\0\1\22\1\0\1\26"+
"\10\0\1\15\1\27\1\11\1\13\1\0\12\12\1\34\6\0\4\40"+
"\1\14\1\40\24\0\1\25\1\37\1\36\1\0\1\30\1\0\1\6"+
"\2\40\1\31\1\4\1\5\5\0\1\7\1\0\1\17\1\0\1\33"+
"\1\0\1\2\1\10\1\1\1\3\1\35\2\0\1\32\1\0\1\23"+
"\1\16\1\24\uff82\0";
/**
* Translates characters to character classes
*/
private static final char [] ZZ_CMAP = zzUnpackCMap(ZZ_CMAP_PACKED);
/**
* Translates DFA states to action switch labels.
*/
private static final int [] ZZ_ACTION = zzUnpackAction();
private static final String ZZ_ACTION_PACKED_0 =
"\12\0\1\1\2\2\1\3\1\4\1\5\1\0\4\6"+
"\1\7\1\6\1\10\1\11\1\12\1\13\1\14\1\15"+
"\1\16\1\0\4\17\1\20\1\17\1\21\1\22\1\23"+
"\1\24\1\25\1\26\2\2\1\27\1\30\2\2\1\31"+
"\1\0\2\4\1\32\6\0\1\33\5\0\2\30\1\34"+
"\4\0\1\7\1\35\6\0\1\20\1\36\3\0\1\37"+
"\2\0\1\40\2\0\1\41\2\0\1\42\2\0\1\43"+
"\1\0\1\35\2\0\1\44\1\0\1\36\1\0\1\45"+
"\2\0\1\46\11\0\1\47\2\0\1\50";
private static int [] zzUnpackAction() {
int [] result = new int[124];
int offset = 0;
offset = zzUnpackAction(ZZ_ACTION_PACKED_0, offset, result);
return result;
}
private static int zzUnpackAction(String packed, int offset, int [] result) {
int i = 0; /* index in packed string */
int j = offset; /* index in unpacked array */
int l = packed.length();
while (i < l) {
int count = packed.charAt(i++);
int value = packed.charAt(i++);
do result[j++] = value; while (--count > 0);
}
return j;
}
/**
* Translates a state to a row index in the transition table
*/
private static final int [] ZZ_ROWMAP = zzUnpackRowMap();
private static final String ZZ_ROWMAP_PACKED_0 =
"\0\0\0\41\0\102\0\143\0\204\0\245\0\306\0\347"+
"\0\u0108\0\u0129\0\u0129\0\u014a\0\u0129\0\u0129\0\u016b\0\u0129"+
"\0\u018c\0\u0129\0\u01ad\0\u01ce\0\u01ef\0\u0210\0\u0231\0\u0129"+
"\0\u0129\0\u0129\0\u0129\0\u0129\0\u0129\0\u0252\0\u0273\0\u0129"+
"\0\u0294\0\u02b5\0\u02d6\0\u02f7\0\u0318\0\u0129\0\u0129\0\u0129"+
"\0\u0129\0\u0129\0\u0129\0\u0339\0\u016b\0\u0129\0\u035a\0\u037b"+
"\0\u035a\0\u0129\0\u039c\0\u0129\0\u03bd\0\u0129\0\u03de\0\u03ff"+
"\0\u0420\0\u0441\0\u0462\0\u0483\0\u0129\0\u04a4\0\u04c5\0\u04e6"+
"\0\u0507\0\u0528\0\u0129\0\u0549\0\u0129\0\u056a\0\u058b\0\u05ac"+
"\0\u05cd\0\u05cd\0\u05ee\0\u060f\0\u0630\0\u0651\0\u0672\0\u0693"+
"\0\u06b4\0\u06b4\0\u06d5\0\u06f6\0\u0717\0\u0738\0\u0129\0\u0759"+
"\0\u077a\0\u0129\0\u079b\0\u07bc\0\u0129\0\u07dd\0\u07fe\0\u0129"+
"\0\u081f\0\u0840\0\u0129\0\u0861\0\u0861\0\u0882\0\u08a3\0\u0129"+
"\0\u08c4\0\u08c4\0\u08e5\0\u0129\0\u0906\0\u0927\0\u0129\0\u0948"+
"\0\u0969\0\u098a\0\u09ab\0\u09cc\0\u09ed\0\u0a0e\0\u0a2f\0\u0a50"+
"\0\u0129\0\u0a71\0\u0a92\0\u0129";
private static int [] zzUnpackRowMap() {
int [] result = new int[124];
int offset = 0;
offset = zzUnpackRowMap(ZZ_ROWMAP_PACKED_0, offset, result);
return result;
}
private static int zzUnpackRowMap(String packed, int offset, int [] result) {
int i = 0; /* index in packed string */
int j = offset; /* index in unpacked array */
int l = packed.length();
while (i < l) {
int high = packed.charAt(i++) << 16;
result[j++] = high | packed.charAt(i++);
}
return j;
}
/**
* The transition table of the DFA
*/
private static final int [] ZZ_TRANS = zzUnpackTrans();
private static final String ZZ_TRANS_PACKED_0 =
"\20\13\1\14\2\15\1\16\15\13\26\17\1\20\10\17"+
"\1\21\1\17\1\22\1\23\3\22\1\24\3\22\1\25"+
"\1\26\4\22\1\27\1\14\2\15\1\30\1\31\1\32"+
"\1\33\1\34\11\22\20\35\1\14\2\15\1\35\1\31"+
"\1\35\1\36\12\35\26\17\1\37\10\17\1\21\1\17"+
"\1\40\1\41\3\40\1\42\3\40\1\43\1\44\4\40"+
"\1\45\1\14\2\15\1\46\1\40\1\47\1\50\7\40"+
"\1\51\2\40\20\52\1\14\2\15\4\52\1\53\6\52"+
"\1\51\2\52\20\17\1\54\2\55\3\17\1\56\10\17"+
"\1\21\1\17\20\57\1\60\2\61\3\57\1\62\10\57"+
"\1\63\1\57\62\0\1\15\17\0\26\17\1\0\10\17"+
"\1\0\1\17\3\64\1\65\15\64\1\0\4\64\1\66"+
"\12\64\2\0\1\67\44\0\1\70\44\0\1\26\32\0"+
"\1\71\5\0\1\26\1\72\1\71\1\0\1\71\25\0"+
"\1\73\65\0\1\74\30\0\3\37\11\0\1\75\6\0"+
"\1\76\44\0\1\77\44\0\1\44\32\0\1\100\5\0"+
"\1\44\1\101\1\100\1\0\1\100\25\0\1\102\35\0"+
"\21\17\1\55\4\17\1\0\10\17\1\0\1\17\26\57"+
"\1\0\10\57\1\0\22\57\1\61\4\57\1\0\10\57"+
"\1\0\1\57\3\103\1\104\15\103\1\0\4\103\1\105"+
"\12\103\4\0\3\106\3\0\1\106\1\0\1\106\14\0"+
"\1\106\6\0\1\106\3\0\1\107\44\0\1\110\42\0"+
"\1\111\1\112\2\0\2\111\34\0\1\113\35\0\1\114"+
"\62\0\1\115\3\0\1\116\6\0\1\117\44\0\1\120"+
"\42\0\1\121\1\122\2\0\2\121\34\0\1\123\35\0"+
"\1\124\35\0\3\125\3\0\1\125\1\0\1\125\14\0"+
"\1\125\6\0\1\125\4\0\3\126\3\0\1\126\1\0"+
"\1\126\14\0\1\126\6\0\1\126\4\0\1\127\44\0"+
"\1\130\42\0\1\112\32\0\1\131\5\0\1\113\1\0"+
"\1\131\1\0\1\131\31\0\1\132\37\0\1\133\40\0"+
"\1\134\36\0\1\135\44\0\1\136\42\0\1\122\32\0"+
"\1\137\5\0\1\123\1\0\1\137\1\0\1\137\31\0"+
"\1\140\35\0\3\141\3\0\1\141\1\0\1\141\14\0"+
"\1\141\6\0\1\141\4\0\3\142\3\0\1\142\1\0"+
"\1\142\14\0\1\142\6\0\1\142\4\0\1\143\45\0"+
"\1\144\1\145\2\0\2\144\23\0\1\146\46\0\1\147"+
"\35\0\1\150\45\0\1\151\1\152\2\0\2\151\26\0"+
"\3\153\3\0\1\153\1\0\1\153\14\0\1\153\6\0"+
"\1\153\4\0\3\154\3\0\1\154\1\0\1\154\14\0"+
"\1\154\6\0\1\154\12\0\1\145\34\0\1\155\35\0"+
"\1\156\47\0\1\152\32\0\3\157\3\0\1\157\1\0"+
"\1\157\14\0\1\157\6\0\1\157\1\0\1\160\43\0"+
"\1\161\66\0\1\162\36\0\1\163\43\0\1\164\33\0"+
"\1\165\16\0\1\166\54\0\3\165\11\0\1\167\34\0"+
"\1\170\30\0\3\167\3\0\1\171\40\0\1\172\32\0"+
"\3\172\11\0\1\173\24\0\3\173\3\0\1\174\12\0";
private static int [] zzUnpackTrans() {
int [] result = new int[2739];
int offset = 0;
offset = zzUnpackTrans(ZZ_TRANS_PACKED_0, offset, result);
return result;
}
private static int zzUnpackTrans(String packed, int offset, int [] result) {
int i = 0; /* index in packed string */
int j = offset; /* index in unpacked array */
int l = packed.length();
while (i < l) {
int count = packed.charAt(i++);
int value = packed.charAt(i++);
value--;
do result[j++] = value; while (--count > 0);
}
return j;
}
/* error codes */
private static final int ZZ_UNKNOWN_ERROR = 0;
private static final int ZZ_NO_MATCH = 1;
private static final int ZZ_PUSHBACK_2BIG = 2;
/* error messages for the codes above */
private static final String ZZ_ERROR_MSG[] = {
"Unkown internal scanner error",
"Error: could not match input",
"Error: pushback value was too large"
};
/**
* ZZ_ATTRIBUTE[aState] contains the attributes of state aState
*/
private static final int [] ZZ_ATTRIBUTE = zzUnpackAttribute();
private static final String ZZ_ATTRIBUTE_PACKED_0 =
"\11\0\1\10\1\11\1\1\2\11\1\1\1\11\1\0"+
"\1\11\5\1\6\11\1\1\1\0\1\11\5\1\6\11"+
"\2\1\1\11\3\1\1\11\1\0\1\11\1\1\1\11"+
"\6\0\1\11\5\0\1\11\1\1\1\11\4\0\2\1"+
"\6\0\2\1\3\0\1\11\2\0\1\11\2\0\1\11"+
"\2\0\1\11\2\0\1\11\1\0\1\1\2\0\1\11"+
"\1\0\1\1\1\0\1\11\2\0\1\11\11\0\1\11"+
"\2\0\1\11";
private static int [] zzUnpackAttribute() {
int [] result = new int[124];
int offset = 0;
offset = zzUnpackAttribute(ZZ_ATTRIBUTE_PACKED_0, offset, result);
return result;
}
private static int zzUnpackAttribute(String packed, int offset, int [] result) {
int i = 0; /* index in packed string */
int j = offset; /* index in unpacked array */
int l = packed.length();
while (i < l) {
int count = packed.charAt(i++);
int value = packed.charAt(i++);
do result[j++] = value; while (--count > 0);
}
return j;
}
/** the input device */
private java.io.Reader zzReader;
/** the current state of the DFA */
private int zzState;
/** the current lexical state */
private int zzLexicalState = YYINITIAL;
/** this buffer contains the current text to be matched and is
the source of the yytext() string */
private char zzBuffer[] = new char[ZZ_BUFFERSIZE];
/** the textposition at the last accepting state */
private int zzMarkedPos;
/** the current text position in the buffer */
private int zzCurrentPos;
/** startRead marks the beginning of the yytext() string in the buffer */
private int zzStartRead;
/** endRead marks the last character in the buffer, that has been read
from input */
private int zzEndRead;
/** number of newlines encountered up to the start of the matched text */
private int yyline;
/** the number of characters up to the start of the matched text */
private int yychar;
/**
* the number of characters from the last newline up to the start of the
* matched text
*/
private int yycolumn;
/**
* zzAtBOL == true <=> the scanner is currently at the beginning of a line
*/
private boolean zzAtBOL = true;
/** zzAtEOF == true <=> the scanner is at the EOF */
private boolean zzAtEOF;
/** denotes if the user-EOF-code has already been executed */
private boolean zzEOFDone;
/* user code: */
/**
* Buffer containing literal or number value
*/
protected final StringBuilder buffer = new StringBuilder();
// NODE IDENTIFIERS
/**
* The size of the path buffer
*/
protected final static int BUFFER_SIZE = 1024;
/**
* The path to a node
*/
protected final IntsRef nodePath = new IntsRef(BUFFER_SIZE);
/**
* Stack of lexical states
*/
protected final Stack states = new Stack();
/**
* Indicates if a leaf node, i.e., a literal, a number, null, or a boolean,
* was encountered, in which case it needs to be closed, either in the COMMA
* state, or in the closing curly bracket.
*/
protected boolean openLeafNode = false;
/**
* Indicates how many nested objects there are.
* A nested object implies a blank node.
*/
protected int nestedObjects = 0;
// DATATYPES
/**
* Datatype representing xsd:string
*/
protected static final char[] XSD_STRING = XSDDatatype.XSD_STRING.toCharArray();
/**
* Datatype representing json:field
*/
protected static final char[] JSON_FIELD = JSONDatatype.JSON_FIELD.toCharArray();
/**
* Datatype representing xsd:double
*/
protected static final char[] XSD_DOUBLE = XSDDatatype.XSD_DOUBLE.toCharArray();
/**
* Datatype representing xsd:long
*/
protected static final char[] XSD_LONG = XSDDatatype.XSD_LONG.toCharArray();
/**
* Datatype representing xsd:boolean
*/
protected static final char[] XSD_BOOLEAN = XSDDatatype.XSD_BOOLEAN.toCharArray();
/**
* Buffer containing the datatype label
*/
protected final StringBuilder dtLabel = new StringBuilder();
/**
* A reference to the current datatype URI
*/
protected char[] datatype;
// lexical states for datatypes
protected final static int DATATYPE_OBJ_OFF = 0;
protected final static int DATATYPE_OBJ_ON = 1;
protected final static int DATATYPE_OBJ_JUNK = 2;
protected final static int DATATYPE_OBJ_ERROR = 3;
protected final static int DATATYPE_OBJ_LABEL = 4;
protected final static int DATATYPE_OBJ_VALUE = 8;
protected final static int DATATYPE_OBJ_OK = 13;
/**
* The current lexical state for a datatype object
*/
protected int datatypeObject = DATATYPE_OBJ_OFF;
/**
* Return the datatype URI.
*/
public final char[] getDatatypeURI() {
return datatype;
}
public final int yychar() {
return yychar;
}
/**
* Fills Lucene TermAttribute with the current string buffer.
*/
public final void getLiteralText(CharTermAttribute t) {
char[] chars = new char[buffer.length()];
buffer.getChars(0, buffer.length(), chars, 0);
t.copyBuffer(chars, 0, chars.length);
}
/**
* Returns the node path of the token
*/
public final IntsRef getNodePath() {
return nodePath;
}
/**
* Initialise inner variables
*/
private void reset() {
states.clear();
Arrays.fill(nodePath.ints, -1);
nodePath.offset = 0;
nodePath.length = 1;
openLeafNode = false;
datatype = null;
nestedObjects = 0;
datatypeObject = DATATYPE_OBJ_OFF;
}
/**
* Add an object to the current node path
*/
private void incrNodeObjectPath() {
ArrayUtils.growAndCopy(nodePath, nodePath.length + 1);
nodePath.length++;
// Initialise node
setLastNode(-1);
}
private void initDatatypeObject() {
// Initialise the datatype data, this is possibly a Datatype object
datatype = null;
datatypeObject = DATATYPE_OBJ_OFF;
buffer.setLength(0);
}
/**
* Decrement the tree level of 1.
*/
private void decrNodeObjectPath() {
nodePath.length--;
}
/** Update the path of the current values of the current object node */
private void setLastNode(int val) {
nodePath.ints[nodePath.length - 1] = val;
}
/** Update the path of the current values of the current object node */
private void addToLastNode(int val) {
nodePath.ints[nodePath.length - 1] += val;
}
/**
* Return the {@link #NUMBER} token type.
*/
private int processNumber() {
buffer.setLength(0);
buffer.append(yytext());
return NUMBER;
}
/**
* Helper method to print an error while scanning a JSON
* document with line and column information.
*/
private String errorMessage(String msg) {
return "Error parsing JSON document at [line=" + yyline + ", column=" + yycolumn + "]: " + msg;
}
/**
* Close the JSON object:
* - decrement the tree level of 1;
* - decrement the tree level of 1 one more time if the last value was a leaf;
* - decrement the tree level of 1 one more time if the object was nested into another one (i.e., the blank node),
* but exclude nested objects in arrays; and
* - remove {@link #sOBJECT} from the stack of states.
*/
private void closeObject() {
final int state = states.pop();
if (state != sOBJECT && state != sARRAY_OBJECT) {
throw new IllegalStateException(errorMessage("Expected '}', got " + yychar()));
}
decrNodeObjectPath();
if (openLeafNode) { // unclosed entry to a leaf node
decrNodeObjectPath();
openLeafNode = false;
}
// this curly bracket closes a nested object
// this requires an additional call to decrNodeObjectPath()
// since nested objects have implicitly a blank node.
// This excludes the nested objects in array (i.e., sARRAY_OBJECT)
if (state == sOBJECT && nestedObjects > 0) {
nestedObjects--;
decrNodeObjectPath();
}
}
/**
* Closes the JSON object and further checks if the node path should be updated.
* It returns a {@link #LITERAL}, which is the value associated to the field "_value_"
* with the datatype equal to the value associated to the field "_datatype_".
*/
private int closeDatatypeObject() {
closeObject();
// when the states stack is empty, I am the root of the JSON tree
if (states.empty()) {
throw new IllegalStateException(errorMessage("A datatype object at the" +
" root of the JSON document is not possible."));
}
if (states.peek() == sOBJECT) {
incrNodeObjectPath();
setLastNode(0);
}
return LITERAL;
}
/**
* Returns the current state. If the state of the top of the stack is a {@link #sARRAY_OBJECT},
* it returns a {@link #sOBJECT} instead.
*/
private int getCurrentState() {
int state = states.peek();
return state == sARRAY_OBJECT ? sOBJECT : state;
}
/**
* Creates a new scanner
* There is also a java.io.InputStream version of this constructor.
*
* @param in the java.io.Reader to read input from.
*/
public ExtendedJsonScanner(java.io.Reader in) {
this.zzReader = in;
}
/**
* Creates a new scanner.
* There is also java.io.Reader version of this constructor.
*
* @param in the java.io.Inputstream to read input from.
*/
public ExtendedJsonScanner(java.io.InputStream in) {
this(new java.io.InputStreamReader(in));
}
/**
* Unpacks the compressed character translation table.
*
* @param packed the packed character translation table
* @return the unpacked character translation table
*/
private static char [] zzUnpackCMap(String packed) {
char [] map = new char[0x10000];
int i = 0; /* index in packed string */
int j = 0; /* index in unpacked array */
while (i < 106) {
int count = packed.charAt(i++);
char value = packed.charAt(i++);
do map[j++] = value; while (--count > 0);
}
return map;
}
/**
* Refills the input buffer.
*
* @return false
, iff there was new input.
*
* @exception java.io.IOException if any I/O-Error occurs
*/
private boolean zzRefill() throws java.io.IOException {
/* first: make room (if you can) */
if (zzStartRead > 0) {
System.arraycopy(zzBuffer, zzStartRead,
zzBuffer, 0,
zzEndRead-zzStartRead);
/* translate stored positions */
zzEndRead-= zzStartRead;
zzCurrentPos-= zzStartRead;
zzMarkedPos-= zzStartRead;
zzStartRead = 0;
}
/* is the buffer big enough? */
if (zzCurrentPos >= zzBuffer.length) {
/* if not: blow it up */
char newBuffer[] = new char[zzCurrentPos*2];
System.arraycopy(zzBuffer, 0, newBuffer, 0, zzBuffer.length);
zzBuffer = newBuffer;
}
/* finally: fill the buffer with new input */
int numRead = zzReader.read(zzBuffer, zzEndRead,
zzBuffer.length-zzEndRead);
if (numRead > 0) {
zzEndRead+= numRead;
return false;
}
// unlikely but not impossible: read 0 characters, but not at end of stream
if (numRead == 0) {
int c = zzReader.read();
if (c == -1) {
return true;
} else {
zzBuffer[zzEndRead++] = (char) c;
return false;
}
}
// numRead < 0
return true;
}
/**
* Closes the input stream.
*/
public final void yyclose() throws java.io.IOException {
zzAtEOF = true; /* indicate end of file */
zzEndRead = zzStartRead; /* invalidate buffer */
if (zzReader != null)
zzReader.close();
}
/**
* Resets the scanner to read from a new input stream.
* Does not close the old reader.
*
* All internal variables are reset, the old input stream
* cannot be reused (internal buffer is discarded and lost).
* Lexical state is set to ZZ_INITIAL.
*
* @param reader the new input stream
*/
public final void yyreset(java.io.Reader reader) {
zzReader = reader;
zzAtBOL = true;
zzAtEOF = false;
zzEOFDone = false;
zzEndRead = zzStartRead = 0;
zzCurrentPos = zzMarkedPos = 0;
yyline = yychar = yycolumn = 0;
zzLexicalState = YYINITIAL;
}
/**
* Returns the current lexical state.
*/
public final int yystate() {
return zzLexicalState;
}
/**
* Enters a new lexical state
*
* @param newState the new lexical state
*/
public final void yybegin(int newState) {
zzLexicalState = newState;
}
/**
* Returns the text matched by the current regular expression.
*/
public final String yytext() {
return new String( zzBuffer, zzStartRead, zzMarkedPos-zzStartRead );
}
/**
* Returns the character at position pos from the
* matched text.
*
* It is equivalent to yytext().charAt(pos), but faster
*
* @param pos the position of the character to fetch.
* A value from 0 to yylength()-1.
*
* @return the character at position pos
*/
public final char yycharat(int pos) {
return zzBuffer[zzStartRead+pos];
}
/**
* Returns the length of the matched text region.
*/
public final int yylength() {
return zzMarkedPos-zzStartRead;
}
/**
* Reports an error that occured while scanning.
*
* In a wellformed scanner (no or only correct usage of
* yypushback(int) and a match-all fallback rule) this method
* will only be called with things that "Can't Possibly Happen".
* If this method is called, something is seriously wrong
* (e.g. a JFlex bug producing a faulty scanner etc.).
*
* Usual syntax/scanner level error handling should be done
* in error fallback rules.
*
* @param errorCode the code of the errormessage to display
*/
private void zzScanError(int errorCode) {
String message;
try {
message = ZZ_ERROR_MSG[errorCode];
}
catch (ArrayIndexOutOfBoundsException e) {
message = ZZ_ERROR_MSG[ZZ_UNKNOWN_ERROR];
}
throw new Error(message);
}
/**
* Pushes the specified amount of characters back into the input stream.
*
* They will be read again by then next call of the scanning method
*
* @param number the number of characters to be read again.
* This number must not be greater than yylength()!
*/
public void yypushback(int number) {
if ( number > yylength() )
zzScanError(ZZ_PUSHBACK_2BIG);
zzMarkedPos -= number;
}
/**
* Resumes scanning until the next regular expression is matched,
* the end of input is encountered or an I/O-Error occurs.
*
* @return the next token
* @exception java.io.IOException if any I/O-Error occurs
*/
public int getNextToken() throws java.io.IOException, java.lang.IllegalStateException {
int zzInput;
int zzAction;
// cached fields:
int zzCurrentPosL;
int zzMarkedPosL;
int zzEndReadL = zzEndRead;
char [] zzBufferL = zzBuffer;
char [] zzCMapL = ZZ_CMAP;
int [] zzTransL = ZZ_TRANS;
int [] zzRowMapL = ZZ_ROWMAP;
int [] zzAttrL = ZZ_ATTRIBUTE;
while (true) {
zzMarkedPosL = zzMarkedPos;
yychar+= zzMarkedPosL-zzStartRead;
zzAction = -1;
zzCurrentPosL = zzCurrentPos = zzStartRead = zzMarkedPosL;
zzState = ZZ_LEXSTATE[zzLexicalState];
zzForAction: {
while (true) {
if (zzCurrentPosL < zzEndReadL)
zzInput = zzBufferL[zzCurrentPosL++];
else if (zzAtEOF) {
zzInput = YYEOF;
break zzForAction;
}
else {
// store back cached positions
zzCurrentPos = zzCurrentPosL;
zzMarkedPos = zzMarkedPosL;
boolean eof = zzRefill();
// get translated positions and possibly new buffer
zzCurrentPosL = zzCurrentPos;
zzMarkedPosL = zzMarkedPos;
zzBufferL = zzBuffer;
zzEndReadL = zzEndRead;
if (eof) {
zzInput = YYEOF;
break zzForAction;
}
else {
zzInput = zzBufferL[zzCurrentPosL++];
}
}
int zzNext = zzTransL[ zzRowMapL[zzState] + zzCMapL[zzInput] ];
if (zzNext == -1) break zzForAction;
zzState = zzNext;
int zzAttributes = zzAttrL[zzState];
if ( (zzAttributes & 1) == 1 ) {
zzAction = zzState;
zzMarkedPosL = zzCurrentPosL;
if ( (zzAttributes & 8) == 8 ) break zzForAction;
}
}
}
// store back cached position
zzMarkedPos = zzMarkedPosL;
switch (zzAction < 0 ? zzAction : ZZ_ACTION[zzAction]) {
case 37:
{ buffer.append(Character.toChars(Integer.parseInt(new String(zzBufferL, zzStartRead+2, zzMarkedPos - zzStartRead - 2 ), 16)));
}
case 41: break;
case 13:
{ throw new IllegalStateException(errorMessage("Found bad character while scanning an object: [" + yytext() + "]"));
}
case 42: break;
case 36:
{ datatype = XSD_BOOLEAN; yybegin(sARRAY_COMMA); return FALSE;
}
case 43: break;
case 21:
{ throw new IllegalStateException(errorMessage("Found bad character while scanning an array: [" + yytext() + "]. Expecting either ',' or ']'."));
}
case 44: break;
case 6:
{ throw new IllegalStateException(errorMessage("Found invalid character while scanning an object: [" + yytext() + "]"));
}
case 45: break;
case 24:
{ dtLabel.append(yytext());
}
case 46: break;
case 29:
{ openLeafNode = true;
incrNodeObjectPath();
datatype = XSD_DOUBLE;
setLastNode(0);
return processNumber();
}
case 47: break;
case 31:
{ openLeafNode = true;
incrNodeObjectPath();
datatype = XSD_BOOLEAN;
setLastNode(0);
return TRUE;
}
case 48: break;
case 18:
{ incrNodeObjectPath();
states.push(sARRAY_COMMA);
setLastNode(0); // initialise the first element of the array
}
case 49: break;
case 17:
{ initDatatypeObject();
incrNodeObjectPath();
states.push(sARRAY_OBJECT);
yybegin(sFIELD);
}
case 50: break;
case 30:
{ datatype = XSD_DOUBLE; yybegin(sARRAY_COMMA); return processNumber();
}
case 51: break;
case 2:
{ /* ignore white space. */
}
case 52: break;
case 39:
{ datatypeObject |= DATATYPE_OBJ_ON;
if ((datatypeObject & DATATYPE_OBJ_VALUE) == DATATYPE_OBJ_VALUE) {
throw new IllegalStateException(errorMessage("Wrong Datatype schema. The field _value_ appears several times."));
}
datatypeObject |= DATATYPE_OBJ_VALUE;
if ((datatypeObject & DATATYPE_OBJ_ERROR) == DATATYPE_OBJ_ERROR) {
throw new IllegalStateException(errorMessage("Wrong Datatype schema. Got unexpected elements in the datatype object."));
}
buffer.setLength(0);
yybegin(sDATATYPE_STRING_VALUE);
}
case 53: break;
case 3:
{ reset();
states.push(sOBJECT);
yybegin(sFIELD);
}
case 54: break;
case 19:
{ datatype = XSD_STRING;
buffer.setLength(0);
yybegin(sSTRING);
}
case 55: break;
case 26:
{ buffer.append('\"');
}
case 56: break;
case 10:
{ incrNodeObjectPath();
yybegin(sARRAY);
states.push(sARRAY_COMMA);
setLastNode(0); // initialise the first element of the array
}
case 57: break;
case 34:
{ datatype = XSD_STRING; yybegin(sARRAY_COMMA); return NULL;
}
case 58: break;
case 14:
{ datatypeObject |= DATATYPE_OBJ_JUNK;
if ((datatypeObject & DATATYPE_OBJ_ERROR) == DATATYPE_OBJ_ERROR) {
throw new IllegalStateException(errorMessage("Wrong Datatype schema. Got unexpected text: " + yytext()));
}
datatype = JSON_FIELD;
addToLastNode(1);
buffer.setLength(0);
yybegin(sFIELD_STRING);
}
case 59: break;
case 7:
{ openLeafNode = true;
incrNodeObjectPath();
datatype = XSD_LONG;
setLastNode(0);
return processNumber();
}
case 60: break;
case 16:
{ datatype = XSD_LONG; yybegin(sARRAY_COMMA); return processNumber();
}
case 61: break;
case 8:
{ initDatatypeObject();
// Two incrementations, because the object introduce a "blank" node
nestedObjects++;
incrNodeObjectPath();
setLastNode(0);
incrNodeObjectPath();
states.push(sOBJECT);
yybegin(sFIELD);
}
case 62: break;
case 40:
{ datatypeObject |= DATATYPE_OBJ_ON;
if ((datatypeObject & DATATYPE_OBJ_LABEL) == DATATYPE_OBJ_LABEL) {
throw new IllegalStateException(errorMessage("Wrong Datatype schema. The field _datatype_ appears several times."));
}
datatypeObject |= DATATYPE_OBJ_LABEL;
if ((datatypeObject & DATATYPE_OBJ_ERROR) == DATATYPE_OBJ_ERROR) {
throw new IllegalStateException(errorMessage("Wrong Datatype schema. Got unexpected elements in the datatype object."));
}
dtLabel.setLength(0);
yybegin(sDATATYPE_LABEL);
}
case 63: break;
case 4:
{ buffer.append(yytext());
}
case 64: break;
case 9:
{ // A datatype object is already closed before,
// in the state sDATATYPE_LABEL or sDATATYPE_STRING_VALUE
if ((datatypeObject & DATATYPE_OBJ_ON) == DATATYPE_OBJ_ON && datatypeObject != DATATYPE_OBJ_OK) {
if ((datatypeObject & DATATYPE_OBJ_LABEL) == DATATYPE_OBJ_LABEL) {
throw new IllegalStateException(errorMessage("Uncomplete datatype object, missing _value_ field."));
} else {
throw new IllegalStateException(errorMessage("Uncomplete datatype object, missing _datatype_ field."));
}
} else if ((datatypeObject & DATATYPE_OBJ_ON) == DATATYPE_OBJ_OFF) {
closeObject();
} else if (datatypeObject == DATATYPE_OBJ_OK && states.peek() == sOBJECT) {
// Decrement the node created in the state
// sDATATYPE_LABEL or sDATATYPE_STRING_VALUE.
// This only happens when in state sOBJECT, since it explicitly
// adds a blank node (i.e., nestedObjects variable).
// In the state sARRAY, the blank node is implicit.
decrNodeObjectPath();
}
// when the states stack is empty, I am the root of the JSON tree
yybegin(states.empty() ? YYINITIAL : this.getCurrentState());
// Switch off the datatype object
datatypeObject = DATATYPE_OBJ_OFF;
}
case 65: break;
case 1:
{ throw new IllegalStateException(errorMessage("Found bad character: [" + yytext() + "]"));
}
case 66: break;
case 12:
{ if (openLeafNode) {
openLeafNode = false;
decrNodeObjectPath();
}
yybegin(sFIELD);
}
case 67: break;
case 22:
{ addToLastNode(1); yybegin(sARRAY);
}
case 68: break;
case 32:
{ openLeafNode = true;
incrNodeObjectPath();
datatype = XSD_STRING;
setLastNode(0);
return NULL;
}
case 69: break;
case 28:
{ dtLabel.append('\"');
}
case 70: break;
case 27:
{ yybegin(sOBJECT); return LITERAL;
}
case 71: break;
case 5:
{ yybegin(this.getCurrentState()); return LITERAL;
}
case 72: break;
case 25:
{ yybegin(sOBJECT);
datatype = new char[dtLabel.length()];
dtLabel.getChars(0, dtLabel.length(), datatype, 0);
// the datatype value is here already
if (datatypeObject == DATATYPE_OBJ_OK) {
return closeDatatypeObject();
}
}
case 73: break;
case 23:
{ yybegin(sOBJECT);
// the datatype label is here already
if (datatypeObject == DATATYPE_OBJ_OK) {
return closeDatatypeObject();
}
}
case 74: break;
case 35:
{ openLeafNode = true;
incrNodeObjectPath();
datatype = XSD_BOOLEAN;
setLastNode(0);
return FALSE;
}
case 75: break;
case 20:
{ decrNodeObjectPath();
int state = states.pop();
if (state != sARRAY_COMMA) {
throw new IllegalStateException(errorMessage("Expected ']', got " + yychar()));
}
yybegin(this.getCurrentState());
}
case 76: break;
case 15:
{ throw new IllegalStateException(errorMessage("Found bad character while scanning an array: [" + yytext() + "]"));
}
case 77: break;
case 11:
{ openLeafNode = true;
incrNodeObjectPath();
datatype = XSD_STRING;
setLastNode(0);
buffer.setLength(0);
yybegin(sSTRING);
}
case 78: break;
case 33:
{ datatype = XSD_BOOLEAN; yybegin(sARRAY_COMMA); return TRUE;
}
case 79: break;
case 38:
{ dtLabel.append(Character.toChars(Integer.parseInt(new String(zzBufferL, zzStartRead+2, zzMarkedPos - zzStartRead - 2 ), 16)));
}
case 80: break;
default:
if (zzInput == YYEOF && zzStartRead == zzCurrentPos) {
zzAtEOF = true;
{
if (!states.empty()) {
throw new IllegalStateException(errorMessage("Check that all arrays/objects/strings are closed"));
}
return YYEOF;
}
}
else {
zzScanError(ZZ_NO_MATCH);
}
}
}
}
}