Many bugfixes and improvements

This commit is contained in:
Ziver Koc 2011-06-24 23:20:59 +00:00
parent c3e3bbf787
commit 363e0c6cfc
52 changed files with 2021 additions and 982 deletions

View file

@ -0,0 +1,101 @@
package zutil.parser;
import zutil.parser.DataNode.DataType;
/**
* http://wiki.theory.org/BitTorrentSpecification
* @author Ziver
*
*/
public class BEncodedParser {
/**
* Returns the representation of the data in the BEncoded string
*
* @param data The data to be decoded
* @return
*/
public static DataNode parse(String data){
return decode_BEncoded(new StringBuffer(data));
}
/**
* Returns the representation of the data in the BEncoded string
*
* @param data The data to be decoded
* @param index The index in data to start from
* @return
*/
private static DataNode decode_BEncoded(StringBuffer data){
String tmp;
char c = ' ';
switch (data.charAt(0)) {
/**
* Integers are prefixed with an i and terminated by an e. For
* example, 123 would bEcode to i123e, -3272002 would bEncode to
* i-3272002e.
*/
case 'i':
//System.out.println("Found Integer at "+index);
data.deleteCharAt(0);
tmp = data.substring(0, data.indexOf("e"));
data.delete(0, tmp.length() + 1);
//System.out.println(tmp);
return new DataNode( new Long(tmp));
/**
* Lists are prefixed with a l and terminated by an e. The list
* should contain a series of bEncoded elements. For example, the
* list of strings ["Monduna", "Bit", "Torrents"] would bEncode to
* l7:Monduna3:Bit8:Torrentse. The list [1, "Monduna", 3, ["Sub", "List"]]
* would bEncode to li1e7:Mondunai3el3:Sub4:Listee
*/
case 'l':
//System.out.println("Found List at "+index);
data.deleteCharAt(0);
DataNode list = new DataNode( DataType.List );
c = data.charAt(0);
while(c != 'e'){
list.add( decode_BEncoded(data) );
c = data.charAt(0);
}
data.deleteCharAt(0);
//MultiPrintStream.out.dump(list);
if(list.size() == 1) return list.get(0);
else return list;
/**
* Dictionaries are prefixed with a d and terminated by an e. They
* are similar to list, except that items are in key value pairs. The
* dictionary {"key":"value", "Monduna":"com", "bit":"Torrents", "number":7}
* would bEncode to d3:key5:value7:Monduna3:com3:bit:8:Torrents6:numberi7ee
*/
case 'd':
//System.out.println("Found Dictionary at "+index);
data.deleteCharAt(0);
DataNode map = new DataNode( DataType.Map );
c = data.charAt(0);
while(c != 'e'){
DataNode tmp2 = decode_BEncoded(data);
map.set(tmp2.getString(), decode_BEncoded(data));
c = data.charAt(0);
}
data.deleteCharAt(0);
//MultiPrintStream.out.dump(map);
return map;
/**
* Strings are prefixed with their length followed by a colon.
* For example, "Monduna" would bEncode to 7:Monduna and "BitTorrents"
* would bEncode to 11:BitTorrents.
*/
default:
//System.out.println("Found String at "+index);
tmp = data.substring(0, data.indexOf(":"));
int length = Integer.parseInt(tmp);
data.delete(0, tmp.length()+1);
String ret = data.substring(0, length);
data.delete(0, length);
//System.out.println(data.substring(i, i+length));
return new DataNode( ret );
}
}
}

View file

@ -1,4 +1,4 @@
package zutil.parser.json;
package zutil.parser;
import java.util.HashMap;
import java.util.Iterator;
@ -8,66 +8,65 @@ import java.util.Map;
/**
* This is an node in an JSON tree,
* it may contain a Map, list or value
* This is a data node used in JSON and BEncoding and other types
*
* @author Ziver
*/
public class JSONNode implements Iterable<JSONNode>{
public enum JSONType{
public class DataNode implements Iterable<DataNode>{
public enum DataType{
Map, List, String, Number, Boolean
}
private Map<String,JSONNode> map = null;
private List<JSONNode> list = null;
private Map<String,DataNode> map = null;
private List<DataNode> list = null;
private String value = null;
private JSONType type;
private DataType type;
/**
* Creates an instance with an Boolean value
*/
public JSONNode(boolean value){
this.type = JSONType.Boolean;
public DataNode(boolean value){
this.type = DataType.Boolean;
this.value = ""+value;
}
/**
* Creates an instance with an int value
*/
public JSONNode(int value){
this.type = JSONType.Number;
public DataNode(int value){
this.type = DataType.Number;
this.value = ""+value;
}
/**
* Creates an instance with an double value
*/
public JSONNode(double value){
this.type = JSONType.Number;
public DataNode(double value){
this.type = DataType.Number;
this.value = ""+value;
}
/**
* Creates an instance with an long value
*/
public JSONNode(long value){
this.type = JSONType.Number;
public DataNode(long value){
this.type = DataType.Number;
this.value = ""+value;
}
/**
* Creates an instance with an String value
*/
public JSONNode(String value){
this.type = JSONType.String;
public DataNode(String value){
this.type = DataType.String;
this.value = value;
}
/**
* Creates an instance with a specific type
*/
public JSONNode(JSONType type){
public DataNode(DataType type){
this.type = type;
switch(type){
case Map:
map = new HashMap<String,JSONNode>(); break;
map = new HashMap<String,DataNode>(); break;
case List:
list = new LinkedList<JSONNode>(); break;
list = new LinkedList<DataNode>(); break;
}
}
@ -75,7 +74,7 @@ public class JSONNode implements Iterable<JSONNode>{
* @param index is the index of the List or Map
* @return an JSONNode that contains the next level of the List or Map
*/
public JSONNode get(int index){
public DataNode get(int index){
if(map != null)
return map.get(""+index);
else if(list != null)
@ -86,7 +85,7 @@ public class JSONNode implements Iterable<JSONNode>{
* @param index is the key in the Map
* @return an JSONNode that contains the next level of the Map
*/
public JSONNode get(String index){
public DataNode get(String index){
if(map != null)
return map.get(index);
return null;
@ -95,7 +94,7 @@ public class JSONNode implements Iterable<JSONNode>{
/**
* @return a iterator for the Map or List or null if the node contains a value
*/
public Iterator<JSONNode> iterator(){
public Iterator<DataNode> iterator(){
if(map != null)
return map.values().iterator();
else if(list != null)
@ -125,51 +124,51 @@ public class JSONNode implements Iterable<JSONNode>{
/**
* Adds a node to the List
*/
public void add(JSONNode node){
public void add(DataNode node){
list.add(node);
}
public void add(boolean value){
list.add(new JSONNode( value ));
list.add(new DataNode( value ));
}
public void add(int value){
list.add(new JSONNode( value ));
list.add(new DataNode( value ));
}
public void add(double value){
list.add(new JSONNode( value ));
list.add(new DataNode( value ));
}
public void add(long value){
list.add(new JSONNode( value ));
list.add(new DataNode( value ));
}
public void add(String value){
list.add(new JSONNode( value ));
list.add(new DataNode( value ));
}
/**
* Adds a node to the Map
*/
public void add(String key, JSONNode node){
public void set(String key, DataNode node){
map.put(key, node);
}
public void add(String key, boolean value){
map.put(key, new JSONNode(value));
public void set(String key, boolean value){
map.put(key, new DataNode(value));
}
public void add(String key, int value){
map.put(key, new JSONNode(value));
public void set(String key, int value){
map.put(key, new DataNode(value));
}
public void add(String key, double value){
map.put(key, new JSONNode(value));
public void set(String key, double value){
map.put(key, new DataNode(value));
}
public void add(String key, long value){
map.put(key, new JSONNode(value));
public void set(String key, long value){
map.put(key, new DataNode(value));
}
public void add(String key, String value){
map.put(key, new JSONNode(value));
public void set(String key, String value){
map.put(key, new DataNode(value));
}
/**
* Sets the value of the node, but only if it is setup as an JSONType.Value
*/
public void set(int value){
if( !this.isValue() ) throw new NullPointerException("The node is not setup as a value");
type = JSONType.Number;
type = DataType.Number;
this.value = ""+value;
}
/**
@ -177,7 +176,7 @@ public class JSONNode implements Iterable<JSONNode>{
*/
public void set(double value){
if( !this.isValue() ) throw new NullPointerException("The node is not setup as a value");
type = JSONType.Number;
type = DataType.Number;
this.value = ""+value;
}
/**
@ -185,7 +184,7 @@ public class JSONNode implements Iterable<JSONNode>{
*/
public void set(boolean value){
if( !this.isValue() ) throw new NullPointerException("The node is not setup as a value");
type = JSONType.Boolean;
type = DataType.Boolean;
this.value = ""+value;
}
/**
@ -193,7 +192,7 @@ public class JSONNode implements Iterable<JSONNode>{
*/
public void set(long value){
if( !this.isValue() ) throw new NullPointerException("The node is not setup as a value");
type = JSONType.Number;
type = DataType.Number;
this.value = ""+value;
}
/**
@ -201,7 +200,7 @@ public class JSONNode implements Iterable<JSONNode>{
*/
public void set(String value){
if( !this.isValue() ) throw new NullPointerException("The node is not setup as a value");
type = JSONType.String;
type = DataType.String;
this.value = value;
}
@ -210,27 +209,70 @@ public class JSONNode implements Iterable<JSONNode>{
* @return if this node contains an Map
*/
public boolean isMap(){
return type == JSONType.Map;
return type == DataType.Map;
}
/**
* @return if this node contains an List
*/
public boolean isList(){
return type == JSONType.List;
return type == DataType.List;
}
/**
* @return if this node contains an value
*/
public boolean isValue(){
return type != JSONType.Map && type != JSONType.List;
return type != DataType.Map && type != DataType.List;
}
/**
* @return the type of the node
*/
public JSONType getType(){
public DataType getType(){
return type;
}
/**
* @return the String value in this map
*/
public String getString(String key){
if( !this.isMap() ) throw new NullPointerException("The node is not setup as a map");
if( !this.map.containsKey(key) )
return null;
return this.get(key).getString();
}
/**
* @return the boolean value in this map
*/
public boolean getBoolean(String key){
if( !this.isMap() ) throw new NullPointerException("The node is not setup as a map");
if( !this.map.containsKey(key) ) throw new NullPointerException("No such key in map");
return this.get(key).getBoolean();
}
/**
* @return the integer value in this map
*/
public int getInt(String key){
if( !this.isMap() ) throw new NullPointerException("The node is not setup as a map");
if( !this.map.containsKey(key) ) throw new NullPointerException("No such key in map");
return this.get(key).getInt();
}
/**
* @return the double value in this map
*/
public double getDouble(String key){
if( !this.isMap() ) throw new NullPointerException("The node is not setup as a map");
if( !this.map.containsKey(key) ) throw new NullPointerException("No such key in map");
return this.get(key).getDouble();
}
/**
* @return the long value in this map
*/
public long getLong(String key){
if( !this.isMap() ) throw new NullPointerException("The node is not setup as a map");
if( !this.map.containsKey(key) ) throw new NullPointerException("No such key in map");
return this.get(key).getLong();
}
/**
* @return the String value in this node, null if its a Map or List
@ -256,6 +298,12 @@ public class JSONNode implements Iterable<JSONNode>{
public double getDouble(){
return Double.parseDouble(value);
}
/**
* @return the long value in this node
*/
public long getLong(){
return Long.parseLong(value);
}
public String toString(){

View file

@ -1,7 +1,8 @@
package zutil.parser.json;
import zutil.io.MultiPrintStream;
import zutil.parser.json.JSONNode.JSONType;
import zutil.parser.DataNode;
import zutil.parser.DataNode.DataType;
/**
* This is a JSON parser class
@ -28,7 +29,7 @@ public class JSONParser{
" { \"type\": \"fax\", \"number\": \"646 555-4567\" }"+
" ]"+
"}");
JSONNode node = parser.read();
DataNode node = parser.read();
MultiPrintStream.out.dump( node );
JSONWriter writer = new JSONWriter( System.out );
writer.write(node);
@ -48,7 +49,7 @@ public class JSONParser{
*
* @return a JSONNode object that is the root of the JSON
*/
public JSONNode read(){
public DataNode read(){
return parse(new StringBuffer(json));
}
@ -67,10 +68,10 @@ public class JSONParser{
* @param json
* @return
*/
protected static JSONNode parse(StringBuffer json){
JSONNode root = null;
JSONNode key = null;
JSONNode node = null;
protected static DataNode parse(StringBuffer json){
DataNode root = null;
DataNode key = null;
DataNode node = null;
int next_index;
while(Character.isWhitespace( json.charAt(0) ) ||
@ -84,26 +85,26 @@ public class JSONParser{
case '}':
return null;
case '{':
root = new JSONNode(JSONType.Map);
root = new DataNode(DataType.Map);
while((key = parse( json )) != null && (node = parse( json )) != null){
root.add( key.toString(), node );
root.set( key.toString(), node );
}
break;
case '[':
root = new JSONNode(JSONType.List);
root = new DataNode(DataType.List);
while((node = parse( json )) != null){
root.add( node );
}
break;
// Parse String
case '\"':
root = new JSONNode(JSONType.String);
root = new DataNode(DataType.String);
next_index = json.indexOf("\"");
root.set( json.substring(0, next_index) );
json.delete(0, next_index+1);
break;
default:
root = new JSONNode(JSONType.Number);
root = new DataNode(DataType.Number);
for(next_index=0; next_index<json.length() ;++next_index)
if( json.charAt(next_index)==',' ||
json.charAt(next_index)==']' ||

View file

@ -5,7 +5,8 @@ import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.Iterator;
import zutil.parser.json.JSONNode.JSONType;
import zutil.parser.DataNode;
import zutil.parser.DataNode.DataType;
/**
* Writes An JSONNode to an String or stream
@ -47,7 +48,7 @@ public class JSONWriter{
*
* @param root is the root node
*/
public void write(JSONNode root){
public void write(DataNode root){
boolean first = true;
switch(root.getType()){
// Write Map
@ -74,7 +75,7 @@ public class JSONWriter{
// Write an list
case List:
out.print('[');
for(JSONNode node : root){
for(DataNode node : root){
if(!first)
out.print(", ");
write( node );
@ -83,7 +84,7 @@ public class JSONWriter{
out.print(']');
break;
default:
if(root.getString() != null && root.getType() == JSONType.String){
if(root.getString() != null && root.getType() == DataType.String){
out.print('\"');
out.print(root.toString());
out.print('\"');