More formatting

By: Dinnerbone <dinnerbone@dinnerbone.com>
This commit is contained in:
Bukkit/Spigot 2011-01-03 15:35:30 +00:00
parent 346e41a6e5
commit 05b2a299c0
12 changed files with 2190 additions and 2184 deletions

View file

@ -15,119 +15,122 @@ import java.util.StringTokenizer;
* @author FangYidong<fangyidong@yahoo.com.cn>
*/
public class ItemList {
private String sp=",";
List items=new ArrayList();
private String sp = ",";
List items = new ArrayList();
public ItemList(){}
public ItemList(String s){
this.split(s,sp,items);
public ItemList() {
}
public ItemList(String s,String sp){
this.sp=s;
this.split(s,sp,items);
public ItemList(String s) {
this.split(s, sp, items);
}
public ItemList(String s,String sp,boolean isMultiToken){
split(s,sp,items,isMultiToken);
public ItemList(String s, String sp) {
this.sp = s;
this.split(s, sp, items);
}
public List getItems(){
public ItemList(String s, String sp, boolean isMultiToken) {
split(s, sp, items, isMultiToken);
}
public List getItems() {
return this.items;
}
public String[] getArray(){
return (String[])this.items.toArray();
public String[] getArray() {
return (String[]) this.items.toArray();
}
public void split(String s,String sp,List append,boolean isMultiToken){
if(s==null || sp==null)
public void split(String s, String sp, List append, boolean isMultiToken) {
if (s == null || sp == null) {
return;
if(isMultiToken){
StringTokenizer tokens=new StringTokenizer(s,sp);
while(tokens.hasMoreTokens()){
}
if (isMultiToken) {
StringTokenizer tokens = new StringTokenizer(s, sp);
while (tokens.hasMoreTokens()) {
append.add(tokens.nextToken().trim());
}
}
else{
this.split(s,sp,append);
} else {
this.split(s, sp, append);
}
}
public void split(String s,String sp,List append){
if(s==null || sp==null)
public void split(String s, String sp, List append) {
if (s == null || sp == null) {
return;
int pos=0;
int prevPos=0;
do{
prevPos=pos;
pos=s.indexOf(sp,pos);
if(pos==-1)
}
int pos = 0;
int prevPos = 0;
do {
prevPos = pos;
pos = s.indexOf(sp, pos);
if (pos == -1) {
break;
append.add(s.substring(prevPos,pos).trim());
pos+=sp.length();
}while(pos!=-1);
}
append.add(s.substring(prevPos, pos).trim());
pos += sp.length();
} while (pos != -1);
append.add(s.substring(prevPos).trim());
}
public void setSP(String sp){
this.sp=sp;
public void setSP(String sp) {
this.sp = sp;
}
public void add(int i,String item){
if(item==null)
public void add(int i, String item) {
if (item == null) {
return;
items.add(i,item.trim());
}
items.add(i, item.trim());
}
public void add(String item){
if(item==null)
public void add(String item) {
if (item == null) {
return;
}
items.add(item.trim());
}
public void addAll(ItemList list){
public void addAll(ItemList list) {
items.addAll(list.items);
}
public void addAll(String s){
this.split(s,sp,items);
public void addAll(String s) {
this.split(s, sp, items);
}
public void addAll(String s,String sp){
this.split(s,sp,items);
public void addAll(String s, String sp) {
this.split(s, sp, items);
}
public void addAll(String s,String sp,boolean isMultiToken){
this.split(s,sp,items,isMultiToken);
public void addAll(String s, String sp, boolean isMultiToken) {
this.split(s, sp, items, isMultiToken);
}
/**
* @param i 0-based
* @return
*/
public String get(int i){
return (String)items.get(i);
public String get(int i) {
return (String) items.get(i);
}
public int size(){
public int size() {
return items.size();
}
public String toString(){
public String toString() {
return toString(sp);
}
public String toString(String sp){
StringBuffer sb=new StringBuffer();
public String toString(String sp) {
StringBuffer sb = new StringBuffer();
for(int i=0;i<items.size();i++){
if(i==0)
for (int i = 0; i < items.size(); i++) {
if (i == 0) {
sb.append(items.get(i));
else{
} else {
sb.append(sp);
sb.append(items.get(i));
}
@ -136,12 +139,12 @@ public class ItemList {
}
public void clear(){
public void clear() {
items.clear();
}
public void reset(){
sp=",";
public void reset() {
sp = ",";
items.clear();
}
}

View file

@ -10,7 +10,6 @@ import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* A JSON array. JSONObject supports java.util.List interface.
*
@ -28,24 +27,25 @@ public class JSONArray extends ArrayList implements List, JSONAware, JSONStreamA
* @param list
* @param out
*/
public static void writeJSONString(List list, Writer out) throws IOException{
if(list == null){
public static void writeJSONString(List list, Writer out) throws IOException {
if (list == null) {
out.write("null");
return;
}
boolean first = true;
Iterator iter=list.iterator();
Iterator iter = list.iterator();
out.write('[');
while(iter.hasNext()){
if(first)
while (iter.hasNext()) {
if (first) {
first = false;
else
} else {
out.write(',');
}
Object value=iter.next();
if(value == null){
Object value = iter.next();
if (value == null) {
out.write("null");
continue;
}
@ -55,7 +55,7 @@ public class JSONArray extends ArrayList implements List, JSONAware, JSONStreamA
out.write(']');
}
public void writeJSONString(Writer out) throws IOException{
public void writeJSONString(Writer out) throws IOException {
writeJSONString(this, out);
}
@ -68,23 +68,25 @@ public class JSONArray extends ArrayList implements List, JSONAware, JSONStreamA
* @param list
* @return JSON text, or "null" if list is null.
*/
public static String toJSONString(List list){
if(list == null)
public static String toJSONString(List list) {
if (list == null) {
return "null";
}
boolean first = true;
StringBuffer sb = new StringBuffer();
Iterator iter=list.iterator();
Iterator iter = list.iterator();
sb.append('[');
while(iter.hasNext()){
if(first)
while (iter.hasNext()) {
if (first) {
first = false;
else
} else {
sb.append(',');
}
Object value=iter.next();
if(value == null){
Object value = iter.next();
if (value == null) {
sb.append("null");
continue;
}
@ -94,14 +96,11 @@ public class JSONArray extends ArrayList implements List, JSONAware, JSONStreamA
return sb.toString();
}
public String toJSONString(){
public String toJSONString() {
return toJSONString(this);
}
public String toString() {
return toJSONString();
}
}

View file

@ -15,7 +15,7 @@ import java.util.Map;
*
* @author FangYidong<fangyidong@yahoo.com.cn>
*/
public class JSONObject extends HashMap implements Map, JSONAware, JSONStreamAware{
public class JSONObject extends HashMap implements Map, JSONAware, JSONStreamAware {
private static final long serialVersionUID = -503443796854799292L;
/**
@ -28,21 +28,22 @@ public class JSONObject extends HashMap implements Map, JSONAware, JSONStreamAwa
* @param out
*/
public static void writeJSONString(Map map, Writer out) throws IOException {
if(map == null){
if (map == null) {
out.write("null");
return;
}
boolean first = true;
Iterator iter=map.entrySet().iterator();
Iterator iter = map.entrySet().iterator();
out.write('{');
while(iter.hasNext()){
if(first)
while (iter.hasNext()) {
if (first) {
first = false;
else
} else {
out.write(',');
Map.Entry entry=(Map.Entry)iter.next();
}
Map.Entry entry = (Map.Entry) iter.next();
out.write('\"');
out.write(escape(String.valueOf(entry.getKey())));
out.write('\"');
@ -52,7 +53,7 @@ public class JSONObject extends HashMap implements Map, JSONAware, JSONStreamAwa
out.write('}');
}
public void writeJSONString(Writer out) throws IOException{
public void writeJSONString(Writer out) throws IOException {
writeJSONString(this, out);
}
@ -65,38 +66,41 @@ public class JSONObject extends HashMap implements Map, JSONAware, JSONStreamAwa
* @param map
* @return JSON text, or "null" if map is null.
*/
public static String toJSONString(Map map){
if(map == null)
public static String toJSONString(Map map) {
if (map == null) {
return "null";
}
StringBuffer sb = new StringBuffer();
boolean first = true;
Iterator iter=map.entrySet().iterator();
Iterator iter = map.entrySet().iterator();
sb.append('{');
while(iter.hasNext()){
if(first)
while (iter.hasNext()) {
if (first) {
first = false;
else
} else {
sb.append(',');
}
Map.Entry entry=(Map.Entry)iter.next();
toJSONString(String.valueOf(entry.getKey()),entry.getValue(), sb);
Map.Entry entry = (Map.Entry) iter.next();
toJSONString(String.valueOf(entry.getKey()), entry.getValue(), sb);
}
sb.append('}');
return sb.toString();
}
public String toJSONString(){
public String toJSONString() {
return toJSONString(this);
}
private static String toJSONString(String key,Object value, StringBuffer sb){
private static String toJSONString(String key, Object value, StringBuffer sb) {
sb.append('\"');
if(key == null)
if (key == null) {
sb.append("null");
else
} else {
JSONValue.escape(key, sb);
}
sb.append('\"').append(':');
sb.append(JSONValue.toJSONString(value));
@ -104,11 +108,11 @@ public class JSONObject extends HashMap implements Map, JSONAware, JSONStreamAwa
return sb.toString();
}
public String toString(){
public String toString() {
return toJSONString();
}
public static String toString(String key,Object value){
public static String toString(String key, Object value) {
StringBuffer sb = new StringBuffer();
toJSONString(key, value, sb);
return sb.toString();
@ -123,7 +127,7 @@ public class JSONObject extends HashMap implements Map, JSONAware, JSONStreamAwa
* @param s
* @return
*/
public static String escape(String s){
public static String escape(String s) {
return JSONValue.escape(s);
}
}

View file

@ -14,7 +14,6 @@ import java.util.Map;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
/**
* @author FangYidong<fangyidong@yahoo.com.cn>
*/
@ -36,18 +35,17 @@ public class JSONValue {
* null
*
*/
public static Object parse(Reader in){
try{
JSONParser parser=new JSONParser();
public static Object parse(Reader in) {
try {
JSONParser parser = new JSONParser();
return parser.parse(in);
}
catch(Exception e){
} catch (Exception e) {
return null;
}
}
public static Object parse(String s){
StringReader in=new StringReader(s);
public static Object parse(String s) {
StringReader in = new StringReader(s);
return parse(in);
}
@ -68,13 +66,13 @@ public class JSONValue {
* @throws IOException
* @throws ParseException
*/
public static Object parseWithException(Reader in) throws IOException, ParseException{
JSONParser parser=new JSONParser();
public static Object parseWithException(Reader in) throws IOException, ParseException {
JSONParser parser = new JSONParser();
return parser.parse(in);
}
public static Object parseWithException(String s) throws ParseException{
JSONParser parser=new JSONParser();
public static Object parseWithException(String s) throws ParseException {
JSONParser parser = new JSONParser();
return parser.parse(s);
}
@ -93,61 +91,63 @@ public class JSONValue {
* @param writer
*/
public static void writeJSONString(Object value, Writer out) throws IOException {
if(value == null){
if (value == null) {
out.write("null");
return;
}
if(value instanceof String){
if (value instanceof String) {
out.write('\"');
out.write(escape((String)value));
out.write(escape((String) value));
out.write('\"');
return;
}
if(value instanceof Double){
if(((Double)value).isInfinite() || ((Double)value).isNaN())
if (value instanceof Double) {
if (((Double) value).isInfinite() || ((Double) value).isNaN()) {
out.write("null");
else
} else {
out.write(value.toString());
}
return;
}
if(value instanceof Float){
if(((Float)value).isInfinite() || ((Float)value).isNaN())
if (value instanceof Float) {
if (((Float) value).isInfinite() || ((Float) value).isNaN()) {
out.write("null");
else
} else {
out.write(value.toString());
}
return;
}
if (value instanceof Number) {
out.write(value.toString());
return;
}
if(value instanceof Number){
if (value instanceof Boolean) {
out.write(value.toString());
return;
}
if(value instanceof Boolean){
out.write(value.toString());
if ((value instanceof JSONStreamAware)) {
((JSONStreamAware) value).writeJSONString(out);
return;
}
if((value instanceof JSONStreamAware)){
((JSONStreamAware)value).writeJSONString(out);
if ((value instanceof JSONAware)) {
out.write(((JSONAware) value).toJSONString());
return;
}
if((value instanceof JSONAware)){
out.write(((JSONAware)value).toJSONString());
if (value instanceof Map) {
JSONObject.writeJSONString((Map) value, out);
return;
}
if(value instanceof Map){
JSONObject.writeJSONString((Map)value, out);
return;
}
if(value instanceof List){
JSONArray.writeJSONString((List)value, out);
if (value instanceof List) {
JSONArray.writeJSONString((List) value, out);
return;
}
@ -168,41 +168,50 @@ public class JSONValue {
* @param value
* @return JSON text, or "null" if value is null or it's an NaN or an INF number.
*/
public static String toJSONString(Object value){
if(value == null)
public static String toJSONString(Object value) {
if (value == null) {
return "null";
}
if(value instanceof String)
return "\""+escape((String)value)+"\"";
if (value instanceof String) {
return "\"" + escape((String) value) + "\"";
}
if(value instanceof Double){
if(((Double)value).isInfinite() || ((Double)value).isNaN())
if (value instanceof Double) {
if (((Double) value).isInfinite() || ((Double) value).isNaN()) {
return "null";
else
} else {
return value.toString();
}
}
if (value instanceof Float) {
if (((Float) value).isInfinite() || ((Float) value).isNaN()) {
return "null";
} else {
return value.toString();
}
}
if (value instanceof Number) {
return value.toString();
}
if(value instanceof Float){
if(((Float)value).isInfinite() || ((Float)value).isNaN())
return "null";
else
if (value instanceof Boolean) {
return value.toString();
}
if(value instanceof Number)
return value.toString();
if ((value instanceof JSONAware)) {
return ((JSONAware) value).toJSONString();
}
if(value instanceof Boolean)
return value.toString();
if (value instanceof Map) {
return JSONObject.toJSONString((Map) value);
}
if((value instanceof JSONAware))
return ((JSONAware)value).toJSONString();
if(value instanceof Map)
return JSONObject.toJSONString((Map)value);
if(value instanceof List)
return JSONArray.toJSONString((List)value);
if (value instanceof List) {
return JSONArray.toJSONString((List) value);
}
return value.toString();
}
@ -212,9 +221,10 @@ public class JSONValue {
* @param s
* @return
*/
public static String escape(String s){
if(s==null)
public static String escape(String s) {
if (s == null) {
return null;
}
StringBuffer sb = new StringBuffer();
escape(s, sb);
return sb.toString();
@ -225,9 +235,9 @@ public class JSONValue {
* @param sb
*/
static void escape(String s, StringBuffer sb) {
for(int i=0;i<s.length();i++){
char ch=s.charAt(i);
switch(ch){
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
switch (ch) {
case '"':
sb.append("\\\"");
break;
@ -254,19 +264,17 @@ public class JSONValue {
break;
default:
//Reference: http://www.unicode.org/versions/Unicode5.1.0/
if((ch>='\u0000' && ch<='\u001F') || (ch>='\u007F' && ch<='\u009F') || (ch>='\u2000' && ch<='\u20FF')){
String ss=Integer.toHexString(ch);
if ((ch >= '\u0000' && ch <= '\u001F') || (ch >= '\u007F' && ch <= '\u009F') || (ch >= '\u2000' && ch <= '\u20FF')) {
String ss = Integer.toHexString(ch);
sb.append("\\u");
for(int k=0;k<4-ss.length();k++){
for (int k = 0; k < 4 - ss.length(); k++) {
sb.append('0');
}
sb.append(ss.toUpperCase());
}
else{
} else {
sb.append(ch);
}
}
}//for
}
}

View file

@ -106,5 +106,4 @@ public interface ContentHandler {
* @throws ParseException
*/
boolean primitive(Object value) throws ParseException, IOException;
}

View file

@ -14,31 +14,30 @@ import java.util.Map;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
/**
* Parser for JSON text. Please note that JSONParser is NOT thread-safe.
*
* @author FangYidong<fangyidong@yahoo.com.cn>
*/
public class JSONParser {
public static final int S_INIT=0;
public static final int S_IN_FINISHED_VALUE=1;//string,number,boolean,null,object,array
public static final int S_IN_OBJECT=2;
public static final int S_IN_ARRAY=3;
public static final int S_PASSED_PAIR_KEY=4;
public static final int S_IN_PAIR_VALUE=5;
public static final int S_END=6;
public static final int S_IN_ERROR=-1;
public static final int S_INIT = 0;
public static final int S_IN_FINISHED_VALUE = 1;//string,number,boolean,null,object,array
public static final int S_IN_OBJECT = 2;
public static final int S_IN_ARRAY = 3;
public static final int S_PASSED_PAIR_KEY = 4;
public static final int S_IN_PAIR_VALUE = 5;
public static final int S_END = 6;
public static final int S_IN_ERROR = -1;
private LinkedList handlerStatusStack;
private Yylex lexer = new Yylex((Reader)null);
private Yylex lexer = new Yylex((Reader) null);
private Yytoken token = null;
private int status = S_INIT;
private int peekStatus(LinkedList statusStack){
if(statusStack.size()==0)
private int peekStatus(LinkedList statusStack) {
if (statusStack.size() == 0) {
return -1;
Integer status=(Integer)statusStack.getFirst();
}
Integer status = (Integer) statusStack.getFirst();
return status.intValue();
}
@ -46,7 +45,7 @@ public class JSONParser {
* Reset the parser to the initial state without resetting the underlying reader.
*
*/
public void reset(){
public void reset() {
token = null;
status = S_INIT;
handlerStatusStack = null;
@ -59,7 +58,7 @@ public class JSONParser {
* @throws IOException
* @throws ParseException
*/
public void reset(Reader in){
public void reset(Reader in) {
lexer.yyreset(in);
reset();
}
@ -67,20 +66,19 @@ public class JSONParser {
/**
* @return The position of the beginning of the current token.
*/
public int getPosition(){
public int getPosition() {
return lexer.getPosition();
}
public Object parse(String s) throws ParseException{
return parse(s, (ContainerFactory)null);
public Object parse(String s) throws ParseException {
return parse(s, (ContainerFactory) null);
}
public Object parse(String s, ContainerFactory containerFactory) throws ParseException{
StringReader in=new StringReader(s);
try{
public Object parse(String s, ContainerFactory containerFactory) throws ParseException {
StringReader in = new StringReader(s);
try {
return parse(in, containerFactory);
}
catch(IOException ie){
} catch (IOException ie) {
/*
* Actually it will never happen.
*/
@ -88,8 +86,8 @@ public class JSONParser {
}
}
public Object parse(Reader in) throws IOException, ParseException{
return parse(in, (ContainerFactory)null);
public Object parse(Reader in) throws IOException, ParseException {
return parse(in, (ContainerFactory) null);
}
/**
@ -108,199 +106,200 @@ public class JSONParser {
* @throws IOException
* @throws ParseException
*/
public Object parse(Reader in, ContainerFactory containerFactory) throws IOException, ParseException{
public Object parse(Reader in, ContainerFactory containerFactory) throws IOException, ParseException {
reset(in);
LinkedList statusStack = new LinkedList();
LinkedList valueStack = new LinkedList();
try{
do{
try {
do {
nextToken();
switch(status){
switch (status) {
case S_INIT:
switch(token.type){
switch (token.type) {
case Yytoken.TYPE_VALUE:
status=S_IN_FINISHED_VALUE;
status = S_IN_FINISHED_VALUE;
statusStack.addFirst(new Integer(status));
valueStack.addFirst(token.value);
break;
case Yytoken.TYPE_LEFT_BRACE:
status=S_IN_OBJECT;
status = S_IN_OBJECT;
statusStack.addFirst(new Integer(status));
valueStack.addFirst(createObjectContainer(containerFactory));
break;
case Yytoken.TYPE_LEFT_SQUARE:
status=S_IN_ARRAY;
status = S_IN_ARRAY;
statusStack.addFirst(new Integer(status));
valueStack.addFirst(createArrayContainer(containerFactory));
break;
default:
status=S_IN_ERROR;
status = S_IN_ERROR;
}//inner switch
break;
case S_IN_FINISHED_VALUE:
if(token.type==Yytoken.TYPE_EOF)
if (token.type == Yytoken.TYPE_EOF) {
return valueStack.removeFirst();
else
} else {
throw new ParseException(getPosition(), ParseException.ERROR_UNEXPECTED_TOKEN, token);
}
case S_IN_OBJECT:
switch(token.type){
switch (token.type) {
case Yytoken.TYPE_COMMA:
break;
case Yytoken.TYPE_VALUE:
if(token.value instanceof String){
String key=(String)token.value;
if (token.value instanceof String) {
String key = (String) token.value;
valueStack.addFirst(key);
status=S_PASSED_PAIR_KEY;
status = S_PASSED_PAIR_KEY;
statusStack.addFirst(new Integer(status));
}
else{
status=S_IN_ERROR;
} else {
status = S_IN_ERROR;
}
break;
case Yytoken.TYPE_RIGHT_BRACE:
if(valueStack.size()>1){
if (valueStack.size() > 1) {
statusStack.removeFirst();
valueStack.removeFirst();
status=peekStatus(statusStack);
}
else{
status=S_IN_FINISHED_VALUE;
status = peekStatus(statusStack);
} else {
status = S_IN_FINISHED_VALUE;
}
break;
default:
status=S_IN_ERROR;
status = S_IN_ERROR;
break;
}//inner switch
break;
case S_PASSED_PAIR_KEY:
switch(token.type){
switch (token.type) {
case Yytoken.TYPE_COLON:
break;
case Yytoken.TYPE_VALUE:
statusStack.removeFirst();
String key=(String)valueStack.removeFirst();
Map parent=(Map)valueStack.getFirst();
parent.put(key,token.value);
status=peekStatus(statusStack);
String key = (String) valueStack.removeFirst();
Map parent = (Map) valueStack.getFirst();
parent.put(key, token.value);
status = peekStatus(statusStack);
break;
case Yytoken.TYPE_LEFT_SQUARE:
statusStack.removeFirst();
key=(String)valueStack.removeFirst();
parent=(Map)valueStack.getFirst();
List newArray=createArrayContainer(containerFactory);
parent.put(key,newArray);
status=S_IN_ARRAY;
key = (String) valueStack.removeFirst();
parent = (Map) valueStack.getFirst();
List newArray = createArrayContainer(containerFactory);
parent.put(key, newArray);
status = S_IN_ARRAY;
statusStack.addFirst(new Integer(status));
valueStack.addFirst(newArray);
break;
case Yytoken.TYPE_LEFT_BRACE:
statusStack.removeFirst();
key=(String)valueStack.removeFirst();
parent=(Map)valueStack.getFirst();
Map newObject=createObjectContainer(containerFactory);
parent.put(key,newObject);
status=S_IN_OBJECT;
key = (String) valueStack.removeFirst();
parent = (Map) valueStack.getFirst();
Map newObject = createObjectContainer(containerFactory);
parent.put(key, newObject);
status = S_IN_OBJECT;
statusStack.addFirst(new Integer(status));
valueStack.addFirst(newObject);
break;
default:
status=S_IN_ERROR;
status = S_IN_ERROR;
}
break;
case S_IN_ARRAY:
switch(token.type){
switch (token.type) {
case Yytoken.TYPE_COMMA:
break;
case Yytoken.TYPE_VALUE:
List val=(List)valueStack.getFirst();
List val = (List) valueStack.getFirst();
val.add(token.value);
break;
case Yytoken.TYPE_RIGHT_SQUARE:
if(valueStack.size()>1){
if (valueStack.size() > 1) {
statusStack.removeFirst();
valueStack.removeFirst();
status=peekStatus(statusStack);
}
else{
status=S_IN_FINISHED_VALUE;
status = peekStatus(statusStack);
} else {
status = S_IN_FINISHED_VALUE;
}
break;
case Yytoken.TYPE_LEFT_BRACE:
val=(List)valueStack.getFirst();
Map newObject=createObjectContainer(containerFactory);
val = (List) valueStack.getFirst();
Map newObject = createObjectContainer(containerFactory);
val.add(newObject);
status=S_IN_OBJECT;
status = S_IN_OBJECT;
statusStack.addFirst(new Integer(status));
valueStack.addFirst(newObject);
break;
case Yytoken.TYPE_LEFT_SQUARE:
val=(List)valueStack.getFirst();
List newArray=createArrayContainer(containerFactory);
val = (List) valueStack.getFirst();
List newArray = createArrayContainer(containerFactory);
val.add(newArray);
status=S_IN_ARRAY;
status = S_IN_ARRAY;
statusStack.addFirst(new Integer(status));
valueStack.addFirst(newArray);
break;
default:
status=S_IN_ERROR;
status = S_IN_ERROR;
}//inner switch
break;
case S_IN_ERROR:
throw new ParseException(getPosition(), ParseException.ERROR_UNEXPECTED_TOKEN, token);
}//switch
if(status==S_IN_ERROR){
if (status == S_IN_ERROR) {
throw new ParseException(getPosition(), ParseException.ERROR_UNEXPECTED_TOKEN, token);
}
}while(token.type!=Yytoken.TYPE_EOF);
}
catch(IOException ie){
} while (token.type != Yytoken.TYPE_EOF);
} catch (IOException ie) {
throw ie;
}
throw new ParseException(getPosition(), ParseException.ERROR_UNEXPECTED_TOKEN, token);
}
private void nextToken() throws ParseException, IOException{
private void nextToken() throws ParseException, IOException {
token = lexer.yylex();
if(token == null)
if (token == null) {
token = new Yytoken(Yytoken.TYPE_EOF, null);
}
}
private Map createObjectContainer(ContainerFactory containerFactory){
if(containerFactory == null)
private Map createObjectContainer(ContainerFactory containerFactory) {
if (containerFactory == null) {
return new JSONObject();
}
Map m = containerFactory.createObjectContainer();
if(m == null)
if (m == null) {
return new JSONObject();
}
return m;
}
private List createArrayContainer(ContainerFactory containerFactory){
if(containerFactory == null)
private List createArrayContainer(ContainerFactory containerFactory) {
if (containerFactory == null) {
return new JSONArray();
}
List l = containerFactory.creatArrayContainer();
if(l == null)
if (l == null) {
return new JSONArray();
}
return l;
}
public void parse(String s, ContentHandler contentHandler) throws ParseException{
public void parse(String s, ContentHandler contentHandler) throws ParseException {
parse(s, contentHandler, false);
}
public void parse(String s, ContentHandler contentHandler, boolean isResume) throws ParseException{
StringReader in=new StringReader(s);
try{
public void parse(String s, ContentHandler contentHandler, boolean isResume) throws ParseException {
StringReader in = new StringReader(s);
try {
parse(in, contentHandler, isResume);
}
catch(IOException ie){
} catch (IOException ie) {
/*
* Actually it will never happen.
*/
@ -308,7 +307,7 @@ public class JSONParser {
}
}
public void parse(Reader in, ContentHandler contentHandler) throws IOException, ParseException{
public void parse(Reader in, ContentHandler contentHandler) throws IOException, ParseException {
parse(in, contentHandler, false);
}
@ -326,13 +325,12 @@ public class JSONParser {
* @throws IOException
* @throws ParseException
*/
public void parse(Reader in, ContentHandler contentHandler, boolean isResume) throws IOException, ParseException{
if(!isResume){
public void parse(Reader in, ContentHandler contentHandler, boolean isResume) throws IOException, ParseException {
if (!isResume) {
reset(in);
handlerStatusStack = new LinkedList();
}
else{
if(handlerStatusStack == null){
} else {
if (handlerStatusStack == null) {
isResume = false;
reset(in);
handlerStatusStack = new LinkedList();
@ -341,113 +339,119 @@ public class JSONParser {
LinkedList statusStack = handlerStatusStack;
try{
do{
switch(status){
try {
do {
switch (status) {
case S_INIT:
contentHandler.startJSON();
nextToken();
switch(token.type){
switch (token.type) {
case Yytoken.TYPE_VALUE:
status=S_IN_FINISHED_VALUE;
status = S_IN_FINISHED_VALUE;
statusStack.addFirst(new Integer(status));
if(!contentHandler.primitive(token.value))
if (!contentHandler.primitive(token.value)) {
return;
}
break;
case Yytoken.TYPE_LEFT_BRACE:
status=S_IN_OBJECT;
status = S_IN_OBJECT;
statusStack.addFirst(new Integer(status));
if(!contentHandler.startObject())
if (!contentHandler.startObject()) {
return;
}
break;
case Yytoken.TYPE_LEFT_SQUARE:
status=S_IN_ARRAY;
status = S_IN_ARRAY;
statusStack.addFirst(new Integer(status));
if(!contentHandler.startArray())
if (!contentHandler.startArray()) {
return;
}
break;
default:
status=S_IN_ERROR;
status = S_IN_ERROR;
}//inner switch
break;
case S_IN_FINISHED_VALUE:
nextToken();
if(token.type==Yytoken.TYPE_EOF){
if (token.type == Yytoken.TYPE_EOF) {
contentHandler.endJSON();
status = S_END;
return;
}
else{
} else {
status = S_IN_ERROR;
throw new ParseException(getPosition(), ParseException.ERROR_UNEXPECTED_TOKEN, token);
}
case S_IN_OBJECT:
nextToken();
switch(token.type){
switch (token.type) {
case Yytoken.TYPE_COMMA:
break;
case Yytoken.TYPE_VALUE:
if(token.value instanceof String){
String key=(String)token.value;
status=S_PASSED_PAIR_KEY;
if (token.value instanceof String) {
String key = (String) token.value;
status = S_PASSED_PAIR_KEY;
statusStack.addFirst(new Integer(status));
if(!contentHandler.startObjectEntry(key))
if (!contentHandler.startObjectEntry(key)) {
return;
}
else{
status=S_IN_ERROR;
} else {
status = S_IN_ERROR;
}
break;
case Yytoken.TYPE_RIGHT_BRACE:
if(statusStack.size()>1){
if (statusStack.size() > 1) {
statusStack.removeFirst();
status=peekStatus(statusStack);
status = peekStatus(statusStack);
} else {
status = S_IN_FINISHED_VALUE;
}
else{
status=S_IN_FINISHED_VALUE;
}
if(!contentHandler.endObject())
if (!contentHandler.endObject()) {
return;
}
break;
default:
status=S_IN_ERROR;
status = S_IN_ERROR;
break;
}//inner switch
break;
case S_PASSED_PAIR_KEY:
nextToken();
switch(token.type){
switch (token.type) {
case Yytoken.TYPE_COLON:
break;
case Yytoken.TYPE_VALUE:
statusStack.removeFirst();
status=peekStatus(statusStack);
if(!contentHandler.primitive(token.value))
status = peekStatus(statusStack);
if (!contentHandler.primitive(token.value)) {
return;
if(!contentHandler.endObjectEntry())
}
if (!contentHandler.endObjectEntry()) {
return;
}
break;
case Yytoken.TYPE_LEFT_SQUARE:
statusStack.removeFirst();
statusStack.addFirst(new Integer(S_IN_PAIR_VALUE));
status=S_IN_ARRAY;
status = S_IN_ARRAY;
statusStack.addFirst(new Integer(status));
if(!contentHandler.startArray())
if (!contentHandler.startArray()) {
return;
}
break;
case Yytoken.TYPE_LEFT_BRACE:
statusStack.removeFirst();
statusStack.addFirst(new Integer(S_IN_PAIR_VALUE));
status=S_IN_OBJECT;
status = S_IN_OBJECT;
statusStack.addFirst(new Integer(status));
if(!contentHandler.startObject())
if (!contentHandler.startObject()) {
return;
}
break;
default:
status=S_IN_ERROR;
status = S_IN_ERROR;
}
break;
@ -458,44 +462,48 @@ public class JSONParser {
*/
statusStack.removeFirst();
status = peekStatus(statusStack);
if(!contentHandler.endObjectEntry())
if (!contentHandler.endObjectEntry()) {
return;
}
break;
case S_IN_ARRAY:
nextToken();
switch(token.type){
switch (token.type) {
case Yytoken.TYPE_COMMA:
break;
case Yytoken.TYPE_VALUE:
if(!contentHandler.primitive(token.value))
if (!contentHandler.primitive(token.value)) {
return;
}
break;
case Yytoken.TYPE_RIGHT_SQUARE:
if(statusStack.size()>1){
if (statusStack.size() > 1) {
statusStack.removeFirst();
status=peekStatus(statusStack);
status = peekStatus(statusStack);
} else {
status = S_IN_FINISHED_VALUE;
}
else{
status=S_IN_FINISHED_VALUE;
}
if(!contentHandler.endArray())
if (!contentHandler.endArray()) {
return;
}
break;
case Yytoken.TYPE_LEFT_BRACE:
status=S_IN_OBJECT;
status = S_IN_OBJECT;
statusStack.addFirst(new Integer(status));
if(!contentHandler.startObject())
if (!contentHandler.startObject()) {
return;
}
break;
case Yytoken.TYPE_LEFT_SQUARE:
status=S_IN_ARRAY;
status = S_IN_ARRAY;
statusStack.addFirst(new Integer(status));
if(!contentHandler.startArray())
if (!contentHandler.startArray()) {
return;
}
break;
default:
status=S_IN_ERROR;
status = S_IN_ERROR;
}//inner switch
break;
@ -505,24 +513,20 @@ public class JSONParser {
case S_IN_ERROR:
throw new ParseException(getPosition(), ParseException.ERROR_UNEXPECTED_TOKEN, token);
}//switch
if(status==S_IN_ERROR){
if (status == S_IN_ERROR) {
throw new ParseException(getPosition(), ParseException.ERROR_UNEXPECTED_TOKEN, token);
}
}while(token.type!=Yytoken.TYPE_EOF);
}
catch(IOException ie){
} while (token.type != Yytoken.TYPE_EOF);
} catch (IOException ie) {
status = S_IN_ERROR;
throw ie;
}
catch(ParseException pe){
} catch (ParseException pe) {
status = S_IN_ERROR;
throw pe;
}
catch(RuntimeException re){
} catch (RuntimeException re) {
status = S_IN_ERROR;
throw re;
}
catch(Error e){
} catch (Error e) {
status = S_IN_ERROR;
throw e;
}

View file

@ -8,24 +8,22 @@ package org.json.simple.parser;
*/
public class ParseException extends Exception {
private static final long serialVersionUID = -7880698968187728548L;
public static final int ERROR_UNEXPECTED_CHAR = 0;
public static final int ERROR_UNEXPECTED_TOKEN = 1;
public static final int ERROR_UNEXPECTED_EXCEPTION = 2;
private int errorType;
private Object unexpectedObject;
private int position;
public ParseException(int errorType){
public ParseException(int errorType) {
this(-1, errorType, null);
}
public ParseException(int errorType, Object unexpectedObject){
public ParseException(int errorType, Object unexpectedObject) {
this(-1, errorType, unexpectedObject);
}
public ParseException(int position, int errorType, Object unexpectedObject){
public ParseException(int position, int errorType, Object unexpectedObject) {
this.position = position;
this.errorType = errorType;
this.unexpectedObject = unexpectedObject;
@ -68,10 +66,10 @@ public class ParseException extends Exception {
this.unexpectedObject = unexpectedObject;
}
public String toString(){
public String toString() {
StringBuffer sb = new StringBuffer();
switch(errorType){
switch (errorType) {
case ERROR_UNEXPECTED_CHAR:
sb.append("Unexpected character (").append(unexpectedObject).append(") at position ").append(position).append(".");
break;

View file

@ -1,19 +1,14 @@
/* The following code was generated by JFlex 1.4.2 */
package org.json.simple.parser;
class Yylex {
/** 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 YYINITIAL = 0;
public static final int STRING_BEGIN = 2;
/**
* 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
@ -23,75 +18,70 @@ class Yylex {
private static final int ZZ_LEXSTATE[] = {
0, 0, 1, 1
};
/**
* Translates characters to character classes
*/
private static final String ZZ_CMAP_PACKED =
"\11\0\1\7\1\7\2\0\1\7\22\0\1\7\1\0\1\11\10\0"+
"\1\6\1\31\1\2\1\4\1\12\12\3\1\32\6\0\4\1\1\5"+
"\1\1\24\0\1\27\1\10\1\30\3\0\1\22\1\13\2\1\1\21"+
"\1\14\5\0\1\23\1\0\1\15\3\0\1\16\1\24\1\17\1\20"+
"\5\0\1\25\1\0\1\26\uff82\0";
"\11\0\1\7\1\7\2\0\1\7\22\0\1\7\1\0\1\11\10\0"
+ "\1\6\1\31\1\2\1\4\1\12\12\3\1\32\6\0\4\1\1\5"
+ "\1\1\24\0\1\27\1\10\1\30\3\0\1\22\1\13\2\1\1\21"
+ "\1\14\5\0\1\23\1\0\1\15\3\0\1\16\1\24\1\17\1\20"
+ "\5\0\1\25\1\0\1\26\uff82\0";
/**
* Translates characters to character classes
*/
private static final char [] ZZ_CMAP = zzUnpackCMap(ZZ_CMAP_PACKED);
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 int[] ZZ_ACTION = zzUnpackAction();
private static final String ZZ_ACTION_PACKED_0 =
"\2\0\2\1\1\2\1\3\1\4\3\1\1\5\1\6"+
"\1\7\1\10\1\11\1\12\1\13\1\14\1\15\5\0"+
"\1\14\1\16\1\17\1\20\1\21\1\22\1\23\1\24"+
"\1\0\1\25\1\0\1\25\4\0\1\26\1\27\2\0"+
"\1\30";
"\2\0\2\1\1\2\1\3\1\4\3\1\1\5\1\6"
+ "\1\7\1\10\1\11\1\12\1\13\1\14\1\15\5\0"
+ "\1\14\1\16\1\17\1\20\1\21\1\22\1\23\1\24"
+ "\1\0\1\25\1\0\1\25\4\0\1\26\1\27\2\0"
+ "\1\30";
private static int [] zzUnpackAction() {
int [] result = new int[45];
private static int[] zzUnpackAction() {
int[] result = new int[45];
int offset = 0;
offset = zzUnpackAction(ZZ_ACTION_PACKED_0, offset, result);
return result;
}
private static int zzUnpackAction(String packed, int offset, int [] 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);
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 int[] ZZ_ROWMAP = zzUnpackRowMap();
private static final String ZZ_ROWMAP_PACKED_0 =
"\0\0\0\33\0\66\0\121\0\154\0\207\0\66\0\242"+
"\0\275\0\330\0\66\0\66\0\66\0\66\0\66\0\66"+
"\0\363\0\u010e\0\66\0\u0129\0\u0144\0\u015f\0\u017a\0\u0195"+
"\0\66\0\66\0\66\0\66\0\66\0\66\0\66\0\66"+
"\0\u01b0\0\u01cb\0\u01e6\0\u01e6\0\u0201\0\u021c\0\u0237\0\u0252"+
"\0\66\0\66\0\u026d\0\u0288\0\66";
"\0\0\0\33\0\66\0\121\0\154\0\207\0\66\0\242"
+ "\0\275\0\330\0\66\0\66\0\66\0\66\0\66\0\66"
+ "\0\363\0\u010e\0\66\0\u0129\0\u0144\0\u015f\0\u017a\0\u0195"
+ "\0\66\0\66\0\66\0\66\0\66\0\66\0\66\0\66"
+ "\0\u01b0\0\u01cb\0\u01e6\0\u01e6\0\u0201\0\u021c\0\u0237\0\u0252"
+ "\0\66\0\66\0\u026d\0\u0288\0\66";
private static int [] zzUnpackRowMap() {
int [] result = new int[45];
private static int[] zzUnpackRowMap() {
int[] result = new int[45];
int offset = 0;
offset = zzUnpackRowMap(ZZ_ROWMAP_PACKED_0, offset, result);
return result;
}
private static int zzUnpackRowMap(String packed, int offset, int [] 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();
@ -101,11 +91,10 @@ class Yylex {
}
return j;
}
/**
* The transition table of the DFA
*/
private static final int ZZ_TRANS [] = {
private static final int ZZ_TRANS[] = {
2, 2, 3, 4, 2, 2, 2, 5, 2, 6,
2, 2, 7, 8, 2, 9, 2, 2, 2, 2,
2, 10, 11, 12, 13, 14, 15, 16, 16, 16,
@ -173,8 +162,7 @@ class Yylex {
-1, -1, -1, -1, -1, -1, -1, -1, -1, 44,
-1, 44, -1, 44, -1, -1, -1, -1, -1, 44,
44, -1, -1, -1, -1, 44, 44, -1, -1, -1,
-1, -1, -1, -1, -1,
};
-1, -1, -1, -1, -1,};
/* error codes */
private static final int ZZ_UNKNOWN_ERROR = 0;
@ -187,90 +175,75 @@ class Yylex {
"Error: could not match input",
"Error: pushback value was too large"
};
/**
* ZZ_ATTRIBUTE[aState] contains the attributes of state <code>aState</code>
*/
private static final int [] ZZ_ATTRIBUTE = zzUnpackAttribute();
private static final int[] ZZ_ATTRIBUTE = zzUnpackAttribute();
private static final String ZZ_ATTRIBUTE_PACKED_0 =
"\2\0\1\11\3\1\1\11\3\1\6\11\2\1\1\11"+
"\5\0\10\11\1\0\1\1\1\0\1\1\4\0\2\11"+
"\2\0\1\11";
"\2\0\1\11\3\1\1\11\3\1\6\11\2\1\1\11"
+ "\5\0\10\11\1\0\1\1\1\0\1\1\4\0\2\11"
+ "\2\0\1\11";
private static int [] zzUnpackAttribute() {
int [] result = new int[45];
private static int[] zzUnpackAttribute() {
int[] result = new int[45];
int offset = 0;
offset = zzUnpackAttribute(ZZ_ATTRIBUTE_PACKED_0, offset, result);
return result;
}
private static int zzUnpackAttribute(String packed, int offset, int [] 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);
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;
/* user code: */
private StringBuffer sb=new StringBuffer();
private StringBuffer sb = new StringBuffer();
int getPosition(){
int getPosition() {
return yychar;
}
}
/**
* Creates a new scanner
@ -298,19 +271,20 @@ int getPosition(){
* @param packed the packed character translation table
* @return the unpacked character translation table
*/
private static char [] zzUnpackCMap(String packed) {
char [] map = new char[0x10000];
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 < 90) {
int count = packed.charAt(i++);
char value = packed.charAt(i++);
do map[j++] = value; while (--count > 0);
do {
map[j++] = value;
} while (--count > 0);
}
return map;
}
/**
* Refills the input buffer.
*
@ -324,29 +298,29 @@ int getPosition(){
if (zzStartRead > 0) {
System.arraycopy(zzBuffer, zzStartRead,
zzBuffer, 0,
zzEndRead-zzStartRead);
zzEndRead - zzStartRead);
/* translate stored positions */
zzEndRead-= zzStartRead;
zzCurrentPos-= zzStartRead;
zzMarkedPos-= zzStartRead;
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];
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);
zzBuffer.length - zzEndRead);
if (numRead > 0) {
zzEndRead+= numRead;
zzEndRead += numRead;
return false;
}
// unlikely but not impossible: read 0 characters, but not at end of stream
@ -364,7 +338,6 @@ int getPosition(){
return true;
}
/**
* Closes the input stream.
*/
@ -372,10 +345,10 @@ int getPosition(){
zzAtEOF = true; /* indicate end of file */
zzEndRead = zzStartRead; /* invalidate buffer */
if (zzReader != null)
if (zzReader != null) {
zzReader.close();
}
}
/**
* Resets the scanner to read from a new input stream.
@ -397,7 +370,6 @@ int getPosition(){
zzLexicalState = YYINITIAL;
}
/**
* Returns the current lexical state.
*/
@ -405,7 +377,6 @@ int getPosition(){
return zzLexicalState;
}
/**
* Enters a new lexical state
*
@ -415,15 +386,13 @@ int getPosition(){
zzLexicalState = newState;
}
/**
* Returns the text matched by the current regular expression.
*/
public final String yytext() {
return new String( zzBuffer, zzStartRead, zzMarkedPos-zzStartRead );
return new String(zzBuffer, zzStartRead, zzMarkedPos - zzStartRead);
}
/**
* Returns the character at position <tt>pos</tt> from the
* matched text.
@ -436,18 +405,16 @@ int getPosition(){
* @return the character at position pos
*/
public final char yycharat(int pos) {
return zzBuffer[zzStartRead+pos];
return zzBuffer[zzStartRead + pos];
}
/**
* Returns the length of the matched text region.
*/
public final int yylength() {
return zzMarkedPos-zzStartRead;
return zzMarkedPos - zzStartRead;
}
/**
* Reports an error that occured while scanning.
*
@ -466,15 +433,13 @@ int getPosition(){
String message;
try {
message = ZZ_ERROR_MSG[errorCode];
}
catch (ArrayIndexOutOfBoundsException e) {
} 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.
*
@ -484,13 +449,13 @@ int getPosition(){
* This number must not be greater than yylength()!
*/
public void yypushback(int number) {
if ( number > yylength() )
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.
@ -506,17 +471,17 @@ int getPosition(){
int zzCurrentPosL;
int zzMarkedPosL;
int zzEndReadL = zzEndRead;
char [] zzBufferL = zzBuffer;
char [] zzCMapL = ZZ_CMAP;
char[] zzBufferL = zzBuffer;
char[] zzCMapL = ZZ_CMAP;
int [] zzTransL = ZZ_TRANS;
int [] zzRowMapL = ZZ_ROWMAP;
int [] zzAttrL = ZZ_ATTRIBUTE;
int[] zzTransL = ZZ_TRANS;
int[] zzRowMapL = ZZ_ROWMAP;
int[] zzAttrL = ZZ_ATTRIBUTE;
while (true) {
zzMarkedPosL = zzMarkedPos;
yychar+= zzMarkedPosL-zzStartRead;
yychar += zzMarkedPosL - zzStartRead;
zzAction = -1;
@ -525,16 +490,16 @@ int getPosition(){
zzState = ZZ_LEXSTATE[zzLexicalState];
zzForAction: {
zzForAction:
{
while (true) {
if (zzCurrentPosL < zzEndReadL)
if (zzCurrentPosL < zzEndReadL) {
zzInput = zzBufferL[zzCurrentPosL++];
else if (zzAtEOF) {
} else if (zzAtEOF) {
zzInput = YYEOF;
break zzForAction;
}
else {
} else {
// store back cached positions
zzCurrentPos = zzCurrentPosL;
zzMarkedPos = zzMarkedPosL;
@ -547,20 +512,23 @@ int getPosition(){
if (eof) {
zzInput = YYEOF;
break zzForAction;
}
else {
} else {
zzInput = zzBufferL[zzCurrentPosL++];
}
}
int zzNext = zzTransL[ zzRowMapL[zzState] + zzCMapL[zzInput] ];
if (zzNext == -1) break zzForAction;
int zzNext = zzTransL[zzRowMapL[zzState] + zzCMapL[zzInput]];
if (zzNext == -1) {
break zzForAction;
}
zzState = zzNext;
int zzAttributes = zzAttrL[zzState];
if ( (zzAttributes & 1) == 1 ) {
if ((zzAttributes & 1) == 1) {
zzAction = zzState;
zzMarkedPosL = zzCurrentPosL;
if ( (zzAttributes & 8) == 8 ) break zzForAction;
if ((zzAttributes & 8) == 8) {
break zzForAction;
}
}
}
@ -570,119 +538,143 @@ int getPosition(){
zzMarkedPos = zzMarkedPosL;
switch (zzAction < 0 ? zzAction : ZZ_ACTION[zzAction]) {
case 11:
{ sb.append(yytext());
case 11: {
sb.append(yytext());
}
case 25: break;
case 4:
{ sb.delete(0, sb.length());yybegin(STRING_BEGIN);
case 25:
break;
case 4: {
sb.delete(0, sb.length());
yybegin(STRING_BEGIN);
}
case 26: break;
case 16:
{ sb.append('\b');
case 26:
break;
case 16: {
sb.append('\b');
}
case 27: break;
case 6:
{ return new Yytoken(Yytoken.TYPE_RIGHT_BRACE,null);
case 27:
break;
case 6: {
return new Yytoken(Yytoken.TYPE_RIGHT_BRACE, null);
}
case 28: break;
case 23:
{ Boolean val=Boolean.valueOf(yytext()); return new Yytoken(Yytoken.TYPE_VALUE, val);
case 28:
break;
case 23: {
Boolean val = Boolean.valueOf(yytext());
return new Yytoken(Yytoken.TYPE_VALUE, val);
}
case 29: break;
case 22:
{ return new Yytoken(Yytoken.TYPE_VALUE, null);
case 29:
break;
case 22: {
return new Yytoken(Yytoken.TYPE_VALUE, null);
}
case 30: break;
case 13:
{ yybegin(YYINITIAL);return new Yytoken(Yytoken.TYPE_VALUE, sb.toString());
case 30:
break;
case 13: {
yybegin(YYINITIAL);
return new Yytoken(Yytoken.TYPE_VALUE, sb.toString());
}
case 31: break;
case 12:
{ sb.append('\\');
case 31:
break;
case 12: {
sb.append('\\');
}
case 32: break;
case 21:
{ Double val=Double.valueOf(yytext()); return new Yytoken(Yytoken.TYPE_VALUE, val);
case 32:
break;
case 21: {
Double val = Double.valueOf(yytext());
return new Yytoken(Yytoken.TYPE_VALUE, val);
}
case 33: break;
case 1:
{ throw new ParseException(yychar, ParseException.ERROR_UNEXPECTED_CHAR, new Character(yycharat(0)));
case 33:
break;
case 1: {
throw new ParseException(yychar, ParseException.ERROR_UNEXPECTED_CHAR, new Character(yycharat(0)));
}
case 34: break;
case 8:
{ return new Yytoken(Yytoken.TYPE_RIGHT_SQUARE,null);
case 34:
break;
case 8: {
return new Yytoken(Yytoken.TYPE_RIGHT_SQUARE, null);
}
case 35: break;
case 19:
{ sb.append('\r');
case 35:
break;
case 19: {
sb.append('\r');
}
case 36: break;
case 15:
{ sb.append('/');
case 36:
break;
case 15: {
sb.append('/');
}
case 37: break;
case 10:
{ return new Yytoken(Yytoken.TYPE_COLON,null);
case 37:
break;
case 10: {
return new Yytoken(Yytoken.TYPE_COLON, null);
}
case 38: break;
case 14:
{ sb.append('"');
case 38:
break;
case 14: {
sb.append('"');
}
case 39: break;
case 5:
{ return new Yytoken(Yytoken.TYPE_LEFT_BRACE,null);
case 39:
break;
case 5: {
return new Yytoken(Yytoken.TYPE_LEFT_BRACE, null);
}
case 40: break;
case 17:
{ sb.append('\f');
case 40:
break;
case 17: {
sb.append('\f');
}
case 41: break;
case 24:
{ try{
int ch=Integer.parseInt(yytext().substring(2),16);
sb.append((char)ch);
}
catch(Exception e){
case 41:
break;
case 24: {
try {
int ch = Integer.parseInt(yytext().substring(2), 16);
sb.append((char) ch);
} catch (Exception e) {
throw new ParseException(yychar, ParseException.ERROR_UNEXPECTED_EXCEPTION, e);
}
}
case 42: break;
case 20:
{ sb.append('\t');
case 42:
break;
case 20: {
sb.append('\t');
}
case 43: break;
case 7:
{ return new Yytoken(Yytoken.TYPE_LEFT_SQUARE,null);
case 43:
break;
case 7: {
return new Yytoken(Yytoken.TYPE_LEFT_SQUARE, null);
}
case 44: break;
case 2:
{ Long val=Long.valueOf(yytext()); return new Yytoken(Yytoken.TYPE_VALUE, val);
case 44:
break;
case 2: {
Long val = Long.valueOf(yytext());
return new Yytoken(Yytoken.TYPE_VALUE, val);
}
case 45: break;
case 18:
{ sb.append('\n');
case 45:
break;
case 18: {
sb.append('\n');
}
case 46: break;
case 9:
{ return new Yytoken(Yytoken.TYPE_COMMA,null);
case 46:
break;
case 9: {
return new Yytoken(Yytoken.TYPE_COMMA, null);
}
case 47: break;
case 3:
{
case 47:
break;
case 3: {
}
case 48: break;
case 48:
break;
default:
if (zzInput == YYEOF && zzStartRead == zzCurrentPos) {
zzAtEOF = true;
return null;
}
else {
} else {
zzScanError(ZZ_NO_MATCH);
}
}
}
}
}

View file

@ -8,26 +8,25 @@ package org.json.simple.parser;
* @author FangYidong<fangyidong@yahoo.com.cn>
*/
public class Yytoken {
public static final int TYPE_VALUE=0;//JSON primitive value: string,number,boolean,null
public static final int TYPE_LEFT_BRACE=1;
public static final int TYPE_RIGHT_BRACE=2;
public static final int TYPE_LEFT_SQUARE=3;
public static final int TYPE_RIGHT_SQUARE=4;
public static final int TYPE_COMMA=5;
public static final int TYPE_COLON=6;
public static final int TYPE_EOF=-1;//end of file
public static final int TYPE_VALUE = 0;//JSON primitive value: string,number,boolean,null
public static final int TYPE_LEFT_BRACE = 1;
public static final int TYPE_RIGHT_BRACE = 2;
public static final int TYPE_LEFT_SQUARE = 3;
public static final int TYPE_RIGHT_SQUARE = 4;
public static final int TYPE_COMMA = 5;
public static final int TYPE_COLON = 6;
public static final int TYPE_EOF = -1;//end of file
public int type = 0;
public Object value = null;
public int type=0;
public Object value=null;
public Yytoken(int type,Object value){
this.type=type;
this.value=value;
public Yytoken(int type, Object value) {
this.type = type;
this.value = value;
}
public String toString(){
public String toString() {
StringBuffer sb = new StringBuffer();
switch(type){
switch (type) {
case TYPE_VALUE:
sb.append("VALUE(").append(value).append(")");
break;